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