Coverage Report - org.kuali.student.lum.lu.ui.course.server.gwt.CourseStateChangeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
CourseStateChangeServiceImpl
0%
0/51
0%
0/38
7.667
 
 1  
 package org.kuali.student.lum.lu.ui.course.server.gwt;
 2  
 
 3  
 import java.util.Date;
 4  
 import java.util.List;
 5  
 
 6  
 import org.kuali.student.core.dto.StatusInfo;
 7  
 import org.kuali.student.core.exceptions.DoesNotExistException;
 8  
 import org.kuali.student.core.exceptions.InvalidParameterException;
 9  
 import org.kuali.student.core.versionmanagement.dto.VersionDisplayInfo;
 10  
 import org.kuali.student.lum.common.client.lu.LUUIConstants;
 11  
 import org.kuali.student.lum.course.dto.CourseInfo;
 12  
 import org.kuali.student.lum.course.service.CourseService;
 13  
 import org.kuali.student.lum.course.service.CourseServiceConstants;
 14  
 import org.springframework.transaction.annotation.Transactional;
 15  
 
 16  
 @Transactional(noRollbackFor={DoesNotExistException.class},rollbackFor={Throwable.class})
 17  0
 public class CourseStateChangeServiceImpl {
 18  
     private CourseService courseService;
 19  
     
 20  
    public StatusInfo changeState(String courseId, String newState, Date currentVersionStart) throws Exception {
 21  
             
 22  0
             CourseInfo thisVerCourse = courseService.getCourse(courseId);
 23  0
             String prevState = thisVerCourse.getState();
 24  0
                 String verIndId = thisVerCourse.getVersionInfo().getVersionIndId();
 25  0
                 VersionDisplayInfo curVerDisplayInfo = courseService.getCurrentVersion(CourseServiceConstants.COURSE_NAMESPACE_URI, verIndId);
 26  0
                 String curVerId = curVerDisplayInfo.getId();
 27  0
                 CourseInfo currVerCourse = courseService.getCourse(curVerId);
 28  0
                 String currVerState = currVerCourse.getState();
 29  0
                 boolean isCurrVer = (courseId.equals(currVerCourse.getId()));
 30  
 
 31  0
                 StatusInfo ret = new StatusInfo();
 32  
                 try {
 33  0
                         if (newState.equals(LUUIConstants.LU_STATE_ACTIVE)) {
 34  0
                                 if (prevState.equals(LUUIConstants.LU_STATE_APPROVED)) {
 35  
                                         // since this is approved if isCurrVer we can assume there are no previously active versions to deal with
 36  0
                                         if (isCurrVer) {
 37  
                                                 // setstate for thisVerCourse and setCurrentVersion(courseId)
 38  0
                                                 updateCourseVersionStates(thisVerCourse, newState, currVerCourse, null, true, currentVersionStart);
 39  0
                                         } else if (currVerState.equals(LUUIConstants.LU_STATE_ACTIVE) ||
 40  
                                                         currVerState.equals(LUUIConstants.LU_STATE_INACTIVE)) {
 41  0
                                                 updateCourseVersionStates(thisVerCourse, newState, currVerCourse, LUUIConstants.LU_STATE_SUPERSEDED, true, currentVersionStart);
 42  
                                         }                                        
 43  
 
 44  0
                                 } else if (prevState.equals(LUUIConstants.LU_STATE_INACTIVE)) {
 45  
 
 46  
                                 }
 47  
                         }
 48  0
                         ret.setSuccess(new Boolean(true));
 49  0
                 } catch (Exception e) {
 50  0
                         ret.setSuccess(new Boolean(false));
 51  0
                         ret.setMessage(e.getMessage());
 52  0
                 }
 53  
 
 54  0
                 return ret;
 55  
     }
 56  
 
 57  
     /**
 58  
      * Based on null values, updates states of thisVerCourse and currVerCourse and sets thisVerCourse as the current version.  Attempts to rollback transaction on exception.
 59  
      *
 60  
      * @param thisVerCourse this is the version that the user selected to change the state
 61  
      * @param thisVerNewState this is state that the user selected to change thisVerCourse to
 62  
      * @param currVerCourse this is the current version of the course (currentVersionStartDt <= now && currentVersionEndDt > now)
 63  
      * @param currVerNewState this is the state that we need to set the current version to.  Set to null to not update the currVerCourse state.
 64  
      * @param makeCurrent if true we'll set thisVerCourse as the current version.
 65  
      * @param currentVersionStart the start date for the new current version to start on and the old current version to end on.  Set to null to use now as the start date.
 66  
      * @throws Exception
 67  
      */
 68  
     @Transactional(readOnly = false)
 69  
     private void updateCourseVersionStates (CourseInfo thisVerCourse, String thisVerNewState, CourseInfo currVerCourse, String currVerNewState, boolean makeCurrent, Date currentVersionStart) throws Exception {
 70  0
             String thisVerPrevState = thisVerCourse.getState();
 71  0
             String currVerPrevState = currVerCourse.getState();
 72  
             
 73  
             // if already current, will throw error if you try to make the current version the current version.
 74  0
             boolean isCurrent = thisVerCourse.getId().equals(currVerCourse.getId());
 75  0
             makeCurrent &= !isCurrent;                
 76  
             
 77  0
             if (thisVerNewState == null) {
 78  0
                     throw new InvalidParameterException("new state cannot be null");
 79  
             } else {
 80  0
                     thisVerCourse.setState(thisVerNewState);
 81  0
                     courseService.updateCourse(thisVerCourse);
 82  
             }
 83  
 
 84  
             // won't get called if previous exception was thrown
 85  0
             if (currVerNewState != null) {
 86  0
                     currVerCourse.setState(currVerNewState);
 87  0
                     courseService.updateCourse(currVerCourse);
 88  
 
 89  
             }
 90  
 
 91  0
             if (makeCurrent == true) {
 92  0
                     courseService.setCurrentCourseVersion(thisVerCourse.getId(), currentVersionStart);
 93  
             }
 94  
             
 95  
             // for all draft and approved courses set the state to superseded.
 96  
             // we should only need to evaluated versions with sequence number
 97  
             // higher than previous active course.  If the course you're 
 98  
             // activating is the current course check all versions. 
 99  0
             if (thisVerPrevState.equals(LUUIConstants.LU_STATE_APPROVED) &&
 100  
                             thisVerNewState.equals(LUUIConstants.LU_STATE_ACTIVE)) {
 101  
 
 102  0
                     List<VersionDisplayInfo> versions = courseService.getVersions(CourseServiceConstants.COURSE_NAMESPACE_URI, thisVerCourse.getVersionInfo().getVersionIndId());                
 103  0
                     Long startSeq = new Long(1);
 104  
                     
 105  0
                         if (!isCurrent && (currVerCourse.getId() != thisVerCourse.getId())) {
 106  0
                                 startSeq = currVerCourse.getVersionInfo().getSequenceNumber() + 1;
 107  
                         }
 108  
                         
 109  0
                         for (VersionDisplayInfo versionInfo: versions) {
 110  0
                             if (versionInfo.getSequenceNumber() >= startSeq) {
 111  0
                                     CourseInfo otherCourse = courseService.getCourse(versionInfo.getId());
 112  0
                                     if (otherCourse.getState().equals(LUUIConstants.LU_STATE_APPROVED) ||
 113  
                                                     otherCourse.getState().equals(LUUIConstants.LU_STATE_SUBMITTED) ||
 114  
                                                     otherCourse.getState().equals(LUUIConstants.LU_STATE_DRAFT)) {
 115  0
                                             otherCourse.setState(LUUIConstants.LU_STATE_SUPERSEDED);
 116  0
                                             courseService.updateCourse(otherCourse);
 117  
                                     }
 118  0
                             }
 119  
                     }                  
 120  
             }
 121  
 
 122  0
     }
 123  
 
 124  
         public void setCourseService(CourseService courseService) {
 125  0
                 this.courseService = courseService;
 126  0
         }
 127  
 }