View Javadoc

1   package org.kuali.student.r2.lum.program.service.assembler;
2   
3   import java.util.List;
4   import java.util.Map;
5   
6   import org.apache.log4j.Logger;
7   import org.kuali.student.r2.lum.service.assembler.CluAssemblerUtils;
8   import org.kuali.student.r1.common.assembly.BOAssembler;
9   import org.kuali.student.r1.common.assembly.BaseDTOAssemblyNode;
10  import org.kuali.student.r1.common.assembly.BaseDTOAssemblyNode.NodeOperation;
11  import org.kuali.student.r2.lum.clu.dto.CluCluRelationInfo;
12  import org.kuali.student.r2.lum.program.dto.assembly.ProgramAtpAssembly;
13  import org.kuali.student.r2.lum.program.dto.assembly.ProgramBasicOrgAssembly;
14  import org.kuali.student.r2.lum.program.dto.assembly.ProgramCodeAssembly;
15  import org.kuali.student.r2.lum.program.dto.assembly.ProgramIdentifierAssembly;
16  import org.kuali.student.r2.lum.program.dto.assembly.ProgramRequirementAssembly;
17  import org.kuali.student.r2.common.assembler.AssemblyException;
18  import org.kuali.student.r2.common.dto.ContextInfo;
19  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
20  import org.kuali.student.r2.lum.clu.dto.AdminOrgInfo;
21  import org.kuali.student.r2.lum.clu.dto.CluInfo;
22  import org.kuali.student.r2.lum.clu.dto.LuCodeInfo;
23  import org.kuali.student.r2.lum.clu.service.CluService;
24  import org.kuali.student.r2.lum.course.dto.LoDisplayInfo;
25  import org.kuali.student.r2.lum.program.dto.CredentialProgramInfo;
26  import org.kuali.student.r2.lum.program.dto.assembly.ProgramCommonAssembly;
27  
28  public class CredentialProgramAssembler implements BOAssembler<CredentialProgramInfo, CluInfo> {
29      final static Logger LOG = Logger.getLogger(CredentialProgramAssembler.class);
30  
31      private ProgramAssemblerUtils programAssemblerUtils;
32      private CluAssemblerUtils cluAssemblerUtils;
33      private CluService cluService;
34  
35  
36      @Override
37      public CredentialProgramInfo assemble(CluInfo baseDTO, CredentialProgramInfo businessDTO, boolean shallowBuild, ContextInfo contextInfo) throws AssemblyException {
38  
39          CredentialProgramInfo cpInfo = (null != businessDTO) ? businessDTO : new CredentialProgramInfo();
40  
41          // Copy all the data from the clu to the credential program
42          if (!ProgramAssemblerConstants.CREDENTIAL_PROGRAM_TYPES.contains(baseDTO.getTypeKey())) {
43              throw new AssemblyException("CredentialProgramAssembler.assemble() called for Clu of incorrect type: " + baseDTO.getTypeKey());
44          }
45          cpInfo.setTypeKey(baseDTO.getTypeKey());
46          cpInfo.setDescr(baseDTO.getDescr());
47          cpInfo.setVersion(baseDTO.getVersion());
48  
49          programAssemblerUtils.assembleBasics(baseDTO, (ProgramCommonAssembly) cpInfo, contextInfo);
50          programAssemblerUtils.assembleIdentifiers(baseDTO, (ProgramIdentifierAssembly) cpInfo);
51          if (null != baseDTO.getOfficialIdentifier().getLevel()) {
52              cpInfo.setProgramLevel(baseDTO.getOfficialIdentifier().getLevel());
53          }
54          programAssemblerUtils.assembleBasicAdminOrgs(baseDTO, (ProgramBasicOrgAssembly) cpInfo);
55          for (AdminOrgInfo org : baseDTO.getAdminOrgs()) {
56              if (ProgramAssemblerConstants.INSTITUTION.equals(org.getTypeKey())) {
57                  cpInfo.setInstitution(org);
58              }
59          }
60          programAssemblerUtils.assembleAtps(baseDTO, (ProgramAtpAssembly) cpInfo);
61          
62          if (baseDTO.getLuCodes() != null) {
63              for (LuCodeInfo codeInfo : baseDTO.getLuCodes()) {
64                  if (ProgramAssemblerConstants.UNIVERSITY_CLASSIFICATION.equals(codeInfo.getType())) {
65                      cpInfo.setUniversityClassification(codeInfo.getValue());
66                  } 
67              }
68          }
69          
70          if (!shallowBuild) {
71  	        programAssemblerUtils.assembleRequirements(baseDTO, (ProgramRequirementAssembly) cpInfo, contextInfo);
72  	        cpInfo.setLearningObjectives(cluAssemblerUtils.assembleLos(baseDTO.getId(), shallowBuild,contextInfo)); 
73  	        cpInfo.setResultOptions(programAssemblerUtils.assembleResultOptions(baseDTO.getId(),contextInfo));
74          }
75          
76          try {
77  
78          	 cpInfo.setCoreProgramIds(cluService.getRelatedCluIdsByCluAndRelationType(baseDTO.getId(), ProgramAssemblerConstants.HAS_CORE_PROGRAM, contextInfo));
79          } catch (Exception e) {
80              throw new AssemblyException(e);
81          }
82  
83          return cpInfo;
84      }
85  
86  
87      @Override
88      public BaseDTOAssemblyNode<CredentialProgramInfo, CluInfo> disassemble(CredentialProgramInfo businessDTO, NodeOperation operation, ContextInfo contextInfo) throws AssemblyException {
89  
90          BaseDTOAssemblyNode<CredentialProgramInfo, CluInfo> result = new BaseDTOAssemblyNode<CredentialProgramInfo, CluInfo>(this);
91  
92          if (businessDTO == null) {
93              // FIXME Unsure now if this is an exception or just return null or
94              // empty assemblyNode
95              LOG.error("credentialProgram to disassemble is null!");
96              throw new AssemblyException("credentialProgram can not be null");
97          }
98  
99          CluInfo clu;
100         try {
101             clu = (NodeOperation.UPDATE == operation) ? cluService.getClu(businessDTO.getId(), contextInfo) : new CluInfo();
102         } catch (Exception e) {
103 			throw new AssemblyException("Error getting existing learning unit during CoreProgram update", e);
104         } 
105         
106         boolean stateChanged = NodeOperation.UPDATE == operation && businessDTO.getStateKey() != null && !businessDTO.getStateKey().equals(clu.getStateKey());
107         
108         programAssemblerUtils.disassembleBasics(clu, (ProgramCommonAssembly) businessDTO);
109         if (businessDTO.getId() == null)
110             businessDTO.setId(clu.getId());
111         programAssemblerUtils.disassembleIdentifiers(clu, (ProgramIdentifierAssembly) businessDTO, operation);
112         programAssemblerUtils.disassembleAdminOrgs(clu, (ProgramBasicOrgAssembly) businessDTO, operation);
113         programAssemblerUtils.disassembleAtps(clu, (ProgramAtpAssembly) businessDTO, operation);
114         programAssemblerUtils.disassembleLuCodes(clu, (ProgramCodeAssembly) businessDTO, operation);
115 
116         if (businessDTO.getProgramRequirements() != null && !businessDTO.getProgramRequirements().isEmpty()) {
117             programAssemblerUtils.disassembleRequirements(clu, (ProgramRequirementAssembly) businessDTO, operation, result, stateChanged, contextInfo);
118         }
119 
120         if (businessDTO.getResultOptions() != null) {
121             disassembleResultOptions(businessDTO, operation, result, contextInfo);
122         }
123 
124         if (businessDTO.getLearningObjectives() != null) {
125             disassembleLearningObjectives(businessDTO, operation, result,contextInfo);
126         }
127 
128         clu.setDescr(businessDTO.getDescr());
129         clu.setTypeKey(businessDTO.getCredentialProgramType());
130 
131 
132 
133         if (businessDTO.getCoreProgramIds() != null && businessDTO.getCoreProgramIds().size() > 0) {
134             disassembleCorePrograms(businessDTO, operation, result, contextInfo);
135         }
136 
137         // Add the Clu to the result
138         result.setNodeData(clu);
139         result.setOperation(operation);
140         result.setBusinessDTORef(businessDTO);
141         return result;
142     }
143 
144     private void disassembleResultOptions(CredentialProgramInfo credential, NodeOperation operation, BaseDTOAssemblyNode<CredentialProgramInfo, CluInfo> result, ContextInfo contextInfo) throws AssemblyException {
145         BaseDTOAssemblyNode<?, ?> resultOptions = cluAssemblerUtils.disassembleCluResults(credential.getId(), credential.getStateKey(), credential.getResultOptions(), operation, ProgramAssemblerConstants.DEGREE_RESULTS, "Result options", "Result option", contextInfo);
146         if (resultOptions != null) {
147             result.getChildNodes().add(resultOptions);
148         }
149     }
150 
151     private void disassembleLearningObjectives(CredentialProgramInfo credential, NodeOperation operation, BaseDTOAssemblyNode<CredentialProgramInfo, CluInfo> result,ContextInfo contextInfo) throws AssemblyException {
152     	try {
153             List<BaseDTOAssemblyNode<?, ?>> loResults = cluAssemblerUtils.disassembleLos(credential.getId(), credential.getStateKey(), (List<LoDisplayInfo>) credential.getLearningObjectives(), operation,contextInfo);
154             if (loResults != null) {
155                 result.getChildNodes().addAll(loResults);
156             }
157         } catch (DoesNotExistException e) {
158         } catch (Exception e) {
159             throw new AssemblyException("Error while disassembling los", e);
160         } 
161     }
162 
163     private void disassembleCorePrograms(CredentialProgramInfo credential, NodeOperation operation, BaseDTOAssemblyNode<CredentialProgramInfo, CluInfo> result, ContextInfo contextInfo) throws AssemblyException {
164         List<BaseDTOAssemblyNode<?, ?>> coreResults;
165 
166         try {
167             Map<String, String> currentRelations = null;
168 
169             if (!NodeOperation.CREATE.equals(operation)) {
170                 currentRelations = programAssemblerUtils.getCluCluRelations(credential.getId(), ProgramAssemblerConstants.HAS_CORE_PROGRAM, contextInfo );
171             }
172 
173             for (String coreProgramId : credential.getCoreProgramIds()) {
174                 coreResults = programAssemblerUtils.addAllRelationNodes(credential.getId(), coreProgramId, ProgramAssemblerConstants.HAS_CORE_PROGRAM, operation, currentRelations);
175                 if (coreResults != null && coreResults.size() > 0) {
176                     result.getChildNodes().addAll(coreResults);
177                 }
178             }
179 
180             if (currentRelations != null && currentRelations.size() > 0) {
181                 for (Map.Entry<String, String> entry : currentRelations.entrySet()) {
182                     // Create a new relation with the id of the relation we want to
183                     // delete
184                     CluCluRelationInfo relationToDelete = new CluCluRelationInfo();
185                     relationToDelete.setId(entry.getValue());
186                     BaseDTOAssemblyNode<Object, CluCluRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<Object, CluCluRelationInfo>(
187                             null);
188                     relationToDeleteNode.setNodeData(relationToDelete);
189                     relationToDeleteNode.setOperation(NodeOperation.DELETE);
190                     result.getChildNodes().add(relationToDeleteNode);
191                 }
192             }
193         } catch (Exception e) {
194             throw new AssemblyException("Error while disassembling Core programs", e);
195         }
196     }
197 
198     public void setProgramAssemblerUtils(ProgramAssemblerUtils programAssemblerUtils) {
199         this.programAssemblerUtils = programAssemblerUtils;
200     }
201 
202     public void setCluService(CluService cluService) {
203         this.cluService = cluService;
204     }
205 
206     public void setCluAssemblerUtils(CluAssemblerUtils cluAssemblerUtils) {
207         this.cluAssemblerUtils = cluAssemblerUtils;
208     }
209 
210 
211 }