1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.lum.service.assembler;
17
18 import org.kuali.student.core.assembly.BaseDTOAssemblyNode;
19 import org.kuali.student.core.assembly.BaseDTOAssemblyNode.NodeOperation;
20 import org.kuali.student.core.assembly.data.AssemblyException;
21 import org.kuali.student.core.dto.RichTextInfo;
22 import org.kuali.student.core.exceptions.DoesNotExistException;
23 import org.kuali.student.core.exceptions.InvalidParameterException;
24 import org.kuali.student.core.exceptions.MissingParameterException;
25 import org.kuali.student.core.exceptions.OperationFailedException;
26 import org.kuali.student.lum.course.dto.LoDisplayInfo;
27 import org.kuali.student.lum.course.service.assembler.LoAssembler;
28 import org.kuali.student.lum.lo.dto.LoInfo;
29 import org.kuali.student.lum.lo.service.LearningObjectiveService;
30 import org.kuali.student.lum.lu.dto.CluLoRelationInfo;
31 import org.kuali.student.lum.lu.dto.CluResultInfo;
32 import org.kuali.student.lum.lu.dto.ResultOptionInfo;
33 import org.kuali.student.lum.lu.service.LuService;
34
35 import java.util.ArrayList;
36 import java.util.Date;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Map.Entry;
41
42
43
44
45
46
47
48 public class CluAssemblerUtils {
49 private LuService luService;
50 private LearningObjectiveService loService;
51 private LoAssembler loAssembler;
52
53 public List<String> assembleCluResults(List<String> resultTypes, List<CluResultInfo> cluResults) throws AssemblyException{
54 if(resultTypes==null){
55 throw new AssemblyException("result types can not be null");
56 }
57 List<String> results = new ArrayList<String>();
58
59 for(CluResultInfo cluResult:cluResults){
60 if(resultTypes.contains(cluResult.getType())){
61
62 for(ResultOptionInfo resultOption: cluResult.getResultOptions()){
63 results.add(resultOption.getResultComponentId());
64 }
65 }
66 }
67 return results;
68 }
69
70 public BaseDTOAssemblyNode<?, ?> disassembleCluResults(String cluId,
71 String cluState, List<String> options, NodeOperation operation, String resultType,
72 String resultsDescription, String resultDescription) throws AssemblyException {
73 BaseDTOAssemblyNode<List<String>, CluResultInfo> cluResultNode = new BaseDTOAssemblyNode<List<String>, CluResultInfo>(null);
74 if(resultType==null){
75 throw new AssemblyException("resultType can not be null");
76 }
77
78
79 Map<String, ResultOptionInfo> currentResults = new HashMap<String, ResultOptionInfo>();
80
81 CluResultInfo cluResult = null;
82
83
84
85 if (!NodeOperation.CREATE.equals(operation)) {
86 try {
87 List<CluResultInfo> cluResultList = luService.getCluResultByClu(cluId);
88
89 for (CluResultInfo currentResult : cluResultList) {
90 if (resultType.equals(currentResult.getType())) {
91 cluResult = currentResult;
92 if(NodeOperation.DELETE.equals(operation)){
93
94 cluResultNode.setOperation(NodeOperation.DELETE);
95 }else{
96
97 cluResultNode.setOperation(NodeOperation.UPDATE);
98 for(ResultOptionInfo resultOption:currentResult.getResultOptions()){
99 currentResults.put(resultOption.getResultComponentId(), resultOption);
100 }
101 }
102 }
103 }
104 } catch (DoesNotExistException e) {
105 } catch (InvalidParameterException e) {
106 throw new AssemblyException("Error getting related " + resultsDescription, e);
107 } catch (MissingParameterException e) {
108 throw new AssemblyException("Error getting related " + resultsDescription, e);
109 } catch (OperationFailedException e) {
110 throw new AssemblyException("Error getting related " + resultsDescription, e);
111 }
112 }
113
114
115 if(!NodeOperation.DELETE.equals(operation)){
116 if(cluResult == null){
117
118 cluResult = new CluResultInfo();
119 cluResult.setCluId(cluId);
120 cluResult.setState(cluState);
121 cluResult.setType(resultType);
122 RichTextInfo desc = new RichTextInfo();
123 desc.setPlain(resultsDescription);
124 cluResult.setDesc(desc);
125 cluResult.setEffectiveDate(new Date());
126 cluResultNode.setOperation(NodeOperation.CREATE);
127 }
128
129 cluResult.setResultOptions(new ArrayList<ResultOptionInfo>());
130
131
132 for (String optionType : options) {
133 if(currentResults.containsKey(optionType)){
134
135 ResultOptionInfo resultOptionInfo = currentResults.get(optionType);
136 cluResult.getResultOptions().add(resultOptionInfo);
137 }else{
138
139 ResultOptionInfo resultOptionInfo = new ResultOptionInfo();
140 RichTextInfo desc = new RichTextInfo();
141 desc.setPlain(resultDescription);
142 resultOptionInfo.setDesc(desc);
143 resultOptionInfo.setResultComponentId(optionType);
144 resultOptionInfo.setState(cluState);
145
146 cluResult.getResultOptions().add(resultOptionInfo);
147 }
148 }
149 }
150
151 cluResultNode.setNodeData(cluResult);
152 return cluResultNode;
153 }
154
155 public List<LoDisplayInfo> assembleLos(String cluId, boolean shallowBuild) throws AssemblyException {
156 List<LoDisplayInfo> loInfos = new ArrayList<LoDisplayInfo>();
157 try {
158 List<CluLoRelationInfo> cluLoRelations = luService.getCluLoRelationsByClu(cluId);
159 for (CluLoRelationInfo cluLoRelation : cluLoRelations) {
160 String loId = cluLoRelation.getLoId();
161 LoInfo lo = loService.getLo(loId);
162 loInfos.add(loAssembler.assemble(lo, null, shallowBuild));
163 }
164 } catch (Exception e) {
165 throw new AssemblyException("Error getting learning objectives", e);
166 }
167
168 return loInfos;
169 }
170
171 public List<BaseDTOAssemblyNode<?, ?>> disassembleLos(String cluId, String cluState, List<LoDisplayInfo> loInfos,
172 NodeOperation operation) throws AssemblyException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException{
173
174 List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
175
176
177
178 Map<String, CluLoRelationInfo> currentCluLoRelations = new HashMap<String, CluLoRelationInfo>();
179 try {
180 List<CluLoRelationInfo> cluLoRelations = luService.getCluLoRelationsByClu(cluId);
181 for(CluLoRelationInfo cluLoRelation:cluLoRelations){
182 if(CluAssemblerConstants.CLU_LO_CLU_SPECIFIC_RELATION.equals(cluLoRelation.getType())){
183 currentCluLoRelations.put(cluLoRelation.getLoId(), cluLoRelation);
184 }
185 }
186 } catch (DoesNotExistException e) {
187 } catch (Exception e) {
188 throw new AssemblyException("Error finding related Los", e);
189 }
190
191
192 for(LoDisplayInfo loDisplay : loInfos){
193
194
195 if (NodeOperation.CREATE == operation
196 || (NodeOperation.UPDATE == operation && !currentCluLoRelations.containsKey(loDisplay.getLoInfo().getId()))) {
197
198
199
200 loDisplay.getLoInfo().setId(null);
201 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = loAssembler
202 .disassemble(loDisplay, NodeOperation.CREATE);
203 results.add(loNode);
204
205
206 CluLoRelationInfo relation = new CluLoRelationInfo();
207 relation.setCluId(cluId);
208 relation.setLoId(loNode.getNodeData().getId());
209 relation.setType(CluAssemblerConstants.CLU_LO_CLU_SPECIFIC_RELATION);
210 relation.setState(cluState);
211
212 BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo> relationNode = new BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo>(
213 null);
214 relationNode.setNodeData(relation);
215 relationNode.setOperation(NodeOperation.CREATE);
216
217 results.add(relationNode);
218 } else if (NodeOperation.UPDATE == operation
219 && currentCluLoRelations.containsKey(loDisplay.getLoInfo().getId())) {
220
221 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = loAssembler
222 .disassemble(loDisplay, NodeOperation.UPDATE);
223 results.add(loNode);
224
225
226
227 currentCluLoRelations.remove(loDisplay.getLoInfo().getId());
228 } else if (NodeOperation.DELETE == operation
229 && currentCluLoRelations.containsKey(loDisplay.getLoInfo().getId())) {
230
231
232 CluLoRelationInfo relationToDelete = currentCluLoRelations.get(loDisplay.getLoInfo().getId());
233 BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo>(
234 null);
235 relationToDeleteNode.setNodeData(relationToDelete);
236 relationToDeleteNode.setOperation(NodeOperation.DELETE);
237 results.add(relationToDeleteNode);
238
239 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = loAssembler
240 .disassemble(loDisplay, NodeOperation.DELETE);
241 results.add(loNode);
242
243
244
245 currentCluLoRelations.remove(loDisplay.getLoInfo().getId());
246 }
247 }
248
249
250
251 for (Entry<String, CluLoRelationInfo> entry : currentCluLoRelations.entrySet()) {
252
253
254 CluLoRelationInfo relationToDelete = entry.getValue();
255 BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo>(
256 null);
257 relationToDeleteNode.setNodeData(relationToDelete);
258 relationToDeleteNode.setOperation(NodeOperation.DELETE);
259 results.add(relationToDeleteNode);
260
261 LoInfo loToDelete = loService.getLo(entry.getKey());
262 LoDisplayInfo loDisplayToDelete = loAssembler.assemble(loToDelete, null, false);
263 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = loAssembler
264 .disassemble(loDisplayToDelete, NodeOperation.DELETE);
265 results.add(loNode);
266 }
267
268 return results;
269 }
270
271
272 public void setLuService(LuService luService) {
273 this.luService = luService;
274 }
275
276 public void setLoService(LearningObjectiveService loService) {
277 this.loService = loService;
278 }
279
280 public void setLoAssembler(LoAssembler loAssembler) {
281 this.loAssembler = loAssembler;
282 }
283 }