1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.lum.course.service.assembler;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Map.Entry;
23
24 import org.apache.log4j.Logger;
25 import org.kuali.student.common.assembly.BOAssembler;
26 import org.kuali.student.common.assembly.BaseDTOAssemblyNode;
27 import org.kuali.student.common.assembly.BaseDTOAssemblyNode.NodeOperation;
28 import org.kuali.student.common.assembly.data.AssemblyException;
29 import org.kuali.student.common.exceptions.DoesNotExistException;
30 import org.kuali.student.common.exceptions.InvalidParameterException;
31 import org.kuali.student.common.exceptions.MissingParameterException;
32 import org.kuali.student.common.exceptions.OperationFailedException;
33 import org.kuali.student.common.util.UUIDHelper;
34 import org.kuali.student.lum.course.dto.ActivityInfo;
35 import org.kuali.student.lum.course.dto.CourseInfo;
36 import org.kuali.student.lum.course.dto.FormatInfo;
37 import org.kuali.student.lum.lu.dto.CluCluRelationInfo;
38 import org.kuali.student.lum.lu.dto.CluInfo;
39 import org.kuali.student.lum.lu.service.LuService;
40
41
42
43
44
45
46
47
48 public class FormatAssembler implements BOAssembler<FormatInfo, CluInfo> {
49 final static Logger LOG = Logger.getLogger(FormatAssembler.class);
50
51 private BOAssembler<ActivityInfo, CluInfo> activityAssembler;
52 private LuService luService;
53
54 @Override
55 public FormatInfo assemble(CluInfo clu, FormatInfo formatInfo,
56 boolean shallowBuild) throws AssemblyException {
57
58 if (clu == null) {
59 return null;
60 }
61
62 FormatInfo format = (null != formatInfo) ? formatInfo
63 : new FormatInfo();
64
65
66 format.setId(clu.getId());
67 format.setType(clu.getType());
68 format.setState(clu.getState());
69 format.setMetaInfo(clu.getMetaInfo());
70 format.setAttributes(clu.getAttributes());
71 format.setDuration(clu.getStdDuration());
72 format.setTermsOffered(clu.getOfferedAtpTypes());
73
74
75 if (!shallowBuild) {
76
77
78 try {
79 List<CluInfo> activities = luService.getRelatedClusByCluId(
80 format.getId(),
81 CourseAssemblerConstants.COURSE_ACTIVITY_RELATION_TYPE);
82 for (CluInfo activity : activities) {
83 ActivityInfo activityInfo = activityAssembler.assemble(
84 activity, null, false);
85 format.getActivities().add(activityInfo);
86 }
87 } catch (DoesNotExistException e) {
88 } catch (Exception e) {
89 throw new AssemblyException("Error getting related activities", e);
90 }
91 }
92 return format;
93 }
94
95 @Override
96 public BaseDTOAssemblyNode<FormatInfo, CluInfo> disassemble(
97 FormatInfo format, NodeOperation operation)
98 throws AssemblyException {
99 BaseDTOAssemblyNode<FormatInfo, CluInfo> result = new BaseDTOAssemblyNode<FormatInfo, CluInfo>(
100 this);
101 if (format == null) {
102
103
104 throw new AssemblyException("Format can not be null");
105 }
106 if (NodeOperation.CREATE != operation && null == format.getId()) {
107 throw new AssemblyException("Course Format Shell's id can not be null");
108 }
109
110 CluInfo clu;
111 try {
112 clu = (NodeOperation.UPDATE == operation) ? clu = luService.getClu(format.getId()) : new CluInfo();
113 } catch (Exception e) {
114 throw new AssemblyException("Error retrieving course format shell during update", e);
115 }
116
117
118 clu.setId(UUIDHelper.genStringUUID(format.getId()));
119
120
121
122
123 clu.setType(CourseAssemblerConstants.COURSE_FORMAT_TYPE);
124 clu.setState(format.getState());
125 clu.setMetaInfo(format.getMetaInfo());
126 clu.setAttributes(format.getAttributes());
127 clu.setStdDuration(format.getDuration());
128 clu.setOfferedAtpTypes(format.getTermsOffered());
129
130
131 result.setNodeData(clu);
132 result.setOperation(operation);
133 result.setBusinessDTORef(format);
134
135
136
137 List<BaseDTOAssemblyNode<?, ?>> activityResults;
138 try {
139 activityResults = disassembleActivities(clu.getId(),
140 format, operation);
141 result.getChildNodes().addAll(activityResults);
142
143 } catch (Exception e) {
144 throw new AssemblyException("Error while disassembling format", e);
145 }
146
147 return result;
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 private List<BaseDTOAssemblyNode<?, ?>> disassembleActivities(String nodeId,
173 FormatInfo format, NodeOperation operation)
174 throws AssemblyException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
175 List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
176
177
178
179 Map<String, String> currentActivityIds = new HashMap<String, String>();
180
181 if (!NodeOperation.CREATE.equals(operation)) {
182 try {
183 List<CluCluRelationInfo> activityRelationships = luService
184 .getCluCluRelationsByClu(format.getId());
185
186 for (CluCluRelationInfo activityRelation : activityRelationships) {
187 if (CourseAssemblerConstants.COURSE_ACTIVITY_RELATION_TYPE
188 .equals(activityRelation.getType())) {
189 currentActivityIds.put(activityRelation
190 .getRelatedCluId(), activityRelation.getId());
191 }
192 }
193 } catch (DoesNotExistException e) {
194 } catch (Exception e) {
195 throw new AssemblyException("Error getting related activities", e);
196 }
197 }
198
199
200 for (ActivityInfo activity : format.getActivities()) {
201
202
203 if (NodeOperation.CREATE == operation
204 || (NodeOperation.UPDATE == operation && !currentActivityIds.containsKey(activity.getId()))) {
205 activity.setState(format.getState());
206
207
208 BaseDTOAssemblyNode<ActivityInfo, CluInfo> activityNode = activityAssembler
209 .disassemble(activity, NodeOperation.CREATE);
210 results.add(activityNode);
211
212
213 CluCluRelationInfo relation = new CluCluRelationInfo();
214 relation.setCluId(nodeId);
215 relation.setRelatedCluId(activityNode.getNodeData().getId());
216
217
218
219 relation
220 .setType(CourseAssemblerConstants.COURSE_ACTIVITY_RELATION_TYPE);
221 relation.setState(format.getState());
222
223 BaseDTOAssemblyNode<FormatInfo, CluCluRelationInfo> relationNode = new BaseDTOAssemblyNode<FormatInfo, CluCluRelationInfo>(
224 null);
225 relationNode.setNodeData(relation);
226 relationNode.setOperation(NodeOperation.CREATE);
227
228 results.add(relationNode);
229 } else if (NodeOperation.UPDATE == operation
230 && currentActivityIds.containsKey(activity.getId())) {
231
232
233 activity.setState(format.getState());
234 BaseDTOAssemblyNode<ActivityInfo, CluInfo> activityNode = activityAssembler
235 .disassemble(activity, NodeOperation.UPDATE);
236 results.add(activityNode);
237
238
239
240 currentActivityIds.remove(activity.getId());
241 } else if (NodeOperation.DELETE == operation
242 && currentActivityIds.containsKey(activity.getId())) {
243
244
245 CluCluRelationInfo relationToDelete = new CluCluRelationInfo();
246 relationToDelete.setId( currentActivityIds.get(activity.getId()) );
247 BaseDTOAssemblyNode<CourseInfo, CluCluRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<CourseInfo, CluCluRelationInfo>(
248 null);
249 relationToDeleteNode.setNodeData(relationToDelete);
250 relationToDeleteNode.setOperation(NodeOperation.DELETE);
251 results.add(relationToDeleteNode);
252
253 BaseDTOAssemblyNode<ActivityInfo, CluInfo> formatNode = activityAssembler
254 .disassemble(activity, NodeOperation.DELETE);
255 results.add(formatNode);
256
257
258
259 currentActivityIds.remove(activity.getId());
260 }
261 }
262
263
264
265 for (Entry<String, String> entry : currentActivityIds.entrySet()) {
266
267
268 CluCluRelationInfo relationToDelete = new CluCluRelationInfo();
269 relationToDelete.setId(entry.getValue());
270 BaseDTOAssemblyNode<FormatInfo, CluCluRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<FormatInfo, CluCluRelationInfo>(
271 null);
272 relationToDeleteNode.setNodeData(relationToDelete);
273 relationToDeleteNode.setOperation(NodeOperation.DELETE);
274 results.add(relationToDeleteNode);
275
276 CluInfo activityCluToDelete = luService.getClu(entry.getKey());
277 ActivityInfo activityToDelete = activityAssembler.assemble(activityCluToDelete, null, false);
278 BaseDTOAssemblyNode<ActivityInfo, CluInfo> activityNode = activityAssembler
279 .disassemble(activityToDelete, NodeOperation.DELETE);
280 results.add(activityNode);
281 }
282
283
284 return results;
285 }
286
287 public BOAssembler<ActivityInfo, CluInfo> getActivityAssembler() {
288 return activityAssembler;
289 }
290
291 public void setActivityAssembler(
292 BOAssembler<ActivityInfo, CluInfo> activityAssembler) {
293 this.activityAssembler = activityAssembler;
294 }
295
296 public LuService getLuService() {
297 return luService;
298 }
299
300 public void setLuService(LuService luService) {
301 this.luService = luService;
302 }
303 }