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