Coverage Report - org.kuali.student.lum.program.server.CredentialProgramStateChangeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
CredentialProgramStateChangeServiceImpl
0%
0/47
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.dto.CredentialProgramInfo;
 12  
 import org.kuali.student.lum.program.dto.ProgramRequirementInfo;
 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 DIFFERENT SERVICE METHODS FOR
 23  
  * 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 CredentialProgramStateChangeServiceImpl 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 credentialProgramId, 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, credentialProgramId, 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 credentialProgramId, 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
         CredentialProgramInfo selectedVersion = programService.getCredentialProgram(credentialProgramId);
 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
             CredentialProgramInfo 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
                 updateCredentialProgramInfoState(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
             updateCredentialProgramInfoState(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
             updateCredentialProgramInfoState(selectedVersion, newState);
 96  
         }
 97  
 
 98  0
     }
 99  
 
 100  
     /**
 101  
      * This method updates the end terms for the major discipline passed into it.
 102  
      * <p>
 103  
      * You must still call updateState() to save the object using the web service.
 104  
      * 
 105  
      * @param credentialProgramInfo
 106  
      * @param endEntryTerm
 107  
      * @param endEnrollTerm
 108  
      */
 109  
     private void setEndTerms(CredentialProgramInfo credentialProgramInfo, String endEntryTerm, String endEnrollTerm) {
 110  0
         credentialProgramInfo.setEndProgramEntryTerm(endEntryTerm);
 111  0
         credentialProgramInfo.setEndTerm(endEnrollTerm);
 112  0
     }
 113  
 
 114  
     /**
 115  
      * This method will update the state of this object and all associated objects.
 116  
      * <p>
 117  
      * It is needed because we need to make separate web service calls to update the state of these objects.
 118  
      * 
 119  
      * @param credentialProgramInfo
 120  
      * @param newState
 121  
      */
 122  
     private void updateCredentialProgramInfoState(CredentialProgramInfo credentialProgramInfo, String newState) throws Exception {
 123  
         // Update the statement tree
 124  0
         List<String> programRequirementIds = credentialProgramInfo.getProgramRequirements();
 125  0
         updateRequirementsState(programRequirementIds, newState);
 126  
  
 127  
         // Credential and core programs do not have variations
 128  
         
 129  
         // Update program
 130  0
         credentialProgramInfo.setState(newState);
 131  0
         programService.updateCredentialProgram(credentialProgramInfo);
 132  0
     }
 133  
 
 134  
     /**
 135  
      * This method will make this version of the major discipline the current one.
 136  
      * 
 137  
      * @param credentialProgramInfo
 138  
      */
 139  
     private void makeCurrent(CredentialProgramInfo credentialProgramInfo) throws Exception {
 140  
 
 141  
         // Check if this is the current version before trying to make it current
 142  
         // (the web service will error if you try to make a version current that is already current)
 143  0
         VersionDisplayInfo currentVersion = programService.getCurrentVersion(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, credentialProgramInfo.getVersionInfo().getVersionIndId());
 144  
 
 145  
         // If this is not the current version, then make it current
 146  0
         if (!currentVersion.getSequenceNumber().equals(credentialProgramInfo.getVersionInfo().getSequenceNumber())) {
 147  0
             programService.setCurrentCredentialProgramVersion(credentialProgramInfo.getId(), null);
 148  
         }
 149  0
     }
 150  
 
 151  
     /**
 152  
      * This method finds the previous version (the version right before this one).
 153  
      * <p>
 154  
      * e.g. v1 = ACTIVE v2 = APPROVED
 155  
      * <p>
 156  
      * If you passed v2 into this method, it would return v1.
 157  
      * <p>
 158  
      * If there is only one major discipline this method will return null.
 159  
      * 
 160  
      * @param credentialProgramInfo
 161  
      * @return
 162  
      */
 163  
     private CredentialProgramInfo findPreviousVersion(CredentialProgramInfo credentialProgramInfo) throws Exception {
 164  
         // Find all previous versions using the version independent indicator
 165  0
         List<VersionDisplayInfo> versions = programService.getVersions(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, credentialProgramInfo.getVersionInfo().getVersionIndId());
 166  
 
 167  
         // Take the sequence number for this version
 168  0
         Long sequenceNumber = credentialProgramInfo.getVersionInfo().getSequenceNumber();
 169  
 
 170  
         // And subtract 1 from the sequence number to get the previous version
 171  0
         sequenceNumber -= 1;
 172  
 
 173  
         // Loop over all versions and find the previous version based on the sequence number
 174  
         /*
 175  
          * NOTE: Dan suggested we loop over all versions and change any version with state=active to state=superseded.
 176  
          * However, we decided not to go that route because we would need to pull back all data for each version to determine
 177  
          * if a version is active, since versioninfo does not have a getState() method
 178  
          */
 179  0
         CredentialProgramInfo previousVersion = null;
 180  0
         for (VersionDisplayInfo versionInfo : versions) {
 181  0
             if (versionInfo.getSequenceNumber().equals(sequenceNumber)) {
 182  0
                 previousVersion = programService.getCredentialProgram(versionInfo.getId());
 183  0
                 break;
 184  
             }
 185  
         }
 186  0
         return previousVersion;
 187  
     }
 188  
 
 189  
     /**
 190  
      * This method will update the requirement state.
 191  
      * <p>
 192  
      * Note that it uses StatementUtil to update the statement tree.
 193  
      * 
 194  
      * @param credentialProgramInfo
 195  
      * @param newState
 196  
      * @throws Exception
 197  
      */
 198  
     public void updateRequirementsState(List<String> programRequirementIds, String newState) throws Exception {
 199  
         
 200  0
         for (String programRequirementId : programRequirementIds) {
 201  
 
 202  
             // Get program requirement from the program service
 203  0
             ProgramRequirementInfo programRequirementInfo = programService.getProgramRequirement(programRequirementId, null, null);
 204  
 
 205  
             // Look in the requirement for the statement tree
 206  0
             StatementTreeViewInfo statementTree = programRequirementInfo.getStatement();
 207  
 
 208  
             // And recursively update the entire tree with the new state
 209  0
             StatementUtil.updateStatementTreeViewInfoState(newState, statementTree);
 210  
 
 211  
             // Update the state of the requirement object
 212  0
             programRequirementInfo.setState(newState);
 213  
 
 214  
             // The write the requirement back to the program service
 215  0
             programService.updateProgramRequirement(programRequirementInfo);
 216  
 
 217  0
         }
 218  0
     }
 219  
 
 220  
     /**
 221  
      * This method is used by Spring to inject the program service into this bean.
 222  
      * 
 223  
      * @param programService
 224  
      */
 225  
     public void setProgramService(ProgramService programService) {
 226  0
         this.programService = programService;
 227  0
     }
 228  
 
 229  
 }