1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
47
48
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
65 String toState = stateModifiedEvent.getToState();
66
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
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
92 if (aoInfo.getScheduleIds() == null || aoInfo.getScheduleIds().isEmpty()) {
93 LOGGER.info("AO has no ADLs to remove");
94 } else {
95
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
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
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 }