1 | |
|
2 | |
|
3 | |
|
4 | |
package org.kuali.student.lum.program.service.impl; |
5 | |
|
6 | |
import static org.apache.commons.collections.CollectionUtils.isEmpty; |
7 | |
import static org.apache.commons.lang.StringUtils.isEmpty; |
8 | |
|
9 | |
import java.util.HashMap; |
10 | |
import java.util.List; |
11 | |
import java.util.Map; |
12 | |
|
13 | |
import org.apache.log4j.Logger; |
14 | |
import org.kuali.student.common.assembly.BOAssembler; |
15 | |
import org.kuali.student.common.assembly.BaseDTOAssemblyNode; |
16 | |
import org.kuali.student.common.assembly.BaseDTOAssemblyNode.NodeOperation; |
17 | |
import org.kuali.student.common.assembly.data.AssemblyException; |
18 | |
import org.kuali.student.common.dto.DtoConstants; |
19 | |
import org.kuali.student.common.exceptions.DoesNotExistException; |
20 | |
import org.kuali.student.common.exceptions.InvalidParameterException; |
21 | |
import org.kuali.student.common.exceptions.MissingParameterException; |
22 | |
import org.kuali.student.common.exceptions.OperationFailedException; |
23 | |
import org.kuali.student.common.util.UUIDHelper; |
24 | |
import org.kuali.student.core.statement.dto.RefStatementRelationInfo; |
25 | |
import org.kuali.student.core.statement.dto.StatementTreeViewInfo; |
26 | |
import org.kuali.student.core.statement.service.StatementService; |
27 | |
import org.kuali.student.core.statement.service.assembler.StatementTreeViewAssembler; |
28 | |
import org.kuali.student.lum.course.service.assembler.LoAssembler; |
29 | |
import org.kuali.student.lum.lo.service.LearningObjectiveService; |
30 | |
import org.kuali.student.lum.lu.dto.CluCluRelationInfo; |
31 | |
import org.kuali.student.lum.lu.dto.CluIdentifierInfo; |
32 | |
import org.kuali.student.lum.lu.dto.CluInfo; |
33 | |
import org.kuali.student.lum.lu.service.LuService; |
34 | |
import org.kuali.student.lum.program.dto.ProgramRequirementInfo; |
35 | |
import org.kuali.student.lum.program.service.assembler.ProgramAssemblerConstants; |
36 | |
import org.kuali.student.lum.program.service.assembler.ProgramAssemblerUtils; |
37 | |
import org.kuali.student.lum.service.assembler.CluAssemblerUtils; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 8 | public class ProgramRequirementAssembler implements BOAssembler<ProgramRequirementInfo, CluInfo> { |
45 | 1 | final static Logger LOG = Logger.getLogger(ProgramRequirementAssembler.class); |
46 | |
|
47 | |
private StatementService statementService; |
48 | |
private StatementTreeViewAssembler statementTreeViewAssembler; |
49 | |
private LearningObjectiveService loService; |
50 | |
private LuService luService; |
51 | |
private LoAssembler loAssembler; |
52 | |
private CluAssemblerUtils cluAssemblerUtils; |
53 | |
private ProgramAssemblerUtils programAssemblerUtils; |
54 | |
|
55 | |
|
56 | |
@Override |
57 | |
public ProgramRequirementInfo assemble(CluInfo clu, |
58 | |
ProgramRequirementInfo progReqInfo, boolean shallowBuild) |
59 | |
throws AssemblyException { |
60 | 12 | ProgramRequirementInfo progReq = (progReqInfo != null ? progReqInfo : new ProgramRequirementInfo()); |
61 | |
|
62 | 12 | if (clu.getOfficialIdentifier() != null) { |
63 | 12 | progReq.setShortTitle(clu.getOfficialIdentifier().getShortName()); |
64 | 12 | progReq.setLongTitle(clu.getOfficialIdentifier().getLongName()); |
65 | |
} |
66 | 12 | progReq.setDescr(clu.getDescr()); |
67 | |
|
68 | |
|
69 | 12 | assembleCredits(clu, progReq); |
70 | |
|
71 | 12 | if (progReq.getStatement() == null) { |
72 | |
try { |
73 | 5 | List<RefStatementRelationInfo> relations = statementService.getRefStatementRelationsByRef(ProgramAssemblerConstants.PROGRAM_REQUIREMENT, clu.getId()); |
74 | |
|
75 | |
|
76 | 5 | StatementTreeViewInfo statementTree = new StatementTreeViewInfo(); |
77 | 5 | if (relations != null) { |
78 | 5 | statementTreeViewAssembler.assemble(statementService.getStatementTreeView(relations.get(0).getStatementId()), statementTree, shallowBuild); |
79 | |
} |
80 | 5 | progReq.setStatement(statementTree); |
81 | 0 | } catch (AssemblyException e) { |
82 | 0 | throw e; |
83 | 0 | } catch (Exception e) { |
84 | 0 | throw new AssemblyException(e); |
85 | 5 | } |
86 | |
} |
87 | |
|
88 | 12 | if (isEmpty(progReq.getLearningObjectives())) { |
89 | 5 | progReq.setLearningObjectives(cluAssemblerUtils.assembleLos(clu.getId(), shallowBuild)); |
90 | |
} |
91 | |
|
92 | 12 | progReq.setMetaInfo(clu.getMetaInfo()); |
93 | 12 | progReq.setType(clu.getType()); |
94 | 12 | progReq.setAttributes(clu.getAttributes()); |
95 | 12 | progReq.setId(clu.getId()); |
96 | |
|
97 | 12 | return progReq; |
98 | |
} |
99 | |
|
100 | |
@Override |
101 | |
public BaseDTOAssemblyNode<ProgramRequirementInfo, CluInfo> disassemble( |
102 | |
ProgramRequirementInfo progReq, NodeOperation operation) |
103 | |
throws AssemblyException { |
104 | |
|
105 | 9 | if (progReq == null) { |
106 | |
|
107 | |
|
108 | 0 | LOG.error("ProgramRequirementInfo to disassemble is null!"); |
109 | 0 | throw new AssemblyException("ProgramRequirementInfo can not be null"); |
110 | |
} |
111 | |
|
112 | 9 | BaseDTOAssemblyNode<ProgramRequirementInfo, CluInfo> result = new BaseDTOAssemblyNode<ProgramRequirementInfo, CluInfo>(null); |
113 | |
|
114 | |
|
115 | 9 | StatementTreeViewInfo statement = progReq.getStatement(); |
116 | 9 | statement.setId(UUIDHelper.genStringUUID(statement.getId())); |
117 | |
BaseDTOAssemblyNode<StatementTreeViewInfo, StatementTreeViewInfo> statementTree; |
118 | |
try { |
119 | 9 | statementTree = statementTreeViewAssembler.disassemble(statement, operation); |
120 | 0 | } catch (AssemblyException e) { |
121 | 0 | throw e; |
122 | 0 | } catch (Exception e) { |
123 | 0 | throw new AssemblyException(e); |
124 | 9 | } |
125 | 9 | result.getChildNodes().add(statementTree); |
126 | |
|
127 | |
CluInfo clu; |
128 | |
try { |
129 | 9 | clu = (NodeOperation.UPDATE == operation) ? luService.getClu(progReq.getId()) : new CluInfo(); |
130 | 0 | } catch (Exception e) { |
131 | 0 | throw new AssemblyException("Error getting existing learning unit during program requirement update", e); |
132 | 9 | } |
133 | |
|
134 | 9 | if (operation.equals(NodeOperation.DELETE)) { |
135 | |
try { |
136 | 2 | final List<CluCluRelationInfo> relations = luService.getCluCluRelationsByClu(progReq.getId()); |
137 | 2 | final BaseDTOAssemblyNode<ProgramRequirementInfo, CluCluRelationInfo> cluRelation = new BaseDTOAssemblyNode<ProgramRequirementInfo, CluCluRelationInfo>(null); |
138 | 2 | if (relations.size() > 1) { |
139 | 0 | throw new AssemblyException("Unable to dissamble ProgramRequirement, more than one CluCluRelation found"); |
140 | 2 | } else if (relations.size() == 1) { |
141 | 1 | cluRelation.setNodeData(relations.get(0)); |
142 | 1 | cluRelation.setOperation(operation); |
143 | 1 | result.getChildNodes().add(cluRelation); |
144 | |
} |
145 | 0 | } catch (Exception e) { |
146 | 0 | throw new AssemblyException(e); |
147 | 2 | } |
148 | |
} |
149 | |
|
150 | 9 | BaseDTOAssemblyNode<ProgramRequirementInfo, CluInfo> cluResult = new BaseDTOAssemblyNode<ProgramRequirementInfo, CluInfo>(this); |
151 | |
|
152 | 9 | cluResult.setNodeData(clu); |
153 | 9 | cluResult.setBusinessDTORef(progReq); |
154 | 9 | cluResult.setOperation(operation); |
155 | 9 | result.getChildNodes().add(cluResult); |
156 | |
|
157 | 9 | programAssemblerUtils.disassembleBasics(clu, progReq); |
158 | |
|
159 | |
|
160 | 9 | disassembleCredits(clu, progReq); |
161 | |
|
162 | 9 | progReq.setId(clu.getId()); |
163 | 9 | CluIdentifierInfo official = null != clu.getOfficialIdentifier() ? clu.getOfficialIdentifier() : new CluIdentifierInfo(); |
164 | 9 | official.setLongName(progReq.getLongTitle()); |
165 | 9 | official.setShortName(progReq.getShortTitle()); |
166 | 9 | official.setState(!isEmpty(clu.getState()) ? clu.getState() : DtoConstants.STATE_ACTIVE); |
167 | |
|
168 | 9 | official.setType(ProgramAssemblerConstants.OFFICIAL); |
169 | 9 | clu.setOfficialIdentifier(official); |
170 | |
|
171 | 9 | clu.setDescr(progReq.getDescr()); |
172 | 9 | clu.setState(!isEmpty(clu.getState()) ? clu.getState() : DtoConstants.STATE_ACTIVE); |
173 | 9 | if (progReq.getLearningObjectives() != null) { |
174 | 9 | disassembleLearningObjectives(progReq, operation, result); |
175 | |
} |
176 | |
|
177 | |
RefStatementRelationInfo relation; |
178 | 9 | if (operation == NodeOperation.CREATE) { |
179 | 6 | relation = new RefStatementRelationInfo(); |
180 | 6 | relation.setId(UUIDHelper.genStringUUID(null)); |
181 | |
} else { |
182 | |
try { |
183 | 3 | relation = statementService.getRefStatementRelationsByRef(ProgramAssemblerConstants.PROGRAM_REQUIREMENT, clu.getId()).get(0); |
184 | 0 | } catch (Exception e) { |
185 | 0 | throw new AssemblyException("Unable to find RefStatementRelation", e); |
186 | 3 | } |
187 | |
} |
188 | |
|
189 | 9 | relation.setType(ProgramAssemblerConstants.PROGRAM_REFERENCE_TYPE); |
190 | 9 | relation.setRefObjectId(clu.getId()); |
191 | 9 | relation.setRefObjectTypeKey(ProgramAssemblerConstants.PROGRAM_REQUIREMENT); |
192 | 9 | relation.setStatementId(statement.getId()); |
193 | 9 | relation.setState(DtoConstants.STATE_ACTIVE); |
194 | |
|
195 | 9 | BaseDTOAssemblyNode<ProgramRequirementInfo, RefStatementRelationInfo> relationNode = new BaseDTOAssemblyNode<ProgramRequirementInfo, RefStatementRelationInfo>(null); |
196 | 9 | relationNode.setNodeData(relation); |
197 | 9 | relationNode.setOperation(operation); |
198 | |
|
199 | 9 | result.getChildNodes().add(relationNode); |
200 | 9 | result.setBusinessDTORef(progReq); |
201 | 9 | result.setOperation(operation); |
202 | 9 | return result; |
203 | |
} |
204 | |
|
205 | |
private void disassembleLearningObjectives(ProgramRequirementInfo progReq, |
206 | |
NodeOperation operation, |
207 | |
BaseDTOAssemblyNode<ProgramRequirementInfo, CluInfo> result) throws AssemblyException { |
208 | |
try { |
209 | 9 | List<BaseDTOAssemblyNode<?, ?>> loResults = cluAssemblerUtils.disassembleLos(progReq.getId(), progReq.getState(), progReq.getLearningObjectives(), operation); |
210 | 9 | if (loResults != null) { |
211 | 9 | result.getChildNodes().addAll(loResults); |
212 | |
} |
213 | 0 | } catch (DoesNotExistException e) { |
214 | 0 | } catch (Exception e) { |
215 | 0 | throw new AssemblyException("Error while disassembling los", e); |
216 | 9 | } |
217 | 9 | } |
218 | |
|
219 | |
private void disassembleCredits(CluInfo clu, ProgramRequirementInfo progReq){ |
220 | 9 | Map<String,String> attributes = null != clu.getAttributes() ? clu.getAttributes() : new HashMap<String,String>(); |
221 | |
|
222 | 9 | if(progReq.getMinCredits() != null){ |
223 | 1 | attributes.put(ProgramAssemblerConstants.MIN_CREDITS, Integer.toString(progReq.getMinCredits())); |
224 | |
}else{ |
225 | 8 | attributes.put(ProgramAssemblerConstants.MIN_CREDITS, null); |
226 | |
} |
227 | 9 | if(progReq.getMaxCredits() != null) { |
228 | 1 | attributes.put(ProgramAssemblerConstants.MAX_CREDITS, Integer.toString(progReq.getMaxCredits())); |
229 | |
}else{ |
230 | 8 | attributes.put(ProgramAssemblerConstants.MAX_CREDITS, null); |
231 | |
} |
232 | |
|
233 | 9 | clu.setAttributes(attributes); |
234 | 9 | } |
235 | |
|
236 | |
private void assembleCredits(CluInfo clu, ProgramRequirementInfo progReq){ |
237 | 12 | Map<String,String> attributes = clu.getAttributes(); |
238 | 12 | if(attributes != null){ |
239 | 12 | String minCredits = attributes.get(ProgramAssemblerConstants.MIN_CREDITS); |
240 | 12 | String maxCredits = attributes.get(ProgramAssemblerConstants.MAX_CREDITS); |
241 | 12 | progReq.setMinCredits(isEmpty(minCredits)?null:Integer.parseInt(minCredits)); |
242 | 12 | progReq.setMaxCredits(isEmpty(maxCredits)?null:Integer.parseInt(maxCredits)); |
243 | |
} |
244 | 12 | } |
245 | |
|
246 | |
public StatementTreeViewAssembler getStatementTreeViewAssembler() { |
247 | 0 | return statementTreeViewAssembler; |
248 | |
} |
249 | |
|
250 | |
public void setStatementTreeViewAssembler( |
251 | |
StatementTreeViewAssembler statementTreeViewAssembler) { |
252 | 1 | this.statementTreeViewAssembler = statementTreeViewAssembler; |
253 | 1 | } |
254 | |
|
255 | |
public StatementService getStatementService() { |
256 | 0 | return statementService; |
257 | |
} |
258 | |
|
259 | |
public void setStatementService(StatementService statementService) { |
260 | 1 | this.statementService = statementService; |
261 | 1 | } |
262 | |
|
263 | |
public LearningObjectiveService getLoService() { |
264 | 0 | return loService; |
265 | |
} |
266 | |
|
267 | |
public void setLoService(LearningObjectiveService loService) { |
268 | 1 | this.loService = loService; |
269 | 1 | } |
270 | |
|
271 | |
public LuService getLuService() { |
272 | 0 | return luService; |
273 | |
} |
274 | |
|
275 | |
public void setLuService(LuService luService) { |
276 | 1 | this.luService = luService; |
277 | 1 | } |
278 | |
|
279 | |
public LoAssembler getLoAssembler() { |
280 | 0 | return loAssembler; |
281 | |
} |
282 | |
|
283 | |
public void setLoAssembler(LoAssembler loAssembler) { |
284 | 1 | this.loAssembler = loAssembler; |
285 | 1 | } |
286 | |
|
287 | |
public CluAssemblerUtils getCluAssemblerUtils() { |
288 | 0 | return cluAssemblerUtils; |
289 | |
} |
290 | |
|
291 | |
public void setCluAssemblerUtils(CluAssemblerUtils cluAssemblerUtils) { |
292 | 1 | this.cluAssemblerUtils = cluAssemblerUtils; |
293 | 1 | } |
294 | |
|
295 | |
public ProgramAssemblerUtils getProgramAssemblerUtils() { |
296 | 0 | return programAssemblerUtils; |
297 | |
} |
298 | |
|
299 | |
public void setProgramAssemblerUtils(ProgramAssemblerUtils programAssemblerUtils) { |
300 | 1 | this.programAssemblerUtils = programAssemblerUtils; |
301 | 1 | } |
302 | |
} |