View Javadoc
1   package org.kuali.student.r2.lum.service.assembler;
2   
3   import org.kuali.student.r1.common.assembly.BaseDTOAssemblyNode;
4   import org.kuali.student.r1.common.assembly.BaseDTOAssemblyNode.NodeOperation;
5   import org.kuali.student.r1.common.assembly.BusinessServiceMethodInvoker;
6   import org.kuali.student.r1.core.statement.dto.RefStatementRelationInfo;
7   import org.kuali.student.r1.core.statement.dto.ReqComponentInfo;
8   import org.kuali.student.r1.core.statement.dto.StatementInfo;
9   import org.kuali.student.r1.core.statement.dto.StatementTreeViewInfo;
10  import org.kuali.student.r1.core.statement.service.StatementService;
11  import org.kuali.student.r2.common.assembler.AssemblyException;
12  import org.kuali.student.r2.common.dto.ContextInfo;
13  import org.kuali.student.r2.common.exceptions.AlreadyExistsException;
14  import org.kuali.student.r2.common.exceptions.CircularReferenceException;
15  import org.kuali.student.r2.common.exceptions.CircularRelationshipException;
16  import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
17  import org.kuali.student.r2.common.exceptions.DependentObjectsExistException;
18  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
19  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
20  import org.kuali.student.r2.common.exceptions.MissingParameterException;
21  import org.kuali.student.r2.common.exceptions.OperationFailedException;
22  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
23  import org.kuali.student.r2.common.exceptions.ReadOnlyException;
24  import org.kuali.student.r2.common.exceptions.UnsupportedActionException;
25  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
26  import org.kuali.student.r2.core.atp.service.AtpService;
27  import org.kuali.student.r2.core.organization.service.OrganizationService;
28  import org.kuali.student.r2.lum.clu.dto.CluCluRelationInfo;
29  import org.kuali.student.r2.lum.clu.dto.CluInfo;
30  import org.kuali.student.r2.lum.clu.dto.CluLoRelationInfo;
31  import org.kuali.student.r2.lum.clu.dto.CluPublicationInfo;
32  import org.kuali.student.r2.lum.clu.dto.CluResultInfo;
33  import org.kuali.student.r2.lum.clu.service.CluService;
34  import org.kuali.student.r2.lum.course.service.assembler.LoCategoryRelationInfo;
35  import org.kuali.student.r2.lum.lo.dto.LoInfo;
36  import org.kuali.student.r2.lum.lo.dto.LoLoRelationInfo;
37  import org.kuali.student.r2.lum.lo.service.LearningObjectiveService;
38  import org.kuali.student.r2.lum.lrc.dto.ResultValuesGroupInfo;
39  import org.kuali.student.r2.lum.lrc.service.LRCService;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  
46  public class LumServiceMethodInvoker implements BusinessServiceMethodInvoker {
47  	private static final Logger LOG = LoggerFactory.getLogger(LumServiceMethodInvoker.class);
48  	private CluService cluService;
49  	private StatementService statementService;
50  	private LearningObjectiveService loService;
51  	private OrganizationService orgService;
52  	private AtpService atpService;
53  	private LRCService lrcService;
54  
55  	@SuppressWarnings("unchecked")
56      @Override
57  	public final void invokeServiceCalls(BaseDTOAssemblyNode results, ContextInfo contextInfo)
58  			throws DependentObjectsExistException, CircularRelationshipException,
59  			AssemblyException, UnsupportedActionException, UnsupportedOperationException, CircularReferenceException, ReadOnlyException, DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException, AlreadyExistsException {
60  
61  	    // For Delete operation process the tree from bottom up
62  	    if(NodeOperation.DELETE == results.getOperation()) {
63              for(BaseDTOAssemblyNode childNode: (List<BaseDTOAssemblyNode>) results.getChildNodes()){
64                  invokeServiceCalls(childNode,contextInfo);
65              }
66  	    }
67  
68  	    invokeServiceCallOnResult(results, contextInfo);
69  
70  		// For create/update process the child nodes from top to bottom
71  		if(NodeOperation.DELETE != results.getOperation()) {
72  		    for(BaseDTOAssemblyNode childNode: (List<BaseDTOAssemblyNode>) results.getChildNodes()){
73  		        invokeServiceCalls(childNode,contextInfo);
74  		    }
75  		}
76  	}
77  
78  	/**
79  	 * @param results
80  	 * @throws AlreadyExistsException
81  	 * @throws DataValidationErrorException
82  	 * @throws DoesNotExistException
83  	 * @throws InvalidParameterException
84  	 * @throws MissingParameterException
85  	 * @throws OperationFailedException
86  	 * @throws PermissionDeniedException
87  	 * @throws AssemblyException
88  	 * @throws VersionMismatchException
89  	 * @throws DependentObjectsExistException
90  	 * @throws CircularRelationshipException
91  	 * @throws UnsupportedActionException
92  	 * @throws UnsupportedOperationException
93  	 * @throws CircularReferenceException
94  	 * @throws ReadOnlyException
95  	 * @throws org.kuali.student.r2.common.exceptions.DataValidationErrorException
96  	 * @throws org.kuali.student.r2.common.exceptions.DoesNotExistException
97  	 * @throws org.kuali.student.r2.common.exceptions.InvalidParameterException
98  	 * @throws org.kuali.student.r2.common.exceptions.MissingParameterException
99  	 * @throws org.kuali.student.r2.common.exceptions.OperationFailedException
100 	 * @throws org.kuali.student.r2.common.exceptions.PermissionDeniedException
101 	 * @throws org.kuali.student.r2.common.exceptions.VersionMismatchException
102 	 * @throws org.kuali.student.r2.common.exceptions.AlreadyExistsException
103 	 */
104 	protected void invokeServiceCallOnResult(BaseDTOAssemblyNode results, ContextInfo contextInfo)
105 			throws AlreadyExistsException, DataValidationErrorException,
106 			DoesNotExistException, InvalidParameterException,
107 			MissingParameterException, OperationFailedException,
108 			PermissionDeniedException, AssemblyException,
109 			VersionMismatchException, DependentObjectsExistException,
110 			CircularRelationshipException, UnsupportedActionException,
111 			UnsupportedOperationException, CircularReferenceException, ReadOnlyException, DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException, AlreadyExistsException {
112 		Object nodeData = results.getNodeData();
113 		if (nodeData == null) {
114 			return;
115 		}
116 
117         LOG.debug("{}: {}", results.getOperation(), nodeData);
118 
119 		if(nodeData instanceof CluInfo){
120 			CluInfo clu = (CluInfo) nodeData;
121 			switch(results.getOperation()){
122 			case CREATE:
123 				CluInfo newClu = cluService.createClu(clu.getTypeKey(), clu, contextInfo);
124 				if(results.getAssembler() != null) {
125 					results.getAssembler().assemble(newClu, results.getBusinessDTORef(), true, contextInfo);
126 				}
127 				break;
128 			case UPDATE:
129 				CluInfo updatedClu = cluService.updateClu(clu.getId(), clu, contextInfo);
130 				if(results.getAssembler() != null) {
131 					results.getAssembler().assemble(updatedClu, results.getBusinessDTORef(), true, contextInfo);
132 				}
133 				break;
134 			case DELETE:
135 				cluService.deleteClu(clu.getId(), contextInfo);
136 				break;
137 			}
138 		}else if(nodeData instanceof CluCluRelationInfo){
139 			CluCluRelationInfo  relation = (CluCluRelationInfo) nodeData;
140 			switch(results.getOperation()){
141 			case CREATE:
142 				CluCluRelationInfo newCluRel = cluService.createCluCluRelation(relation.getCluId(), relation.getRelatedCluId(), relation.getTypeKey(), relation, contextInfo);
143 				// Update the businessDTO if one exists for the cluclurelation (for e.g. CourseJointInfo)
144 				if(null != results.getBusinessDTORef()) {
145 					results.getAssembler().assemble(newCluRel, results.getBusinessDTORef(), true, contextInfo);
146 				}
147 				break;
148 			case UPDATE:
149 				CluCluRelationInfo updatedCluRel = cluService.updateCluCluRelation(relation.getId(), relation, contextInfo);
150 				// Update the businessDTO if one exists for the cluclurelation (for e.g. CourseJointInfo)
151 				if(null != results.getBusinessDTORef()) {
152 					results.getAssembler().assemble(updatedCluRel, results.getBusinessDTORef(), true, contextInfo);
153 				}
154 				break;
155 			case DELETE:
156 				cluService.deleteCluCluRelation(relation.getId(), contextInfo);
157 				break;
158 			}
159 		}else if(nodeData instanceof CluResultInfo){
160 			CluResultInfo cluResult = (CluResultInfo) nodeData;
161 			switch(results.getOperation()){
162 			case CREATE:
163 				cluService.createCluResult(cluResult.getCluId(), cluResult.getTypeKey(), cluResult, contextInfo);
164 				break;
165 			case UPDATE:
166 				cluService.updateCluResult(cluResult.getId(), cluResult, contextInfo);
167 				break;
168 			case DELETE:
169 				cluService.deleteCluResult(cluResult.getId(), contextInfo);
170 				break;
171 			}
172 		}else if(nodeData instanceof LoCategoryRelationInfo){
173 			LoCategoryRelationInfo loCategoryRelation = (LoCategoryRelationInfo) nodeData;
174 			switch(results.getOperation()){
175 			case CREATE:
176 				loService.addLoCategoryToLo(loCategoryRelation.getCategoryId(), loCategoryRelation.getLoId(), contextInfo);
177 				break;
178 			case UPDATE:
179 				throw new UnsupportedOperationException("Can't call update on lo category relations, just add and remove");
180 			case DELETE:
181 				loService.removeLoCategoryFromLo(loCategoryRelation.getCategoryId(), loCategoryRelation.getLoId(), contextInfo);
182 				break;
183 			}
184 		}else if(nodeData instanceof LoInfo){
185 			LoInfo lo = (LoInfo) nodeData;
186 			switch(results.getOperation()){
187 			case CREATE:
188 				LoInfo createdLo = loService.createLo(lo.getLoRepositoryKey(), lo.getTypeKey(), lo, contextInfo);
189 				if(null != results.getBusinessDTORef()) {
190 					results.getAssembler().assemble(createdLo, results.getBusinessDTORef(), true, contextInfo);
191 				}
192 				break;
193 			case UPDATE:
194 				LoInfo updatedLo = loService.updateLo(lo.getId(), lo, contextInfo);
195 				if(null != results.getBusinessDTORef()) {
196 					results.getAssembler().assemble(updatedLo, results.getBusinessDTORef(), true, contextInfo);
197 				}
198 				break;
199 			case DELETE:
200 				loService.deleteLo(lo.getId(), contextInfo);
201 				break;
202 			}
203 		}else if(nodeData instanceof LoLoRelationInfo){
204 			LoLoRelationInfo loRelation = (LoLoRelationInfo) nodeData;
205 			switch(results.getOperation()){
206 			case CREATE:
207 				loService.createLoLoRelation(loRelation.getTypeKey(), loRelation, contextInfo);
208 				break;
209 			case UPDATE:
210 				loService.updateLoLoRelation(loRelation.getId(), loRelation, contextInfo);
211  				break;
212 			case DELETE:
213 				loService.deleteLoLoRelation(loRelation.getId(), contextInfo);
214 				break;
215 			}
216 		}else if(nodeData instanceof CluLoRelationInfo){
217 			CluLoRelationInfo cluLoRelation = (CluLoRelationInfo) nodeData;
218 			switch(results.getOperation()){
219 			case CREATE:
220 				cluService.createCluLoRelation(cluLoRelation.getCluId(), cluLoRelation.getLoId(), cluLoRelation.getTypeKey(), cluLoRelation, contextInfo);
221 				break;
222 			case UPDATE:
223 				cluService.updateCluLoRelation(cluLoRelation.getLoId(), cluLoRelation, contextInfo);
224 				break;
225 			case DELETE:
226 				cluService.deleteCluLoRelation(cluLoRelation.getId(), contextInfo);
227 				break;
228 			}
229 		}else if(nodeData instanceof ResultValuesGroupInfo){
230 		    ResultValuesGroupInfo resultComponent = (ResultValuesGroupInfo) nodeData;
231 			switch(results.getOperation()){
232 			case CREATE:
233                 List<String> rvKeys = new ArrayList<String>();
234                 //Do a get-create on each of the result values to ensure they exist.
235                 for(String resultValue : resultComponent.getResultValueKeys()){
236                     rvKeys.add(lrcService.getCreateResultValueForScale(resultValue, resultComponent.getResultScaleKey(), contextInfo).getKey());
237                 }
238                 resultComponent.setResultValueKeys(rvKeys);
239                 ResultValuesGroupInfo createdResultComponent = lrcService.createResultValuesGroup(resultComponent.getResultScaleKey(), resultComponent.getTypeKey(), resultComponent, contextInfo);
240 				//Copy the created back to the reference Should there be an assembler for this?
241 				if(results.getBusinessDTORef()!=null&& results.getBusinessDTORef() instanceof ResultValuesGroupInfo){
242 				    ResultValuesGroupInfo resultComponentToUpdate = (ResultValuesGroupInfo) results.getBusinessDTORef();
243 					resultComponentToUpdate.setKey(createdResultComponent.getKey());
244 					resultComponentToUpdate.setTypeKey(createdResultComponent.getTypeKey());
245 					resultComponentToUpdate.setDescr(createdResultComponent.getDescr());
246 					resultComponentToUpdate.setEffectiveDate(createdResultComponent.getEffectiveDate());
247 					resultComponentToUpdate.setExpirationDate(createdResultComponent.getExpirationDate());
248 					resultComponentToUpdate.setMeta(createdResultComponent.getMeta());
249 					resultComponentToUpdate.setName(createdResultComponent.getName());
250 					resultComponentToUpdate.setResultValueKeys(createdResultComponent.getResultValueKeys());
251 					resultComponentToUpdate.setStateKey(createdResultComponent.getStateKey());
252 				}
253 				break;
254 			case UPDATE:
255 				lrcService.updateResultValuesGroup(resultComponent.getKey(), resultComponent, contextInfo);
256 				break;
257 			case DELETE:
258 				lrcService.deleteResultValuesGroup(resultComponent.getKey(), contextInfo);
259 				break;
260 			}
261 		} else if(nodeData instanceof RefStatementRelationInfo){
262 			RefStatementRelationInfo relation = (RefStatementRelationInfo) nodeData;
263 			switch(results.getOperation()){
264 			case CREATE:
265 				RefStatementRelationInfo created = statementService.createRefStatementRelation(relation);
266 				relation.setMetaInfo(created.getMetaInfo());
267 				break;
268 			case UPDATE:
269 				RefStatementRelationInfo updated = statementService.updateRefStatementRelation(relation.getId(), relation);
270 				relation.setMetaInfo(updated.getMetaInfo());
271 				break;
272 			case DELETE:
273 				statementService.deleteRefStatementRelation(relation.getId());
274 				break;
275 			}
276 		} else if(nodeData instanceof StatementInfo){
277 			StatementInfo statement = (StatementInfo) nodeData;
278 			switch(results.getOperation()){
279 			case CREATE:
280 				StatementInfo created = statementService.createStatement(statement.getType(), statement);
281 				if(results.getAssembler() != null && results.getBusinessDTORef() != null) {
282 					results.getAssembler().assemble(created, results.getBusinessDTORef(), true, contextInfo);
283 				}
284 				break;
285 			case UPDATE:
286 				StatementInfo updated = statementService.updateStatement(statement.getId(), statement);
287 				if(results.getAssembler() != null && results.getBusinessDTORef() != null) {
288 					results.getAssembler().assemble(updated, results.getBusinessDTORef(), true, contextInfo);
289 				}
290 				break;
291 			case DELETE:
292 				statementService.deleteStatement(statement.getId());
293 				break;
294 			}
295 		} else if(nodeData instanceof ReqComponentInfo){
296 			ReqComponentInfo reqComp = (ReqComponentInfo) nodeData;
297 			switch(results.getOperation()){
298 			case CREATE:
299 				ReqComponentInfo created = statementService.createReqComponent(reqComp.getType(), reqComp);
300 				reqComp.setMetaInfo(created.getMetaInfo());
301 				break;
302 			case UPDATE:
303 				ReqComponentInfo updated = statementService.updateReqComponent(reqComp.getId(), reqComp);
304 				reqComp.setMetaInfo(updated.getMetaInfo());
305 				break;
306 			case DELETE:
307 				statementService.deleteReqComponent(reqComp.getId());
308 				break;
309 			}
310 		}else if(nodeData instanceof StatementTreeViewInfo){
311 			StatementTreeViewInfo treeView = (StatementTreeViewInfo) nodeData;
312 			switch(results.getOperation()){
313 			case CREATE:
314 				StatementTreeViewInfo created = statementService.createStatementTreeView(treeView);
315 				if(results.getAssembler() != null && results.getBusinessDTORef() != null) {
316 					results.getAssembler().assemble(created, results.getBusinessDTORef(), true, contextInfo);
317 				}
318 				break;
319 			case UPDATE:
320 				StatementTreeViewInfo updated = statementService.updateStatementTreeView(treeView.getId(), treeView);
321 				if(results.getAssembler() != null && results.getBusinessDTORef() != null) {
322 					results.getAssembler().assemble(updated, results.getBusinessDTORef(), true, contextInfo);
323 				}
324 				break;
325 			case DELETE:
326 				statementService.deleteStatementTreeView(treeView.getId());
327 				break;
328 			}
329    		}else if(nodeData instanceof CluPublicationInfo){
330 			CluPublicationInfo cluPublication = (CluPublicationInfo) nodeData;
331 			switch(results.getOperation()){
332 			case CREATE:
333 				cluService.createCluPublication(cluPublication.getCluId(), cluPublication.getTypeKey(), cluPublication, contextInfo);
334 				break;
335 			case UPDATE:
336 				cluService.updateCluPublication(cluPublication.getId(), cluPublication, contextInfo);
337 				break;
338 			case DELETE:
339 				cluService.deleteCluPublication(cluPublication.getId(), contextInfo);
340 				break;
341 			}
342 		}else{
343 			throw new UnsupportedActionException("This service invoker does not know how to handle nodeData for "+nodeData.getClass().getName());
344 		}
345 
346 	}
347 
348 	public CluService getCluService() {
349 		return cluService;
350 	}
351 
352 	public void setCluService(CluService cluService) {
353 		this.cluService = cluService;
354 	}
355 
356 	public StatementService getStatementService() {
357 		return statementService;
358 	}
359 
360 	public void setStatementService(StatementService statementService) {
361 		this.statementService = statementService;
362 	}
363 
364 	public LearningObjectiveService getLoService() {
365 		return loService;
366 	}
367 
368 	public void setLoService(LearningObjectiveService loService) {
369 		this.loService = loService;
370 	}
371 
372 	public OrganizationService getOrgService() {
373 		return orgService;
374 	}
375 
376 	public void setOrgService(OrganizationService orgService) {
377 		this.orgService = orgService;
378 	}
379 
380 	public AtpService getAtpService() {
381 		return atpService;
382 	}
383 
384 	public void setAtpService(AtpService atpService) {
385 		this.atpService = atpService;
386 	}
387 
388 	public void setLrcService(LRCService lrcService) {
389 		this.lrcService = lrcService;
390 	}
391 
392 }