Coverage Report - org.kuali.student.lum.program.server.CredentialProgramStateChangeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
CredentialProgramStateChangeServiceImpl
0%
0/57
0%
0/24
2.222
 
 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, 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 endInstAdmitTerm, 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  
             // Update previous versions to superseded and set end terms on previous current version.
 75  0
                 updatePreviousVersions(selectedVersion, endEntryTerm, endEnrollTerm, endInstAdmitTerm);
 76  
                 
 77  
             // Update state of all associated objects for current version
 78  
             // NOTE: we must update state BEFORE making the version current
 79  0
             updateCredentialProgramInfoState(selectedVersion, newState);
 80  
 
 81  
             // Make this the current version
 82  0
             makeCurrent(selectedVersion);
 83  
         } else {
 84  
 
 85  
             // Update state of all associated objects for current version
 86  0
             updateCredentialProgramInfoState(selectedVersion, newState);
 87  
         }
 88  
 
 89  0
     }
 90  
 
 91  
     /**
 92  
      * This method finds all previous versions of program and sets all previous ACTIVE,APPROVED,DRAFT versions to SUPERSEDED and
 93  
      * sets new end terms for previous current version.
 94  
  
 95  
      * @param selectedVersion The version of credential  program being activated
 96  
      * @param endEntryTerm The new end entry term to set on previous active version
 97  
      * @param endEnrollTerm The new end enroll term to set on previous active version
 98  
      * @throws Exception
 99  
      */
 100  
     private void updatePreviousVersions (CredentialProgramInfo selectedVersion, String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm) throws Exception {
 101  
             // Get the current version of major discipline given the selected version
 102  0
             CredentialProgramInfo currentVersion = getCurrentVersion(selectedVersion);
 103  
             
 104  0
             boolean isSelectedVersionCurrent = selectedVersion.getId().equals(currentVersion.getId());
 105  
             
 106  
             //Set the end terms on the current version of major discipline and update it's state to superseded
 107  0
             setEndTerms(currentVersion, endEntryTerm, endEnrollTerm);
 108  0
             updateCredentialProgramInfoState(currentVersion, DtoConstants.STATE_SUPERSEDED);
 109  
 
 110  
                 // Loop through all previous active or approved programs and set the state to superseded.
 111  
                 // We should only need to evaluated versions with sequence number
 112  
                 // higher than previous active program
 113  
 
 114  0
                 List<VersionDisplayInfo> versions = programService.getVersions(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, 
 115  
                                 selectedVersion.getVersionInfo().getVersionIndId());
 116  0
                 Long startSeq = new Long(1);
 117  
 
 118  0
                 if (!isSelectedVersionCurrent) {
 119  0
                         startSeq = currentVersion.getVersionInfo().getSequenceNumber() + 1;
 120  
                 }
 121  
 
 122  0
                 for (VersionDisplayInfo versionInfo : versions) {
 123  0
                         boolean isVersionNewerThanCurrentVersion = versionInfo.getSequenceNumber() >= startSeq;
 124  0
                         boolean isVersionSelectedVersion = versionInfo.getSequenceNumber().equals(selectedVersion.getVersionInfo().getSequenceNumber());  
 125  0
                         boolean updateState = isVersionNewerThanCurrentVersion && !isVersionSelectedVersion;
 126  0
                         if (updateState) {
 127  0
                                 CredentialProgramInfo otherProgram = programService.getCredentialProgram(versionInfo.getId());
 128  0
                                 if (otherProgram.getState().equals(DtoConstants.STATE_APPROVED) ||
 129  
                                         otherProgram.getState().equals(DtoConstants.STATE_ACTIVE)){
 130  0
                                 updateCredentialProgramInfoState(otherProgram, DtoConstants.STATE_SUPERSEDED);
 131  
                                 }                
 132  
                         }
 133  0
                 }            
 134  
 
 135  0
     }
 136  
     
 137  
         /**
 138  
          * Get the current version of program given the selected version of program
 139  
          * 
 140  
          * @param verIndId
 141  
          */
 142  
         protected CredentialProgramInfo getCurrentVersion(CredentialProgramInfo credentialProgramInfo)
 143  
                         throws Exception {
 144  
                 // Get version independent id of program
 145  0
                 String verIndId = credentialProgramInfo.getVersionInfo().getVersionIndId();
 146  
 
 147  
                 // Get id of current version of program given the version independent id
 148  0
                 VersionDisplayInfo curVerDisplayInfo = programService.getCurrentVersion(
 149  
                                 ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, verIndId);
 150  0
                 String curVerId = curVerDisplayInfo.getId();
 151  
 
 152  
                 // Return the current version of the course
 153  0
                 CredentialProgramInfo currentVersion = programService.getCredentialProgram(curVerId);
 154  
 
 155  0
                 return currentVersion;
 156  
         }
 157  
     
 158  
     /**
 159  
      * This method updates the end terms for the major discipline passed into it.
 160  
      * <p>
 161  
      * You must still call updateState() to save the object using the web service.
 162  
      * 
 163  
      * @param credentialProgramInfo
 164  
      * @param endEntryTerm
 165  
      * @param endEnrollTerm
 166  
      */
 167  
     private void setEndTerms(CredentialProgramInfo credentialProgramInfo, String endEntryTerm, String endEnrollTerm) {
 168  0
         credentialProgramInfo.setEndProgramEntryTerm(endEntryTerm);
 169  0
         credentialProgramInfo.setEndTerm(endEnrollTerm);
 170  0
     }
 171  
 
 172  
     /**
 173  
      * This method will update the state of this object and all associated objects.
 174  
      * <p>
 175  
      * It is needed because we need to make separate web service calls to update the state of these objects.
 176  
      * 
 177  
      * @param credentialProgramInfo
 178  
      * @param newState
 179  
      */
 180  
     private void updateCredentialProgramInfoState(CredentialProgramInfo credentialProgramInfo, String newState) throws Exception {
 181  
         // Update the statement tree
 182  0
         List<String> programRequirementIds = credentialProgramInfo.getProgramRequirements();
 183  0
         updateRequirementsState(programRequirementIds, newState);
 184  
  
 185  
         // Credential and core programs do not have variations
 186  
         
 187  
         // Update program
 188  0
         credentialProgramInfo.setState(newState);
 189  0
         programService.updateCredentialProgram(credentialProgramInfo);
 190  0
     }
 191  
 
 192  
     /**
 193  
      * This method will make this version of the major discipline the current one.
 194  
      * 
 195  
      * @param credentialProgramInfo
 196  
      */
 197  
     private void makeCurrent(CredentialProgramInfo credentialProgramInfo) throws Exception {
 198  
 
 199  
         // Check if this is the current version before trying to make it current
 200  
         // (the web service will error if you try to make a version current that is already current)
 201  0
         VersionDisplayInfo currentVersion = programService.getCurrentVersion(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, credentialProgramInfo.getVersionInfo().getVersionIndId());
 202  
 
 203  
         // If this is not the current version, then make it current
 204  0
         if (!currentVersion.getSequenceNumber().equals(credentialProgramInfo.getVersionInfo().getSequenceNumber())) {
 205  0
             programService.setCurrentCredentialProgramVersion(credentialProgramInfo.getId(), null);
 206  
         }
 207  0
     }
 208  
 
 209  
     /**
 210  
      * This method will update the requirement state.
 211  
      * <p>
 212  
      * Note that it uses StatementUtil to update the statement tree.
 213  
      * 
 214  
      * @param credentialProgramInfo
 215  
      * @param newState
 216  
      * @throws Exception
 217  
      */
 218  
     public void updateRequirementsState(List<String> programRequirementIds, String newState) throws Exception {
 219  
         
 220  0
         for (String programRequirementId : programRequirementIds) {
 221  
 
 222  
             // Get program requirement from the program service
 223  0
             ProgramRequirementInfo programRequirementInfo = programService.getProgramRequirement(programRequirementId, null, null);
 224  
 
 225  
             // Look in the requirement for the statement tree
 226  0
             StatementTreeViewInfo statementTree = programRequirementInfo.getStatement();
 227  
 
 228  
             // And recursively update the entire tree with the new state
 229  0
             StatementUtil.updateStatementTreeViewInfoState(newState, statementTree);
 230  
 
 231  
             // Update the state of the requirement object
 232  0
             programRequirementInfo.setState(newState);
 233  
 
 234  
             // The write the requirement back to the program service
 235  0
             programService.updateProgramRequirement(programRequirementInfo);
 236  
 
 237  0
         }
 238  0
     }
 239  
 
 240  
     /**
 241  
      * This method is used by Spring to inject the program service into this bean.
 242  
      * 
 243  
      * @param programService
 244  
      */
 245  
     public void setProgramService(ProgramService programService) {
 246  0
         this.programService = programService;
 247  0
     }
 248  
 
 249  
 }