| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |  package org.kuali.student.lum.workflow; | 
  | 5 |  |   | 
  | 6 |  |  import java.util.Iterator; | 
  | 7 |  |  import java.util.List; | 
  | 8 |  |  import java.util.Map; | 
  | 9 |  |   | 
  | 10 |  |  import javax.xml.namespace.QName; | 
  | 11 |  |   | 
  | 12 |  |  import org.apache.commons.lang.StringUtils; | 
  | 13 |  |  import org.kuali.rice.core.resourceloader.GlobalResourceLoader; | 
  | 14 |  |  import org.kuali.rice.kew.actiontaken.ActionTakenValue; | 
  | 15 |  |  import org.kuali.rice.kew.postprocessor.ActionTakenEvent; | 
  | 16 |  |  import org.kuali.rice.kew.postprocessor.DocumentRouteStatusChange; | 
  | 17 |  |  import org.kuali.rice.kew.postprocessor.IDocumentEvent; | 
  | 18 |  |  import org.kuali.rice.kew.util.KEWConstants; | 
  | 19 |  |  import org.kuali.student.common.dto.DtoConstants; | 
  | 20 |  |  import org.kuali.student.common.exceptions.DoesNotExistException; | 
  | 21 |  |  import org.kuali.student.common.exceptions.OperationFailedException; | 
  | 22 |  |  import org.kuali.student.core.proposal.dto.ProposalInfo; | 
  | 23 |  |  import org.kuali.student.core.statement.dto.ReqComponentInfo; | 
  | 24 |  |  import org.kuali.student.core.statement.dto.StatementTreeViewInfo; | 
  | 25 |  |  import org.kuali.student.lum.course.dto.CourseInfo; | 
  | 26 |  |  import org.kuali.student.lum.course.service.CourseService; | 
  | 27 |  |  import org.kuali.student.lum.lu.LUConstants; | 
  | 28 |  |  import org.springframework.transaction.annotation.Transactional; | 
  | 29 |  |   | 
  | 30 |  |   | 
  | 31 |  |   | 
  | 32 |  |   | 
  | 33 |  |   | 
  | 34 |  |  @Transactional(readOnly=true, rollbackFor={Throwable.class}) | 
  | 35 | 0 |  public class CoursePostProcessorBase extends KualiStudentPostProcessorBase { | 
  | 36 | 0 |      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CoursePostProcessorBase.class); | 
  | 37 |  |   | 
  | 38 |  |      private CourseService courseService; | 
  | 39 |  |      private CourseStateChangeServiceImpl courseStateChangeService; | 
  | 40 |  |       | 
  | 41 |  |       | 
  | 42 |  |   | 
  | 43 |  |   | 
  | 44 |  |   | 
  | 45 |  |   | 
  | 46 |  |   | 
  | 47 |  |   | 
  | 48 |  |   | 
  | 49 |  |   | 
  | 50 |  |   | 
  | 51 |  |      | 
  | 52 |  |      @Override | 
  | 53 |  |      protected void processWithdrawActionTaken(ActionTakenEvent actionTakenEvent, ProposalInfo proposalInfo) throws Exception { | 
  | 54 |  |               | 
  | 55 | 0 |              if (proposalInfo != null){ | 
  | 56 | 0 |                      String proposalDocType=proposalInfo.getType();             | 
  | 57 |  |                       | 
  | 58 |  |                       | 
  | 59 |  |                       | 
  | 60 | 0 |                      if ( LUConstants.PROPOSAL_TYPE_COURSE_CREATE.equals(proposalDocType) | 
  | 61 |  |                                          ||  LUConstants.PROPOSAL_TYPE_COURSE_MODIFY.equals(proposalDocType)) { | 
  | 62 | 0 |                                  LOG.info("Will set CLU state to '" | 
  | 63 |  |                                                  + DtoConstants.STATE_NOT_APPROVED + "'"); | 
  | 64 |  |                                   | 
  | 65 | 0 |                                  CourseInfo courseInfo = getCourseService().getCourse( | 
  | 66 |  |                                                  getCourseId(proposalInfo)); | 
  | 67 |  |                                   | 
  | 68 | 0 |                                  updateCourse(actionTakenEvent, DtoConstants.STATE_NOT_APPROVED, | 
  | 69 |  |                                                  courseInfo, proposalInfo); | 
  | 70 | 0 |                          }  | 
  | 71 |  |                           | 
  | 72 |  |                           | 
  | 73 | 0 |                                                  else if ( LUConstants.PROPOSAL_TYPE_COURSE_RETIRE.equals(proposalDocType)) { | 
  | 74 | 0 |                                  LOG.info("Withdrawing a retire proposal with ID'" + proposalInfo.getId()  | 
  | 75 |  |                                                  + ", will not change any CLU state as there is no new CLU object to set."); | 
  | 76 |  |                          } | 
  | 77 | 0 |                  } else { | 
  | 78 | 0 |                      LOG.info("Proposal Info is null when a withdraw proposal action was taken, doing nothing."); | 
  | 79 |  |              } | 
  | 80 | 0 |      } | 
  | 81 |  |   | 
  | 82 |  |      @Override | 
  | 83 |  |      protected boolean processCustomActionTaken(ActionTakenEvent actionTakenEvent, ActionTakenValue actionTaken, ProposalInfo proposalInfo) throws Exception { | 
  | 84 | 0 |          String cluId = getCourseId(proposalInfo); | 
  | 85 | 0 |          CourseInfo courseInfo = getCourseService().getCourse(cluId); | 
  | 86 |  |           | 
  | 87 | 0 |          updateCourse(actionTakenEvent, null, courseInfo, proposalInfo); | 
  | 88 | 0 |          return true; | 
  | 89 |  |      } | 
  | 90 |  |   | 
  | 91 |  |      | 
  | 92 |  |   | 
  | 93 |  |   | 
  | 94 |  |   | 
  | 95 |  |   | 
  | 96 |  |      @Override    | 
  | 97 |  |      protected boolean processCustomRouteStatusChange(DocumentRouteStatusChange statusChangeEvent, ProposalInfo proposalInfo) throws Exception { | 
  | 98 |  |   | 
  | 99 | 0 |              String courseId = getCourseId(proposalInfo);         | 
  | 100 | 0 |          String prevEndTermAtpId = proposalInfo.getAttributes().get("prevEndTerm"); | 
  | 101 |  |           | 
  | 102 |  |           | 
  | 103 | 0 |          CourseInfo courseInfo = getCourseService().getCourse(courseId); | 
  | 104 |  |           | 
  | 105 |  |           | 
  | 106 | 0 |          String newCourseState = getCluStateForRouteStatus(courseInfo.getState(), statusChangeEvent.getNewRouteStatus(), proposalInfo.getType()); | 
  | 107 |  |           | 
  | 108 |  |           | 
  | 109 | 0 |          if (newCourseState != null){ | 
  | 110 | 0 |                  if(DtoConstants.STATE_ACTIVE.equals(newCourseState)){      | 
  | 111 |  |                           | 
  | 112 |  |                           | 
  | 113 |  |                           | 
  | 114 | 0 |                          getCourseStateChangeService().changeState(courseId, newCourseState, prevEndTermAtpId); | 
  | 115 |  |                  } else | 
  | 116 |  |                           | 
  | 117 |  |                           | 
  | 118 |  |                           | 
  | 119 |  |                           | 
  | 120 | 0 |                          if(DtoConstants.STATE_RETIRED.equals(newCourseState)){ | 
  | 121 | 0 |                                  retireCourseByProposalCopyAndSave(newCourseState, courseInfo, proposalInfo); | 
  | 122 | 0 |                              getCourseStateChangeService().changeState(courseId, newCourseState, prevEndTermAtpId); | 
  | 123 |  |                  } | 
  | 124 |  |                    else{  | 
  | 125 | 0 |                          updateCourse(statusChangeEvent, newCourseState, courseInfo, proposalInfo); | 
  | 126 |  |                  }                        | 
  | 127 |  |          } | 
  | 128 | 0 |          return true; | 
  | 129 |  |      } | 
  | 130 |  |   | 
  | 131 |  |       | 
  | 132 |  |   | 
  | 133 |  |   | 
  | 134 |  |   | 
  | 135 |  |   | 
  | 136 |  |   | 
  | 137 |  |   | 
  | 138 |  |   | 
  | 139 |  |   | 
  | 140 |  |   | 
  | 141 |  |   | 
  | 142 |  |   | 
  | 143 |  |   | 
  | 144 |  |   | 
  | 145 |  |   | 
  | 146 |  |   | 
  | 147 |  |   | 
  | 148 |  |   | 
  | 149 |  |   | 
  | 150 |  |   | 
  | 151 |  |   | 
  | 152 |  |      protected void retireCourseByProposalCopyAndSave(String courseState, CourseInfo courseInfo, ProposalInfo proposalInfo) throws Exception { | 
  | 153 |  |           | 
  | 154 |  |               | 
  | 155 |  |               | 
  | 156 |  |               | 
  | 157 | 0 |          if (DtoConstants.STATE_RETIRED.equals(courseState)){ | 
  | 158 | 0 |                  if ((proposalInfo != null) && (proposalInfo.getAttributes() != null)) | 
  | 159 |  |                  { | 
  | 160 | 0 |                     String rationale = proposalInfo.getRationale(); | 
  | 161 | 0 |                  String proposedEndTerm = proposalInfo.getAttributes().get("proposedEndTerm");               | 
  | 162 | 0 |                  String proposedLastTermOffered = proposalInfo.getAttributes().get("proposedLastTermOffered"); | 
  | 163 | 0 |                  String proposedLastCourseCatalogYear = proposalInfo.getAttributes().get("proposedLastCourseCatalogYear"); | 
  | 164 |  |                         | 
  | 165 | 0 |                  courseInfo.setEndTerm(proposedEndTerm);                     | 
  | 166 | 0 |              courseInfo.getAttributes().put("retirementRationale", rationale); | 
  | 167 | 0 |              courseInfo.getAttributes().put("lastTermOffered", proposedLastTermOffered); | 
  | 168 | 0 |              courseInfo.getAttributes().put("lastPublicationYear", proposedLastCourseCatalogYear); | 
  | 169 |  |               | 
  | 170 |  |                         | 
  | 171 |  |                 | 
  | 172 |  |                 | 
  | 173 |  |                 | 
  | 174 |  |                 | 
  | 175 |  |                 | 
  | 176 |  |                 | 
  | 177 |  |                         | 
  | 178 |  |                         | 
  | 179 |  |                 | 
  | 180 | 0 |                        if ((proposalInfo!=null) && (courseInfo!=null) &&  | 
  | 181 |  |                                          (courseInfo.getAttributes().get("lastTermOffered")==null)) { | 
  | 182 | 0 |                                 courseInfo.getAttributes().put("lastTermOffered", proposalInfo.getAttributes().get("proposedEndTerm")); | 
  | 183 |  |                        } | 
  | 184 |  |                  } | 
  | 185 |  |          } | 
  | 186 |  |           | 
  | 187 | 0 |          getCourseService().updateCourse(courseInfo); | 
  | 188 | 0 |      } | 
  | 189 |  |   | 
  | 190 |  |      protected String getCourseId(ProposalInfo proposalInfo) throws OperationFailedException { | 
  | 191 | 0 |          if (proposalInfo.getProposalReference().size() != 1) { | 
  | 192 | 0 |              LOG.error("Found " + proposalInfo.getProposalReference().size() + " CLU objects linked to proposal with proposalId='" + proposalInfo.getId() + "'. Must have exactly 1 linked."); | 
  | 193 | 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."); | 
  | 194 |  |          } | 
  | 195 | 0 |          return proposalInfo.getProposalReference().get(0); | 
  | 196 |  |      } | 
  | 197 |  |   | 
  | 198 |  |       | 
  | 199 |  |   | 
  | 200 |  |   | 
  | 201 |  |   | 
  | 202 |  |   | 
  | 203 |  |   | 
  | 204 |  |   | 
  | 205 |  |   | 
  | 206 |  |   | 
  | 207 |  |      protected String getCluStateForRouteStatus(String currentCluState, String newWorkflowStatusCode, String docType) { | 
  | 208 | 0 |              if (LUConstants.PROPOSAL_TYPE_COURSE_RETIRE.equals(docType)) { | 
  | 209 |  |                       | 
  | 210 |  |                       | 
  | 211 | 0 |                      if (KEWConstants.ROUTE_HEADER_PROCESSED_CD.equals(newWorkflowStatusCode)){ | 
  | 212 | 0 |                              return DtoConstants.STATE_RETIRED; | 
  | 213 |  |                      }         | 
  | 214 | 0 |                      return null;   | 
  | 215 |  |              } else { | 
  | 216 |  |               | 
  | 217 | 0 |                      if (StringUtils.equals(KEWConstants.ROUTE_HEADER_SAVED_CD, newWorkflowStatusCode)) { | 
  | 218 | 0 |                      return getCourseStateFromNewState(currentCluState, DtoConstants.STATE_DRAFT); | 
  | 219 | 0 |                  } else if (KEWConstants.ROUTE_HEADER_CANCEL_CD .equals(newWorkflowStatusCode)) { | 
  | 220 | 0 |                      return getCourseStateFromNewState(currentCluState, DtoConstants.STATE_NOT_APPROVED); | 
  | 221 | 0 |                  } else if (KEWConstants.ROUTE_HEADER_ENROUTE_CD.equals(newWorkflowStatusCode)) { | 
  | 222 | 0 |                      return getCourseStateFromNewState(currentCluState, DtoConstants.STATE_DRAFT); | 
  | 223 | 0 |                  } else if (KEWConstants.ROUTE_HEADER_DISAPPROVED_CD.equals(newWorkflowStatusCode)) { | 
  | 224 |  |                       | 
  | 225 |  |   | 
  | 226 |  |   | 
  | 227 | 0 |                      return getCourseStateFromNewState(currentCluState, DtoConstants.STATE_NOT_APPROVED); | 
  | 228 | 0 |                  } else if (KEWConstants.ROUTE_HEADER_PROCESSED_CD.equals(newWorkflowStatusCode)) { | 
  | 229 | 0 |                                return getCourseStateFromNewState(currentCluState, DtoConstants.STATE_ACTIVE); | 
  | 230 | 0 |                  } else if (KEWConstants.ROUTE_HEADER_EXCEPTION_CD.equals(newWorkflowStatusCode)) { | 
  | 231 | 0 |                      return getCourseStateFromNewState(currentCluState, DtoConstants.STATE_DRAFT); | 
  | 232 |  |                  } else { | 
  | 233 |  |                       | 
  | 234 | 0 |                      return null; | 
  | 235 |  |                  } | 
  | 236 |  |              } | 
  | 237 |  |      } | 
  | 238 |  |   | 
  | 239 |  |       | 
  | 240 |  |   | 
  | 241 |  |   | 
  | 242 |  |   | 
  | 243 |  |      protected String getCourseStateFromNewState(String currentCourseState, String newCourseState) { | 
  | 244 | 0 |          if (LOG.isInfoEnabled()) { | 
  | 245 | 0 |              LOG.info("current CLU state is '" + currentCourseState + "' and new CLU state will be '" + newCourseState + "'"); | 
  | 246 |  |          } | 
  | 247 | 0 |          return getStateFromNewState(currentCourseState, newCourseState); | 
  | 248 |  |      } | 
  | 249 |  |   | 
  | 250 |  |      @Transactional(readOnly=false,noRollbackFor={DoesNotExistException.class},rollbackFor={Throwable.class}) | 
  | 251 |  |      protected void updateCourse(IDocumentEvent iDocumentEvent, String courseState, CourseInfo courseInfo, ProposalInfo proposalInfo) throws Exception { | 
  | 252 |  |           | 
  | 253 | 0 |          boolean requiresSave = false; | 
  | 254 | 0 |          if (courseState != null) { | 
  | 255 | 0 |              if (LOG.isInfoEnabled()) { | 
  | 256 | 0 |                  LOG.info("Setting state '" + courseState + "' on CLU with cluId='" + courseInfo.getId() + "'"); | 
  | 257 |  |              } | 
  | 258 |  |    | 
  | 259 |  |               | 
  | 260 | 0 |              courseInfo.setState(courseState); | 
  | 261 |  |               | 
  | 262 | 0 |              requiresSave = true; | 
  | 263 |  |          } | 
  | 264 | 0 |          if (LOG.isInfoEnabled()) { | 
  | 265 | 0 |              LOG.info("Running preProcessCluSave with cluId='" + courseInfo.getId() + "'"); | 
  | 266 |  |          } | 
  | 267 | 0 |          requiresSave |= preProcessCourseSave(iDocumentEvent, courseInfo); | 
  | 268 |  |   | 
  | 269 | 0 |          if (requiresSave) { | 
  | 270 | 0 |              getCourseService().updateCourse(courseInfo); | 
  | 271 |  |               | 
  | 272 |  |               | 
  | 273 | 0 |              if (DtoConstants.STATE_ACTIVE.equals(courseState) && courseInfo.getVersionInfo().getCurrentVersionStart() == null){ | 
  | 274 |  |                       | 
  | 275 |  |                   | 
  | 276 |  |                       | 
  | 277 |  |                       | 
  | 278 | 0 |                              getCourseService().setCurrentCourseVersion(courseInfo.getId(), null); | 
  | 279 |  |                       | 
  | 280 |  |              } | 
  | 281 |  |               | 
  | 282 | 0 |              List<StatementTreeViewInfo> statementTreeViewInfos = courseService.getCourseStatements(courseInfo.getId(), null, null); | 
  | 283 | 0 |              if(statementTreeViewInfos!=null){ | 
  | 284 | 0 |                      statementTreeViewInfoStateSetter(courseInfo.getState(), statementTreeViewInfos.iterator()); | 
  | 285 |  |                       | 
  | 286 | 0 |                      for(Iterator<StatementTreeViewInfo> it = statementTreeViewInfos.iterator(); it.hasNext();) | 
  | 287 | 0 |                                  courseService.updateCourseStatement(courseInfo.getId(), it.next()); | 
  | 288 |  |              } | 
  | 289 |  |          } | 
  | 290 |  |           | 
  | 291 | 0 |      } | 
  | 292 |  |   | 
  | 293 |  |      protected boolean preProcessCourseSave(IDocumentEvent iDocumentEvent, CourseInfo courseInfo) { | 
  | 294 | 0 |          return false; | 
  | 295 |  |      } | 
  | 296 |  |   | 
  | 297 |  |      protected CourseService getCourseService() { | 
  | 298 | 0 |          if (this.courseService == null) { | 
  | 299 | 0 |              this.courseService = (CourseService) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/course","CourseService"));  | 
  | 300 |  |          } | 
  | 301 | 0 |          return this.courseService; | 
  | 302 |  |      } | 
  | 303 |  |      protected CourseStateChangeServiceImpl getCourseStateChangeService() { | 
  | 304 | 0 |          if (this.courseStateChangeService == null) { | 
  | 305 | 0 |              this.courseStateChangeService = new CourseStateChangeServiceImpl(); | 
  | 306 | 0 |              this.courseStateChangeService.setCourseService(getCourseService()); | 
  | 307 |  |          } | 
  | 308 | 0 |          return this.courseStateChangeService; | 
  | 309 |  |      }     | 
  | 310 |  |       | 
  | 311 |  |   | 
  | 312 |  |   | 
  | 313 |  |      | 
  | 314 |  |      public void statementTreeViewInfoStateSetter(String courseState, Iterator<StatementTreeViewInfo> itr) { | 
  | 315 | 0 |              while(itr.hasNext()) { | 
  | 316 | 0 |                  StatementTreeViewInfo statementTreeViewInfo = (StatementTreeViewInfo)itr.next(); | 
  | 317 | 0 |                  statementTreeViewInfo.setState(courseState); | 
  | 318 | 0 |                  List<ReqComponentInfo> reqComponents = statementTreeViewInfo.getReqComponents(); | 
  | 319 | 0 |                  for(Iterator<ReqComponentInfo> it = reqComponents.iterator(); it.hasNext();) | 
  | 320 | 0 |                          it.next().setState(courseState); | 
  | 321 |  |   | 
  | 322 | 0 |                  statementTreeViewInfoStateSetter(courseState, statementTreeViewInfo.getStatements().iterator()); | 
  | 323 | 0 |          } | 
  | 324 | 0 |      } | 
  | 325 |  |  } |