Coverage Report - org.kuali.student.lum.service.assembler.CluAssemblerUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
CluAssemblerUtils
80%
106/131
78%
41/52
6.714
 
 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.service.assembler;
 17  
 
 18  
 import org.kuali.student.common.assembly.BaseDTOAssemblyNode;
 19  
 import org.kuali.student.common.assembly.BaseDTOAssemblyNode.NodeOperation;
 20  
 import org.kuali.student.common.assembly.data.AssemblyException;
 21  
 import org.kuali.student.common.dto.DtoConstants;
 22  
 import org.kuali.student.common.dto.RichTextInfo;
 23  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 24  
 import org.kuali.student.common.exceptions.InvalidParameterException;
 25  
 import org.kuali.student.common.exceptions.MissingParameterException;
 26  
 import org.kuali.student.common.exceptions.OperationFailedException;
 27  
 import org.kuali.student.lum.course.dto.LoDisplayInfo;
 28  
 import org.kuali.student.lum.course.service.assembler.LoAssembler;
 29  
 import org.kuali.student.lum.lo.dto.LoInfo;
 30  
 import org.kuali.student.lum.lo.service.LearningObjectiveService;
 31  
 import org.kuali.student.lum.lu.dto.CluLoRelationInfo;
 32  
 import org.kuali.student.lum.lu.dto.CluResultInfo;
 33  
 import org.kuali.student.lum.lu.dto.ResultOptionInfo;
 34  
 import org.kuali.student.lum.lu.service.LuService;
 35  
 
 36  
 import java.util.ArrayList;
 37  
 import java.util.Date;
 38  
 import java.util.HashMap;
 39  
 import java.util.List;
 40  
 import java.util.Map;
 41  
 import java.util.Map.Entry;
 42  
 
 43  
 /**
 44  
  * This is a description of what this class does - jimt don't forget to fill this in.
 45  
  *
 46  
  * @author Kuali Rice Team (kuali-rice@googlegroups.com)
 47  
  *
 48  
  */
 49  2
 public class CluAssemblerUtils {
 50  
     private LuService luService;
 51  
     private LearningObjectiveService loService;
 52  
     private LoAssembler loAssembler;
 53  
 
 54  
     public List<String> assembleCluResults(List<String> resultTypes, List<CluResultInfo> cluResults) throws AssemblyException{
 55  60
                 if(resultTypes==null){
 56  0
                         throw new AssemblyException("result types can not be null");
 57  
                 }
 58  60
                 List<String> results = new ArrayList<String>();
 59  
                 //Loop through all the CluResults to find those with the matching types
 60  60
                 for(CluResultInfo cluResult:cluResults){
 61  57
                         if(resultTypes.contains(cluResult.getType())){
 62  
                                 //Loop through all options and add to the list of Strings
 63  57
                                 for(ResultOptionInfo resultOption: cluResult.getResultOptions()){
 64  76
                                         results.add(resultOption.getResultComponentId());
 65  
                                 }
 66  
                         }
 67  
                 }
 68  60
                 return results;
 69  
         }
 70  
 
 71  
     public BaseDTOAssemblyNode<?, ?> disassembleCluResults(String cluId,
 72  
                         String cluState, List<String> options, NodeOperation operation, String resultType,
 73  
                         String resultsDescription, String resultDescription) throws AssemblyException {
 74  36
                 BaseDTOAssemblyNode<List<String>, CluResultInfo> cluResultNode = new BaseDTOAssemblyNode<List<String>, CluResultInfo>(null);
 75  36
                 if(resultType==null){
 76  0
                         throw new AssemblyException("resultType can not be null");
 77  
                 }
 78  
 
 79  
                 // Get the current options and put them in a map of option type id/cluResult
 80  36
                 Map<String, ResultOptionInfo> currentResults = new HashMap<String, ResultOptionInfo>();
 81  
 
 82  36
                 CluResultInfo cluResult = null;
 83  
 
 84  
         //TODO Check this for proper handling of multiple CluResultInfos?  
 85  
                 //If this is not a create, lookup the results for this clu
 86  36
                 if (!NodeOperation.CREATE.equals(operation)) {
 87  
                         try {
 88  25
                                 List<CluResultInfo> cluResultList = luService.getCluResultByClu(cluId);
 89  
 
 90  25
                                 for (CluResultInfo currentResult : cluResultList) {
 91  18
                                         if (resultType.equals(currentResult.getType())) {
 92  18
                                                 cluResult = currentResult;
 93  18
                                                 if(NodeOperation.DELETE.equals(operation)){
 94  
                                                         //if this is a delete, then we only need the CluResultInfo
 95  0
                                                         cluResultNode.setOperation(NodeOperation.DELETE);
 96  
                                                 }else{
 97  
                                                         //Find all the Result options and store in a map for easy access later
 98  18
                                                         cluResultNode.setOperation(NodeOperation.UPDATE);
 99  
                                                         
 100  18
                                                         for(ResultOptionInfo resultOption:currentResult.getResultOptions()){
 101  
                                                                 // Set the state of the result option so it appears in the KSLU_RSLT_OPT table
 102  
                                                             // States include Draft, Approved, Superseded etc.
 103  24
                                                             resultOption.setState(cluState);
 104  24
                                                             currentResults.put(resultOption.getResultComponentId(), resultOption);
 105  
                                                         }
 106  
                                                 }
 107  
                                         }
 108  
                                 }
 109  0
                         } catch (DoesNotExistException e) {
 110  0
                         } catch (InvalidParameterException e) {
 111  0
                                 throw new AssemblyException("Error getting related " + resultsDescription, e);
 112  0
                         } catch (MissingParameterException e) {
 113  0
                                 throw new AssemblyException("Error getting related " + resultsDescription, e);
 114  0
                         } catch (OperationFailedException e) {
 115  0
                                 throw new AssemblyException("Error getting related " + resultsDescription, e);
 116  25
                         }
 117  
                 }
 118  
 
 119  
                 //If this is a delete we don't need the result options, just the CluResultInfo
 120  36
                 if(!NodeOperation.DELETE.equals(operation)){
 121  36
                         if(cluResult == null){
 122  
                                 //Create a new resultInfo of the given type if one does not exist and set operation to Create
 123  18
                                 cluResult = new CluResultInfo();
 124  18
                                 cluResult.setCluId(cluId);
 125  18
                                 cluResult.setState(cluState);
 126  18
                                 cluResult.setType(resultType);
 127  18
                                 RichTextInfo desc = new RichTextInfo();
 128  18
                                 desc.setPlain(resultsDescription);
 129  18
                                 cluResult.setDesc(desc);
 130  18
                                 cluResult.setEffectiveDate(new Date());
 131  18
                                 cluResultNode.setOperation(NodeOperation.CREATE);
 132  
                         }
 133  
 
 134  
                         
 135  36
                         cluResult.setResultOptions(new ArrayList<ResultOptionInfo>());
 136  
 
 137  
             // Set the state of the result so it appears in the KSLU_CLU_RSLT table
 138  
             // States include Draft, Approved, Superseded etc.
 139  36
                         cluResult.setState(cluState);
 140  
                         
 141  
                         // Loop through all the credit options in this clu
 142  36
                         for (String optionType : options) {
 143  57
                                 if(currentResults.containsKey(optionType)){
 144  
                                         //If the option exists already copy it to the new list of result options
 145  24
                                         ResultOptionInfo resultOptionInfo = currentResults.get(optionType);
 146  
                     
 147  
                                         // Set the state of the result option so it appears in the KSLU_RSLT_OPT table
 148  
                     // States include Draft, Approved, Superseded etc.
 149  24
                                         resultOptionInfo.setState(cluState);
 150  24
                                         cluResult.getResultOptions().add(resultOptionInfo);
 151  24
                                 }else{
 152  
                                         //Otherwise create a new result option
 153  33
                                         ResultOptionInfo resultOptionInfo = new ResultOptionInfo();
 154  33
                                         RichTextInfo desc = new RichTextInfo();
 155  33
                                         desc.setPlain(resultDescription);
 156  33
                                         resultOptionInfo.setDesc(desc);
 157  33
                                         resultOptionInfo.setResultComponentId(optionType);
 158  
                     
 159  
                                         // Set the state of the result option so it appears in the KSLU_RSLT_OPT table
 160  
                     // States include Draft, Approved, Superseded etc.
 161  33
                     resultOptionInfo.setState(cluState);
 162  
 
 163  33
                                         cluResult.getResultOptions().add(resultOptionInfo);
 164  57
                                 }
 165  
                         }
 166  
                 }
 167  
 
 168  36
                 cluResultNode.setNodeData(cluResult);
 169  36
                 return cluResultNode;
 170  
         }
 171  
 
 172  
     public List<LoDisplayInfo> assembleLos(String cluId, boolean shallowBuild) throws AssemblyException {
 173  97
         List<LoDisplayInfo> loInfos = new ArrayList<LoDisplayInfo>();
 174  
         try {
 175  97
             List<CluLoRelationInfo> cluLoRelations = luService.getCluLoRelationsByClu(cluId);
 176  97
             for (CluLoRelationInfo cluLoRelation : cluLoRelations) {
 177  82
                 String loId = cluLoRelation.getLoId();
 178  82
                 LoInfo lo = loService.getLo(loId);
 179  82
                 loInfos.add(loAssembler.assemble(lo, null, shallowBuild));
 180  82
             }
 181  0
         } catch (Exception e) {
 182  0
             throw new AssemblyException("Error getting learning objectives", e);
 183  97
         }
 184  
 
 185  97
         return loInfos;
 186  
     }
 187  
     
 188  
         public List<BaseDTOAssemblyNode<?, ?>> disassembleLos(String cluId, String cluState, List<LoDisplayInfo> loInfos,
 189  
                         NodeOperation operation) throws AssemblyException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException{
 190  
                 // TODO Auto-generated method stub
 191  61
                 List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
 192  
 
 193  
                 // Get the current formats and put them in a map of format id/relation
 194  
                 // id
 195  61
                 Map<String, CluLoRelationInfo> currentCluLoRelations = new HashMap<String, CluLoRelationInfo>();
 196  
                 try {
 197  61
                         List<CluLoRelationInfo> cluLoRelations = luService.getCluLoRelationsByClu(cluId);
 198  61
                         for(CluLoRelationInfo cluLoRelation:cluLoRelations){
 199  16
                                 if(CluAssemblerConstants.CLU_LO_CLU_SPECIFIC_RELATION.equals(cluLoRelation.getType())){
 200  16
                                         currentCluLoRelations.put(cluLoRelation.getLoId(), cluLoRelation);
 201  
                                 }
 202  
                         }
 203  0
                 } catch (DoesNotExistException e) {
 204  0
                 } catch (Exception e) {
 205  0
                         throw new AssemblyException("Error finding related Los", e);
 206  61
                 }
 207  
 
 208  
                 // Loop through all the los in this clu
 209  61
                 for(LoDisplayInfo loDisplay : loInfos){
 210  
 
 211  
                         // If this is a clu create/new lo update then all los will be created
 212  70
                     if (NodeOperation.CREATE == operation
 213  
                             || (NodeOperation.UPDATE == operation &&  !currentCluLoRelations.containsKey(loDisplay.getLoInfo().getId()))) {
 214  
 
 215  
                 // the lo does not exist, so create
 216  
                 // Assemble and add the lo
 217  54
                             loDisplay.getLoInfo().setId(null);
 218  54
                             loDisplay.getLoInfo().setState(cluState);
 219  54
                 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = loAssembler
 220  
                         .disassemble(loDisplay, NodeOperation.CREATE);
 221  54
                 results.add(loNode);
 222  
 
 223  
                 // Create the relationship and add it as well
 224  54
                 CluLoRelationInfo relation = new CluLoRelationInfo();
 225  54
                 relation.setCluId(cluId);
 226  54
                 relation.setLoId(loNode.getNodeData().getId());
 227  54
                 relation.setType(CluAssemblerConstants.CLU_LO_CLU_SPECIFIC_RELATION);
 228  
                 
 229  
                 // Relations can be either Active or Suspended
 230  
                 // For now, we set them all to Active
 231  
                 // DO NOT use states like draft, superseded, etc for relations
 232  54
                 relation.setState(DtoConstants.STATE_ACTIVE);
 233  
 
 234  54
                 BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo> relationNode = new BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo>(
 235  
                         null);
 236  54
                 relationNode.setNodeData(relation);
 237  54
                 relationNode.setOperation(NodeOperation.CREATE);
 238  
 
 239  54
                 results.add(relationNode);
 240  54
             } else if (NodeOperation.UPDATE == operation
 241  
                                         && currentCluLoRelations.containsKey(loDisplay.getLoInfo().getId())) {
 242  
                                 // On update, we need to change the state of the LO to
 243  
                 // match the state of the parent program
 244  14
                 loDisplay.getLoInfo().setState(cluState);
 245  14
                 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = loAssembler
 246  
                                 .disassemble(loDisplay, NodeOperation.UPDATE);
 247  14
                                 results.add(loNode);
 248  
 
 249  
                                 // remove this entry from the map so we can tell what needs to
 250  
                                 // be deleted at the end
 251  14
                                 currentCluLoRelations.remove(loDisplay.getLoInfo().getId());
 252  14
                         } else if (NodeOperation.DELETE == operation
 253  
                     && currentCluLoRelations.containsKey(loDisplay.getLoInfo().getId())) {
 254  
 
 255  
                 // Delete the Format and its relation
 256  2
                                 CluLoRelationInfo relationToDelete = currentCluLoRelations.get(loDisplay.getLoInfo().getId());
 257  2
                 BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo>(
 258  
                         null);
 259  2
                 relationToDeleteNode.setNodeData(relationToDelete);
 260  2
                 relationToDeleteNode.setOperation(NodeOperation.DELETE);
 261  2
                 results.add(relationToDeleteNode);
 262  
 
 263  2
                 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = loAssembler
 264  
                                         .disassemble(loDisplay, NodeOperation.DELETE);
 265  2
                 results.add(loNode);
 266  
 
 267  
                 // remove this entry from the map so we can tell what needs to
 268  
                 // be deleted at the end
 269  2
                 currentCluLoRelations.remove(loDisplay.getLoInfo().getId());
 270  70
                         }
 271  
                 }
 272  
 
 273  
         // Now any leftover lo ids are no longer needed, so delete
 274  
         // los and relations
 275  61
         for (Entry<String, CluLoRelationInfo> entry : currentCluLoRelations.entrySet()) {
 276  
             // Create a new relation with the id of the relation we want to
 277  
             // delete
 278  0
                 CluLoRelationInfo relationToDelete = entry.getValue();
 279  0
             BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo>(
 280  
                     null);
 281  0
             relationToDeleteNode.setNodeData(relationToDelete);
 282  0
             relationToDeleteNode.setOperation(NodeOperation.DELETE);
 283  0
             results.add(relationToDeleteNode);
 284  
 
 285  0
             LoInfo loToDelete = loService.getLo(entry.getKey());
 286  0
             LoDisplayInfo loDisplayToDelete = loAssembler.assemble(loToDelete, null, false);
 287  0
             BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = loAssembler
 288  
                             .disassemble(loDisplayToDelete, NodeOperation.DELETE);
 289  0
             results.add(loNode);
 290  0
         }
 291  
 
 292  61
                 return results;
 293  
         }
 294  
 
 295  
     // Spring setters
 296  
     public void setLuService(LuService luService) {
 297  2
         this.luService = luService;
 298  2
     }
 299  
 
 300  
     public void setLoService(LearningObjectiveService loService) {
 301  2
         this.loService = loService;
 302  2
     }
 303  
 
 304  
     public void setLoAssembler(LoAssembler loAssembler) {
 305  2
         this.loAssembler = loAssembler;
 306  2
     }
 307  
 }