Coverage Report - org.kuali.student.lum.workflow.CoursePostProcessorBase
 
Classes in this File Line Coverage Branch Coverage Complexity
CoursePostProcessorBase
0%
0/54
0%
0/32
3.556
 
 1  
 /**
 2  
  * 
 3  
  */
 4  
 package org.kuali.student.lum.workflow;
 5  
 
 6  
 import javax.xml.namespace.QName;
 7  
 
 8  
 import org.apache.commons.lang.StringUtils;
 9  
 import org.kuali.rice.core.resourceloader.GlobalResourceLoader;
 10  
 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
 11  
 import org.kuali.rice.kew.postprocessor.ActionTakenEvent;
 12  
 import org.kuali.rice.kew.postprocessor.DocumentRouteStatusChange;
 13  
 import org.kuali.rice.kew.postprocessor.IDocumentEvent;
 14  
 import org.kuali.rice.kew.util.KEWConstants;
 15  
 import org.kuali.student.core.exceptions.OperationFailedException;
 16  
 import org.kuali.student.core.proposal.dto.ProposalInfo;
 17  
 import org.kuali.student.lum.course.dto.CourseInfo;
 18  
 import org.kuali.student.lum.course.service.CourseService;
 19  
 import org.kuali.student.lum.course.service.CourseServiceConstants;
 20  
 import org.kuali.student.lum.lu.LUConstants;
 21  
 import org.springframework.transaction.annotation.Transactional;
 22  
 
 23  
 /**
 24  
  * A base post processor class for Course document types in Workflow.
 25  
  *
 26  
  */
 27  
 @Transactional(readOnly=true, rollbackFor={Throwable.class})
 28  0
 public class CoursePostProcessorBase extends KualiStudentPostProcessorBase {
 29  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CoursePostProcessorBase.class);
 30  
 
 31  
     private CourseService courseService;
 32  
 
 33  
     @Override
 34  
     protected void processWithdrawActionTaken(ActionTakenEvent actionTakenEvent, ProposalInfo proposalInfo) throws Exception {
 35  0
         LOG.info("Will set CLU state to '" + LUConstants.LU_STATE_SUBMITTED + "'");
 36  0
         CourseInfo courseInfo = getCourseService().getCourse(getCourseId(proposalInfo));
 37  0
         updateCourse(actionTakenEvent, LUConstants.LU_STATE_SUBMITTED, courseInfo);
 38  0
     }
 39  
 
 40  
     @Override
 41  
     protected boolean processCustomActionTaken(ActionTakenEvent actionTakenEvent, ActionTakenValue actionTaken, ProposalInfo proposalInfo) throws Exception {
 42  0
         String cluId = getCourseId(proposalInfo);
 43  0
         CourseInfo courseInfo = getCourseService().getCourse(cluId);
 44  0
         updateCourse(actionTakenEvent, null, courseInfo);
 45  0
         return true;
 46  
     }
 47  
 
 48  
     @Override
 49  
     protected boolean processCustomRouteStatusChange(DocumentRouteStatusChange statusChangeEvent, ProposalInfo proposalInfo) throws Exception {
 50  
         // update the course state if the cluState value is not null (allows for clearing of the state)
 51  0
         String courseId = getCourseId(proposalInfo);
 52  0
         CourseInfo courseInfo = getCourseService().getCourse(courseId);
 53  0
         String courseState = getCluStateForRouteStatus(courseInfo.getState(), statusChangeEvent.getNewRouteStatus());
 54  0
         updateCourse(statusChangeEvent, courseState, courseInfo);
 55  0
         return true;
 56  
     }
 57  
 
 58  
     protected String getCourseId(ProposalInfo proposalInfo) throws OperationFailedException {
 59  0
         if (proposalInfo.getProposalReference().size() != 1) {
 60  0
             LOG.error("Found " + proposalInfo.getProposalReference().size() + " CLU objects linked to proposal with proposalId='" + proposalInfo.getId() + "'. Must have exactly 1 linked.");
 61  0
             throw new OperationFailedException("Found " + proposalInfo.getProposalReference().size() + " CLU objects linked to proposal with docId='" + proposalInfo.getWorkflowId() + "' and proposalId='" + proposalInfo.getId() + "'. Must have exactly 1 linked.");
 62  
         }
 63  0
         return proposalInfo.getProposalReference().get(0);
 64  
     }
 65  
 
 66  
     /**
 67  
      * @param currentCluState - the current state set on the CLU
 68  
      * @param newWorkflowStatusCode - the new route status code that is getting set on the workflow document
 69  
      * @return the CLU state to set or null if the CLU does not need it's state changed
 70  
      */
 71  
     protected String getCluStateForRouteStatus(String currentCluState, String newWorkflowStatusCode) {
 72  0
         if (StringUtils.equals(KEWConstants.ROUTE_HEADER_SAVED_CD, newWorkflowStatusCode)) {
 73  0
             return getCourseStateFromNewState(currentCluState, LUConstants.LU_STATE_DRAFT);
 74  0
         } else if (KEWConstants.ROUTE_HEADER_CANCEL_CD .equals(newWorkflowStatusCode)) {
 75  0
             return getCourseStateFromNewState(currentCluState, LUConstants.LU_STATE_DRAFT);
 76  0
         } else if (KEWConstants.ROUTE_HEADER_ENROUTE_CD.equals(newWorkflowStatusCode)) {
 77  0
             return getCourseStateFromNewState(currentCluState, LUConstants.LU_STATE_SUBMITTED);
 78  0
         } else if (KEWConstants.ROUTE_HEADER_DISAPPROVED_CD.equals(newWorkflowStatusCode)) {
 79  
             /* current requirements state that on a Withdraw (which is a KEW Disapproval) the 
 80  
              * CLU state should be submitted so no special handling required here
 81  
              */
 82  0
             return getCourseStateFromNewState(currentCluState, LUConstants.LU_STATE_SUBMITTED);
 83  0
         } else if (KEWConstants.ROUTE_HEADER_PROCESSED_CD.equals(newWorkflowStatusCode)) {
 84  0
             return getCourseStateFromNewState(currentCluState, LUConstants.LU_STATE_APPROVED);
 85  0
         } else if (KEWConstants.ROUTE_HEADER_EXCEPTION_CD.equals(newWorkflowStatusCode)) {
 86  0
             return getCourseStateFromNewState(currentCluState, LUConstants.LU_STATE_SUBMITTED);
 87  
         } else {
 88  
             // no status to set
 89  0
             return null;
 90  
         }
 91  
     }
 92  
 
 93  
     /**
 94  
      * Default behavior is to return the <code>newCluState</code> variable only if it differs from the
 95  
      * <code>currentCluState</code> value. Otherwise <code>null</code> will be returned.
 96  
      */
 97  
     protected String getCourseStateFromNewState(String currentCourseState, String newCourseState) {
 98  0
         if (LOG.isInfoEnabled()) {
 99  0
             LOG.info("current CLU state is '" + currentCourseState + "' and new CLU state will be '" + newCourseState + "'");
 100  
         }
 101  0
         return getStateFromNewState(currentCourseState, newCourseState);
 102  
     }
 103  
 
 104  
     @Transactional(readOnly=false)
 105  
     protected void updateCourse(IDocumentEvent iDocumentEvent, String courseState, CourseInfo courseInfo) throws Exception {
 106  
         // only change the state if the course is not currently set to that state
 107  0
         boolean requiresSave = false;
 108  0
         if (courseState != null) {
 109  0
             if (LOG.isInfoEnabled()) {
 110  0
                 LOG.info("Setting state '" + courseState + "' on CLU with cluId='" + courseInfo.getId() + "'");
 111  
             }
 112  0
             courseInfo.setState(courseState);
 113  0
             requiresSave = true;
 114  
         }
 115  0
         if (LOG.isInfoEnabled()) {
 116  0
             LOG.info("Running preProcessCluSave with cluId='" + courseInfo.getId() + "'");
 117  
         }
 118  0
         requiresSave |= preProcessCourseSave(iDocumentEvent, courseInfo);
 119  
 
 120  0
         if (requiresSave) {
 121  0
             getCourseService().updateCourse(courseInfo);
 122  
             
 123  
             //For a newly approved course (w/no prior active versions), make the new course the current version.
 124  0
             if (LUConstants.LU_STATE_APPROVED.equals(courseState) && courseInfo.getVersionInfo().getCurrentVersionStart() == null){
 125  
                     // TODO: set states of other approved courses to superseded                
 126  
                 
 127  
                     // if current version's state is not active then we can set this course as the active course
 128  0
                     if (!LUConstants.LU_STATE_ACTIVE.equals(getCourseService().getCourse(getCourseService().getCurrentVersion(CourseServiceConstants.COURSE_NAMESPACE_URI, courseInfo.getVersionInfo().getVersionIndId()).getId()).getState())) { 
 129  0
                             getCourseService().setCurrentCourseVersion(courseInfo.getId(), null);
 130  
                     }
 131  
             }
 132  
         }
 133  
 
 134  0
     }
 135  
 
 136  
     protected boolean preProcessCourseSave(IDocumentEvent iDocumentEvent, CourseInfo courseInfo) {
 137  0
         return false;
 138  
     }
 139  
 
 140  
     protected CourseService getCourseService() {
 141  0
         if (this.courseService == null) {
 142  0
             this.courseService = (CourseService) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/course","CourseService")); 
 143  
         }
 144  0
         return this.courseService;
 145  
     }
 146  
 
 147  
 }