View Javadoc

1   /**
2    * Copyright 2013 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   * Created by Charles on 9/20/13
16   */
17  package org.kuali.student.poc.eventproc.handler.impl;
18  
19  import org.apache.log4j.Logger;
20  import org.kuali.student.enrollment.courseoffering.dto.FormatOfferingInfo;
21  import org.kuali.student.enrollment.lui.dto.LuiInfo;
22  import org.kuali.student.poc.eventproc.api.KSHandler;
23  import org.kuali.student.poc.eventproc.api.KSInternalEventProcessor;
24  import org.kuali.student.poc.eventproc.event.KSEvent;
25  import org.kuali.student.poc.eventproc.event.KSEventFactory;
26  import org.kuali.student.poc.eventproc.event.KSHandlerResult;
27  import org.kuali.student.poc.eventproc.event.KSEventType;
28  import org.kuali.student.poc.eventproc.event.subclass.event.KSFOStateModifiedEvent;
29  import org.kuali.student.poc.eventproc.event.subclass.event.KSRecomputeCOStateEvent;
30  import org.kuali.student.poc.eventproc.event.subclass.type.KSFOStateModifiedEventType;
31  import org.kuali.student.poc.eventproc.handler.impl.helper.FoCoRgComputeStateUtil;
32  import org.kuali.student.r2.common.dto.ContextInfo;
33  import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
34  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
35  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
36  import org.kuali.student.r2.common.exceptions.MissingParameterException;
37  import org.kuali.student.r2.common.exceptions.OperationFailedException;
38  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
39  import org.kuali.student.r2.common.exceptions.ReadOnlyException;
40  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
41  
42  import java.util.ArrayList;
43  import java.util.List;
44  
45  /**
46   * Handles request to recompute CO state
47   *
48   * @author Kuali Student Team
49   */
50  public class CourseOfferingRecomputeStateHandler implements KSHandler {
51      public static final Logger LOGGER = Logger.getLogger(CourseOfferingRecomputeStateHandler.class);
52      KSInternalEventProcessor processor;
53  
54      public CourseOfferingRecomputeStateHandler(KSInternalEventProcessor processor) {
55          this.processor = processor;
56      }
57  
58      @Override
59      public boolean handlesEvent(KSEvent event) {
60          return event instanceof KSFOStateModifiedEvent ||
61                  event instanceof KSRecomputeCOStateEvent;
62      }
63  
64      @Override
65      public KSHandlerResult processEvent(KSEvent event, ContextInfo context)
66              throws PermissionDeniedException, MissingParameterException, InvalidParameterException,
67              OperationFailedException, DoesNotExistException, ReadOnlyException, DataValidationErrorException, VersionMismatchException {
68          if (!handlesEvent(event)) {
69              return new KSHandlerResult(KSHandlerResult.FAIL_HANDLER_WONT_PROCESS, CourseOfferingRecomputeStateHandler.class);
70          }
71          String coId = null;
72          if (event instanceof KSFOStateModifiedEvent) {
73              KSFOStateModifiedEvent stateModifiedEvent = (KSFOStateModifiedEvent) event;
74              String foId = stateModifiedEvent.getFoId();
75              FormatOfferingInfo foInfo = processor.getCoService().getFormatOffering(foId, context);
76              coId = foInfo.getCourseOfferingId();
77          } else {
78              KSRecomputeCOStateEvent recomputeEvent = (KSRecomputeCOStateEvent) event;
79              // Must be KSEventFactory.CO_RECOMPUTE_STATE_EVENT_TYPE
80              coId = recomputeEvent.getCoId();
81          }
82          List<FormatOfferingInfo> foInfos = processor.getCoService().getFormatOfferingsByCourseOffering(coId, context);
83          String toCoState = FoCoRgComputeStateUtil.computeCoState(foInfos);
84          LuiInfo coLui = processor.getLuiService().getLui(coId, context);
85          coLui.setStateKey(toCoState);
86          LuiInfo modifiedCoLui = processor.getLuiService().updateLui(coLui.getId(), coLui, context);
87          LOGGER.info("Setting CO to state: " + modifiedCoLui.getStateKey());
88  
89          // No further propagation
90          KSHandlerResult eventResult = new KSHandlerResult(KSHandlerResult.SUCCESS, CourseOfferingRecomputeStateHandler.class);
91          return eventResult;
92      }
93  
94      @Override
95      public String getName() {
96          return FormatOfferingRecomputeStateHandler.class.getSimpleName();
97      }
98  
99      @Override
100     public void setEventProcessor(KSInternalEventProcessor processor) {
101         this.processor = processor;
102     }
103 
104     @Override
105     public List<KSEventType> getEventTypesHandled() {
106         List<KSEventType> result = new ArrayList<KSEventType>();
107         result.add(KSEventFactory.FO_STATE_MODIFIED_EVENT_TYPE);
108         result.add(KSEventFactory.CO_RECOMPUTE_STATE_EVENT_TYPE);
109         return result;
110     }
111 }