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