Coverage Report - org.kuali.student.lum.lu.ui.course.server.gwt.CourseStateChangeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
CourseStateChangeServiceImpl
0%
0/68
0%
0/44
5.6
 
 1  
 package org.kuali.student.lum.lu.ui.course.server.gwt;
 2  
 
 3  
 import java.util.Date;
 4  
 import java.util.Iterator;
 5  
 import java.util.List;
 6  
 
 7  
 import org.kuali.student.common.dto.DtoConstants;
 8  
 import org.kuali.student.common.dto.StatusInfo;
 9  
 import org.kuali.student.common.exceptions.CircularReferenceException;
 10  
 import org.kuali.student.common.exceptions.DataValidationErrorException;
 11  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 12  
 import org.kuali.student.common.exceptions.InvalidParameterException;
 13  
 import org.kuali.student.common.exceptions.MissingParameterException;
 14  
 import org.kuali.student.common.exceptions.OperationFailedException;
 15  
 import org.kuali.student.common.exceptions.PermissionDeniedException;
 16  
 import org.kuali.student.common.exceptions.VersionMismatchException;
 17  
 import org.kuali.student.common.versionmanagement.dto.VersionDisplayInfo;
 18  
 import org.kuali.student.core.statement.dto.ReqComponentInfo;
 19  
 import org.kuali.student.core.statement.dto.StatementTreeViewInfo;
 20  
 import org.kuali.student.lum.course.dto.CourseInfo;
 21  
 import org.kuali.student.lum.course.service.CourseService;
 22  
 import org.kuali.student.lum.course.service.CourseServiceConstants;
 23  
 import org.springframework.transaction.annotation.Transactional;
 24  
 
 25  
 @Transactional(noRollbackFor={DoesNotExistException.class},rollbackFor={Throwable.class})
 26  0
 public class CourseStateChangeServiceImpl {
 27  
     private CourseService courseService;
 28  
     
 29  
    public StatusInfo changeState(String courseId, String newState, Date currentVersionStart) throws Exception {
 30  
             
 31  0
             CourseInfo thisVerCourse = courseService.getCourse(courseId);
 32  0
             String prevState = thisVerCourse.getState();
 33  0
                 String verIndId = thisVerCourse.getVersionInfo().getVersionIndId();
 34  0
                 VersionDisplayInfo curVerDisplayInfo = courseService.getCurrentVersion(CourseServiceConstants.COURSE_NAMESPACE_URI, verIndId);
 35  0
                 String curVerId = curVerDisplayInfo.getId();
 36  0
                 CourseInfo currVerCourse = courseService.getCourse(curVerId);
 37  0
                 String currVerState = currVerCourse.getState();
 38  0
                 boolean isCurrVer = (courseId.equals(currVerCourse.getId()));
 39  
 
 40  0
                 StatusInfo ret = new StatusInfo();
 41  
                 try {
 42  0
                         if (newState.equals(DtoConstants.STATE_ACTIVE)) {
 43  0
                                 if (prevState.equals(DtoConstants.STATE_APPROVED)) {
 44  
                                         // since this is approved if isCurrVer we can assume there are no previously active versions to deal with
 45  0
                                         if (isCurrVer) {
 46  
                                                 // setstate for thisVerCourse and setCurrentVersion(courseId)
 47  0
                                                 updateCourseVersionStates(thisVerCourse, newState, currVerCourse, null, true, currentVersionStart);
 48  0
                                         } else if (currVerState.equals(DtoConstants.STATE_ACTIVE) ||
 49  
                                                         currVerState.equals(DtoConstants.STATE_INACTIVE)) {
 50  0
                                                 updateCourseVersionStates(thisVerCourse, newState, currVerCourse, DtoConstants.STATE_SUPERSEDED, true, currentVersionStart);
 51  
                                         }                                        
 52  
 
 53  0
                                 } else if (prevState.equals(DtoConstants.STATE_INACTIVE)) {
 54  
 
 55  
                                 }
 56  
                         }
 57  0
                         ret.setSuccess(new Boolean(true));
 58  0
                 } catch (Exception e) {
 59  0
                         ret.setSuccess(new Boolean(false));
 60  0
                         ret.setMessage(e.getMessage());
 61  0
                 }
 62  
 
 63  
                 
 64  0
                 return ret;
 65  
     }
 66  
 
 67  
     /**
 68  
      * Based on null values, updates states of thisVerCourse and currVerCourse and sets thisVerCourse as the current version.  Attempts to rollback transaction on exception.
 69  
      *
 70  
      * @param thisVerCourse this is the version that the user selected to change the state
 71  
      * @param thisVerNewState this is state that the user selected to change thisVerCourse to
 72  
      * @param currVerCourse this is the current version of the course (currentVersionStartDt <= now && currentVersionEndDt > now)
 73  
      * @param currVerNewState this is the state that we need to set the current version to.  Set to null to not update the currVerCourse state.
 74  
      * @param makeCurrent if true we'll set thisVerCourse as the current version.
 75  
      * @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.
 76  
      * @throws Exception
 77  
      */
 78  
     @Transactional(readOnly = false)
 79  
     private void updateCourseVersionStates (CourseInfo thisVerCourse, String thisVerNewState, CourseInfo currVerCourse, String currVerNewState, boolean makeCurrent, Date currentVersionStart) throws Exception {
 80  0
             String thisVerPrevState = thisVerCourse.getState();
 81  0
             String currVerPrevState = currVerCourse.getState();
 82  
             
 83  
             // if already current, will throw error if you try to make the current version the current version.
 84  0
             boolean isCurrent = thisVerCourse.getId().equals(currVerCourse.getId());
 85  0
             makeCurrent &= !isCurrent;                
 86  
             
 87  0
             if (thisVerNewState == null) {
 88  0
                     throw new InvalidParameterException("new state cannot be null");
 89  
             } else {
 90  0
                     thisVerCourse.setState(thisVerNewState);
 91  0
                     courseService.updateCourse(thisVerCourse);
 92  0
                     updateStatementTreeViewInfoState(thisVerCourse);                 
 93  
             }
 94  
 
 95  
             // won't get called if previous exception was thrown
 96  0
             if (currVerNewState != null) {
 97  0
                     currVerCourse.setState(currVerNewState);
 98  0
                     courseService.updateCourse(currVerCourse);
 99  0
                     updateStatementTreeViewInfoState(currVerCourse);
 100  
             }
 101  
 
 102  0
             if (makeCurrent == true) {
 103  0
                     courseService.setCurrentCourseVersion(thisVerCourse.getId(), currentVersionStart);
 104  
             }
 105  
             
 106  
             // for all draft and approved courses set the state to superseded.
 107  
             // we should only need to evaluated versions with sequence number
 108  
             // higher than previous active course.  If the course you're 
 109  
             // activating is the current course check all versions. 
 110  0
             if (thisVerPrevState.equals(DtoConstants.STATE_APPROVED) &&
 111  
                             thisVerNewState.equals(DtoConstants.STATE_ACTIVE)) {
 112  
 
 113  0
                     List<VersionDisplayInfo> versions = courseService.getVersions(CourseServiceConstants.COURSE_NAMESPACE_URI, thisVerCourse.getVersionInfo().getVersionIndId());                
 114  0
                     Long startSeq = new Long(1);
 115  
                     
 116  0
                         if (!isCurrent && (currVerCourse.getId() != thisVerCourse.getId())) {
 117  0
                                 startSeq = currVerCourse.getVersionInfo().getSequenceNumber() + 1;
 118  
                         }
 119  
                         
 120  0
                         for (VersionDisplayInfo versionInfo: versions) {
 121  0
                             if (versionInfo.getSequenceNumber() >= startSeq) {
 122  0
                                     CourseInfo otherCourse = courseService.getCourse(versionInfo.getId());
 123  0
                                     if (otherCourse.getState().equals(DtoConstants.STATE_APPROVED) ||
 124  
                                                     otherCourse.getState().equals(DtoConstants.STATE_SUBMITTED) ||
 125  
                                                     otherCourse.getState().equals(DtoConstants.STATE_DRAFT)) {
 126  0
                                             otherCourse.setState(DtoConstants.STATE_SUPERSEDED);
 127  0
                                             courseService.updateCourse(otherCourse);
 128  0
                                             updateStatementTreeViewInfoState(otherCourse);        
 129  
                                     }
 130  0
                             }
 131  
                     }                  
 132  
             }
 133  
 
 134  0
     }
 135  
 
 136  
         public void setCourseService(CourseService courseService) {
 137  0
                 this.courseService = courseService;
 138  0
         }
 139  
         
 140  
         public void statementTreeViewInfoStateSetter(String courseState, Iterator<StatementTreeViewInfo> itr) {
 141  0
                 while(itr.hasNext()) {
 142  0
                         StatementTreeViewInfo statementTreeViewInfo = (StatementTreeViewInfo)itr.next();
 143  0
                         statementTreeViewInfo.setState(courseState);
 144  0
                 List<ReqComponentInfo> reqComponents = statementTreeViewInfo.getReqComponents();
 145  0
                 for(Iterator<ReqComponentInfo> it = reqComponents.iterator(); it.hasNext();)
 146  0
                         it.next().setState(courseState);
 147  
 
 148  0
                 statementTreeViewInfoStateSetter(courseState, statementTreeViewInfo.getStatements().iterator());
 149  0
             }
 150  0
         } 
 151  
         
 152  
         public void updateStatementTreeViewInfoState(CourseInfo courseInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, DataValidationErrorException, CircularReferenceException, VersionMismatchException {
 153  0
                 List<StatementTreeViewInfo> statementTreeViewInfos = courseService.getCourseStatements(courseInfo.getId(), null, null);
 154  0
                 statementTreeViewInfoStateSetter(courseInfo.getState(), statementTreeViewInfos.iterator());
 155  0
                 for(Iterator<StatementTreeViewInfo> it = statementTreeViewInfos.iterator(); it.hasNext();)
 156  0
                         courseService.updateCourseStatement(courseInfo.getId(), it.next());        
 157  0
         }
 158  
         
 159  
 }