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/24/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.poc.eventproc.api.KSHandler;
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.event.KSEventFactory;
25  import org.kuali.student.poc.eventproc.event.KSHandlerResult;
26  import org.kuali.student.poc.eventproc.event.KSEventType;
27  import org.kuali.student.poc.eventproc.event.subclass.event.KSAOStateModifiedEvent;
28  import org.kuali.student.r2.common.dto.ContextInfo;
29  import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
30  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
31  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
32  import org.kuali.student.r2.common.exceptions.MissingParameterException;
33  import org.kuali.student.r2.common.exceptions.OperationFailedException;
34  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
35  import org.kuali.student.r2.common.exceptions.ReadOnlyException;
36  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
37  import org.kuali.student.r2.common.util.constants.CourseOfferingServiceConstants;
38  import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
39  import org.kuali.student.r2.core.scheduling.dto.ScheduleRequestInfo;
40  import org.kuali.student.r2.core.scheduling.dto.ScheduleRequestSetInfo;
41  
42  import java.util.ArrayList;
43  import java.util.List;
44  
45  /**
46   * Listens for state modified event to cancel/draft and drops ADLs
47   *
48   * @author Kuali Student Team
49   */
50  public class ActivityOfferingDropADLsHandler implements KSHandler {
51      public static final Logger LOGGER = Logger.getLogger(ActivityOfferingDropADLsHandler.class);
52      KSInternalEventProcessor processor;
53  
54      public ActivityOfferingDropADLsHandler(KSInternalEventProcessor processor) {
55          this.processor = processor;
56      }
57  
58      @Override
59      public boolean handlesEvent(KSEvent event) {
60          if (!(event instanceof KSAOStateModifiedEvent)) {
61              return false;
62          }
63          KSAOStateModifiedEvent stateModifiedEvent = (KSAOStateModifiedEvent) event;
64          // Must be ao state modified event--fetch to state
65          String toState = stateModifiedEvent.getToState();
66          // Only handles if toState is draft/canceled
67          boolean result = LuiServiceConstants.LUI_AO_STATE_CANCELED_KEY.equals(toState) ||
68                  LuiServiceConstants.LUI_AO_STATE_DRAFT_KEY.equals(toState);
69          return result;
70      }
71  
72      @Override
73      public KSHandlerResult processEvent(KSEvent event, ContextInfo context)
74              throws PermissionDeniedException, MissingParameterException, InvalidParameterException, OperationFailedException, DoesNotExistException, ReadOnlyException, DataValidationErrorException, VersionMismatchException {
75          if (!handlesEvent(event)) {
76              return new KSHandlerResult(KSHandlerResult.FAIL_HANDLER_WONT_PROCESS, ActivityOfferingDropADLsHandler.class);
77          }
78          KSAOStateModifiedEvent stateModifiedEvent = (KSAOStateModifiedEvent) event;
79          String aoId = stateModifiedEvent.getAoId();
80          ActivityOfferingInfo aoInfo = processor.getCoService().getActivityOffering(aoId, context);
81          List<ScheduleRequestSetInfo> srsList =
82                  processor.getSchedulingService().getScheduleRequestSetsByRefObject(CourseOfferingServiceConstants.REF_OBJECT_URI_ACTIVITY_OFFERING, aoId, context);
83          ScheduleRequestSetInfo srs = null;
84          if (srsList.size() == 1) {
85              // Not able to handle multiple SRS yet (partial colocation, etc)
86              srs = srsList.get(0);
87          } else {
88              throw new OperationFailedException("Expecting SRS list to be size 1: actual size = " + srsList.size());
89          }
90          if (srs.getRefObjectIds().size() > 1) {
91              // This AO is co-located, so just remove the schedule IDs, but do not delete them
92              if (aoInfo.getScheduleIds() == null || aoInfo.getScheduleIds().isEmpty()) {
93                  LOGGER.info("AO has no ADLs to remove");
94              } else {
95                  // Empty it out
96                  LOGGER.info("Removing ADLs from AO");
97                  aoInfo.setScheduleIds(new ArrayList<String>());
98                  processor.getCoService().updateActivityOffering(aoInfo.getId(), aoInfo, context);
99              }
100         } else if (srs.getRefObjectIds().size() == 1) {
101             for (String scheduleId: aoInfo.getScheduleIds()) {
102                 processor.getSchedulingService().deleteSchedule(scheduleId, context);
103             }
104             // Empty it out
105             LOGGER.info("Removing ADLs from AO");
106             aoInfo.setScheduleIds(new ArrayList<String>());
107             processor.getCoService().updateActivityOffering(aoInfo.getId(), aoInfo, context);
108             List<ScheduleRequestInfo> requests =
109                     processor.getSchedulingService().getScheduleRequestsByScheduleRequestSet(srs.getId(), context);
110             for (ScheduleRequestInfo request: requests) {
111                 // Clear out the schedule IDs from the schedule (should this really be done by updateSR?) --cclin
112                 request.setScheduleId(null);
113                 processor.getSchedulingService().updateScheduleRequest(request.getId(), request, context);
114             }
115         }
116         return new KSHandlerResult(KSHandlerResult.SUCCESS, ActivityOfferingDropADLsHandler.class);
117     }
118 
119     @Override
120     public String getName() {
121         return ActivityOfferingDropADLsHandler.class.getSimpleName();
122     }
123 
124     @Override
125     public void setEventProcessor(KSInternalEventProcessor processor) {
126         this.processor = processor;
127     }
128 
129     @Override
130     public List<KSEventType> getEventTypesHandled() {
131         List<KSEventType> result = new ArrayList<KSEventType>();
132         result.add(KSEventFactory.AO_STATE_MODIFIED_EVENT_TYPE);
133         return result;
134     }
135 }