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