View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   * Created by Charles on 7/9/12
16   */
17  package org.kuali.student.enrollment.class2.courseofferingset.model;
18  
19  import org.kuali.student.enrollment.courseofferingset.dto.SocRolloverResultInfo;
20  import org.kuali.student.enrollment.courseofferingset.infc.SocRolloverResult;
21  import org.kuali.student.r2.common.assembler.DynAttrConverter;
22  import org.kuali.student.r2.common.assembler.DynamicAttrReadOnlyWrapper;
23  import org.kuali.student.r2.common.assembler.DynamicAttrWrapper;
24  import org.kuali.student.r2.common.assembler.TransformUtility;
25  import org.kuali.student.r2.common.dto.AttributeInfo;
26  import org.kuali.student.r2.common.util.constants.CourseOfferingSetServiceConstants;
27  
28  import java.util.Date;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  /**
33   * This class converts dynamic attributes from SocRolloverResults
34   *
35   * @author Kuali Student Team
36   */
37  public class SocRolloverResultDynAttrConverter {
38      private static Map<String, DynamicAttrReadOnlyWrapper<SocRolloverResult>> _createDynAttrReadOnlyMap(SocRolloverResult result) {
39          Map<String, DynamicAttrReadOnlyWrapper<SocRolloverResult>> dynAttrReadOnlyMap =
40                  new HashMap<String, DynamicAttrReadOnlyWrapper<SocRolloverResult>>();
41          // Date initiated dyn attr
42          dynAttrReadOnlyMap.put(CourseOfferingSetServiceConstants.DATE_INITIATED_RESULT_DYNATTR_KEY,
43                  new DynamicAttrReadOnlyWrapper<SocRolloverResult>(result, Date.class,
44                          CourseOfferingSetServiceConstants.DATE_INITIATED_RESULT_DYNATTR_KEY) {
45                      @Override
46                      public String writeOutStringValue() {
47                          DynAttrConverter<Date> dateConverter = (DynAttrConverter<Date>) TransformUtility.getConverterByClass(Date.class);
48                          String dateStr = dateConverter.convertNativeValueToString(getDto().getDateInitiated());
49                          return dateStr;
50                      }
51                  });
52          // Date completed dyn attr
53          dynAttrReadOnlyMap.put(CourseOfferingSetServiceConstants.DATE_COMPLETED_RESULT_DYNATTR_KEY,
54                  new DynamicAttrReadOnlyWrapper<SocRolloverResult>(result, Date.class,
55                          CourseOfferingSetServiceConstants.DATE_COMPLETED_RESULT_DYNATTR_KEY) {
56                      @Override
57                      public String writeOutStringValue() {
58                          DynAttrConverter<Date> dateConverter = (DynAttrConverter<Date>) TransformUtility.getConverterByClass(Date.class);
59                          String dateStr = dateConverter.convertNativeValueToString(getDto().getDateCompleted());
60                          return dateStr;
61                      }
62                  });
63          // CO Created dyn attribute
64          dynAttrReadOnlyMap.put(CourseOfferingSetServiceConstants.CO_CREATED_RESULT_DYNATTR_KEY,
65                  new DynamicAttrReadOnlyWrapper<SocRolloverResult>(result, Date.class,
66                          CourseOfferingSetServiceConstants.CO_CREATED_RESULT_DYNATTR_KEY) {
67                      @Override
68                      public String writeOutStringValue() {
69                          DynAttrConverter<Integer> intConverter = (DynAttrConverter<Integer>) TransformUtility.getConverterByClass(Integer.class);
70                          String intStr = intConverter.convertNativeValueToString(getDto().getCourseOfferingsCreated());
71                          return intStr;
72                      }
73                  });
74          // CO Created dyn attribute
75          dynAttrReadOnlyMap.put(CourseOfferingSetServiceConstants.CO_SKIPPED_RESULT_DYNATTR_KEY,
76                  new DynamicAttrReadOnlyWrapper<SocRolloverResult>(result, Date.class,
77                          CourseOfferingSetServiceConstants.CO_SKIPPED_RESULT_DYNATTR_KEY) {
78  
79                      @Override
80                      public String writeOutStringValue() {
81                          DynAttrConverter<Integer> intConverter = (DynAttrConverter<Integer>) TransformUtility.getConverterByClass(Integer.class);
82                          String intStr = intConverter.convertNativeValueToString(getDto().getCourseOfferingsSkipped());
83                          return intStr;
84                      }
85                  });
86          // AO Created dyn attribute
87          dynAttrReadOnlyMap.put(CourseOfferingSetServiceConstants.AO_CREATED_RESULT_DYNATTR_KEY,
88                  new DynamicAttrReadOnlyWrapper<SocRolloverResult>(result, Date.class,
89                          CourseOfferingSetServiceConstants.AO_CREATED_RESULT_DYNATTR_KEY) {
90  
91                      @Override
92                      public String writeOutStringValue() {
93                          DynAttrConverter<Integer> intConverter = (DynAttrConverter<Integer>) TransformUtility.getConverterByClass(Integer.class);
94                          String intStr = intConverter.convertNativeValueToString(getDto().getActivityOfferingsCreated());
95                          return intStr;
96                      }
97                  });
98          // AO Created dyn attribute
99          dynAttrReadOnlyMap.put(CourseOfferingSetServiceConstants.AO_SKIPPED_RESULT_DYNATTR_KEY,
100                 new DynamicAttrReadOnlyWrapper<SocRolloverResult>(result, Date.class,
101                         CourseOfferingSetServiceConstants.AO_SKIPPED_RESULT_DYNATTR_KEY) {
102 
103                     @Override
104                     public String writeOutStringValue() {
105                         DynAttrConverter<Integer> intConverter = (DynAttrConverter<Integer>) TransformUtility.getConverterByClass(Integer.class);
106                         String intStr = intConverter.convertNativeValueToString(getDto().getActivityOfferingsSkipped());
107                         return intStr;
108                     }
109                 });
110         return dynAttrReadOnlyMap;
111     }
112 
113     private static Map<String, DynamicAttrWrapper<SocRolloverResultInfo>> _createDynAttrMap(SocRolloverResultInfo result) {
114         Map<String, DynamicAttrWrapper<SocRolloverResultInfo>> dynAttrMap = new HashMap<String, DynamicAttrWrapper<SocRolloverResultInfo>>();
115         // Date initiated dyn attr
116         dynAttrMap.put(CourseOfferingSetServiceConstants.DATE_INITIATED_RESULT_DYNATTR_KEY,
117                 new DynamicAttrWrapper<SocRolloverResultInfo>(result, Date.class,
118                         CourseOfferingSetServiceConstants.DATE_INITIATED_RESULT_DYNATTR_KEY) {
119 
120                     @Override
121                     public void readInStringValue(String attrValue) {
122                         DynAttrConverter<Date> dateConverter = (DynAttrConverter<Date>) TransformUtility.getConverterByClass(Date.class);
123                         Date convertedDate = dateConverter.convertStringValueToNative(attrValue);
124                         getDto().setDateInitiated(convertedDate);
125                     }
126 
127                     @Override
128                     public String writeOutStringValue() {
129                         DynAttrConverter<Date> dateConverter = (DynAttrConverter<Date>) TransformUtility.getConverterByClass(Date.class);
130                         String dateStr = dateConverter.convertNativeValueToString(getDto().getDateInitiated());
131                         return dateStr;
132                     }
133                 });
134         // Date completed dyn attr
135         dynAttrMap.put(CourseOfferingSetServiceConstants.DATE_COMPLETED_RESULT_DYNATTR_KEY,
136                 new DynamicAttrWrapper<SocRolloverResultInfo>(result, Date.class,
137                         CourseOfferingSetServiceConstants.DATE_COMPLETED_RESULT_DYNATTR_KEY) {
138 
139                     @Override
140                     public void readInStringValue(String attrValue) {
141                         DynAttrConverter<Date> dateConverter = (DynAttrConverter<Date>) TransformUtility.getConverterByClass(Date.class);
142                         Date convertedDate = dateConverter.convertStringValueToNative(attrValue);
143                         getDto().setDateCompleted(convertedDate);
144                     }
145 
146                     @Override
147                     public String writeOutStringValue() {
148                         DynAttrConverter<Date> dateConverter = (DynAttrConverter<Date>) TransformUtility.getConverterByClass(Date.class);
149                         String dateStr = dateConverter.convertNativeValueToString(getDto().getDateCompleted());
150                         return dateStr;
151                     }
152                 });
153         // CO Created dyn attribute
154         dynAttrMap.put(CourseOfferingSetServiceConstants.CO_CREATED_RESULT_DYNATTR_KEY,
155                 new DynamicAttrWrapper<SocRolloverResultInfo>(result, Date.class,
156                         CourseOfferingSetServiceConstants.CO_CREATED_RESULT_DYNATTR_KEY) {
157 
158                     @Override
159                     public void readInStringValue(String attrValue) {
160                         DynAttrConverter<Integer> intConverter = (DynAttrConverter<Integer>) TransformUtility.getConverterByClass(Integer.class);
161                         Integer intVal = intConverter.convertStringValueToNative(attrValue);
162                         getDto().setCourseOfferingsCreated(intVal);
163                     }
164 
165                     @Override
166                     public String writeOutStringValue() {
167                         DynAttrConverter<Integer> intConverter = (DynAttrConverter<Integer>) TransformUtility.getConverterByClass(Integer.class);
168                         String intStr = intConverter.convertNativeValueToString(getDto().getCourseOfferingsCreated());
169                         return intStr;
170                     }
171                 });
172         // CO Created dyn attribute
173         dynAttrMap.put(CourseOfferingSetServiceConstants.CO_SKIPPED_RESULT_DYNATTR_KEY,
174                 new DynamicAttrWrapper<SocRolloverResultInfo>(result, Date.class,
175                         CourseOfferingSetServiceConstants.CO_SKIPPED_RESULT_DYNATTR_KEY) {
176 
177                     @Override
178                     public void readInStringValue(String attrValue) {
179                         DynAttrConverter<Integer> intConverter = (DynAttrConverter<Integer>) TransformUtility.getConverterByClass(Integer.class);
180                         Integer intVal = intConverter.convertStringValueToNative(attrValue);
181                         getDto().setCourseOfferingsSkipped(intVal);
182                     }
183 
184                     @Override
185                     public String writeOutStringValue() {
186                         DynAttrConverter<Integer> intConverter = (DynAttrConverter<Integer>) TransformUtility.getConverterByClass(Integer.class);
187                         String intStr = intConverter.convertNativeValueToString(getDto().getCourseOfferingsSkipped());
188                         return intStr;
189                     }
190                 });
191         // AO Created dyn attribute
192         dynAttrMap.put(CourseOfferingSetServiceConstants.AO_CREATED_RESULT_DYNATTR_KEY,
193                 new DynamicAttrWrapper<SocRolloverResultInfo>(result, Date.class,
194                         CourseOfferingSetServiceConstants.AO_CREATED_RESULT_DYNATTR_KEY) {
195 
196                     @Override
197                     public void readInStringValue(String attrValue) {
198                         DynAttrConverter<Integer> intConverter = (DynAttrConverter<Integer>) TransformUtility.getConverterByClass(Integer.class);
199                         Integer intVal = intConverter.convertStringValueToNative(attrValue);
200                         getDto().setActivityOfferingsCreated(intVal);
201                     }
202 
203                     @Override
204                     public String writeOutStringValue() {
205                         DynAttrConverter<Integer> intConverter = (DynAttrConverter<Integer>) TransformUtility.getConverterByClass(Integer.class);
206                         String intStr = intConverter.convertNativeValueToString(getDto().getActivityOfferingsCreated());
207                         return intStr;
208                     }
209                 });
210         // AO Created dyn attribute
211         dynAttrMap.put(CourseOfferingSetServiceConstants.AO_SKIPPED_RESULT_DYNATTR_KEY,
212                 new DynamicAttrWrapper<SocRolloverResultInfo>(result, Date.class,
213                         CourseOfferingSetServiceConstants.AO_SKIPPED_RESULT_DYNATTR_KEY) {
214 
215                     @Override
216                     public void readInStringValue(String attrValue) {
217                         DynAttrConverter<Integer> intConverter = (DynAttrConverter<Integer>) TransformUtility.getConverterByClass(Integer.class);
218                         Integer intVal = intConverter.convertStringValueToNative(attrValue);
219                         getDto().setActivityOfferingsSkipped(intVal);
220                     }
221 
222                     @Override
223                     public String writeOutStringValue() {
224                         DynAttrConverter<Integer> intConverter = (DynAttrConverter<Integer>) TransformUtility.getConverterByClass(Integer.class);
225                         String intStr = intConverter.convertNativeValueToString(getDto().getActivityOfferingsSkipped());
226                         return intStr;
227                     }
228                 });
229         return dynAttrMap;
230     }
231 
232     private static Map<String, SocRolloverResultAttributeEntity> _createEntityAttrMap(SocRolloverResultEntity entity) {
233         Map<String, SocRolloverResultAttributeEntity> entityAttrMap = new HashMap<String, SocRolloverResultAttributeEntity>();
234         if (entity.getAttributes() != null) {
235             for (SocRolloverResultAttributeEntity attrEnt: entity.getAttributes()) {
236                 entityAttrMap.put(attrEnt.getKey(), attrEnt);
237             }
238         }
239         return entityAttrMap;
240     }
241 
242     public static void copyDtoDynAttrsToEntity(SocRolloverResult result, SocRolloverResultEntity entity) {
243         Map<String, DynamicAttrReadOnlyWrapper<SocRolloverResult>> dynAttrMap = _createDynAttrReadOnlyMap(result);
244         Map<String, SocRolloverResultAttributeEntity> entityAttrMap = _createEntityAttrMap(entity);
245         //Code Changed for JIRA-8997 - SONAR Critical issues - Performance - Inefficient use of keySet iterator instead of entrySet iterator
246         for(Map.Entry<String, DynamicAttrReadOnlyWrapper<SocRolloverResult>> entry: dynAttrMap.entrySet()){
247             String attrName =  entry.getKey();
248             SocRolloverResultAttributeEntity attrEntity = entityAttrMap.get(attrName);
249             DynamicAttrReadOnlyWrapper<SocRolloverResult> wrapper = entry.getValue();
250             if (attrEntity == null) {
251                 // Can't find this in the list of attributes, so create a new one
252                 AttributeInfo attr = new AttributeInfo(attrName, wrapper.writeOutStringValue());
253                 SocRolloverResultAttributeEntity newAttrEntity = new SocRolloverResultAttributeEntity(attr, entity);
254                 entity.getAttributes().add(newAttrEntity);
255             } else {
256                 // Reuse the attribute
257                 attrEntity.setValue(wrapper.writeOutStringValue());
258             }
259         }
260     }
261 
262     public static void copyEntityAttrsToDtoDynAttrs(SocRolloverResultEntity entity, SocRolloverResultInfo info) {
263         Map<String, DynamicAttrWrapper<SocRolloverResultInfo>> dynAttrMap = _createDynAttrMap(info);
264         Map<String, SocRolloverResultAttributeEntity> entityAttrMap = _createEntityAttrMap(entity);
265         //Code Changed for JIRA-8997 - SONAR Critical issues - Performance - Inefficient use of keySet iterator instead of entrySet iterator
266         for(Map.Entry<String, DynamicAttrWrapper<SocRolloverResultInfo>> entry: dynAttrMap.entrySet()){
267             String attrName = entry.getKey();
268             SocRolloverResultAttributeEntity attrEntity = entityAttrMap.get(attrName);
269             DynamicAttrWrapper<SocRolloverResultInfo> wrapper = entry.getValue();
270             if (attrEntity != null) {
271                 // Reuse the attribute
272                 wrapper.readInStringValue(attrEntity.getValue());
273             }
274         }
275     }
276 }