An exercise in refactoring groovy code

Legacy Code


1:  public def getDBSectionContent_Deprecated(designSectionId) {  
2:            def dbSectionObj = databaseQueries.getSectionBySectionId(designSectionId, false)  
3:            com.vce.scs.section.domain.Section dbSectionObject = sectionContentValidator.  
4:                                                  convertDBSectionResponseToDomain(dbSectionObj)  
5:            def dbSectionContentListObject = new CopyOnWriteArrayList<Content>()  
6:            dbSectionContentListObject.addAll(dbSectionObject.contents)  
7:            def fieldInfoMap  
8:            def dbfieldTypeValueListObject  
9:            def deleteSectionContentObject = new CopyOnWriteArrayList<String>()  
10:            if (dbSectionContentListObject.size() > 0) {  
11:                 for (def i = 0; i <= dbSectionContentListObject.size() - 1; i++) {  
12:                      log.info dbSectionContentListObject[i].type  
13:                      if (dbSectionContentListObject[i].type == "fieldGroup") {  
14:                           log.info dbSectionContentListObject[i].itemId  
15:                           def visible = false  
16:                           dbSectionContentListObject[i].columns.eachWithIndex { fieldgroupCol, index ->  
17:                                fieldgroupCol.eachWithIndex { fieldgroupColumn, ind ->  
18:                                     if (fieldgroupColumn != null) {  
19:                                          log.info fieldgroupColumn.type  
20:                                          if (fieldgroupColumn.type == "field") {  
21:                                               log.info fieldgroupColumn.itemId  
22:                                               fieldInfoMap = databaseQueriesSS.getFieldInfoByFieldId(fieldgroupColumn.itemId)  
23:                                               def isFormula = databaseQueriesSS.getFieldHasFomula(fieldgroupColumn.itemId)  
24:                                               def isRules = databaseQueriesSS.fieldHasRules(fieldgroupColumn.itemId)  
25:                                               log.info isFormula  
26:                                               if (fieldInfoMap.basicAttributes.triggerType == "Manual") {  
27:                                                    visible = true  
28:                                                    log.info fieldInfoMap.basicAttributes.triggerType  
29:                                                    Column columnObj = new Column()  
30:                                                    //Column columnObj=fieldgroupColumn  
31:                                                    columnObj.label = fieldInfoMap.label  
32:                                                    columnObj.type = fieldgroupColumn["type"]  
33:                                                    columnObj.itemId = fieldgroupColumn.itemId  
34:                                                    columnObj.isPosiitoned = fieldgroupColumn.isPosiitoned  
35:                                                    columnObj.name = fieldgroupColumn.name  
36:                                                    log.info "----------------Field Type" + fieldInfoMap.fieldType.values  
37:                                                    dbfieldTypeValueListObject = MongoDBUtils.  
38:                          convertDbResultList(fieldInfoMap.fieldType.values, FieldValues.class)  
39:                                                    FieldType fieldType = new FieldType()  
40:                                                    fieldType.type = fieldInfoMap.fieldType.type  
41:                                                    fieldType.values = dbfieldTypeValueListObject  
42:                                                    fieldType.fieldValue = fieldInfoMap.fieldType.fieldValue  
43:                                                    columnObj.fieldType = fieldType  
44:                                                    //columnObj.isPosiitoned=fieldInfoMap.isPositioned  
45:                                                    columnObj.labelPlacement = dbSectionObj.contentLayout.labelPlacement  
46:                                                    columnObj.description = fieldInfoMap.description  
47:                                                    columnObj.basicAttributes = fieldInfoMap.basicAttributes  
48:                                                    columnObj.hasFormulas = databaseQueriesSS.  
49:                          getFieldHasFomula(fieldgroupColumn.itemId)  
50:                                                    columnObj.hasRules = databaseQueriesSS.fieldHasRules(fieldgroupColumn.itemId)  
51:                                                    columnObj.visible = true  
52:                                                    if (fieldInfoMap.fieldType.type != "dropdown") {  
53:                                                         columnObj.value = fieldInfoMap.fieldType.fieldValue  
54:                                                    } else {  
55:                                                         def dropdownValues = fieldInfoMap.fieldType.values  
56:                                                         dropdownValues.each { dropdownValue ->  
57:                                                              log.info dropdownValue  
58:                                                              log.info dropdownValue.selected  
59:                                                              if (dropdownValue.selected == true) {  
60:                                                                   columnObj.value = dropdownValue.value  
61:                                                              }  
62:                                                         }  
63:                                                    }  
64:                                                    log.info columnObj  
65:                                               } else {  
66:                                                    log.info dbSectionContentListObject  
67:                                                    log.info dbSectionContentListObject[i].columns[index][ind]  
68:                                                    dbSectionContentListObject[i].columns[index].remove(fieldgroupColumn)  
69:                                                    log.info dbSectionContentListObject  
70:                                               }  
71:                                               ind = ind + 1  
72:                                          }  
73:                                          index = index + 1  
74:                                     }  
75:                                }  
76:                           }  
77:                           dbSectionContentListObject[i].visible = visible  
78:                           def fieldGroupVisible = false  
79:                           dbSectionContentListObject[i].columns.each { objectColumn ->  
80:                                if (objectColumn != []) {  
81:                                     fieldGroupVisible = true  
82:                                }  
83:                           }  
84:                           if (fieldGroupVisible == false) {  
85:                                deleteSectionContentObject.add(dbSectionContentListObject[i])  
86:                           }  
87:                      } else if ((dbSectionContentListObject[i].type == "field")) {  
88:                           log.info dbSectionContentListObject[i].itemId  
89:                           fieldInfoMap = databaseQueriesSS.getFieldInfoByFieldId(dbSectionContentListObject[i].itemId)  
90:                           dbSectionContentListObject[i].visible = true  
91:                           if (fieldInfoMap.basicAttributes.triggerType == "Manual") {  
92:                                dbSectionContentListObject[i].name = fieldInfoMap.name  
93:                                dbSectionContentListObject[i].isPositioned = fieldInfoMap.isPositioned  
94:                                dbSectionContentListObject[i].labelPlacement = dbSectionObj.contentLayout.labelPlacement  
95:                                dbSectionContentListObject[i].description = fieldInfoMap.description  
96:                                dbSectionContentListObject[i].basicAttributes = fieldInfoMap.basicAttributes  
97:                                dbSectionContentListObject[i].label = fieldInfoMap.label  
98:                                dbSectionContentListObject[i].isPositioned = fieldInfoMap.isPositioned  
99:                                dbSectionContentListObject[i].hasFormulas = databaseQueriesSS.  
100:                                  getFieldHasFomula(dbSectionContentListObject[i].itemId)  
101:                                dbSectionContentListObject[i].hasRules = databaseQueriesSS.  
102:                                    fieldHasRules(dbSectionContentListObject[i].itemId)  
103:                                dbSectionContentListObject[i].visible = true  
104:                                dbSectionContentListObject[i].columns = null  
105:                                log.info fieldInfoMap.fieldType.type  
106:                                if (fieldInfoMap.fieldType.type != "dropdown") {  
107:                                     dbSectionContentListObject[i].value = fieldInfoMap.fieldType.fieldValue  
108:                                } else {  
109:                                     def dropdownValues = fieldInfoMap.fieldType.values  
110:                                     dropdownValues.each { dropdownValue ->  
111:                                          log.info dropdownValue  
112:                                          log.info dropdownValue.selected  
113:                                          if (dropdownValue.selected == true) {  
114:                                               dbSectionContentListObject[i].value = dropdownValue.value  
115:                                          }  
116:                                     }  
117:                                }  
118:                                dbfieldTypeValueListObject = MongoDBUtils.  
119:                          convertDbResultList(fieldInfoMap.fieldType.values, FieldValues.class)  
120:                                FieldType fieldType = new FieldType()  
121:                                fieldType.type = fieldInfoMap.fieldType.type  
122:                                fieldType.values = dbfieldTypeValueListObject  
123:                                fieldType.fieldValue = fieldInfoMap.fieldType.fieldValue  
124:                                dbSectionContentListObject[i].fieldType = fieldType  
125:                           } else {  
126:                                log.info deleteSectionContentObject  
127:                                deleteSectionContentObject.add(dbSectionContentListObject[i])  
128:                                log.info deleteSectionContentObject  
129:                           }  
130:                      } else if (dbSectionContentListObject[i].type == "subSection") {  
131:                           log.info dbSectionContentListObject[i].itemId  
132:                           def visible = false  
133:                           def index = 0  
134:                           log.info dbSectionContentListObject[i].columns.size()  
135:                           dbSectionContentListObject[i].columns.eachWithIndex { subSectionCol, Index ->  
136:                                def colremove = new CopyOnWriteArrayList()  
137:                                subSectionCol.eachWithIndex { subSectionColumn, ind ->  
138:                                     log.info subSectionColumn  
139:                                     if (subSectionColumn != null) {  
140:                                          log.info subSectionColumn  
141:                                          log.info subSectionColumn.type  
142:                                          if (subSectionColumn.type == "field") {  
143:                                               log.info subSectionColumn.itemId  
144:                                               fieldInfoMap = databaseQueriesSS.getFieldInfoByFieldId(subSectionColumn.itemId)  
145:                                               log.info fieldInfoMap  
146:                                               if (fieldInfoMap.basicAttributes.triggerType == "Manual") {  
147:                                                    visible = true  
148:                                                    log.info fieldInfoMap.basicAttributes.triggerType  
149:                                                    Column columnObj = subSectionColumn  
150:                                                    columnObj.label = fieldInfoMap.label  
151:                                                    log.info "----------------Field Type" + fieldInfoMap.fieldType.values  
152:                                                    dbfieldTypeValueListObject = MongoDBUtils.  
153:                          convertDbResultList(fieldInfoMap.fieldType.values, FieldValues.class)  
154:                                                    FieldType fieldType = new FieldType()  
155:                                                    fieldType.type = fieldInfoMap.fieldType.type  
156:                                                    fieldType.values = dbfieldTypeValueListObject  
157:                                                    fieldType.fieldValue = fieldInfoMap.fieldType.fieldValue  
158:                                                    columnObj.fieldType = fieldType  
159:                                                    //columnObj.isPosiitoned=fieldInfoMap.isPositioned  
160:                                                    columnObj.labelPlacement = dbSectionObj.contentLayout.labelPlacement  
161:                                                    columnObj.description = fieldInfoMap.description  
162:                                                    columnObj.basicAttributes = fieldInfoMap.basicAttributes  
163:                                                    columnObj.visible = true  
164:                                                    columnObj.hasFormulas = databaseQueriesSS.  
165:                                      getFieldHasFomula(subSectionColumn.itemId)  
166:                                                    columnObj.hasRules = databaseQueriesSS.fieldHasRules(subSectionColumn.itemId)  
167:                                                    if (fieldInfoMap.fieldType.type != "dropdown") {  
168:                                                         columnObj.value = fieldInfoMap.fieldType.fieldValue  
169:                                                    } else {  
170:                                                         def dropdownValues = fieldInfoMap.fieldType.values  
171:                                                         dropdownValues.each { dropdownValue ->  
172:                                                              log.info dropdownValue  
173:                                                              log.info dropdownValue.selected  
174:                                                              if (dropdownValue.selected == true) {  
175:                                                                   columnObj.value = dropdownValue.value  
176:                                                              }  
177:                                                         }  
178:                                                    }  
179:                                                    visible = true  
180:                                                    log.info columnObj  
181:                                                    log.info dbSectionContentListObject  
182:                                               } else {  
183:                                                    colremove.add(subSectionColumn)  
184:                                               }  
185:                                               ind = ind + 1  
186:                                          }  
187:                                     }  
188:                                }  
189:                                subSectionCol.removeAll(colremove)  
190:                                index = index + 1  
191:                           }  
192:                           dbSectionContentListObject[i].visible = visible  
193:                           def subSecitonGroupVisible = false  
194:                           dbSectionContentListObject[i].columns.each { objectColumn ->  
195:                                if (objectColumn != []) {  
196:                                     subSecitonGroupVisible = true  
197:                                }  
198:                           }  
199:                           if (subSecitonGroupVisible == false) {  
200:                                deleteSectionContentObject.add(dbSectionContentListObject[i])  
201:                           }  
202:                           if (visible == true) {  
203:                                def subSectionMap = databaseQueriesSS.  
204:                          getSubSectionsBySubSectionFromDb(dbSectionContentListObject[i].itemId)  
205:                                dbSectionContentListObject[i].section = designSectionId  
206:                                dbSectionContentListObject[i].name = subSectionMap.name  
207:                                dbSectionContentListObject[i].text = subSectionMap.text  
208:                                dbSectionContentListObject[i].isPositioned = subSectionMap.isPositioned  
209:                                ContentLayout contentLayout = new ContentLayout()  
210:                                contentLayout.labelPlacement = subSectionMap.contentLayout.labelPlacement  
211:                                contentLayout.columns = subSectionMap.contentLayout.columns  
212:                                dbSectionContentListObject[i].contentLayout = contentLayout  
213:                                dbSectionContentListObject[i].status = subSectionMap.status  
214:                                log.info dbSectionContentListObject[i]  
215:                           }  
216:                      }  
217:                 }  
218:                 log.info dbSectionContentListObject  
219:                 log.info dbSectionContentListObject.size()  
220:                 dbSectionContentListObject.removeAll(deleteSectionContentObject)  
221:                 log.info dbSectionContentListObject.size()  
222:                 log.info " dbSectionContentListObject : " + dbSectionContentListObject  
223:                 return dbSectionContentListObject  
224:            }  
225:       }  


Refactored Code

1:   private enum ContentType{  
2:      FIELD_GROUP("fieldGroup"),  
3:      SUB_SECTION("subSection"),  
4:      FIELD("field")  
5:      private String value  
6:      ContentType(String value) {  
7:        this.value = value  
8:      }  
9:      @Override  
10:      public String toString() {  
11:        return this.value  
12:      }  
13:    }  
14:    public def getDBSectionContent(def designSectionId) {  
15:      log.info "Retrieve Section Contents from the Database"  
16:      def dbSection = databaseQueries.getSectionBySectionId(designSectionId, false)  
17:      log.info "Retrieve all sections from database ${dbSection}"  
18:      com.vce.scs.section.domain.Section designSection = sectionContentValidator.  
19:                                    convertDBSectionResponseToDomain(dbSection)  
20:      log.info "Parse database section to design section object ${designSection}"  
21:      def contents = new CopyOnWriteArrayList<Content>()  
22:      contents.addAll(designSection.contents)  
23:      if (contents.size() > 0) {  
24:        for (def content : contents) {  
25:          def dupContent = content  
26:          contents.remove(content)  
27:          switch (dupContent.type) {  
28:            case ContentType.FIELD_GROUP.toString():  
29:              dupContent = retrieveContents("FieldGroup ", dupContent)  
30:              break  
31:            case ContentType.SUB_SECTION.toString():  
32:              dupContent = retrieveContents("Subsection ", dupContent)  
33:              break  
34:            case ContentType.FIELD.toString():  
35:              dupContent = retrieveField(dupContent)  
36:              break  
37:            default:  
38:              throw new RuntimeException("Invalid Content Type ${dupContent.type}!")  
39:          }  
40:          contents.add(dupContent)  
41:        }  
42:      } else {  
43:        throw new RuntimeException("There are no contents in the section!")  
44:      }  
45:      contents.removeAll(Collections.singleton(null))  
46:      return contents  
47:    }  
48:    private def retrieveContents(def elementType, def content) {  
49:      log.info "${elementType} content item id : ${content.itemId}"  
50:      def columns = new CopyOnWriteArrayList(content.columns)  
51:      for (def column : columns) {  
52:        def dupColumn = new CopyOnWriteArrayList(column)  
53:        columns.remove(column)  
54:        for (def item : dupColumn) {  
55:          def dupItem = item  
56:          dupColumn.remove(item)  
57:          if (dupItem != null && dupItem.type == ContentType.FIELD.toString()) {  
58:            dupItem = retrieveField(dupItem)  
59:            dupColumn.add(dupItem)  
60:          } else {  
61:            throw new RuntimeException("A non field object ${dupItem?.type} is present in ${elementType}!")  
62:          }  
63:        }  
64:        dupColumn.removeAll(Collections.singleton(null))  
65:        columns.add(dupColumn)  
66:      }  
67:      content.columns = columns  
68:      return (content.columns == null) ? null : content  
69:    }  
70:    private def retrieveField(def item){  
71:      def fieldObjectId = item.itemId  
72:      log.info "Field Id is ${fieldObjectId.toString()}"  
73:      def field = databaseQueriesSS.getFieldInfoByFieldId(fieldObjectId)  
74:      // retain fields of trigger type manual and purge the rest  
75:      return (field.basicAttributes.triggerType != "Manual") ? null : item  
76:    }  

Comments

Popular posts from this blog

FileSystemUtils

Report

AbstractTestNgBaseTest