Coverage Report - org.kuali.student.lum.workflow.MajorDisciplineStateChangeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
MajorDisciplineStateChangeServiceImpl
0%
0/54
0%
0/16
2
 
 1  
 package org.kuali.student.lum.workflow;
 2  
 
 3  
 import java.util.List;
 4  
 
 5  
 import org.kuali.student.common.dto.DtoConstants;
 6  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 7  
 import org.kuali.student.common.exceptions.InvalidParameterException;
 8  
 import org.kuali.student.common.versionmanagement.dto.VersionDisplayInfo;
 9  
 import org.kuali.student.core.statement.dto.StatementTreeViewInfo;
 10  
 import org.kuali.student.lum.program.dto.MajorDisciplineInfo;
 11  
 import org.kuali.student.lum.program.dto.ProgramRequirementInfo;
 12  
 import org.kuali.student.lum.program.dto.ProgramVariationInfo;
 13  
 import org.kuali.student.lum.program.service.ProgramService;
 14  
 import org.kuali.student.lum.program.service.ProgramServiceConstants;
 15  
 import org.springframework.transaction.annotation.Transactional;
 16  
 
 17  
 /**
 18  
  * This class is called whenever the state of a major discipline changes.
 19  
  * <p>
 20  
  * We have a separate class because the operations need to be marked with the @Transactional annotation.
 21  
  * <p>
 22  
  * THIS CLASS IS DUPLICATED FOR MAJOR DISCIPLINE, CORE, AND CREDENTIAL PROGRAMS SINCE THERE ARE
 23  
  * DIFFERENT SERVICE METHODS FOR EACH OF THESE TYPES (THOUGH THEY ARE SIMILAR)
 24  
  * <P>
 25  
  * 
 26  
  * @author Kuali Rice Team (kuali-rice@googlegroups.com)
 27  
  */
 28  
 @Transactional(noRollbackFor = {DoesNotExistException.class}, rollbackFor = {Throwable.class})
 29  0
 public class MajorDisciplineStateChangeServiceImpl implements StateChangeService{
 30  
 
 31  
     /**
 32  
      * The program service - injected by spring.
 33  
      */
 34  
     private ProgramService programService;
 35  
 
 36  
     /**
 37  
      * This method is called by workflow when the state changes.
 38  
      * 
 39  
      * @param majorDisciplineId
 40  
      * @param state
 41  
      * @return
 42  
      * @throws Exception
 43  
      */
 44  
     public void changeState(String majorDisciplineId, String newState) throws Exception {
 45  
         // This method will be called from workflow.
 46  
         // Since we cannot activate a program from the workflow we do not need to add endEntryTerm and endEnrollTerm
 47  0
         changeState(null, null, majorDisciplineId, newState);
 48  0
     }
 49  
 
 50  
     /**
 51  
      * This method is called from the UI (servlet) when state changes.
 52  
      * 
 53  
      * @param endEntryTerm
 54  
      * @param endEnrollTerm
 55  
      * @param programType
 56  
      * @param majorDisciplineId
 57  
      * @param newState
 58  
      * @return
 59  
      * @throws Exception
 60  
      */
 61  
     public void changeState(String endEntryTerm, String endEnrollTerm, String majorDisciplineId, String newState) throws Exception {
 62  
 
 63  
         // New state must not be null
 64  0
         if (newState == null)
 65  0
             throw new InvalidParameterException("new state cannot be null");
 66  
 
 67  
         // The version selected in the UI
 68  0
         MajorDisciplineInfo selectedVersion = programService.getMajorDiscipline(majorDisciplineId);
 69  
 
 70  
         // If we are activating this version we need to mark the previous version superseded,
 71  
         // update the previous version end terms, and make the selected version current.
 72  0
         if (newState.equals(DtoConstants.STATE_ACTIVE)) {
 73  
 
 74  
             // Find the previous version
 75  0
             MajorDisciplineInfo previousVersion = findPreviousVersion(selectedVersion);
 76  
 
 77  0
             if (previousVersion != null) {
 78  
 
 79  
                 // Set end terms on previous version
 80  0
                 setEndTerms(previousVersion, endEntryTerm, endEnrollTerm);
 81  
 
 82  
                 // Mark previous version as superseded and update state on all associated objects
 83  0
                 updateMajorDisciplineInfoState(previousVersion, DtoConstants.STATE_SUPERSEDED);
 84  
             }
 85  
 
 86  
             // Update state of all associated objects for current version
 87  
             // NOTE: we must update state BEFORE making the version current
 88  0
             updateMajorDisciplineInfoState(selectedVersion, newState);
 89  
 
 90  
             // Make this the current version
 91  0
             makeCurrent(selectedVersion);
 92  0
         } else {
 93  
 
 94  
             // Update state of all associated objects for current version
 95  0
             updateMajorDisciplineInfoState(selectedVersion, newState);
 96  
         }
 97  
 
 98  
       
 99  
 
 100  0
     }
 101  
 
 102  
     /**
 103  
      * This method updates the end terms for the major discipline passed into it.
 104  
      * <p>
 105  
      * You must still call updateState() to save the object using the web service.
 106  
      * 
 107  
      * @param majorDisciplineInfo
 108  
      * @param endEntryTerm
 109  
      * @param endEnrollTerm
 110  
      */
 111  
     private void setEndTerms(MajorDisciplineInfo majorDisciplineInfo, String endEntryTerm, String endEnrollTerm) {
 112  0
         majorDisciplineInfo.setEndProgramEntryTerm(endEntryTerm);
 113  0
         majorDisciplineInfo.setEndTerm(endEnrollTerm);
 114  0
     }
 115  
 
 116  
     /**
 117  
      * This method will update the state of this object and all associated objects.
 118  
      * <p>
 119  
      * It is needed because we need to make separate web service calls to update the state of these objects.
 120  
      * 
 121  
      * @param majorDisciplineInfo
 122  
      * @param newState
 123  
      */
 124  
     private void updateMajorDisciplineInfoState(MajorDisciplineInfo majorDisciplineInfo, String newState) throws Exception {
 125  
         // Update the statement tree
 126  0
         List<String> programRequirementIds = majorDisciplineInfo.getProgramRequirements();
 127  0
         updateRequirementsState(programRequirementIds, newState);
 128  
 
 129  
         
 130  
         // Update any variations 
 131  0
         List<ProgramVariationInfo> variationList = majorDisciplineInfo.getVariations();
 132  0
         updateVariationsRequirementsState(variationList, newState);
 133  
         
 134  
         
 135  
         // Update major discipline
 136  0
         majorDisciplineInfo.setState(newState);
 137  0
         programService.updateMajorDiscipline(majorDisciplineInfo);
 138  0
     }
 139  
 
 140  
     /**
 141  
      * This method will make this version of the major discipline the current one.
 142  
      * 
 143  
      * @param majorDisciplineInfo
 144  
      */
 145  
     private void makeCurrent(MajorDisciplineInfo majorDisciplineInfo) throws Exception {
 146  
 
 147  
         // Check if this is the current version before trying to make it current
 148  
         // (the web service will error if you try to make a version current that is already current)
 149  0
         VersionDisplayInfo currentVersion = programService.getCurrentVersion(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, majorDisciplineInfo.getVersionInfo().getVersionIndId());
 150  
 
 151  
         // If this is not the current version, then make it current
 152  0
         if (!currentVersion.getSequenceNumber().equals(majorDisciplineInfo.getVersionInfo().getSequenceNumber())) {
 153  0
             programService.setCurrentMajorDisciplineVersion(majorDisciplineInfo.getId(), null);
 154  
         }
 155  0
     }
 156  
 
 157  
     /**
 158  
      * This method finds the previous version (the version right before this one).
 159  
      * <p>
 160  
      * e.g. v1 = ACTIVE v2 = APPROVED
 161  
      * <p>
 162  
      * If you passed v2 into this method, it would return v1.
 163  
      * <p>
 164  
      * If there is only one major discipline this method will return null.
 165  
      * 
 166  
      * @param majorDisciplineInfo
 167  
      * @return
 168  
      */
 169  
     private MajorDisciplineInfo findPreviousVersion(MajorDisciplineInfo majorDisciplineInfo) throws Exception {
 170  
         // Find all previous versions using the version independent indicator
 171  0
         List<VersionDisplayInfo> versions = programService.getVersions(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, majorDisciplineInfo.getVersionInfo().getVersionIndId());
 172  
 
 173  
         // Take the sequence number for this version
 174  0
         Long sequenceNumber = majorDisciplineInfo.getVersionInfo().getSequenceNumber();
 175  
 
 176  
         // And subtract 1 from the sequence number to get the previous version
 177  0
         sequenceNumber -= 1;
 178  
 
 179  
         // Loop over all versions and find the previous version based on the sequence number
 180  
         /*
 181  
          * NOTE: Dan suggested we loop over all versions and change any version with state=active to state=superseded.
 182  
          * However, we decided not to go that route because we would need to pull back all data for each version to determine
 183  
          * if a version is active, since versioninfo does not have a getState() method
 184  
          */
 185  0
         MajorDisciplineInfo previousVersion = null;
 186  0
         for (VersionDisplayInfo versionInfo : versions) {
 187  0
             if (versionInfo.getSequenceNumber().equals(sequenceNumber)) {
 188  0
                 previousVersion = programService.getMajorDiscipline(versionInfo.getId());
 189  0
                 break;
 190  
             }
 191  
         }
 192  0
         return previousVersion;
 193  
     }
 194  
 
 195  
     /**
 196  
      * This method will update the requirement state.
 197  
      * <p>
 198  
      * Note that it uses StatementUtil to update the statement tree.
 199  
      * 
 200  
      * @param majorDisciplineInfo
 201  
      * @param newState
 202  
      * @throws Exception
 203  
      */
 204  
     public void updateRequirementsState(List<String> programRequirementIds, String newState) throws Exception {
 205  
     
 206  0
         for (String programRequirementId : programRequirementIds) {
 207  
 
 208  
             // Get program requirement from the program service
 209  0
             ProgramRequirementInfo programRequirementInfo = programService.getProgramRequirement(programRequirementId, null, null);
 210  
 
 211  
             // Look in the requirement for the statement tree
 212  0
             StatementTreeViewInfo statementTree = programRequirementInfo.getStatement();
 213  
 
 214  
             // And recursively update the entire tree with the new state
 215  0
             StatementUtil.updateStatementTreeViewInfoState(newState, statementTree);
 216  
 
 217  
             // Update the state of the requirement object
 218  0
             programRequirementInfo.setState(newState);
 219  
 
 220  
             // The write the requirement back to the program service
 221  0
             programService.updateProgramRequirement(programRequirementInfo);
 222  
 
 223  0
         }
 224  0
     }
 225  
 
 226  
     /**
 227  
      * This method will update the requirements of each variation.
 228  
      * <p>
 229  
      * We need to do this here as opposed to in the assemblers, since we need
 230  
      * to make calls out to a separate web service.  Also, this needs to be done here
 231  
      * because changing state no longer calls the save function.
 232  
      * <p>
 233  
      * Note that core and credential programs do not have variations so
 234  
      * this method isn't necessary.
 235  
      * <p>
 236  
      * 
 237  
      * @param majorDisciplineInfo
 238  
      * @param newState
 239  
      * @throws Exception
 240  
      */
 241  
     public void updateVariationsRequirementsState(List<ProgramVariationInfo> variationList, String newState) throws Exception {
 242  
 
 243  
         // Iterate over all variations
 244  0
         for (ProgramVariationInfo variation : variationList) {
 245  
      
 246  
             // Get the requirements 
 247  0
             List<String> programRequirementIds = variation.getProgramRequirements();
 248  
             
 249  
             // Call the method that will update the requirements state for the program
 250  
             // This will also update the statement tree
 251  0
             updateRequirementsState(programRequirementIds, newState);
 252  0
          }
 253  0
     }
 254  
     
 255  
 
 256  
     /**
 257  
      * This method is used by Spring to inject the program service into this bean.
 258  
      * 
 259  
      * @param programService
 260  
      */
 261  
     public void setProgramService(ProgramService programService) {
 262  0
         this.programService = programService;
 263  0
     }
 264  
 
 265  
 }