001    package org.kuali.student.r2.lum.service.assembler;
002    
003    import org.apache.log4j.Logger;
004    import org.kuali.student.r1.common.assembly.BaseDTOAssemblyNode;
005    import org.kuali.student.r1.common.assembly.BaseDTOAssemblyNode.NodeOperation;
006    import org.kuali.student.r1.common.assembly.BusinessServiceMethodInvoker;
007    import org.kuali.student.r1.core.statement.dto.RefStatementRelationInfo;
008    import org.kuali.student.r1.core.statement.dto.ReqComponentInfo;
009    import org.kuali.student.r1.core.statement.dto.StatementInfo;
010    import org.kuali.student.r1.core.statement.dto.StatementTreeViewInfo;
011    import org.kuali.student.r1.core.statement.service.StatementService;
012    import org.kuali.student.r2.common.assembler.AssemblyException;
013    import org.kuali.student.r2.common.dto.ContextInfo;
014    import org.kuali.student.r2.common.exceptions.AlreadyExistsException;
015    import org.kuali.student.r2.common.exceptions.CircularReferenceException;
016    import org.kuali.student.r2.common.exceptions.CircularRelationshipException;
017    import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
018    import org.kuali.student.r2.common.exceptions.DependentObjectsExistException;
019    import org.kuali.student.r2.common.exceptions.DoesNotExistException;
020    import org.kuali.student.r2.common.exceptions.InvalidParameterException;
021    import org.kuali.student.r2.common.exceptions.MissingParameterException;
022    import org.kuali.student.r2.common.exceptions.OperationFailedException;
023    import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
024    import org.kuali.student.r2.common.exceptions.ReadOnlyException;
025    import org.kuali.student.r2.common.exceptions.UnsupportedActionException;
026    import org.kuali.student.r2.common.exceptions.VersionMismatchException;
027    import org.kuali.student.r2.core.atp.service.AtpService;
028    import org.kuali.student.r2.core.organization.service.OrganizationService;
029    import org.kuali.student.r2.lum.clu.dto.CluCluRelationInfo;
030    import org.kuali.student.r2.lum.clu.dto.CluInfo;
031    import org.kuali.student.r2.lum.clu.dto.CluLoRelationInfo;
032    import org.kuali.student.r2.lum.clu.dto.CluPublicationInfo;
033    import org.kuali.student.r2.lum.clu.dto.CluResultInfo;
034    import org.kuali.student.r2.lum.clu.service.CluService;
035    import org.kuali.student.r2.lum.course.service.assembler.LoCategoryRelationInfo;
036    import org.kuali.student.r2.lum.lo.dto.LoInfo;
037    import org.kuali.student.r2.lum.lo.dto.LoLoRelationInfo;
038    import org.kuali.student.r2.lum.lo.service.LearningObjectiveService;
039    import org.kuali.student.r2.lum.lrc.dto.ResultValuesGroupInfo;
040    import org.kuali.student.r2.lum.lrc.service.LRCService;
041    
042    import java.util.List;
043    
044    public class LumServiceMethodInvoker implements BusinessServiceMethodInvoker {
045            final Logger LOG = Logger.getLogger(LumServiceMethodInvoker.class);
046            private CluService cluService;
047            private StatementService statementService;
048            private LearningObjectiveService loService;
049            private OrganizationService orgService;
050            private AtpService atpService;
051            private LRCService lrcService;
052    
053            @SuppressWarnings("unchecked")
054        @Override
055            public final void invokeServiceCalls(BaseDTOAssemblyNode results, ContextInfo contextInfo)
056                            throws DependentObjectsExistException, CircularRelationshipException,
057                            AssemblyException, UnsupportedActionException, UnsupportedOperationException, CircularReferenceException, ReadOnlyException, DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException, AlreadyExistsException {
058    
059                // For Delete operation process the tree from bottom up
060                if(NodeOperation.DELETE == results.getOperation()) {
061                for(BaseDTOAssemblyNode childNode: (List<BaseDTOAssemblyNode>) results.getChildNodes()){
062                    invokeServiceCalls(childNode,contextInfo);
063                }
064                }
065    
066                invokeServiceCallOnResult(results, contextInfo);
067    
068                    // For create/update process the child nodes from top to bottom
069                    if(NodeOperation.DELETE != results.getOperation()) {
070                        for(BaseDTOAssemblyNode childNode: (List<BaseDTOAssemblyNode>) results.getChildNodes()){
071                            invokeServiceCalls(childNode,contextInfo);
072                        }
073                    }
074            }
075    
076            /**
077             * @param results
078             * @throws AlreadyExistsException
079             * @throws DataValidationErrorException
080             * @throws DoesNotExistException
081             * @throws InvalidParameterException
082             * @throws MissingParameterException
083             * @throws OperationFailedException
084             * @throws PermissionDeniedException
085             * @throws AssemblyException
086             * @throws VersionMismatchException
087             * @throws DependentObjectsExistException
088             * @throws CircularRelationshipException
089             * @throws UnsupportedActionException
090             * @throws UnsupportedOperationException
091             * @throws CircularReferenceException
092             * @throws ReadOnlyException
093             * @throws org.kuali.student.r2.common.exceptions.DataValidationErrorException
094             * @throws org.kuali.student.r2.common.exceptions.DoesNotExistException
095             * @throws org.kuali.student.r2.common.exceptions.InvalidParameterException
096             * @throws org.kuali.student.r2.common.exceptions.MissingParameterException
097             * @throws org.kuali.student.r2.common.exceptions.OperationFailedException
098             * @throws org.kuali.student.r2.common.exceptions.PermissionDeniedException
099             * @throws org.kuali.student.r2.common.exceptions.VersionMismatchException
100             * @throws org.kuali.student.r2.common.exceptions.AlreadyExistsException
101             */
102            protected void invokeServiceCallOnResult(BaseDTOAssemblyNode results, ContextInfo contextInfo)
103                            throws AlreadyExistsException, DataValidationErrorException,
104                            DoesNotExistException, InvalidParameterException,
105                            MissingParameterException, OperationFailedException,
106                            PermissionDeniedException, AssemblyException,
107                            VersionMismatchException, DependentObjectsExistException,
108                            CircularRelationshipException, UnsupportedActionException,
109                            UnsupportedOperationException, CircularReferenceException, ReadOnlyException, DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException, AlreadyExistsException {
110                    Object nodeData = results.getNodeData();
111                    if (nodeData == null) {
112                            return;
113                    }
114    
115                    if (LOG.isDebugEnabled()) {
116                            LOG.debug(results.getOperation() + ": " + nodeData);
117                    }
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                    //Do a get-create on each of the result values to ensure they exist.
234                    for(String resultValue : resultComponent.getResultValueKeys()){
235                        lrcService.getCreateResultValueForScale(resultValue, resultComponent.getResultScaleKey(), contextInfo).getKey();
236                    }
237                    ResultValuesGroupInfo createdResultComponent = lrcService.createResultValuesGroup(resultComponent.getResultScaleKey(), resultComponent.getTypeKey(), resultComponent, contextInfo);
238                                    //Copy the created back to the reference Should there be an assembler for this?
239                                    if(results.getBusinessDTORef()!=null&& results.getBusinessDTORef() instanceof ResultValuesGroupInfo){
240                                        ResultValuesGroupInfo resultComponentToUpdate = (ResultValuesGroupInfo) results.getBusinessDTORef();
241                                            resultComponentToUpdate.setKey(createdResultComponent.getKey());
242                                            resultComponentToUpdate.setTypeKey(createdResultComponent.getTypeKey());
243                                            resultComponentToUpdate.setDescr(createdResultComponent.getDescr());
244                                            resultComponentToUpdate.setEffectiveDate(createdResultComponent.getEffectiveDate());
245                                            resultComponentToUpdate.setExpirationDate(createdResultComponent.getExpirationDate());
246                                            resultComponentToUpdate.setMeta(createdResultComponent.getMeta());
247                                            resultComponentToUpdate.setName(createdResultComponent.getName());
248                                            resultComponentToUpdate.setResultValueKeys(createdResultComponent.getResultValueKeys());
249                                            resultComponentToUpdate.setStateKey(createdResultComponent.getStateKey());
250                                    }
251                                    break;
252                            case UPDATE:
253                                    lrcService.updateResultValuesGroup(resultComponent.getKey(), resultComponent, contextInfo);
254                                    break;
255                            case DELETE:
256                                    lrcService.deleteResultValuesGroup(resultComponent.getKey(), contextInfo);
257                                    break;
258                            }
259                    } else if(nodeData instanceof RefStatementRelationInfo){
260                            RefStatementRelationInfo relation = (RefStatementRelationInfo) nodeData;
261                            switch(results.getOperation()){
262                            case CREATE:
263                                    RefStatementRelationInfo created = statementService.createRefStatementRelation(relation);
264                                    relation.setMetaInfo(created.getMetaInfo());
265                                    break;
266                            case UPDATE:
267                                    RefStatementRelationInfo updated = statementService.updateRefStatementRelation(relation.getId(), relation);
268                                    relation.setMetaInfo(updated.getMetaInfo());
269                                    break;
270                            case DELETE:
271                                    statementService.deleteRefStatementRelation(relation.getId());
272                                    break;
273                            }
274                    } else if(nodeData instanceof StatementInfo){
275                            StatementInfo statement = (StatementInfo) nodeData;
276                            switch(results.getOperation()){
277                            case CREATE:
278                                    StatementInfo created = statementService.createStatement(statement.getType(), statement);
279                                    if(results.getAssembler() != null && results.getBusinessDTORef() != null) {
280                                            results.getAssembler().assemble(created, results.getBusinessDTORef(), true, contextInfo);
281                                    }
282                                    break;
283                            case UPDATE:
284                                    StatementInfo updated = statementService.updateStatement(statement.getId(), statement);
285                                    if(results.getAssembler() != null && results.getBusinessDTORef() != null) {
286                                            results.getAssembler().assemble(updated, results.getBusinessDTORef(), true, contextInfo);
287                                    }
288                                    break;
289                            case DELETE:
290                                    statementService.deleteStatement(statement.getId());
291                                    break;
292                            }
293                    } else if(nodeData instanceof ReqComponentInfo){
294                            ReqComponentInfo reqComp = (ReqComponentInfo) nodeData;
295                            switch(results.getOperation()){
296                            case CREATE:
297                                    ReqComponentInfo created = statementService.createReqComponent(reqComp.getType(), reqComp);
298                                    reqComp.setMetaInfo(created.getMetaInfo());
299                                    break;
300                            case UPDATE:
301                                    ReqComponentInfo updated = statementService.updateReqComponent(reqComp.getId(), reqComp);
302                                    reqComp.setMetaInfo(updated.getMetaInfo());
303                                    break;
304                            case DELETE:
305                                    statementService.deleteReqComponent(reqComp.getId());
306                                    break;
307                            }
308                    }else if(nodeData instanceof StatementTreeViewInfo){
309                            StatementTreeViewInfo treeView = (StatementTreeViewInfo) nodeData;
310                            switch(results.getOperation()){
311                            case CREATE:
312                                    StatementTreeViewInfo created = statementService.createStatementTreeView(treeView);
313                                    if(results.getAssembler() != null && results.getBusinessDTORef() != null) {
314                                            results.getAssembler().assemble(created, results.getBusinessDTORef(), true, contextInfo);
315                                    }
316                                    break;
317                            case UPDATE:
318                                    StatementTreeViewInfo updated = statementService.updateStatementTreeView(treeView.getId(), treeView);
319                                    if(results.getAssembler() != null && results.getBusinessDTORef() != null) {
320                                            results.getAssembler().assemble(updated, results.getBusinessDTORef(), true, contextInfo);
321                                    }
322                                    break;
323                            case DELETE:
324                                    statementService.deleteStatementTreeView(treeView.getId());
325                                    break;
326                            }
327                    }else if(nodeData instanceof CluPublicationInfo){
328                            CluPublicationInfo cluPublication = (CluPublicationInfo) nodeData;
329                            switch(results.getOperation()){
330                            case CREATE:
331                                    cluService.createCluPublication(cluPublication.getCluId(), cluPublication.getTypeKey(), cluPublication, contextInfo);
332                                    break;
333                            case UPDATE:
334                                    cluService.updateCluPublication(cluPublication.getId(), cluPublication, contextInfo);
335                                    break;
336                            case DELETE:
337                                    cluService.deleteCluPublication(cluPublication.getId(), contextInfo);
338                                    break;
339                            }
340                    }else{
341                            throw new UnsupportedActionException("This service invoker does not know how to handle nodeData for "+nodeData.getClass().getName());
342                    }
343    
344            }
345    
346            public CluService getCluService() {
347                    return cluService;
348            }
349    
350            public void setCluService(CluService cluService) {
351                    this.cluService = cluService;
352            }
353    
354            public StatementService getStatementService() {
355                    return statementService;
356            }
357    
358            public void setStatementService(StatementService statementService) {
359                    this.statementService = statementService;
360            }
361    
362            public LearningObjectiveService getLoService() {
363                    return loService;
364            }
365    
366            public void setLoService(LearningObjectiveService loService) {
367                    this.loService = loService;
368            }
369    
370            public OrganizationService getOrgService() {
371                    return orgService;
372            }
373    
374            public void setOrgService(OrganizationService orgService) {
375                    this.orgService = orgService;
376            }
377    
378            public AtpService getAtpService() {
379                    return atpService;
380            }
381    
382            public void setAtpService(AtpService atpService) {
383                    this.atpService = atpService;
384            }
385    
386            public void setLrcService(LRCService lrcService) {
387                    this.lrcService = lrcService;
388            }
389    
390    }