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