1 package org.kuali.student.enrollment.class2.courseoffering.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.log4j.Logger;
21 import org.kuali.rice.kim.api.KimConstants;
22 import org.kuali.rice.krad.util.GlobalVariables;
23 import org.kuali.student.enrollment.class2.courseoffering.dto.ActivityOfferingWrapper;
24 import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingListSectionWrapper;
25 import org.kuali.student.enrollment.class2.courseoffering.form.CourseOfferingManagementForm;
26 import org.kuali.student.r2.common.dto.ContextInfo;
27 import org.kuali.student.r2.common.util.ContextUtils;
28 import org.kuali.student.r2.common.util.constants.CourseOfferingSetServiceConstants;
29 import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
30 import org.kuali.student.r2.core.acal.dto.KeyDateInfo;
31 import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
32 import org.kuali.student.r2.core.constants.AtpServiceConstants;
33
34 import java.util.Date;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38
39
40
41
42
43
44 public class CourseOfferingManagementToolbarUtil {
45
46 private static final Logger LOGGER = Logger.getLogger(CourseOfferingManagementToolbarUtil.class);
47
48 public static void processCoToolbarForUser(List<CourseOfferingListSectionWrapper> coListWrapperList, CourseOfferingManagementForm form){
49
50 long startTime = System.currentTimeMillis();
51
52 form.setEnableAddButton(false);
53 String socStateKey = form.getSocStateKey();
54 String socState = socStateKey==null?null:socStateKey.substring(socStateKey.lastIndexOf('.')+1);
55 String socSchedulingState = form.getSocSchedulingStateKey();
56
57 String principalId = GlobalVariables.getUserSession().getPerson().getPrincipalId();
58
59
60 Map<String,String> permissionDetails = new HashMap<String,String>();
61 Map<String,String> roleQualifications = new HashMap<String,String>();
62
63
64 roleQualifications.put("subjectArea", form.getSubjectCode());
65
66 permissionDetails.put("socState", socState);
67
68
69 Date termClassStartDate = form.getTermClassStartDate();
70 Date now = new Date();
71 if (termClassStartDate == null || now.before(termClassStartDate)) {
72 permissionDetails.put("termClassStartDateLater", "true");
73 }
74 permissionDetails.put(KimConstants.AttributeConstants.VIEW_ID, form.getViewId());
75
76
77 if (checkBzLogicForCOButtons(socStateKey, socSchedulingState, "", "addCO")) {
78
79 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, "addCO");
80 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
81 form.setEnableAddButton(true);
82 }
83 }
84
85
86 if (coListWrapperList != null && !coListWrapperList.isEmpty()) {
87 for (CourseOfferingListSectionWrapper coListWrapper : coListWrapperList) {
88 String coStateKey = coListWrapper.getCourseOfferingStateKey();
89 String coState = coStateKey.substring(coStateKey.lastIndexOf('.') + 1);
90
91 permissionDetails.put("coState", coState);
92 roleQualifications.put("offeringAdminOrgId", coListWrapper.getAdminOrg());
93
94
95 coListWrapper.setEnableCopyCOActionLink(false);
96 coListWrapper.setEnableEditCOActionLink(false);
97 coListWrapper.setEnableApproveButton(false);
98 coListWrapper.setEnableReinstateButton(false);
99 coListWrapper.setEnableSuspendButton(false);
100 coListWrapper.setEnableCancelButton(false);
101 coListWrapper.setEnableDeleteButton(false);
102
103
104 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, "copyCOonManageCOsPage");
105 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
106 coListWrapper.setEnableCopyCOActionLink(true);
107 }
108
109 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, "editCO");
110 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
111 coListWrapper.setEnableEditCOActionLink(true);
112 }
113
114
115
116 if (checkBzLogicForCOButtons(socStateKey, socSchedulingState, coStateKey, "approveCO")) {
117
118 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, "approveCO");
119 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
120 coListWrapper.setEnableApproveButton(true);
121 }
122 }
123
124
125 if (checkBzLogicForCOButtons(socStateKey, socSchedulingState, coStateKey, "deleteCO")) {
126
127 if(coStateKey.equals(LuiServiceConstants.LUI_CO_STATE_PLANNED_KEY)) {
128 ContextInfo contextInfo = ContextUtils.createDefaultContextInfo();
129 Date termRegStartDate = null;
130 try {
131
132 List<TypeInfo> regPeriods = CourseOfferingManagementUtil.getTypeService().getTypesForGroupType(AtpServiceConstants.MILESTONE_REGISTRATION_PERIOD_GROUP_TYPE_KEY, contextInfo);
133 List<KeyDateInfo> keyDateInfoList = CourseOfferingManagementUtil.getAcademicCalendarService().getKeyDatesForTerm(form.getTermInfo().getId(), contextInfo);
134 if (keyDateInfoList != null && keyDateInfoList.size() > 0) {
135 for (KeyDateInfo keyDateInfo : keyDateInfoList) {
136 for (TypeInfo regPeriod : regPeriods) {
137 if (keyDateInfo.getTypeKey().equalsIgnoreCase(regPeriod.getKey()) && keyDateInfo.getStartDate() != null) {
138 if (termRegStartDate == null || keyDateInfo.getStartDate().before(termRegStartDate)) {
139 termRegStartDate = keyDateInfo.getStartDate();
140 }
141 }
142 }
143 }
144 }
145 } catch (Exception e){
146 throw new RuntimeException("Exception in processCoToolbarForUser(): "+e);
147 }
148 Date nowReg = new Date();
149 if (termRegStartDate == null || nowReg.before(termRegStartDate)) {
150 permissionDetails.put("termRegStartDateLater", "true");
151 }
152 }
153
154 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, "deleteCO");
155 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
156 coListWrapper.setEnableDeleteButton(true);
157 }
158 }
159
160 }
161 }
162 LOGGER.info("******** CO Toolbar AuthZ Check *********" + (System.currentTimeMillis()-startTime) + "ms");
163 }
164
165 public static void processAoToolbarForUser(List<ActivityOfferingWrapper> activityWrapperList, CourseOfferingManagementForm form){
166 form.setEnableAddButton(false);
167 form.setEnableMoveAOButton(false);
168 form.setEnableAddClusterButton(false);
169 String principalId = GlobalVariables.getUserSession().getPerson().getPrincipalId();
170
171 String socStateKey = form.getSocStateKey();
172 String socState = socStateKey==null?null:socStateKey.substring(socStateKey.lastIndexOf('.')+1);
173 String socSchedulingState = form.getSocSchedulingStateKey();
174
175
176 Map<String,String> permissionDetails = new HashMap<String,String>();
177 Map<String,String> roleQualifications = new HashMap<String,String>();
178
179
180 roleQualifications.put("offeringAdminOrgId", form.getAdminOrg());
181 roleQualifications.put("subjectArea", form.getSubjectCode());
182
183 permissionDetails.put("socState", socState);
184
185
186 Date termClassStartDate = form.getTermClassStartDate();
187 Date now = new Date();
188 if (termClassStartDate == null || now.before(termClassStartDate)) {
189 permissionDetails.put("termClassStartDateLater", "true");
190 }
191
192 permissionDetails.put(KimConstants.AttributeConstants.VIEW_ID, form.getViewId());
193
194
195 if (checkBzLogicForAOButtons(socStateKey, socSchedulingState, "", ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_ADD)) {
196
197 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_ADD);
198 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
199 form.setEnableAddButton(true);
200 }
201
202 }
203
204
205 if (checkBzLogicForAOButtons(socStateKey, socSchedulingState, "", ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_ADD_CLUSTER)) {
206
207 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_ADD_CLUSTER);
208 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
209 form.setEnableAddClusterButton(true);
210 }
211 }
212
213 if (activityWrapperList != null && !activityWrapperList.isEmpty()) {
214 for (ActivityOfferingWrapper activityWrapper : activityWrapperList) {
215
216
217 String aoStateKey = activityWrapper.getAoInfo().getStateKey();
218 String aoState = aoStateKey.substring(aoStateKey.lastIndexOf('.') + 1);
219
220 permissionDetails.put("aoState", aoState);
221
222
223 activityWrapper.setEnableCopyAOActionLink(false);
224 activityWrapper.setEnableEditAOActionLink(false);
225 activityWrapper.setEnableMoveToButton(false);
226 activityWrapper.setEnableApproveButton(false);
227 activityWrapper.setEnableDraftButton(false);
228 activityWrapper.setEnableCancelButton(false);
229 activityWrapper.setEnableSuspendButton(false);
230 activityWrapper.setEnableReinstateButton(false);
231 activityWrapper.setEnableDeleteButton(false);
232
233
234 if (checkBzLogicForAOButtons(socStateKey, socSchedulingState, aoStateKey, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_APPROVE)) {
235
236 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_APPROVE);
237 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
238 activityWrapper.setEnableApproveButton(true);
239 }
240 }
241
242
243 if (checkBzLogicForAOButtons(socStateKey, socSchedulingState, "", ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_DELETE)) {
244
245 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_DELETE);
246 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
247 activityWrapper.setEnableDeleteButton(true);
248 }
249 }
250
251
252 if (checkBzLogicForAOButtons(socStateKey, socSchedulingState, aoStateKey, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_SET_DRAFT)) {
253
254 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_SET_DRAFT);
255 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
256 activityWrapper.setEnableDraftButton(true);
257 }
258 }
259
260
261 if (checkBzLogicForAOButtons(socStateKey, socSchedulingState, "", ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_MOVE)) {
262
263 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_MOVE);
264 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
265 activityWrapper.setEnableMoveToButton(true);
266 }
267 }
268
269
270 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, "copyAOonManageAOsPage");
271 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
272 activityWrapper.setEnableCopyAOActionLink(true);
273 }
274
275 String colocatedAO = "";
276 if (activityWrapper.isColocatedAO()) {
277 colocatedAO = "true";
278 }
279 permissionDetails.put("colocatedAO", colocatedAO);
280 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, "editAOonManageAOsPage");
281 if(CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails,roleQualifications)){
282 activityWrapper.setEnableEditAOActionLink(true);
283 }
284
285
286 if (!activityWrapper.isColocatedAO() && checkBzLogicForAOButtons(socStateKey, socSchedulingState, aoStateKey, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_CANCEL)) {
287 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_CANCEL);
288 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
289 activityWrapper.setEnableCancelButton(true);
290 }
291 }
292
293
294 if (checkBzLogicForAOButtons(socStateKey, socSchedulingState, aoStateKey, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_SUSPEND)) {
295 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_SUSPEND);
296 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
297 activityWrapper.setEnableSuspendButton(true);
298 }
299 }
300
301
302 if (checkBzLogicForAOButtons(socStateKey, socSchedulingState, aoStateKey, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_REINSTATE)) {
303 permissionDetails.put(KimConstants.AttributeConstants.ACTION_EVENT, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_REINSTATE);
304 if (CourseOfferingManagementUtil.getPermissionService().isAuthorizedByTemplate(principalId, "KS-ENR", KimConstants.PermissionTemplateNames.PERFORM_ACTION, permissionDetails, roleQualifications)) {
305 activityWrapper.setEnableReinstateButton(true);
306 }
307 }
308 }
309 }
310
311 }
312
313 private static boolean checkBzLogicForCOButtons(String socState, String socSchedulingState, String coStateKey, String actionEvent){
314 boolean bzEnableButton = false;
315 if(StringUtils.equals(actionEvent, "approveCO")) {
316 if((StringUtils.equals(coStateKey, LuiServiceConstants.LUI_CO_STATE_DRAFT_KEY) || StringUtils.equals(coStateKey, LuiServiceConstants.LUI_CO_STATE_PLANNED_KEY)) &&
317 (StringUtils.equals(socState, CourseOfferingSetServiceConstants.OPEN_SOC_STATE_KEY) ||
318 StringUtils.equals(socState, CourseOfferingSetServiceConstants.DRAFT_SOC_STATE_KEY) ||
319 isSOCLockedAndMSEInProgress(socState, socSchedulingState))){
320 bzEnableButton = true;
321 }
322 } else if (StringUtils.equals(actionEvent, "addCO")) {
323 bzEnableButton = true;
324 } else if (StringUtils.equals(actionEvent, "deleteCO")) {
325 bzEnableButton = true;
326 }
327
328 return bzEnableButton;
329 }
330
331 private static boolean checkBzLogicForAOButtons(String socState, String socSchedulingState, String aoStateKey, String actionEvent){
332 boolean bzEnableButton = false;
333 if(StringUtils.equals(actionEvent, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_APPROVE)) {
334 if(StringUtils.equals(aoStateKey, LuiServiceConstants.LUI_AO_STATE_DRAFT_KEY) &&
335 (StringUtils.equals(socState, CourseOfferingSetServiceConstants.OPEN_SOC_STATE_KEY) ||
336 StringUtils.equals(socState, CourseOfferingSetServiceConstants.DRAFT_SOC_STATE_KEY) ||
337 isSOCLockedAndMSEInProgress(socState, socSchedulingState))){
338 bzEnableButton = true;
339 }
340 } else if (StringUtils.equals(actionEvent, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_ADD)) {
341 bzEnableButton = true;
342 } else if (StringUtils.equals(actionEvent, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_SET_DRAFT)) {
343 if(StringUtils.equals(aoStateKey, LuiServiceConstants.LUI_AO_STATE_APPROVED_KEY)){
344 bzEnableButton = true;
345 }
346 } else if (StringUtils.equals(actionEvent, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_DELETE)) {
347 bzEnableButton = true;
348 } else if (StringUtils.equals(actionEvent, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_ADD_CLUSTER)) {
349 bzEnableButton = true;
350 } else if (StringUtils.equals(actionEvent, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_MOVE)) {
351 bzEnableButton = true;
352 } else if(StringUtils.equals(actionEvent, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_CANCEL)){
353 if(StringUtils.equals(aoStateKey, LuiServiceConstants.LUI_AO_STATE_APPROVED_KEY) ||
354 StringUtils.equals(aoStateKey, LuiServiceConstants.LUI_AO_STATE_DRAFT_KEY) ||
355 StringUtils.equals(aoStateKey, LuiServiceConstants.LUI_AO_STATE_OFFERED_KEY) ||
356 StringUtils.equals(aoStateKey, LuiServiceConstants.LUI_AO_STATE_SUSPENDED_KEY)){
357 bzEnableButton = true;
358 }
359 } else if(StringUtils.equals(actionEvent, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_SUSPEND)){
360 if((StringUtils.equals(aoStateKey, LuiServiceConstants.LUI_AO_STATE_APPROVED_KEY) ||
361 StringUtils.equals(aoStateKey, LuiServiceConstants.LUI_AO_STATE_DRAFT_KEY) ||
362 StringUtils.equals(aoStateKey, LuiServiceConstants.LUI_AO_STATE_OFFERED_KEY)) &&
363 !StringUtils.equals(socState, CourseOfferingSetServiceConstants.OPEN_SOC_STATE_KEY) &&
364 !StringUtils.equals(socState, CourseOfferingSetServiceConstants.DRAFT_SOC_STATE_KEY)){
365 bzEnableButton = true;
366 }
367 } else if(StringUtils.equals(actionEvent, ActivityOfferingConstants.ACTIVITYOFFERING_ACTION_REINSTATE)){
368 if(StringUtils.equals(aoStateKey, LuiServiceConstants.LUI_AO_STATE_CANCELED_KEY) ||
369 StringUtils.equals(aoStateKey, LuiServiceConstants.LUI_AO_STATE_SUSPENDED_KEY)){
370 bzEnableButton = true;
371 }
372 }
373
374 return bzEnableButton;
375 }
376
377 private static boolean isInProgress(String socState, String socSchedulingState){
378 boolean inProgress = false;
379 if(StringUtils.equals(socState, CourseOfferingSetServiceConstants.PUBLISHING_SOC_STATE_KEY) ||
380 (StringUtils.equals(socState, CourseOfferingSetServiceConstants.LOCKED_SOC_STATE_KEY) &&
381 StringUtils.equals(socSchedulingState, CourseOfferingSetServiceConstants.SOC_SCHEDULING_STATE_IN_PROGRESS))){
382 inProgress = true;
383 }
384 return inProgress;
385 }
386
387 private static boolean isSOCLockedAndMSEInProgress(String socState, String socSchedulingState){
388 boolean socLockedAndMSEInProgress = false;
389 if(StringUtils.equals(socState, CourseOfferingSetServiceConstants.LOCKED_SOC_STATE_KEY) &&
390 !StringUtils.equals(socSchedulingState, CourseOfferingSetServiceConstants.SOC_SCHEDULING_STATE_IN_PROGRESS)){
391 socLockedAndMSEInProgress = true;
392 }
393 return socLockedAndMSEInProgress;
394 }
395 }