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/15/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.ActivityOfferingInfo;
21  import org.kuali.student.enrollment.lui.dto.LuiInfo;
22  import org.kuali.student.poc.eventproc.api.KSInternalEventProcessor;
23  import org.kuali.student.poc.eventproc.event.KSEvent;
24  import org.kuali.student.poc.eventproc.api.KSHandler;
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.KSAOStateModifiedEvent;
29  import org.kuali.student.poc.eventproc.event.subclass.event.KSRecomputeFOStateEvent;
30  import org.kuali.student.poc.eventproc.event.subclass.type.KSAOStateModifiedEventType;
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   * Handler for recomputing FO state
47   *
48   * @author Kuali Student Team
49   */
50  public class FormatOfferingRecomputeStateHandler implements KSHandler {
51      public static final Logger LOGGER = Logger.getLogger(FormatOfferingRecomputeStateHandler.class);
52      KSInternalEventProcessor processor;
53  
54      public FormatOfferingRecomputeStateHandler(KSInternalEventProcessor processor) {
55          this.processor = processor;
56      }
57  
58      @Override
59      public boolean handlesEvent(KSEvent event) {
60          return event instanceof KSAOStateModifiedEvent ||
61                  event instanceof KSRecomputeFOStateEvent;
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, FormatOfferingRecomputeStateHandler.class);
70          }
71          String foId = null;
72          if (event instanceof KSAOStateModifiedEvent) {
73              KSAOStateModifiedEvent stateModifiedEvent = (KSAOStateModifiedEvent) event;
74              String aoId = stateModifiedEvent.getAoId();
75              ActivityOfferingInfo aoInfo = processor.getCoService().getActivityOffering(aoId, context);
76              foId = aoInfo.getFormatOfferingId();
77          } else {
78              KSRecomputeFOStateEvent recomputeEvent = (KSRecomputeFOStateEvent) event;
79              // Must be KSEventFactory.FO_RECOMPUTE_STATE_EVENT_TYPE
80              foId = recomputeEvent.getFoId();
81          }
82          List<ActivityOfferingInfo> aoInfos = processor.getCoService().getActivityOfferingsByFormatOffering(foId, context);
83          String toFoState = FoCoRgComputeStateUtil.computeFoState(aoInfos);
84          LuiInfo foLui = processor.getLuiService().getLui(foId, context);
85          String fromFoState = foLui.getStateKey();
86          if (fromFoState.equals(toFoState)) {
87              LOGGER.info("FO state unchanged (fromState = " + fromFoState + ", toState = " + toFoState + ")");
88              return new KSHandlerResult(KSHandlerResult.FAIL_STATE_UNCHANGED, FormatOfferingRecomputeStateHandler.class);
89          }
90          foLui.setStateKey(toFoState);
91  
92          LuiInfo modifiedFoLui = processor.getLuiService().updateLui(foLui.getId(), foLui, context);
93          LOGGER.info("Setting FO to state: " + modifiedFoLui.getStateKey());
94  
95          // Fire compute CO event
96          KSHandlerResult eventResult = new KSHandlerResult(KSHandlerResult.SUCCESS, FormatOfferingRecomputeStateHandler.class);
97          KSEvent foStateModifiedEvent = KSEventFactory.createFormatOfferingStateModifiedEvent(foId);
98          processor.internalFireEvent(foStateModifiedEvent, context);
99          event.addDownstreamEvent(foStateModifiedEvent); // Tracks other events caused by "event"
100         return eventResult;
101     }
102 
103     @Override
104     public String getName() {
105         return FormatOfferingRecomputeStateHandler.class.getSimpleName();
106     }
107 
108     @Override
109     public void setEventProcessor(KSInternalEventProcessor processor) {
110         this.processor = processor;
111     }
112 
113     @Override
114     public List<KSEventType> getEventTypesHandled() {
115         List<KSEventType> result = new ArrayList<KSEventType>();
116         result.add(KSEventFactory.AO_STATE_MODIFIED_EVENT_TYPE);
117         result.add(KSEventFactory.FO_RECOMPUTE_STATE_EVENT_TYPE);
118         return result;
119     }
120 }