Coverage Report - org.kuali.student.lum.program.server.MajorDisciplineStateChangeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
MajorDisciplineStateChangeServiceImpl
0%
0/86
0%
0/32
2.7
 
 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  
         // The version selected in the UI
 75  0
         MajorDisciplineInfo selectedVersion = programService.getMajorDiscipline(majorDisciplineId);
 76  
 
 77  
         // If we are activating this version we need to mark the previous version superseded,
 78  
         // update the previous version end terms, and make the selected version current.
 79  0
         if (newState.equals(DtoConstants.STATE_ACTIVE)) {
 80  
 
 81  
             // Find the previous version
 82  0
             MajorDisciplineInfo previousVersion = findPreviousVersion(selectedVersion);
 83  
 
 84  0
             if (previousVersion != null) {
 85  
 
 86  
                 // Set end terms on previous version
 87  0
                 setEndTerms(previousVersion, endEntryTerm, endEnrollTerm, endInstAdmitTerm);
 88  
 
 89  
                 // Mark previous version as superseded and update state on all associated objects
 90  0
                 updateMajorDisciplineInfoState(previousVersion, DtoConstants.STATE_SUPERSEDED);
 91  
             }
 92  
 
 93  
             // Update state of all associated objects for current version
 94  
             // NOTE: we must update state BEFORE making the version current
 95  0
             updateMajorDisciplineInfoState(selectedVersion, newState);
 96  
 
 97  
             // Make this the current version
 98  0
             makeCurrent(selectedVersion);
 99  0
         } else {
 100  
 
 101  
             // Update state of all associated objects for current version
 102  0
             updateMajorDisciplineInfoState(selectedVersion, newState);
 103  
         }
 104  
 
 105  
       
 106  
 
 107  0
     }
 108  
 
 109  
     /**
 110  
      * This method updates the end terms for the major discipline passed into it.
 111  
      * <p>
 112  
      * You must still call updateState() to save the object using the web service.
 113  
      * 
 114  
      * @param majorDisciplineInfo
 115  
      * @param endEntryTerm
 116  
      * @param endEnrollTerm
 117  
      * @param endInstAdmitTerm 
 118  
      * @throws OperationFailedException 
 119  
      * @throws MissingParameterException 
 120  
      * @throws InvalidParameterException 
 121  
      * @throws DoesNotExistException 
 122  
      */
 123  
     private void setEndTerms(MajorDisciplineInfo majorDisciplineInfo, String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm) throws InvalidParameterException, MissingParameterException, OperationFailedException, DoesNotExistException {
 124  
         
 125  
             //Set the end terms on the major discipline
 126  0
             majorDisciplineInfo.setEndProgramEntryTerm(endEntryTerm);
 127  0
         majorDisciplineInfo.setEndTerm(endEnrollTerm);
 128  0
         majorDisciplineInfo.getAttributes().put("endInstAdmitTerm", endInstAdmitTerm);
 129  
         
 130  
         //Check if there are variations to process
 131  0
         if(!majorDisciplineInfo.getVariations().isEmpty()){
 132  
                 
 133  
                 //Find the major's end term atps and obtain their date information
 134  0
                            AtpInfo majorEndEntryTermAtp = atpService.getAtp(endEntryTerm);
 135  0
                            Date majorEndEntryTermEndDate = majorEndEntryTermAtp.getEndDate();
 136  0
                            AtpInfo majorEndEnrollTermAtp = atpService.getAtp(endEnrollTerm);
 137  0
                            Date majorEndEnrollTermEndDate = majorEndEnrollTermAtp.getEndDate();
 138  0
                        AtpInfo majorEndInstAdmitTermAtp = atpService.getAtp(endInstAdmitTerm);
 139  0
                        Date majorEndInstAdmitTermEndDate = majorEndInstAdmitTermAtp.getEndDate();
 140  
     
 141  
                        //Loop through the variations
 142  0
                 for(ProgramVariationInfo variation:majorDisciplineInfo.getVariations()){
 143  
                         //compare dates to get the older of the two end terms
 144  0
                             if(variation.getEndProgramEntryTerm() != null){
 145  0
                                     AtpInfo variationEndEntryTermAtp = atpService.getAtp(variation.getEndProgramEntryTerm());
 146  0
                                     Date variationEndEntryTermEndDate = variationEndEntryTermAtp.getEndDate();
 147  0
                                     if(majorEndEnrollTermEndDate.compareTo(variationEndEntryTermEndDate)<=0){
 148  0
                                             variation.setEndProgramEntryTerm(endEntryTerm);
 149  
                                     }
 150  0
                             }else{
 151  0
                                     variation.setEndProgramEntryTerm(endEntryTerm);
 152  
                             }
 153  
                             //compare dates to get the older of the two end terms
 154  0
                             if(variation.getEndTerm() != null){
 155  0
                                     AtpInfo variationEndTermAtp = atpService.getAtp(variation.getEndTerm());
 156  0
                                     Date variationEndTermEndDate = variationEndTermAtp.getEndDate();
 157  0
                                     if(majorEndEntryTermEndDate.compareTo(variationEndTermEndDate)<=0){
 158  0
                                             variation.setEndTerm(endEnrollTerm);
 159  
                                     }
 160  0
                             }else{
 161  0
                                     variation.setEndTerm(endEnrollTerm);
 162  
                             }
 163  
                             //compare dates to get the older of the two end terms
 164  0
                             if(variation.getAttributes().get("endInstAdmitTerm") != null){
 165  0
                                     AtpInfo variationEndInstAdmitAtp = atpService.getAtp(variation.getAttributes().get("endInstAdmitTerm"));
 166  0
                                     Date variationEndInstAdmitEndDate = variationEndInstAdmitAtp.getEndDate();
 167  0
                                     if(majorEndInstAdmitTermEndDate.compareTo(variationEndInstAdmitEndDate)<=0){
 168  0
                                             variation.getAttributes().put("endInstAdmitTerm", endInstAdmitTerm);
 169  
                                     }
 170  0
                             }else{
 171  0
                                     variation.getAttributes().put("endInstAdmitTerm", endInstAdmitTerm);
 172  
                             }
 173  
                             
 174  
                 }
 175  
         }
 176  0
     }
 177  
 
 178  
     /**
 179  
      * This method will update the state of this object and all associated objects.
 180  
      * <p>
 181  
      * It is needed because we need to make separate web service calls to update the state of these objects.
 182  
      * 
 183  
      * @param majorDisciplineInfo
 184  
      * @param newState
 185  
      */
 186  
     private void updateMajorDisciplineInfoState(MajorDisciplineInfo majorDisciplineInfo, String newState) throws Exception {
 187  
         // Update the statement tree
 188  0
         List<String> programRequirementIds = majorDisciplineInfo.getProgramRequirements();
 189  0
         updateRequirementsState(programRequirementIds, newState);
 190  
 
 191  
         
 192  
         // Update any variations 
 193  0
         List<ProgramVariationInfo> variationList = majorDisciplineInfo.getVariations();
 194  0
         updateVariationsRequirementsState(variationList, newState);
 195  
         
 196  
         
 197  
         // Update major discipline
 198  0
         majorDisciplineInfo.setState(newState);
 199  0
         programService.updateMajorDiscipline(majorDisciplineInfo);
 200  0
     }
 201  
 
 202  
     /**
 203  
      * This method will make this version of the major discipline the current one.
 204  
      * 
 205  
      * @param majorDisciplineInfo
 206  
      */
 207  
     private void makeCurrent(MajorDisciplineInfo majorDisciplineInfo) throws Exception {
 208  
 
 209  
         // Check if this is the current version before trying to make it current
 210  
         // (the web service will error if you try to make a version current that is already current)
 211  0
         VersionDisplayInfo currentVersion = programService.getCurrentVersion(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, majorDisciplineInfo.getVersionInfo().getVersionIndId());
 212  
 
 213  
         // If this is not the current version, then make it current
 214  0
         if (!currentVersion.getSequenceNumber().equals(majorDisciplineInfo.getVersionInfo().getSequenceNumber())) {
 215  0
             programService.setCurrentMajorDisciplineVersion(majorDisciplineInfo.getId(), null);
 216  
         }
 217  0
     }
 218  
 
 219  
     /**
 220  
      * This method finds the previous version (the version right before this one).
 221  
      * <p>
 222  
      * e.g. v1 = ACTIVE v2 = APPROVED
 223  
      * <p>
 224  
      * If you passed v2 into this method, it would return v1.
 225  
      * <p>
 226  
      * If there is only one major discipline this method will return null.
 227  
      * 
 228  
      * @param majorDisciplineInfo
 229  
      * @return
 230  
      */
 231  
     private MajorDisciplineInfo findPreviousVersion(MajorDisciplineInfo majorDisciplineInfo) throws Exception {
 232  
         // Find all previous versions using the version independent indicator
 233  0
         List<VersionDisplayInfo> versions = programService.getVersions(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, majorDisciplineInfo.getVersionInfo().getVersionIndId());
 234  
 
 235  
         // Take the sequence number for this version
 236  0
         Long sequenceNumber = majorDisciplineInfo.getVersionInfo().getSequenceNumber();
 237  
 
 238  
         // And subtract 1 from the sequence number to get the previous version
 239  0
         sequenceNumber -= 1;
 240  
 
 241  
         // Loop over all versions and find the previous version based on the sequence number
 242  
         /*
 243  
          * NOTE: Dan suggested we loop over all versions and change any version with state=active to state=superseded.
 244  
          * However, we decided not to go that route because we would need to pull back all data for each version to determine
 245  
          * if a version is active, since versioninfo does not have a getState() method
 246  
          */
 247  0
         MajorDisciplineInfo previousVersion = null;
 248  0
         for (VersionDisplayInfo versionInfo : versions) {
 249  0
             if (versionInfo.getSequenceNumber().equals(sequenceNumber)) {
 250  0
                 previousVersion = programService.getMajorDiscipline(versionInfo.getId());
 251  0
                 break;
 252  
             }
 253  
         }
 254  0
         return previousVersion;
 255  
     }
 256  
 
 257  
     /**
 258  
      * This method will update the requirement state.
 259  
      * <p>
 260  
      * Note that it uses StatementUtil to update the statement tree.
 261  
      * 
 262  
      * @param majorDisciplineInfo
 263  
      * @param newState
 264  
      * @throws Exception
 265  
      */
 266  
     public void updateRequirementsState(List<String> programRequirementIds, String newState) throws Exception {
 267  
     
 268  0
         for (String programRequirementId : programRequirementIds) {
 269  
 
 270  
             // Get program requirement from the program service
 271  0
             ProgramRequirementInfo programRequirementInfo = programService.getProgramRequirement(programRequirementId, null, null);
 272  
 
 273  
             // Look in the requirement for the statement tree
 274  0
             StatementTreeViewInfo statementTree = programRequirementInfo.getStatement();
 275  
 
 276  
             // And recursively update the entire tree with the new state
 277  0
             StatementUtil.updateStatementTreeViewInfoState(newState, statementTree);
 278  
 
 279  
             // Update the state of the requirement object
 280  0
             programRequirementInfo.setState(newState);
 281  
 
 282  
             // The write the requirement back to the program service
 283  0
             programService.updateProgramRequirement(programRequirementInfo);
 284  
 
 285  0
         }
 286  0
     }
 287  
 
 288  
     /**
 289  
      * This method will update the requirements of each variation.
 290  
      * <p>
 291  
      * We need to do this here as opposed to in the assemblers, since we need
 292  
      * to make calls out to a separate web service.  Also, this needs to be done here
 293  
      * because changing state no longer calls the save function.
 294  
      * <p>
 295  
      * Note that core and credential programs do not have variations so
 296  
      * this method isn't necessary.
 297  
      * <p>
 298  
      * 
 299  
      * @param majorDisciplineInfo
 300  
      * @param newState
 301  
      * @throws Exception
 302  
      */
 303  
     public void updateVariationsRequirementsState(List<ProgramVariationInfo> variationList, String newState) throws Exception {
 304  
 
 305  
         // Iterate over all variations
 306  0
         for (ProgramVariationInfo variation : variationList) {
 307  
      
 308  
             // Get the requirements 
 309  0
             List<String> programRequirementIds = variation.getProgramRequirements();
 310  
             
 311  
             // Call the method that will update the requirements state for the program
 312  
             // This will also update the statement tree
 313  0
             updateRequirementsState(programRequirementIds, newState);
 314  0
          }
 315  0
     }
 316  
 
 317  
     /**
 318  
      * This method is used by Spring to inject the program service into this bean.
 319  
      * 
 320  
      * @param programService
 321  
      */
 322  
     public void setProgramService(ProgramService programService) {
 323  0
         this.programService = programService;
 324  0
     }
 325  
     
 326  
     public void setAtpService(AtpService atpService) {
 327  0
                 this.atpService = atpService;
 328  0
         }
 329  
 }