Coverage Report - org.kuali.student.lum.program.server.ProgramStateChangeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ProgramStateChangeServiceImpl
0%
0/48
0%
0/14
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.client.ProgramConstants;
 12  
 import org.kuali.student.lum.program.dto.MajorDisciplineInfo;
 13  
 import org.kuali.student.lum.program.dto.ProgramRequirementInfo;
 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 DIFFERENT
 24  
  * SERVICE METHODS FOR EACH OF THESE TYPES (THOUGH THEY ARE SIMILAR)
 25  
  * <P>
 26  
  * @author Kuali Rice Team (kuali-rice@googlegroups.com)
 27  
  */
 28  
 @Transactional(noRollbackFor = {DoesNotExistException.class}, rollbackFor = {Throwable.class})
 29  0
 public class ProgramStateChangeServiceImpl {
 30  
 
 31  
     /**
 32  
      * The program service - injected by spring.
 33  
      */
 34  
     private ProgramService programService;
 35  
 
 36  
  
 37  
 
 38  
     /**
 39  
      * This method is called by workflow when the state changes.
 40  
      * 
 41  
      * @param majorDisciplineId
 42  
      * @param state
 43  
      * @return
 44  
      * @throws Exception
 45  
      */
 46  
     public void changeState( String majorDisciplineId, String newState) throws Exception {
 47  
         // This method will be called from workflow.
 48  
         // Since we cannot activate a program from the workflow we do not need to add endEntryTerm and endEnrollTerm
 49  0
         changeState(null, null, null, majorDisciplineId, newState);
 50  0
     }
 51  
 
 52  
     /**
 53  
      * This method is called from the UI (servlet) when state changes.
 54  
      * 
 55  
      * @param endEntryTerm
 56  
      * @param endEnrollTerm
 57  
      * @param programType
 58  
      * @param majorDisciplineId
 59  
      * @param newState
 60  
      * @return
 61  
      * @throws Exception
 62  
      */
 63  
     public void changeState(String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm,  String majorDisciplineId, String newState) throws Exception {
 64  
 
 65  
         // New state must not be null
 66  0
         if (newState == null)
 67  0
             throw new InvalidParameterException("new state cannot be null");
 68  
 
 69  
         // The version selected in the UI
 70  0
         MajorDisciplineInfo selectedVersion = programService.getMajorDiscipline(majorDisciplineId);
 71  
 
 72  
         // If we are activating this version we need to mark the previous version superseded,
 73  
         // update the previous version end terms, and make the selected version current.
 74  0
         if (newState.equals(DtoConstants.STATE_ACTIVE)) {
 75  
 
 76  
             // Find the previous version
 77  0
             MajorDisciplineInfo previousVersion = findPreviousVersion(selectedVersion);
 78  
 
 79  0
             if (previousVersion != null) {
 80  
 
 81  
                 // Set end terms on previous version
 82  0
                 setEndTerms(previousVersion, endEntryTerm, endEnrollTerm, endInstAdmitTerm);
 83  
 
 84  
                 // Mark previous version as superseded and update state on all associated objects
 85  0
                 updateMajorDisciplineInfoState(previousVersion, DtoConstants.STATE_SUPERSEDED);
 86  
             }
 87  
 
 88  
             // Update state of all associated objects for current version
 89  
             // NOTE: we must update state BEFORE making the version current
 90  0
             updateMajorDisciplineInfoState(selectedVersion, newState);
 91  
 
 92  
             // Make this the current version
 93  0
             makeCurrent(selectedVersion);
 94  0
         } else {
 95  
 
 96  
             // Update state of all associated objects for current version
 97  0
             updateMajorDisciplineInfoState(selectedVersion, newState);
 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  
      * @param endInstAdmitTerm 
 112  
      */
 113  
     private void setEndTerms(MajorDisciplineInfo majorDisciplineInfo, String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm) {
 114  0
         majorDisciplineInfo.setEndProgramEntryTerm(endEntryTerm);
 115  0
         majorDisciplineInfo.setEndTerm(endEnrollTerm);
 116  0
         majorDisciplineInfo.getAttributes().put(ProgramConstants.END_INSTITUTIONAL_ADMIT_TERM, endInstAdmitTerm);
 117  0
     }
 118  
 
 119  
     /**
 120  
      * This method will update the state of this object and all associated objects.
 121  
      * <p>
 122  
      * It is needed because we need to make separate web service calls to update the state of these objects.
 123  
      * 
 124  
      * @param majorDisciplineInfo
 125  
      * @param newState
 126  
      */
 127  
     private void updateMajorDisciplineInfoState(MajorDisciplineInfo majorDisciplineInfo, String newState) throws Exception {
 128  
         // Update the statement tree
 129  0
         updateRequirementsState(majorDisciplineInfo, newState);
 130  
          
 131  
         // Update major discipline
 132  0
         majorDisciplineInfo.setState(newState);
 133  0
         programService.updateMajorDiscipline(majorDisciplineInfo);
 134  0
     }
 135  
 
 136  
     /**
 137  
      * This method will make this version of the major discipline the current one.
 138  
      * 
 139  
      * @param majorDisciplineInfo
 140  
      */
 141  
     private void makeCurrent(MajorDisciplineInfo majorDisciplineInfo) throws Exception {
 142  
 
 143  
         // Check if this is the current version before trying to make it current
 144  
         // (the web service will error if you try to make a version current that is already current)
 145  0
         VersionDisplayInfo currentVersion = programService.getCurrentVersion(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, majorDisciplineInfo.getVersionInfo().getVersionIndId());
 146  
 
 147  
         // If this is not the current version, then make it current
 148  0
         if (!currentVersion.getSequenceNumber().equals(majorDisciplineInfo.getVersionInfo().getSequenceNumber())) {
 149  0
             programService.setCurrentMajorDisciplineVersion(majorDisciplineInfo.getId(), null);
 150  
         }
 151  0
     }
 152  
 
 153  
     /**
 154  
      * This method finds the previous version (the version right before this one).
 155  
      * <p>
 156  
      * e.g. v1 = ACTIVE v2 = APPROVED
 157  
      * <p>
 158  
      * If you passed v2 into this method, it would return v1.
 159  
      * <p>
 160  
      * If there is only one major discipline this method will return null.
 161  
      * 
 162  
      * @param majorDisciplineInfo
 163  
      * @return
 164  
      */
 165  
     private MajorDisciplineInfo findPreviousVersion(MajorDisciplineInfo majorDisciplineInfo) throws Exception {
 166  
         // Find all previous versions using the version independent indicator
 167  0
         List<VersionDisplayInfo> versions = programService.getVersions(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, majorDisciplineInfo.getVersionInfo().getVersionIndId());
 168  
 
 169  
         // Take the sequence number for this version
 170  0
         Long sequenceNumber = majorDisciplineInfo.getVersionInfo().getSequenceNumber();
 171  
 
 172  
         // And subtract 1 from the sequence number to get the previous version
 173  0
         sequenceNumber -= 1;
 174  
 
 175  
         // Loop over all versions and find the previous version based on the sequence number
 176  
         /*
 177  
          * NOTE: Dan suggested we loop over all versions and change any version with state=active to
 178  
          *       state=superseded.  However, we decided not to go that route because we would need
 179  
          *       to pull back all data for each version to determine if a version is active, since
 180  
          *       versioninfo does not have a getState() method
 181  
          */
 182  0
         MajorDisciplineInfo previousVersion = null;
 183  0
         for (VersionDisplayInfo versionInfo : versions) {
 184  0
             if (versionInfo.getSequenceNumber().equals(sequenceNumber)) {
 185  0
                 previousVersion = programService.getMajorDiscipline(versionInfo.getId());
 186  0
                 break;
 187  
             }
 188  
         }
 189  0
         return previousVersion;
 190  
     }
 191  
 
 192  
     /**
 193  
      * This method will update the requirement state.
 194  
      * <p>
 195  
      * Note that it uses StatementUtil to update the statement tree.
 196  
      * 
 197  
      * @param majorDisciplineInfo
 198  
      * @param newState
 199  
      * @throws Exception
 200  
      */
 201  
     public void updateRequirementsState(MajorDisciplineInfo majorDisciplineInfo, String newState) throws Exception {
 202  
 
 203  
         // Loop over list of requirement ids returned in the major discipline object
 204  0
         List<String> programRequirementIds = majorDisciplineInfo.getProgramRequirements();
 205  0
         for (String programRequirementId : programRequirementIds) {
 206  
 
 207  
             // Get program requirement from the program service
 208  0
             ProgramRequirementInfo programRequirementInfo = programService.getProgramRequirement(programRequirementId, null, null);
 209  
 
 210  
             // Look in the requirement for the statement tree
 211  0
             StatementTreeViewInfo statementTree = programRequirementInfo.getStatement();
 212  
 
 213  
             // And recursively update the entire tree with the new state
 214  0
             StatementUtil.updateStatementTreeViewInfoState(newState, statementTree);
 215  
 
 216  
             // Update the state of the requirement object
 217  0
             programRequirementInfo.setState(newState);
 218  
 
 219  
             // The write the requirement back to the program service
 220  0
             programService.updateProgramRequirement(programRequirementInfo);
 221  
 
 222  0
         }
 223  0
     }
 224  
  
 225  
     /**
 226  
      * This method is used by Spring to inject the program service into this bean.
 227  
      * 
 228  
      * @param programService
 229  
      */
 230  
     public void setProgramService(ProgramService programService) {
 231  0
         this.programService = programService;
 232  0
     }
 233  
 
 234  
 }