| 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 | 0 | public class ProgramRequirementAssembler implements BOAssembler<ProgramRequirementInfo, CluInfo> { |
| 45 | 0 | 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 | 0 | ProgramRequirementInfo progReq = (progReqInfo != null ? progReqInfo : new ProgramRequirementInfo()); |
| 61 | |
|
| 62 | 0 | if (clu.getOfficialIdentifier() != null) { |
| 63 | 0 | progReq.setShortTitle(clu.getOfficialIdentifier().getShortName()); |
| 64 | 0 | progReq.setLongTitle(clu.getOfficialIdentifier().getLongName()); |
| 65 | |
} |
| 66 | 0 | progReq.setDescr(clu.getDescr()); |
| 67 | |
|
| 68 | |
|
| 69 | 0 | assembleCredits(clu, progReq); |
| 70 | |
|
| 71 | 0 | if (progReq.getStatement() == null) { |
| 72 | |
try { |
| 73 | 0 | List<RefStatementRelationInfo> relations = statementService.getRefStatementRelationsByRef(ProgramAssemblerConstants.PROGRAM_REQUIREMENT, clu.getId()); |
| 74 | |
|
| 75 | |
|
| 76 | 0 | StatementTreeViewInfo statementTree = new StatementTreeViewInfo(); |
| 77 | 0 | if (relations != null) { |
| 78 | 0 | statementTreeViewAssembler.assemble(statementService.getStatementTreeView(relations.get(0).getStatementId()), statementTree, shallowBuild); |
| 79 | |
} |
| 80 | 0 | progReq.setStatement(statementTree); |
| 81 | 0 | } catch (AssemblyException e) { |
| 82 | 0 | throw e; |
| 83 | 0 | } catch (Exception e) { |
| 84 | 0 | throw new AssemblyException(e); |
| 85 | 0 | } |
| 86 | |
} |
| 87 | |
|
| 88 | 0 | if (isEmpty(progReq.getLearningObjectives())) { |
| 89 | 0 | progReq.setLearningObjectives(cluAssemblerUtils.assembleLos(clu.getId(), shallowBuild)); |
| 90 | |
} |
| 91 | |
|
| 92 | 0 | progReq.setMetaInfo(clu.getMetaInfo()); |
| 93 | 0 | progReq.setType(clu.getType()); |
| 94 | 0 | progReq.setAttributes(clu.getAttributes()); |
| 95 | 0 | progReq.setId(clu.getId()); |
| 96 | |
|
| 97 | 0 | return progReq; |
| 98 | |
} |
| 99 | |
|
| 100 | |
@Override |
| 101 | |
public BaseDTOAssemblyNode<ProgramRequirementInfo, CluInfo> disassemble( |
| 102 | |
ProgramRequirementInfo progReq, NodeOperation operation) |
| 103 | |
throws AssemblyException { |
| 104 | |
|
| 105 | 0 | 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 | 0 | BaseDTOAssemblyNode<ProgramRequirementInfo, CluInfo> result = new BaseDTOAssemblyNode<ProgramRequirementInfo, CluInfo>(null); |
| 113 | |
|
| 114 | |
|
| 115 | 0 | StatementTreeViewInfo statement = progReq.getStatement(); |
| 116 | 0 | statement.setId(UUIDHelper.genStringUUID(statement.getId())); |
| 117 | |
BaseDTOAssemblyNode<StatementTreeViewInfo, StatementTreeViewInfo> statementTree; |
| 118 | |
try { |
| 119 | 0 | 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 | 0 | } |
| 125 | 0 | result.getChildNodes().add(statementTree); |
| 126 | |
|
| 127 | |
CluInfo clu; |
| 128 | |
try { |
| 129 | 0 | 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 | 0 | } |
| 133 | |
|
| 134 | 0 | if (operation.equals(NodeOperation.DELETE)) { |
| 135 | |
try { |
| 136 | 0 | final List<CluCluRelationInfo> relations = luService.getCluCluRelationsByClu(progReq.getId()); |
| 137 | 0 | final BaseDTOAssemblyNode<ProgramRequirementInfo, CluCluRelationInfo> cluRelation = new BaseDTOAssemblyNode<ProgramRequirementInfo, CluCluRelationInfo>(null); |
| 138 | 0 | if (relations.size() > 1) { |
| 139 | 0 | throw new AssemblyException("Unable to dissamble ProgramRequirement, more than one CluCluRelation found"); |
| 140 | 0 | } else if (relations.size() == 1) { |
| 141 | 0 | cluRelation.setNodeData(relations.get(0)); |
| 142 | 0 | cluRelation.setOperation(operation); |
| 143 | 0 | result.getChildNodes().add(cluRelation); |
| 144 | |
} |
| 145 | 0 | } catch (Exception e) { |
| 146 | 0 | throw new AssemblyException(e); |
| 147 | 0 | } |
| 148 | |
} |
| 149 | |
|
| 150 | 0 | BaseDTOAssemblyNode<ProgramRequirementInfo, CluInfo> cluResult = new BaseDTOAssemblyNode<ProgramRequirementInfo, CluInfo>(this); |
| 151 | |
|
| 152 | 0 | cluResult.setNodeData(clu); |
| 153 | 0 | cluResult.setBusinessDTORef(progReq); |
| 154 | 0 | cluResult.setOperation(operation); |
| 155 | 0 | result.getChildNodes().add(cluResult); |
| 156 | |
|
| 157 | 0 | programAssemblerUtils.disassembleBasics(clu, progReq); |
| 158 | |
|
| 159 | |
|
| 160 | 0 | disassembleCredits(clu, progReq); |
| 161 | |
|
| 162 | 0 | progReq.setId(clu.getId()); |
| 163 | 0 | CluIdentifierInfo official = null != clu.getOfficialIdentifier() ? clu.getOfficialIdentifier() : new CluIdentifierInfo(); |
| 164 | 0 | official.setLongName(progReq.getLongTitle()); |
| 165 | 0 | official.setShortName(progReq.getShortTitle()); |
| 166 | 0 | official.setState(!isEmpty(clu.getState()) ? clu.getState() : DtoConstants.STATE_ACTIVE); |
| 167 | |
|
| 168 | 0 | official.setType(ProgramAssemblerConstants.OFFICIAL); |
| 169 | 0 | clu.setOfficialIdentifier(official); |
| 170 | |
|
| 171 | 0 | clu.setDescr(progReq.getDescr()); |
| 172 | 0 | clu.setState(!isEmpty(clu.getState()) ? clu.getState() : DtoConstants.STATE_ACTIVE); |
| 173 | 0 | if (progReq.getLearningObjectives() != null) { |
| 174 | 0 | disassembleLearningObjectives(progReq, operation, result); |
| 175 | |
} |
| 176 | |
|
| 177 | |
RefStatementRelationInfo relation; |
| 178 | 0 | if (operation == NodeOperation.CREATE) { |
| 179 | 0 | relation = new RefStatementRelationInfo(); |
| 180 | 0 | relation.setId(UUIDHelper.genStringUUID(null)); |
| 181 | |
} else { |
| 182 | |
try { |
| 183 | 0 | 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 | 0 | } |
| 187 | |
} |
| 188 | |
|
| 189 | 0 | relation.setType(ProgramAssemblerConstants.PROGRAM_REFERENCE_TYPE); |
| 190 | 0 | relation.setRefObjectId(clu.getId()); |
| 191 | 0 | relation.setRefObjectTypeKey(ProgramAssemblerConstants.PROGRAM_REQUIREMENT); |
| 192 | 0 | relation.setStatementId(statement.getId()); |
| 193 | 0 | relation.setState(DtoConstants.STATE_ACTIVE); |
| 194 | |
|
| 195 | 0 | BaseDTOAssemblyNode<ProgramRequirementInfo, RefStatementRelationInfo> relationNode = new BaseDTOAssemblyNode<ProgramRequirementInfo, RefStatementRelationInfo>(null); |
| 196 | 0 | relationNode.setNodeData(relation); |
| 197 | 0 | relationNode.setOperation(operation); |
| 198 | |
|
| 199 | 0 | result.getChildNodes().add(relationNode); |
| 200 | 0 | result.setBusinessDTORef(progReq); |
| 201 | 0 | result.setOperation(operation); |
| 202 | 0 | return result; |
| 203 | |
} |
| 204 | |
|
| 205 | |
private void disassembleLearningObjectives(ProgramRequirementInfo progReq, |
| 206 | |
NodeOperation operation, |
| 207 | |
BaseDTOAssemblyNode<ProgramRequirementInfo, CluInfo> result) throws AssemblyException { |
| 208 | |
try { |
| 209 | 0 | List<BaseDTOAssemblyNode<?, ?>> loResults = cluAssemblerUtils.disassembleLos(progReq.getId(), progReq.getState(), progReq.getLearningObjectives(), operation); |
| 210 | 0 | if (loResults != null) { |
| 211 | 0 | 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 | 0 | } |
| 217 | 0 | } |
| 218 | |
|
| 219 | |
private void disassembleCredits(CluInfo clu, ProgramRequirementInfo progReq){ |
| 220 | 0 | Map<String,String> attributes = null != clu.getAttributes() ? clu.getAttributes() : new HashMap<String,String>(); |
| 221 | |
|
| 222 | 0 | if(progReq.getMinCredits() != null){ |
| 223 | 0 | attributes.put(ProgramAssemblerConstants.MIN_CREDITS, Integer.toString(progReq.getMinCredits())); |
| 224 | |
}else{ |
| 225 | 0 | attributes.put(ProgramAssemblerConstants.MIN_CREDITS, null); |
| 226 | |
} |
| 227 | 0 | if(progReq.getMaxCredits() != null) { |
| 228 | 0 | attributes.put(ProgramAssemblerConstants.MAX_CREDITS, Integer.toString(progReq.getMaxCredits())); |
| 229 | |
}else{ |
| 230 | 0 | attributes.put(ProgramAssemblerConstants.MAX_CREDITS, null); |
| 231 | |
} |
| 232 | |
|
| 233 | 0 | clu.setAttributes(attributes); |
| 234 | 0 | } |
| 235 | |
|
| 236 | |
private void assembleCredits(CluInfo clu, ProgramRequirementInfo progReq){ |
| 237 | 0 | Map<String,String> attributes = clu.getAttributes(); |
| 238 | 0 | if(attributes != null){ |
| 239 | 0 | String minCredits = attributes.get(ProgramAssemblerConstants.MIN_CREDITS); |
| 240 | 0 | String maxCredits = attributes.get(ProgramAssemblerConstants.MAX_CREDITS); |
| 241 | 0 | progReq.setMinCredits(isEmpty(minCredits)?null:Integer.parseInt(minCredits)); |
| 242 | 0 | progReq.setMaxCredits(isEmpty(maxCredits)?null:Integer.parseInt(maxCredits)); |
| 243 | |
} |
| 244 | 0 | } |
| 245 | |
|
| 246 | |
public StatementTreeViewAssembler getStatementTreeViewAssembler() { |
| 247 | 0 | return statementTreeViewAssembler; |
| 248 | |
} |
| 249 | |
|
| 250 | |
public void setStatementTreeViewAssembler( |
| 251 | |
StatementTreeViewAssembler statementTreeViewAssembler) { |
| 252 | 0 | this.statementTreeViewAssembler = statementTreeViewAssembler; |
| 253 | 0 | } |
| 254 | |
|
| 255 | |
public StatementService getStatementService() { |
| 256 | 0 | return statementService; |
| 257 | |
} |
| 258 | |
|
| 259 | |
public void setStatementService(StatementService statementService) { |
| 260 | 0 | this.statementService = statementService; |
| 261 | 0 | } |
| 262 | |
|
| 263 | |
public LearningObjectiveService getLoService() { |
| 264 | 0 | return loService; |
| 265 | |
} |
| 266 | |
|
| 267 | |
public void setLoService(LearningObjectiveService loService) { |
| 268 | 0 | this.loService = loService; |
| 269 | 0 | } |
| 270 | |
|
| 271 | |
public LuService getLuService() { |
| 272 | 0 | return luService; |
| 273 | |
} |
| 274 | |
|
| 275 | |
public void setLuService(LuService luService) { |
| 276 | 0 | this.luService = luService; |
| 277 | 0 | } |
| 278 | |
|
| 279 | |
public LoAssembler getLoAssembler() { |
| 280 | 0 | return loAssembler; |
| 281 | |
} |
| 282 | |
|
| 283 | |
public void setLoAssembler(LoAssembler loAssembler) { |
| 284 | 0 | this.loAssembler = loAssembler; |
| 285 | 0 | } |
| 286 | |
|
| 287 | |
public CluAssemblerUtils getCluAssemblerUtils() { |
| 288 | 0 | return cluAssemblerUtils; |
| 289 | |
} |
| 290 | |
|
| 291 | |
public void setCluAssemblerUtils(CluAssemblerUtils cluAssemblerUtils) { |
| 292 | 0 | this.cluAssemblerUtils = cluAssemblerUtils; |
| 293 | 0 | } |
| 294 | |
|
| 295 | |
public ProgramAssemblerUtils getProgramAssemblerUtils() { |
| 296 | 0 | return programAssemblerUtils; |
| 297 | |
} |
| 298 | |
|
| 299 | |
public void setProgramAssemblerUtils(ProgramAssemblerUtils programAssemblerUtils) { |
| 300 | 0 | this.programAssemblerUtils = programAssemblerUtils; |
| 301 | 0 | } |
| 302 | |
} |