Coverage Report - org.kuali.student.lum.program.service.assembler.MajorDisciplineAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
MajorDisciplineAssembler
86%
146/168
70%
55/78
4.688
 
 1  
 /*
 2  
  * Copyright 2007 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl1.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.student.lum.program.service.assembler;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 import java.util.Map;
 21  
 
 22  
 import org.apache.log4j.Logger;
 23  
 import org.kuali.student.common.assembly.BOAssembler;
 24  
 import org.kuali.student.common.assembly.BaseDTOAssemblyNode;
 25  
 import org.kuali.student.common.assembly.BaseDTOAssemblyNode.NodeOperation;
 26  
 import org.kuali.student.common.assembly.data.AssemblyException;
 27  
 import org.kuali.student.common.dto.AmountInfo;
 28  
 import org.kuali.student.common.dto.DtoConstants;
 29  
 import org.kuali.student.common.exceptions.DataValidationErrorException;
 30  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 31  
 import org.kuali.student.common.exceptions.InvalidParameterException;
 32  
 import org.kuali.student.common.exceptions.MissingParameterException;
 33  
 import org.kuali.student.common.exceptions.OperationFailedException;
 34  
 import org.kuali.student.lum.course.service.assembler.CourseAssembler;
 35  
 import org.kuali.student.lum.lu.dto.CluCluRelationInfo;
 36  
 import org.kuali.student.lum.lu.dto.CluInfo;
 37  
 import org.kuali.student.lum.lu.service.LuService;
 38  
 import org.kuali.student.lum.program.dto.CoreProgramInfo;
 39  
 import org.kuali.student.lum.program.dto.MajorDisciplineInfo;
 40  
 import org.kuali.student.lum.program.dto.ProgramVariationInfo;
 41  
 import org.kuali.student.lum.service.assembler.CluAssemblerUtils;
 42  
 
 43  
 
 44  
 /**
 45  
  * @author KS TODO - Much of this should be shared with ProgramVariationAssembler (and probably other Program Assemblers to
 46  
  *         come). AssemblerUtils?
 47  
  */
 48  12
 public class MajorDisciplineAssembler implements BOAssembler<MajorDisciplineInfo, CluInfo> {
 49  1
     final static Logger LOG = Logger.getLogger(CourseAssembler.class);
 50  
 
 51  
     private LuService luService;
 52  
 
 53  
     private ProgramVariationAssembler programVariationAssembler;
 54  
     private CoreProgramAssembler coreProgramAssembler;
 55  
     private CluAssemblerUtils cluAssemblerUtils;
 56  
     private ProgramAssemblerUtils programAssemblerUtils;
 57  
 
 58  
     @Override
 59  
     public MajorDisciplineInfo assemble(CluInfo clu, MajorDisciplineInfo majorDiscipline, boolean shallowBuild) throws AssemblyException {
 60  27
         MajorDisciplineInfo mdInfo = (null != majorDiscipline) ? majorDiscipline : new MajorDisciplineInfo();
 61  
 
 62  
         // Copy all the data from the clu to the majordiscipline
 63  27
         programAssemblerUtils.assembleBasics(clu, mdInfo);
 64  27
         programAssemblerUtils.assembleIdentifiers(clu, mdInfo);
 65  27
         programAssemblerUtils.assembleBasicAdminOrgs(clu, mdInfo);
 66  27
         programAssemblerUtils.assembleFullOrgs(clu, mdInfo);
 67  27
         programAssemblerUtils.assembleAtps(clu, mdInfo);
 68  27
         programAssemblerUtils.assembleLuCodes(clu, mdInfo);
 69  
 
 70  27
         mdInfo.setIntensity((null != clu.getIntensity()) ? clu.getIntensity().getUnitType() : null);
 71  27
         mdInfo.setStdDuration(clu.getStdDuration());
 72  27
         mdInfo.setPublishedInstructors(clu.getInstructors());
 73  27
         mdInfo.setCampusLocations(clu.getCampusLocations());        
 74  27
         mdInfo.setAccreditingAgencies(clu.getAccreditations());
 75  27
         mdInfo.setEffectiveDate(clu.getEffectiveDate());
 76  27
         mdInfo.setDescr(clu.getDescr());
 77  27
         mdInfo.setVersionInfo(clu.getVersionInfo());
 78  27
         mdInfo.setNextReviewPeriod(clu.getNextReviewPeriod());
 79  
 
 80  27
         if (!shallowBuild) {
 81  14
                 programAssemblerUtils.assembleRequirements(clu, mdInfo);
 82  14
             mdInfo.setCredentialProgramId(programAssemblerUtils.getCredentialProgramID(clu.getId()));
 83  14
             mdInfo.setResultOptions(programAssemblerUtils.assembleResultOptions(clu.getId()));
 84  14
             mdInfo.setLearningObjectives(cluAssemblerUtils.assembleLos(clu.getId(), shallowBuild));
 85  14
             mdInfo.setVariations(assembleVariations(clu.getId(), shallowBuild));
 86  14
             mdInfo.setOrgCoreProgram(assembleCoreProgram(clu.getId(), shallowBuild));
 87  14
             programAssemblerUtils.assemblePublications(clu, mdInfo);
 88  
         }
 89  
         
 90  27
        return mdInfo;
 91  
     }
 92  
 
 93  
     private CoreProgramInfo assembleCoreProgram(String cluId, boolean shallowBuild) throws AssemblyException {
 94  14
         CoreProgramInfo coreProgramInfo = null;
 95  
         try {
 96  14
             List<CluInfo> corePrograms = luService.getRelatedClusByCluId(cluId, ProgramAssemblerConstants.HAS_CORE_PROGRAM);
 97  
             // TODO - is it an error if there's more than one core program?
 98  14
             if (corePrograms.size() == 1) {
 99  12
                 coreProgramInfo = coreProgramAssembler.assemble(corePrograms.get(0), null, shallowBuild);
 100  2
             } else if (corePrograms.size() > 1) {
 101  0
                 throw new AssemblyException(new DataValidationErrorException("MajorDiscipline has more than one associated Core Program"));
 102  
             }
 103  0
         } catch (Exception e) {
 104  0
             throw new AssemblyException(e);
 105  14
         }
 106  14
         return coreProgramInfo;
 107  
     }
 108  
 
 109  
     private List<ProgramVariationInfo> assembleVariations(String cluId, boolean shallowBuild) throws AssemblyException {
 110  14
         List<ProgramVariationInfo> variations = new ArrayList<ProgramVariationInfo>();
 111  
 
 112  
         try {
 113  14
                 Map<String, CluCluRelationInfo> currentRelations = null;
 114  14
                 currentRelations = programAssemblerUtils.getCluCluActiveRelations(cluId, ProgramAssemblerConstants.HAS_PROGRAM_VARIATION);
 115  
                 
 116  14
                 if(currentRelations != null && !currentRelations.isEmpty()){
 117  14
                         for (String variationId : currentRelations.keySet()) {
 118  30
                                 CluInfo variationClu = luService.getClu(variationId);
 119  30
                                 variations.add(programVariationAssembler.assemble(variationClu, null, shallowBuild));
 120  30
                         }
 121  
                 }
 122  0
         } catch (Exception e) {
 123  0
             throw new AssemblyException(e);
 124  14
         }
 125  14
         return variations;
 126  
     }
 127  
 
 128  
     @Override
 129  
     public BaseDTOAssemblyNode<MajorDisciplineInfo, CluInfo> disassemble(MajorDisciplineInfo major, NodeOperation operation) throws AssemblyException {
 130  11
                 if (major == null) {
 131  0
                     LOG.error("Major for  disassemble is null!");
 132  0
                         throw new AssemblyException("Major cannot be null");
 133  
                 }
 134  
 
 135  
         //TODO   IDs for objects w/o ids
 136  
 
 137  11
                 BaseDTOAssemblyNode<MajorDisciplineInfo, CluInfo> result = new BaseDTOAssemblyNode<MajorDisciplineInfo, CluInfo>(
 138  
                                 this);
 139  
                 
 140  
                 CluInfo clu;
 141  
                 try {
 142  11
                         clu = (NodeOperation.UPDATE == operation) ? luService.getClu(major.getId()) : new CluInfo();
 143  0
         } catch (Exception e) {
 144  0
                         throw new AssemblyException("Error getting existing learning unit during major update", e);
 145  11
         } 
 146  
         
 147  11
         boolean stateChanged = NodeOperation.UPDATE == operation && major.getState() != null && !major.getState().equals(clu.getState());
 148  
         
 149  11
         programAssemblerUtils.disassembleBasics(clu, major);
 150  11
         if (major.getId() == null)
 151  3
             major.setId(clu.getId());
 152  11
         programAssemblerUtils.disassembleLuCodes(clu, major, operation);
 153  11
         programAssemblerUtils.disassembleAdminOrgs(clu, major, operation);
 154  11
         programAssemblerUtils.disassembleAtps(clu, major, operation);
 155  11
         programAssemblerUtils.disassembleIdentifiers(clu, major, operation);
 156  11
         programAssemblerUtils.disassemblePublications(clu, major, operation, result);
 157  
         
 158  11
         if(major.getProgramRequirements() != null && !major.getProgramRequirements().isEmpty()) {
 159  4
                 programAssemblerUtils.disassembleRequirements(clu, major, operation, result, stateChanged);                
 160  
         }
 161  
 
 162  11
         if (major.getVariations() != null && !major.getVariations().isEmpty()) {
 163  
             try {
 164  11
                                 disassembleVariations(major, operation, result);
 165  0
                         } catch (Exception e) {
 166  0
                                 throw new AssemblyException("Error diassembling Variations during major update", e);
 167  11
                         } 
 168  
         }
 169  11
         if (major.getOrgCoreProgram() != null ) {
 170  9
             disassembleCoreProgram(major, operation, result);
 171  
         }
 172  11
         if (major.getCredentialProgramId() != null) {
 173  11
             disassembleCredentialProgram(major, operation, result);
 174  
         }
 175  11
         if (major.getResultOptions() != null) {
 176  11
             disassembleResultOptions(major, operation, result);
 177  
         }
 178  11
         if (major.getLearningObjectives() != null) {
 179  11
             disassembleLearningObjectives(major, operation, result);
 180  
         }
 181  
 
 182  11
         AmountInfo intensity = new AmountInfo();
 183  11
         intensity.setUnitType(major.getIntensity());
 184  11
                 clu.setIntensity(intensity);
 185  11
         clu.setStdDuration(major.getStdDuration());
 186  11
         clu.setInstructors(major.getPublishedInstructors());
 187  
 
 188  11
         clu.setNextReviewPeriod(major.getNextReviewPeriod());
 189  11
         clu.setEffectiveDate(major.getEffectiveDate());
 190  
 
 191  11
         clu.setCampusLocations(major.getCampusLocations());
 192  11
         clu.setDescr(major.getDescr());
 193  
 
 194  11
         clu.setAccreditations(major.getAccreditingAgencies());
 195  11
         clu.setNextReviewPeriod(major.getNextReviewPeriod());
 196  11
         clu.setState(major.getState());
 197  
 
 198  
                 // Add the Clu to the result
 199  11
                 result.setNodeData(clu);
 200  11
                 result.setOperation(operation);
 201  11
                 result.setBusinessDTORef(major);
 202  
 
 203  11
             return result;
 204  
     }
 205  
 
 206  
     private void disassembleLearningObjectives(MajorDisciplineInfo major, NodeOperation operation, BaseDTOAssemblyNode<MajorDisciplineInfo, CluInfo> result) throws AssemblyException {
 207  
         try {
 208  11
             List<BaseDTOAssemblyNode<?, ?>> loResults = cluAssemblerUtils.disassembleLos(major.getId(), major.getState(),  major.getLearningObjectives(), operation);
 209  11
             if (loResults != null) {
 210  11
                 result.getChildNodes().addAll(loResults);
 211  
             }
 212  0
         } catch (DoesNotExistException e) {
 213  0
         } catch (Exception e) {
 214  0
             throw new AssemblyException("Error while disassembling los", e);
 215  11
         }
 216  11
     }
 217  
 
 218  
     private void disassembleResultOptions(MajorDisciplineInfo major, NodeOperation operation, BaseDTOAssemblyNode<MajorDisciplineInfo, CluInfo> result) throws AssemblyException {
 219  
         //TODO Check for ProgramAssemblerConstants.CERTIFICATE_RESULTS too
 220  
         
 221  11
         BaseDTOAssemblyNode<?, ?> degreeResults = cluAssemblerUtils.disassembleCluResults(
 222  
                 major.getId(), major.getState(), major.getResultOptions(), operation, ProgramAssemblerConstants.DEGREE_RESULTS, "Result options", "Result option");
 223  11
         if (degreeResults != null) {
 224  11
             result.getChildNodes().add(degreeResults);
 225  
         }
 226  11
     }
 227  
 
 228  
     private void disassembleCredentialProgram(MajorDisciplineInfo major, NodeOperation operation, BaseDTOAssemblyNode<MajorDisciplineInfo, CluInfo> result) throws AssemblyException {
 229  
 
 230  
         List<BaseDTOAssemblyNode<?,?>> credentialResults;
 231  
         try {
 232  11
             credentialResults = programAssemblerUtils.disassembleCredentialProgram(major, operation, ProgramAssemblerConstants.HAS_MAJOR_PROGRAM);
 233  11
             if (credentialResults != null) {
 234  11
                 result.getChildNodes().addAll(credentialResults);
 235  
             }
 236  0
         } catch (Exception e) {
 237  0
             throw new AssemblyException("Error while disassembling Credential program", e);
 238  11
         }
 239  11
     }
 240  
 
 241  
     private void disassembleVariations(MajorDisciplineInfo major, NodeOperation operation, BaseDTOAssemblyNode<MajorDisciplineInfo, CluInfo> result) throws AssemblyException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 242  11
             Map<String, CluCluRelationInfo> currentRelations = null;
 243  11
             List<BaseDTOAssemblyNode<?, ?>> nodes = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
 244  
             
 245  11
             if (!NodeOperation.CREATE.equals(operation)){
 246  8
                     currentRelations = programAssemblerUtils.getCluCluActiveRelations(major.getId(), ProgramAssemblerConstants.HAS_PROGRAM_VARIATION);
 247  
             }
 248  
             
 249  
             // Loop through all the variations in this MD
 250  11
         for (ProgramVariationInfo variation : major.getVariations()) {
 251  
             BaseDTOAssemblyNode<?,?> variationNode;
 252  23
             variation.setState(major.getState());
 253  
             try {
 254  23
                     if (NodeOperation.UPDATE.equals(operation) && variation.getId() != null
 255  
                                                 && (currentRelations != null && currentRelations.containsKey(variation.getId()))) {
 256  
                         // If the relationship already exists update it
 257  
                         // remove this entry from the map so we can tell what needs to be deleted at the end
 258  16
                             variationNode = programVariationAssembler.disassemble(variation, operation);
 259  16
                             if (variationNode != null) nodes.add(variationNode);
 260  16
                             currentRelations.remove(variation.getId());  
 261  7
                     } else if (!NodeOperation.DELETE.equals(operation)) {
 262  
                                         // the variation does not exist, so create variation & cluclurelation
 263  7
                             variationNode = programVariationAssembler.disassemble(variation, NodeOperation.CREATE);
 264  7
                             if (variationNode != null) nodes.add(variationNode);
 265  7
                                         programAssemblerUtils.addCreateRelationNode(major.getId(), variation.getId(), ProgramAssemblerConstants.HAS_PROGRAM_VARIATION, nodes);
 266  
                                 }
 267  0
             } catch (Exception e) {
 268  0
                 throw new AssemblyException("Error while disassembling Variation", e);
 269  23
             } 
 270  
         }
 271  
         
 272  
         // Now any leftover variation ids are no longer needed, so suspend them
 273  11
         if(currentRelations != null && currentRelations.size() > 0){
 274  1
                 programAssemblerUtils.addSuspendedRelationNodes(currentRelations, nodes);          
 275  1
                 addInactivateVariationNodes(currentRelations, nodes);
 276  
         }
 277  
 
 278  11
         result.getChildNodes().addAll(nodes);
 279  11
     }
 280  
 
 281  
     private void addInactivateVariationNodes(Map<String, CluCluRelationInfo> currentRelations, List<BaseDTOAssemblyNode<?, ?>> nodes) throws AssemblyException{
 282  1
             for (String variationId : currentRelations.keySet()) {
 283  
                         CluInfo variationClu;
 284  
                         try {
 285  1
                                 variationClu = luService.getClu(variationId);
 286  1
                                 ProgramVariationInfo delVariation = programVariationAssembler.assemble(variationClu, null, true);
 287  1
                                 delVariation.setState(DtoConstants.STATE_SUSPENDED);
 288  1
                                 BaseDTOAssemblyNode<?,?> variationNode = programVariationAssembler.disassemble(delVariation , NodeOperation.UPDATE);
 289  1
                                 if (variationNode != null) nodes.add(variationNode);
 290  0
                         } catch (Exception e) {
 291  0
                                 throw new AssemblyException("Error while disassembling variation, deactivateVariations", e);
 292  1
                         }
 293  1
             }
 294  1
     }
 295  
     
 296  
     private void disassembleCoreProgram(MajorDisciplineInfo major, NodeOperation operation, BaseDTOAssemblyNode<MajorDisciplineInfo, CluInfo> result) throws AssemblyException {
 297  
 
 298  
         BaseDTOAssemblyNode<?,?> coreResults;
 299  
         try {
 300  9
                 major.getOrgCoreProgram().setState(major.getState());
 301  9
             coreResults = coreProgramAssembler.disassemble(major.getOrgCoreProgram(), operation);
 302  9
             if (coreResults != null) {
 303  9
                 result.getChildNodes().add(coreResults);
 304  
             }
 305  0
         } catch (Exception e) {
 306  0
             throw new AssemblyException("Error while disassembling Core program", e);
 307  9
         }
 308  9
     }
 309  
 
 310  
     // Setters for Spring
 311  
     public void setLuService(LuService luService) {
 312  1
         this.luService = luService;
 313  1
     }
 314  
 
 315  
         public void setProgramVariationAssembler(ProgramVariationAssembler programVariationAssembler) {
 316  1
         this.programVariationAssembler = programVariationAssembler;
 317  1
     }
 318  
 
 319  
     public ProgramVariationAssembler getProgramVariationAssembler() {
 320  12
         return programVariationAssembler;
 321  
     }
 322  
 
 323  
     public void setCoreProgramAssembler(CoreProgramAssembler coreProgramAssembler) {
 324  1
         this.coreProgramAssembler = coreProgramAssembler;
 325  1
     }
 326  
 
 327  
     public void setCluAssemblerUtils(CluAssemblerUtils cluAssemblerUtils) {
 328  1
         this.cluAssemblerUtils = cluAssemblerUtils;
 329  1
     }
 330  
 
 331  
     public void setProgramAssemblerUtils(ProgramAssemblerUtils programAssemblerUtils) {
 332  1
         this.programAssemblerUtils = programAssemblerUtils;
 333  1
     }
 334  
 }