Coverage Report - org.kuali.student.lum.workflow.MajorDisciplineStateChangeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
MajorDisciplineStateChangeServiceImpl
0%
0/93
0%
0/38
2.818
 
 1  
 package org.kuali.student.lum.workflow;
 2  
 
 3  
 import java.util.Date;
 4  
 import java.util.List;
 5  
 
 6  
 import org.kuali.student.common.dto.DtoConstants;
 7  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 8  
 import org.kuali.student.common.exceptions.InvalidParameterException;
 9  
 import org.kuali.student.common.exceptions.MissingParameterException;
 10  
 import org.kuali.student.common.exceptions.OperationFailedException;
 11  
 import org.kuali.student.common.versionmanagement.dto.VersionDisplayInfo;
 12  
 import org.kuali.student.core.atp.dto.AtpInfo;
 13  
 import org.kuali.student.core.atp.service.AtpService;
 14  
 import org.kuali.student.core.statement.dto.StatementTreeViewInfo;
 15  
 import org.kuali.student.lum.program.dto.MajorDisciplineInfo;
 16  
 import org.kuali.student.lum.program.dto.ProgramRequirementInfo;
 17  
 import org.kuali.student.lum.program.dto.ProgramVariationInfo;
 18  
 import org.kuali.student.lum.program.service.ProgramService;
 19  
 import org.kuali.student.lum.program.service.ProgramServiceConstants;
 20  
 import org.springframework.transaction.annotation.Transactional;
 21  
 
 22  
 /**
 23  
  * This class is called whenever the state of a major discipline changes.
 24  
  * <p>
 25  
  * We have a separate class because the operations need to be marked with the @Transactional annotation.
 26  
  * <p>
 27  
  * THIS CLASS IS DUPLICATED FOR MAJOR DISCIPLINE, CORE, AND CREDENTIAL PROGRAMS SINCE THERE ARE
 28  
  * DIFFERENT SERVICE METHODS FOR EACH OF THESE TYPES (THOUGH THEY ARE SIMILAR)
 29  
  * <P>
 30  
  * 
 31  
  * @author Kuali Rice Team (kuali-rice@googlegroups.com)
 32  
  */
 33  
 @Transactional(noRollbackFor = {DoesNotExistException.class}, rollbackFor = {Throwable.class})
 34  0
 public class MajorDisciplineStateChangeServiceImpl implements StateChangeService{
 35  
 
 36  
     /**
 37  
      * The program service - injected by spring.
 38  
      */
 39  
     private ProgramService programService;
 40  
     private AtpService atpService;    
 41  
 
 42  
     /**
 43  
      * This method is called by workflow when the state changes.
 44  
      * 
 45  
      * @param majorDisciplineId
 46  
      * @param state
 47  
      * @return
 48  
      * @throws Exception
 49  
      */
 50  
     public void changeState(String majorDisciplineId, String newState) throws Exception {
 51  
         // This method will be called from workflow.
 52  
         // Since we cannot activate a program from the workflow we do not need to add endEntryTerm and endEnrollTerm
 53  0
         changeState(null, null, null, majorDisciplineId, newState);
 54  0
     }
 55  
 
 56  
     /**
 57  
      * This method is called from the UI (servlet) when state changes.
 58  
      * 
 59  
      * @param endEntryTerm
 60  
      * @param endEnrollTerm
 61  
      * @param programType
 62  
      * @param majorDisciplineId
 63  
      * @param newState
 64  
      * @return
 65  
      * @throws Exception
 66  
      */
 67  
     public void changeState(String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm, String majorDisciplineId, String newState) throws Exception {
 68  
 
 69  
         // A null state is valid in some cases!
 70  
         // If rice work flow returned a code that LUM is not going to process, then
 71  
         // our ProgramPostProcessorBase will return null 
 72  
         // So, if we see a null, assume we are not supposed to process the code and simply return from
 73  
         // the method without changing state
 74  
         // 1. Blanket Approve Proposal
 75  
         // 1.1 When blanket approved is selected, rice will change status through the following codes:
 76  
         // 1.2 Workflow Status Code = R  ... Change LUM state to "Draft", create new version, update state of all objects
 77  
         // 1.3 Workflow Status Code = A  ... Code 'A' is not processed.  Do not change state in LUM.
 78  
         // 1.4 Workflow Status Code = P  ... Change LUM state to "Approved", do not create new version, update requirements and variations to state "Approved"
 79  
 
 80  0
         if (newState == null){
 81  0
             return;
 82  
         }
 83  
                    
 84  
         // The version selected in the UI
 85  0
         MajorDisciplineInfo selectedVersion = programService.getMajorDiscipline(majorDisciplineId);
 86  
 
 87  
         // If we are activating this version we need to mark the previous version superseded,
 88  
         // update the previous version end terms, and make the selected version current.
 89  0
         if (newState.equals(DtoConstants.STATE_ACTIVE)) {
 90  
 
 91  
             // Update previous versions to superseded and set end terms on previous current version.
 92  0
                 updatePreviousVersions(selectedVersion, endEntryTerm, endEnrollTerm, endInstAdmitTerm);
 93  
 
 94  
             // Update state of all associated objects for current version
 95  
             // NOTE: we must update state BEFORE making the version current
 96  0
             updateMajorDisciplineInfoState(selectedVersion, newState);
 97  
 
 98  
             // Make this the current version
 99  0
             makeCurrent(selectedVersion);
 100  
         } else {
 101  
 
 102  
             // Update state of all associated objects for current version
 103  0
             updateMajorDisciplineInfoState(selectedVersion, newState);
 104  
         }
 105  
 
 106  
       
 107  
 
 108  0
     }
 109  
 
 110  
     /**
 111  
      * This method will update the state of this object and all associated objects.
 112  
      * <p>
 113  
      * It is needed because we need to make separate web service calls to update the state of these objects.
 114  
      * 
 115  
      * @param majorDisciplineInfo
 116  
      * @param newState
 117  
      */
 118  
     private void updateMajorDisciplineInfoState(MajorDisciplineInfo majorDisciplineInfo, String newState) throws Exception {
 119  
         // Update the statement tree
 120  0
         List<String> programRequirementIds = majorDisciplineInfo.getProgramRequirements();
 121  0
         updateRequirementsState(programRequirementIds, newState);
 122  
 
 123  
         
 124  
         // Update any variations 
 125  0
         List<ProgramVariationInfo> variationList = majorDisciplineInfo.getVariations();
 126  0
         updateVariationsRequirementsState(variationList, newState);
 127  
         
 128  
         
 129  
         // Update major discipline
 130  0
         majorDisciplineInfo.setState(newState);
 131  0
         programService.updateMajorDiscipline(majorDisciplineInfo);
 132  0
     }
 133  
 
 134  
     /**
 135  
      * This method will make this version of the major discipline the current one.
 136  
      * 
 137  
      * @param majorDisciplineInfo
 138  
      */
 139  
     private void makeCurrent(MajorDisciplineInfo majorDisciplineInfo) throws Exception {
 140  
 
 141  
         // Check if this is the current version before trying to make it current
 142  
         // (the web service will error if you try to make a version current that is already current)
 143  0
         VersionDisplayInfo currentVersion = programService.getCurrentVersion(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, majorDisciplineInfo.getVersionInfo().getVersionIndId());
 144  
 
 145  
         // If this is not the current version, then make it current
 146  0
         if (!currentVersion.getSequenceNumber().equals(majorDisciplineInfo.getVersionInfo().getSequenceNumber())) {
 147  0
             programService.setCurrentMajorDisciplineVersion(majorDisciplineInfo.getId(), null);
 148  
         }
 149  0
     }
 150  
 
 151  
 
 152  
     /**
 153  
      * This method finds all previous versions of program and sets all previous ACTIVE,APPROVED,DRAFT versions to SUPERSEDED and
 154  
      * sets new end terms for previous current version.
 155  
  
 156  
      * @param majorDisciplineInfo The version of major discipline program being activated
 157  
      * @param endEntryTerm The new end entry term to set on previous active version
 158  
      * @param endEnrollTerm The new end enroll term to set on previous active version
 159  
      * @throws Exception
 160  
      */
 161  
     private void updatePreviousVersions (MajorDisciplineInfo selectedVersion, String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm) throws Exception {
 162  
             // Get the current version of major discipline given the selected version
 163  0
             MajorDisciplineInfo currentVersion = getCurrentVersion(selectedVersion);
 164  
             
 165  0
             boolean isSelectedVersionCurrent = selectedVersion.getId().equals(currentVersion.getId());
 166  
             
 167  
             //Set the end terms on the current version of major discipline and update it's state to superseded
 168  0
             setEndTerms(currentVersion, endEntryTerm, endEnrollTerm, endInstAdmitTerm);
 169  0
             updateMajorDisciplineInfoState(currentVersion, DtoConstants.STATE_SUPERSEDED);
 170  
 
 171  
                 // Loop through all previous active or approved programs and set the state to superseded.
 172  
                 // We should only need to evaluated versions with sequence number
 173  
                 // higher than previous active program
 174  
 
 175  0
                 List<VersionDisplayInfo> versions = programService.getVersions(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, 
 176  
                                 selectedVersion.getVersionInfo().getVersionIndId());
 177  0
                 Long startSeq = new Long(1);
 178  
 
 179  0
                 if (!isSelectedVersionCurrent) {
 180  0
                         startSeq = currentVersion.getVersionInfo().getSequenceNumber() + 1;
 181  
                 }
 182  
 
 183  0
                 for (VersionDisplayInfo versionInfo : versions) {
 184  0
                         if (versionInfo.getSequenceNumber() >= startSeq  && versionInfo.getSequenceNumber() != selectedVersion.getVersionInfo().getSequenceNumber()) {
 185  0
                                 MajorDisciplineInfo otherProgram = programService.getMajorDiscipline(versionInfo.getId());
 186  0
                                 if (otherProgram.getState().equals(DtoConstants.STATE_APPROVED) ||
 187  
                                         otherProgram.getState().equals(DtoConstants.STATE_ACTIVE)){
 188  0
                                 updateMajorDisciplineInfoState(otherProgram, DtoConstants.STATE_SUPERSEDED);
 189  
                                 }                
 190  0
                         }
 191  
                 }            
 192  
 
 193  0
     }
 194  
 
 195  
         /**
 196  
          * Get the current version of program given the selected version of program
 197  
          * 
 198  
          * @param verIndId
 199  
          */
 200  
         protected MajorDisciplineInfo getCurrentVersion(MajorDisciplineInfo majorDisciplineInfo)
 201  
                         throws Exception {
 202  
                 // Get version independent id of program
 203  0
                 String verIndId = majorDisciplineInfo.getVersionInfo().getVersionIndId();
 204  
 
 205  
                 // Get id of current version of program given the version independent id
 206  0
                 VersionDisplayInfo curVerDisplayInfo = programService.getCurrentVersion(
 207  
                                 ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, verIndId);
 208  0
                 String curVerId = curVerDisplayInfo.getId();
 209  
 
 210  
                 // Return the current version of the course
 211  0
                 MajorDisciplineInfo currentVersion = programService.getMajorDiscipline(curVerId);
 212  
 
 213  0
                 return currentVersion;
 214  
         }
 215  
 
 216  
     /**
 217  
      * This method updates the end terms for the major discipline passed into it.
 218  
      * <p>
 219  
      * You must still call updateState() to save the object using the web service.
 220  
      * 
 221  
      * @param majorDisciplineInfo
 222  
      * @param endEntryTerm
 223  
      * @param endEnrollTerm
 224  
      * @param endInstAdmitTerm 
 225  
      * @throws OperationFailedException 
 226  
      * @throws MissingParameterException 
 227  
      * @throws InvalidParameterException 
 228  
      * @throws DoesNotExistException 
 229  
      */
 230  
     private void setEndTerms(MajorDisciplineInfo majorDisciplineInfo, String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm) throws InvalidParameterException, MissingParameterException, OperationFailedException, DoesNotExistException {
 231  
         
 232  
             //Set the end terms on the major discipline
 233  0
             majorDisciplineInfo.setEndProgramEntryTerm(endEntryTerm);
 234  0
         majorDisciplineInfo.setEndTerm(endEnrollTerm);
 235  0
         majorDisciplineInfo.getAttributes().put("endInstAdmitTerm", endInstAdmitTerm);
 236  
         
 237  
         //Check if there are variations to process
 238  0
         if(!majorDisciplineInfo.getVariations().isEmpty()){
 239  
                 
 240  
                 //Find the major's end term atps and obtain their date information
 241  0
                            AtpInfo majorEndEntryTermAtp = atpService.getAtp(endEntryTerm);
 242  0
                            Date majorEndEntryTermEndDate = majorEndEntryTermAtp.getEndDate();
 243  0
                            AtpInfo majorEndEnrollTermAtp = atpService.getAtp(endEnrollTerm);
 244  0
                            Date majorEndEnrollTermEndDate = majorEndEnrollTermAtp.getEndDate();
 245  0
                        AtpInfo majorEndInstAdmitTermAtp = atpService.getAtp(endInstAdmitTerm);
 246  0
                        Date majorEndInstAdmitTermEndDate = majorEndInstAdmitTermAtp.getEndDate();
 247  
     
 248  
                        //Loop through the variations
 249  0
                 for(ProgramVariationInfo variation:majorDisciplineInfo.getVariations()){
 250  
                         //compare dates to get the older of the two end terms
 251  0
                             if(variation.getEndProgramEntryTerm() != null){
 252  0
                                     AtpInfo variationEndEntryTermAtp = atpService.getAtp(variation.getEndProgramEntryTerm());
 253  0
                                     Date variationEndEntryTermEndDate = variationEndEntryTermAtp.getEndDate();
 254  0
                                     if(majorEndEnrollTermEndDate.compareTo(variationEndEntryTermEndDate)<=0){
 255  0
                                             variation.setEndProgramEntryTerm(endEntryTerm);
 256  
                                     }
 257  0
                             }else{
 258  0
                                     variation.setEndProgramEntryTerm(endEntryTerm);
 259  
                             }
 260  
                             //compare dates to get the older of the two end terms
 261  0
                             if(variation.getEndTerm() != null){
 262  0
                                     AtpInfo variationEndTermAtp = atpService.getAtp(variation.getEndTerm());
 263  0
                                     Date variationEndTermEndDate = variationEndTermAtp.getEndDate();
 264  0
                                     if(majorEndEntryTermEndDate.compareTo(variationEndTermEndDate)<=0){
 265  0
                                             variation.setEndTerm(endEnrollTerm);
 266  
                                     }
 267  0
                             }else{
 268  0
                                     variation.setEndTerm(endEnrollTerm);
 269  
                             }
 270  
                             //compare dates to get the older of the two end terms
 271  0
                             if(variation.getAttributes().get("endInstAdmitTerm") != null){
 272  0
                                     AtpInfo variationEndInstAdmitAtp = atpService.getAtp(variation.getAttributes().get("endInstAdmitTerm"));
 273  0
                                     Date variationEndInstAdmitEndDate = variationEndInstAdmitAtp.getEndDate();
 274  0
                                     if(majorEndInstAdmitTermEndDate.compareTo(variationEndInstAdmitEndDate)<=0){
 275  0
                                             variation.getAttributes().put("endInstAdmitTerm", endInstAdmitTerm);
 276  
                                     }
 277  0
                             }else{
 278  0
                                     variation.getAttributes().put("endInstAdmitTerm", endInstAdmitTerm);
 279  
                             }
 280  
                             
 281  
                 }
 282  
         }
 283  0
     }
 284  
 
 285  
         /**
 286  
      * This method will update the requirement state.
 287  
      * <p>
 288  
      * Note that it uses StatementUtil to update the statement tree.
 289  
      * 
 290  
      * @param majorDisciplineInfo
 291  
      * @param newState
 292  
      * @throws Exception
 293  
      */
 294  
     public void updateRequirementsState(List<String> programRequirementIds, String newState) throws Exception {
 295  
     
 296  0
         for (String programRequirementId : programRequirementIds) {
 297  
 
 298  
             // Get program requirement from the program service
 299  0
             ProgramRequirementInfo programRequirementInfo = programService.getProgramRequirement(programRequirementId, null, null);
 300  
 
 301  
             // Look in the requirement for the statement tree
 302  0
             StatementTreeViewInfo statementTree = programRequirementInfo.getStatement();
 303  
 
 304  
             // And recursively update the entire tree with the new state
 305  0
             StatementUtil.updateStatementTreeViewInfoState(newState, statementTree);
 306  
 
 307  
             // Update the state of the requirement object
 308  0
             programRequirementInfo.setState(newState);
 309  
 
 310  
             // The write the requirement back to the program service
 311  0
             programService.updateProgramRequirement(programRequirementInfo);
 312  
 
 313  0
         }
 314  0
     }
 315  
 
 316  
     /**
 317  
      * This method will update the requirements of each variation.
 318  
      * <p>
 319  
      * We need to do this here as opposed to in the assemblers, since we need
 320  
      * to make calls out to a separate web service.  Also, this needs to be done here
 321  
      * because changing state no longer calls the save function.
 322  
      * <p>
 323  
      * Note that core and credential programs do not have variations so
 324  
      * this method isn't necessary.
 325  
      * <p>
 326  
      * 
 327  
      * @param majorDisciplineInfo
 328  
      * @param newState
 329  
      * @throws Exception
 330  
      */
 331  
     public void updateVariationsRequirementsState(List<ProgramVariationInfo> variationList, String newState) throws Exception {
 332  
 
 333  
         // Iterate over all variations
 334  0
         for (ProgramVariationInfo variation : variationList) {
 335  
      
 336  
             // Get the requirements 
 337  0
             List<String> programRequirementIds = variation.getProgramRequirements();
 338  
             
 339  
             // Call the method that will update the requirements state for the program
 340  
             // This will also update the statement tree
 341  0
             updateRequirementsState(programRequirementIds, newState);
 342  0
          }
 343  0
     }
 344  
     
 345  
 
 346  
     /**
 347  
      * This method is used by Spring to inject the program service into this bean.
 348  
      * 
 349  
      * @param programService
 350  
      */
 351  
     public void setProgramService(ProgramService programService) {
 352  0
         this.programService = programService;
 353  0
     }
 354  
 
 355  
     public void setAtpService(AtpService atpService) {
 356  0
                 this.atpService = atpService;
 357  0
         }
 358  
 }