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