Clover Coverage Report - Kuali Student 1.2-SNAPSHOT (Aggregated)
Coverage timestamp: Thu Mar 3 2011 04:02:59 EST
../../../../../../../img/srcFileCovDistChart9.png 30% of files have more coverage
124   297   43   17.71
30   221   0.35   3.5
7     6.14  
2    
 
  LoAssembler       Line # 29 116 0% 39 20 86.4% 0.8639456
  LoAssembler.LoDisplayComparator       Line # 280 8 0% 4 4 71.4% 0.71428573
 
  (35)
 
1    package org.kuali.student.lum.course.service.assembler;
2   
3    import java.util.ArrayList;
4    import java.util.Collections;
5    import java.util.Comparator;
6    import java.util.HashMap;
7    import java.util.HashSet;
8    import java.util.List;
9    import java.util.Map;
10    import java.util.Set;
11    import java.util.Map.Entry;
12   
13    import org.kuali.student.common.assembly.BOAssembler;
14    import org.kuali.student.common.assembly.BaseDTOAssemblyNode;
15    import org.kuali.student.common.assembly.BaseDTOAssemblyNode.NodeOperation;
16    import org.kuali.student.common.assembly.data.AssemblyException;
17    import org.kuali.student.common.exceptions.DoesNotExistException;
18    import org.kuali.student.common.exceptions.InvalidParameterException;
19    import org.kuali.student.common.exceptions.MissingParameterException;
20    import org.kuali.student.common.exceptions.OperationFailedException;
21    import org.kuali.student.common.util.UUIDHelper;
22    import org.kuali.student.lum.course.dto.LoDisplayInfo;
23    import org.kuali.student.lum.lo.dto.LoCategoryInfo;
24    import org.kuali.student.lum.lo.dto.LoInfo;
25    import org.kuali.student.lum.lo.dto.LoLoRelationInfo;
26    import org.kuali.student.lum.lo.service.LearningObjectiveService;
27   
28   
 
29    public class LoAssembler implements BOAssembler<LoDisplayInfo, LoInfo> {
30   
31    private LearningObjectiveService loService;
32   
 
33  1059 toggle @Override
34    public LoDisplayInfo assemble(LoInfo lo, LoDisplayInfo loDisplayInfo,
35    boolean shallowBuild) throws AssemblyException {
36   
37  1059 LoDisplayInfo loDisplay = (null != loDisplayInfo) ? loDisplayInfo : new LoDisplayInfo();
38   
39  1059 loDisplay.setLoInfo(lo);
40   
41  1059 if (!shallowBuild) {
42  387 String loId = lo.getId();
43  387 try {
44  387 List<LoCategoryInfo> loCategories = loService.getLoCategoriesForLo(loId);
45  387 loDisplay.setLoCategoryInfoList(loCategories);
46    } catch (DoesNotExistException e) {
47    } catch (Exception e) {
48  0 throw new AssemblyException("Error getting learning objective categories", e);
49    }
50  387 try {
51  387 List<LoInfo> childLos = loService.getRelatedLosByLoId(loId,CourseAssemblerConstants.COURSE_LO_RELATION_INCLUDES);
52  387 for(LoInfo childLo:childLos){
53  304 LoDisplayInfo childLoDisplay = assemble(childLo, null, shallowBuild);
54  304 childLoDisplay.setParentLoRelationid(lo.getId());
55  304 childLoDisplay.setParentRelType(CourseAssemblerConstants.COURSE_LO_RELATION_INCLUDES);
56  304 loDisplay.getLoDisplayInfoList().add(childLoDisplay);
57    }
58  387 if(loDisplay.getLoDisplayInfoList().size()>1){
59  151 Collections.sort(loDisplay.getLoDisplayInfoList(), LoDisplayComparator.getInstance());
60    }
61    } catch (DoesNotExistException e) {
62    } catch (Exception e) {
63  0 throw new AssemblyException("Error getting learning objective", e);
64    }
65   
66    }
67  1059 return loDisplay;
68    }
69   
 
70  703 toggle @Override
71    //Creation of categories is done in the LoCategoryRpcGwtServlet
72    public BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> disassemble(
73    LoDisplayInfo loDisplay, NodeOperation operation)
74    throws AssemblyException {
75   
76  703 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> result = new BaseDTOAssemblyNode<LoDisplayInfo, LoInfo>(this);
77   
78    //see if this is a new LuInfo
79  703 if (loDisplay == null || loDisplay.getLoInfo() == null) {
80  0 throw new AssemblyException("Lo can not be null");
81    }
82  703 if (NodeOperation.CREATE != operation && null == loDisplay.getLoInfo().getId()) {
83  0 throw new AssemblyException("Lo id can not be null");
84    }
85   
86    //set the id if it's not there already(important for creating relations)
87  703 loDisplay.getLoInfo().setId(UUIDHelper.genStringUUID(loDisplay.getLoInfo().getId()));
88   
89    //Default these values
90  703 loDisplay.getLoInfo().setType(CourseAssemblerConstants.COURSE_LO_TYPE);
91  703 loDisplay.getLoInfo().setLoRepositoryKey(CourseAssemblerConstants.COURSE_LO_REPOSITORY_KEY);
92   
93   
94    //Populate the node
95  703 result.setBusinessDTORef(loDisplay);
96  703 result.setNodeData(loDisplay.getLoInfo());
97  703 result.setOperation(operation);
98   
99    //Process the child los
100  703 try {
101  703 List<BaseDTOAssemblyNode<?, ?>> childLoNodes = disassembleChildLos(loDisplay, operation);
102  703 result.getChildNodes().addAll(childLoNodes);
103    } catch (DoesNotExistException e) {
104    } catch (Exception e) {
105  0 throw new AssemblyException("Error disassembling child los", e);
106    }
107   
108    //Process the categories
109  703 try {
110  703 List<BaseDTOAssemblyNode<?, ?>> categoryNodes = disassembleCategories(loDisplay, operation);
111  703 result.getChildNodes().addAll(categoryNodes);
112    } catch (Exception e) {
113  0 throw new AssemblyException("Error disassembling categories", e);
114    }
115   
116  703 return result;
117    }
118   
 
119  703 toggle private List<BaseDTOAssemblyNode<?, ?>> disassembleCategories(
120    LoDisplayInfo loDisplay, NodeOperation operation) throws AssemblyException {
121   
122  703 List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
123   
124    //Category relations
125  703 Set<String> currentCategoryIds = new HashSet<String>();
126    //Get current relations
127  703 if (!NodeOperation.CREATE.equals(operation)) {
128  52 try {
129  52 List<LoCategoryInfo> categories = loService.getLoCategoriesForLo(loDisplay.getLoInfo().getId());
130  52 for (LoCategoryInfo category : categories) {
131  71 currentCategoryIds.add(category.getId());
132    }
133    } catch (DoesNotExistException e) {
134    } catch (Exception e) {
135  0 throw new AssemblyException("Error getting categories", e);
136    }
137    }
138    //Update
139  703 for (LoCategoryInfo category : loDisplay.getLoCategoryInfoList()) {
140   
141    // If this is a format create/new activity update then all activities will be created
142  1360 if (NodeOperation.CREATE == operation
143    || (NodeOperation.UPDATE == operation && !currentCategoryIds.contains(category.getId()))) {
144   
145  1289 LoCategoryRelationInfo loCategoryRelation = new LoCategoryRelationInfo();
146  1289 loCategoryRelation.setCategoryId(category.getId());
147  1289 loCategoryRelation.setLoId(loDisplay.getLoInfo().getId());
148   
149  1289 BaseDTOAssemblyNode<LoDisplayInfo, LoCategoryRelationInfo> relationToAddNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoCategoryRelationInfo>(null);
150  1289 relationToAddNode.setNodeData(loCategoryRelation);
151  1289 relationToAddNode.setOperation(NodeOperation.CREATE);
152  1289 results.add(relationToAddNode);
153   
154  1289 currentCategoryIds.remove(category.getId());
155  71 } else if (NodeOperation.UPDATE == operation
156    && currentCategoryIds.contains(category.getId())) {
157  29 currentCategoryIds.remove(category.getId());
158    }
159    }
160    //Delete leftovers
161  703 for(String categoryId:currentCategoryIds){
162  42 LoCategoryRelationInfo loCategoryRelation = new LoCategoryRelationInfo();
163  42 loCategoryRelation.setCategoryId(categoryId);
164  42 loCategoryRelation.setLoId(loDisplay.getLoInfo().getId());
165   
166  42 BaseDTOAssemblyNode<LoDisplayInfo, LoCategoryRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoCategoryRelationInfo>(null);
167  42 relationToDeleteNode.setNodeData(loCategoryRelation);
168  42 relationToDeleteNode.setOperation(NodeOperation.DELETE);
169  42 results.add(relationToDeleteNode);
170    }
171   
172  703 return results;
173    }
174   
 
175  703 toggle private List<BaseDTOAssemblyNode<?, ?>> disassembleChildLos(LoDisplayInfo loDisplay, NodeOperation operation) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, AssemblyException{
176  703 List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
177  703 Map<String,LoLoRelationInfo> currentLoRelations = new HashMap<String,LoLoRelationInfo>();
178    //Make lu lu relations
179  703 if (!NodeOperation.CREATE.equals(operation)) {
180  52 try {
181  52 List<LoLoRelationInfo> loRelations = loService.getLoLoRelationsByLoId(loDisplay.getLoInfo().getId());
182  52 for (LoLoRelationInfo loRelation : loRelations) {
183    //getLoLoRelationsByLoId returns if the lo is related or if it is the owner(this seems wrong)
184  66 if(CourseAssemblerConstants.COURSE_LO_RELATION_INCLUDES.equals(loRelation.getType())&&
185    !loDisplay.getLoInfo().getId().equals(loRelation.getRelatedLoId())){
186  30 currentLoRelations.put(loRelation.getRelatedLoId(), loRelation);
187    }
188    }
189    } catch (DoesNotExistException e) {
190    } catch (Exception e) {
191  0 throw new AssemblyException("Error getting categories", e);
192    }
193    }
194   
195    // Loop through all the activities in this format
196  703 for (LoDisplayInfo childDisplay : loDisplay.getLoDisplayInfoList()) {
197   
198    // If this is a format create/new activity update then all activities will be created
199  582 if (NodeOperation.CREATE == operation
200    || (NodeOperation.UPDATE == operation && !currentLoRelations.containsKey(childDisplay.getLoInfo().getId()))) {
201   
202    // the lo does not exist, so create
203    // Assemble and add the lo
204  552 childDisplay.getLoInfo().setId(null);
205  552 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = this
206    .disassemble(childDisplay, NodeOperation.CREATE);
207  552 results.add(loNode);
208   
209    // Create the relationship and add it as well
210  552 LoLoRelationInfo relation = new LoLoRelationInfo();
211  552 relation.setLoId(loDisplay.getLoInfo().getId());
212  552 relation.setRelatedLoId(loNode.getNodeData().getId());
213  552 relation.setType(CourseAssemblerConstants.COURSE_LO_RELATION_INCLUDES);
214  552 relation.setState(loDisplay.getLoInfo().getState());
215   
216   
217  552 BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo> relationNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo>(
218    null);
219  552 relationNode.setNodeData(relation);
220  552 relationNode.setOperation(NodeOperation.CREATE);
221   
222  552 results.add(relationNode);
223  30 } else if (NodeOperation.UPDATE == operation
224    && currentLoRelations.containsKey(childDisplay.getLoInfo().getId())) {
225    // If the lo already has this child lo, then just update the
226    // child lo
227  12 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = this
228    .disassemble(childDisplay, NodeOperation.UPDATE);
229  12 results.add(loNode);
230   
231    // remove this entry from the map so we can tell what needs to
232    // be deleted at the end
233  12 currentLoRelations.remove(childDisplay.getLoInfo().getId());
234  18 } else if (NodeOperation.DELETE == operation
235    && currentLoRelations.containsKey(childDisplay.getLoInfo().getId())) {
236   
237    // Delete the Format and its relation
238  18 LoLoRelationInfo relationToDelete = currentLoRelations.get(childDisplay.getLoInfo().getId());
239  18 BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo>(
240    null);
241  18 relationToDeleteNode.setNodeData(relationToDelete);
242  18 relationToDeleteNode.setOperation(NodeOperation.DELETE);
243  18 results.add(relationToDeleteNode);
244   
245  18 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = this
246    .disassemble(childDisplay, NodeOperation.DELETE);
247  18 results.add(loNode);
248   
249    // remove this entry from the map so we can tell what needs to
250    // be deleted at the end
251  18 currentLoRelations.remove(childDisplay.getLoInfo().getId());
252    }
253    }
254   
255    // Now any leftover lo ids are no longer needed, so delete
256    // los and relations
257  703 for (Entry<String, LoLoRelationInfo> entry : currentLoRelations.entrySet()) {
258    // Create a new relation with the id of the relation we want to
259    // delete
260  0 LoLoRelationInfo relationToDelete = entry.getValue();
261  0 BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo>(
262    null);
263  0 relationToDeleteNode.setNodeData(relationToDelete);
264  0 relationToDeleteNode.setOperation(NodeOperation.DELETE);
265  0 results.add(relationToDeleteNode);
266   
267  0 LoInfo loToDelete = loService.getLo(entry.getKey());
268  0 LoDisplayInfo loDisplayToDelete = this.assemble(loToDelete, null, false);
269  0 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = this.disassemble(loDisplayToDelete, NodeOperation.DELETE);
270  0 results.add(loNode);
271    }
272  703 return results;
273   
274    }
275   
 
276  2 toggle public void setLoService(LearningObjectiveService loService) {
277  2 this.loService = loService;
278    }
279   
 
280    public static class LoDisplayComparator implements Comparator<LoDisplayInfo>{
281    private static LoDisplayComparator instance = new LoDisplayComparator();
 
282  151 toggle @Override
283    public int compare(LoDisplayInfo o1, LoDisplayInfo o2) {
284  151 String o1Sequence = o1.getLoInfo().getAttributes().get(CourseAssemblerConstants.COURSE_LO_SEQUENCE);
285  151 String o2Sequence = o1.getLoInfo().getAttributes().get(CourseAssemblerConstants.COURSE_LO_SEQUENCE);
286  151 if(o1Sequence!=null){
287  0 return o1Sequence.compareTo(o2Sequence);
288  151 }else if(o2Sequence!=null){
289  0 return -1;
290    }
291  151 return 0;
292    }
 
293  151 toggle public static LoDisplayComparator getInstance() {
294  151 return instance;
295    }
296    }
297    }