1 package org.kuali.ole.select.controller;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.ingest.pojo.MatchBo;
6 import org.kuali.ole.select.bo.*;
7 import org.kuali.ole.service.OleLicenseRequestService;
8 import org.kuali.ole.service.impl.OleLicenseRequestServiceImpl;
9 import org.kuali.rice.core.api.config.property.ConfigContext;
10 import org.kuali.rice.core.api.config.property.ConfigurationService;
11 import org.kuali.rice.core.api.util.RiceKeyConstants;
12 import org.kuali.rice.kew.actionitem.ActionItem;
13 import org.kuali.rice.kew.actionlist.service.ActionListService;
14 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
15 import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
16 import org.kuali.rice.kew.api.KewApiConstants;
17 import org.kuali.rice.kew.api.action.ActionRequest;
18 import org.kuali.rice.kew.service.KEWServiceLocator;
19 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
20 import org.kuali.rice.krad.service.KRADServiceLocator;
21 import org.kuali.rice.krad.uif.UifConstants;
22 import org.kuali.rice.krad.uif.UifParameters;
23 import org.kuali.rice.krad.uif.container.CollectionGroup;
24 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
25 import org.kuali.rice.krad.util.GlobalVariables;
26 import org.kuali.rice.krad.util.KRADConstants;
27 import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
28 import org.kuali.rice.krad.web.form.DocumentFormBase;
29 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
30 import org.kuali.rice.krad.web.form.UifFormBase;
31 import org.kuali.rice.krms.api.KrmsApiServiceLocator;
32 import org.kuali.rice.krms.api.engine.*;
33 import org.kuali.rice.krms.impl.repository.AgendaBo;
34 import org.springframework.stereotype.Controller;
35 import org.springframework.util.FileCopyUtils;
36 import org.springframework.validation.BindingResult;
37 import org.springframework.web.bind.annotation.ModelAttribute;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.multipart.MultipartFile;
40 import org.springframework.web.servlet.ModelAndView;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import java.io.*;
45 import java.sql.Timestamp;
46 import java.util.*;
47
48
49
50
51 @Controller
52 @RequestMapping(value = "/oleLicenseRequest")
53 public class OleLicenseRequestController extends MaintenanceDocumentController {
54
55 private OleLicenseRequestService oleLicenseRequestService;
56 private final String RULE_EVALUATED = "Rule Evaluated";
57 private final String ROUTED_EXTERNAL = "Routed External";
58 protected static final String REDIRECT_PREFIX = "redirect:";
59
60 private OleLicenseRequestService getOleLicenseRequestService() {
61 if (oleLicenseRequestService == null) {
62 oleLicenseRequestService = new OleLicenseRequestServiceImpl();
63 }
64 return oleLicenseRequestService;
65 }
66
67
68
69
70
71
72
73
74
75
76 @RequestMapping(params = "methodToCall=addEventLogLine")
77 public ModelAndView addEventLogLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
78 HttpServletRequest request, HttpServletResponse response) {
79 MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
80 String selectedCollectionPath = form.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
81 CollectionGroup collectionGroup = form.getPostedView().getViewIndex().getCollectionGroupByPath(
82 selectedCollectionPath);
83 String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
84 Object eventObject = ObjectPropertyUtils.getPropertyValue(uifForm, addLinePath);
85 OleEventLogBo oleEventLogBo = (OleEventLogBo) eventObject;
86 oleEventLogBo.setEventType(OLEConstants.OleLicenseRequest.USER);
87 oleEventLogBo.setCurrentTimeStamp();
88 return addLine(uifForm, result, request, response);
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102 @Override
103 @RequestMapping(params = "methodToCall=route")
104 public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
105 HttpServletRequest request, HttpServletResponse response) {
106
107 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
108 MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
109 OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo) document.getNewMaintainableObject().getDataObject();
110 OleLicenseRequestBo oldLicenseRequestBo = (OleLicenseRequestBo) document.getOldMaintainableObject().getDataObject();
111 if (!validateFields(oleLicenseRequestBo)) {
112 return getUIFModelAndView(form);
113 }
114 if (oldLicenseRequestBo != null && oleLicenseRequestBo.getLocationId() != null &&
115 oleLicenseRequestBo.getLocationId() != OLEConstants.OleLicenseRequest.LICENSE_INITIAL_LOCATON &&
116 !oleLicenseRequestBo.getLocationId().equals(oldLicenseRequestBo.getLocationId())) {
117 updateEventLogForLocation(oleLicenseRequestBo, "location", null);
118 }
119 Map<String, String> licenses = getLicenseWorkFlows();
120 if (licenses.containsKey(oleLicenseRequestBo.getLicenseRequestWorkflowTypeCode())) {
121 oleLicenseRequestBo.setLicenseRequestStatusCode(licenses.get(oleLicenseRequestBo.getLicenseRequestWorkflowTypeCode()));
122 document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(
123 getLicenseRequestName(licenses.get(oleLicenseRequestBo.getLicenseRequestWorkflowTypeCode())));
124 performWorkflowAction(form, UifConstants.WorkflowAction.SAVE, true);
125 assignActionRequests(document.getDocumentHeader().getDocumentNumber(), oleLicenseRequestBo.getAssignee());
126 return close(form, result, request, response);
127 }
128 oleLicenseRequestBo.setLicenseRequestStatusCode(OLEConstants.OleLicenseRequest.LICENSE_NEEDED);
129 document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(
130 getLicenseRequestName(OLEConstants.OleLicenseRequest.LICENSE_NEEDED));
131 processAgreementDocument(oleLicenseRequestBo);
132 String licenseRequestWorkflowTypeCode = oleLicenseRequestBo.getLicenseRequestWorkflowTypeCode();
133 if (licenseRequestWorkflowTypeCode.equalsIgnoreCase(OLEConstants.OleLicenseRequest.SIGNATORY_ONLY)) {
134 oleLicenseRequestBo.setLicenseRequestStatusCode(OLEConstants.OleLicenseRequest.PENDING_SIGNATURE);
135 document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(
136 getLicenseRequestName(oleLicenseRequestBo.getLicenseRequestStatusCode()));
137 } else if (licenseRequestWorkflowTypeCode.equalsIgnoreCase(OLEConstants.OleLicenseRequest.REVIEW_ONLY)) {
138 oleLicenseRequestBo.setLicenseRequestStatusCode(OLEConstants.OleLicenseRequest.PENDING_REVIEW);
139 document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(
140 getLicenseRequestName(oleLicenseRequestBo.getLicenseRequestStatusCode()));
141 } else if (licenseRequestWorkflowTypeCode.equalsIgnoreCase(OLEConstants.OleLicenseRequest.APPROVE_ONLY)) {
142 oleLicenseRequestBo.setLicenseRequestStatusCode(OLEConstants.OleLicenseRequest.PENDING_APPROVAL);
143 document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(
144 getLicenseRequestName(oleLicenseRequestBo.getLicenseRequestStatusCode()));
145 } else if (licenseRequestWorkflowTypeCode.equalsIgnoreCase(OLEConstants.OleLicenseRequest.FULL_APPROVAL)
146 || licenseRequestWorkflowTypeCode.equalsIgnoreCase(OLEConstants.OleLicenseRequest.ADDENDUM)) {
147 oleLicenseRequestBo.setLicenseRequestStatusCode(OLEConstants.OleLicenseRequest.PENDING_REVIEW);
148 document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(
149 getLicenseRequestName(oleLicenseRequestBo.getLicenseRequestStatusCode()));
150 }
151
152 boolean valid = false;
153 EngineResults engineResult = executeEngineResults(oleLicenseRequestBo);
154 List<ResultEvent> allResults = engineResult.getAllResults();
155 for (Iterator<ResultEvent> resultEventIterator = allResults.iterator(); resultEventIterator.hasNext(); ) {
156 ResultEvent resultEvent = resultEventIterator.next();
157 if (resultEvent.getType().equals(RULE_EVALUATED)) {
158 valid |= resultEvent.getResult();
159 }
160 }
161 if (!valid) {
162 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OleLicenseRequest.LICENSE_RULE_VAILDATIONS);
163 }
164
165 String currentUser = GlobalVariables.getUserSession().getPrincipalName();
166 if (currentUser.equalsIgnoreCase(oleLicenseRequestBo.getAssignee()) && oleLicenseRequestBo.getEventLogs().size() < 1) {
167 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OleLicenseRequest.LICENSE_EVENT_LOG_CHECK);
168 }
169 if (oleLicenseRequestBo.getLinkedAgreement() != null & oleLicenseRequestBo.getAgreementId() == null) {
170 String agreementcontent = getOleLicenseRequestService().getAgreementContent(oleLicenseRequestBo.getLinkedAgreement());
171 if (agreementcontent != null) {
172 oleLicenseRequestBo.setAgreementId(getOleLicenseRequestService().ingestAgreementContent(agreementcontent));
173 oleLicenseRequestBo.setLinkedAgreement("");
174 } else {
175 LOG.error("Error while retrieving the onixpl content");
176 }
177 }
178
179
180 return super.route(form, result, request, response);
181 }
182
183
184
185
186
187
188
189 private boolean validateFields(OleLicenseRequestBo oleLicenseRequestBo) {
190 boolean isFieldsValid = true;
191 LOG.debug("Inside ValidateFields method");
192 if (oleLicenseRequestBo.getLicenseRequestWorkflowTypeCode() == null ||
193 oleLicenseRequestBo.getLicenseRequestWorkflowTypeCode().isEmpty()) {
194 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS,
195 OLEConstants.OleLicenseRequest.ERROR_REQUIRED, OLEConstants.OleLicenseRequest.INITIATE_LR_WORKFLOW);
196 isFieldsValid &= false;
197 }
198 if (oleLicenseRequestBo.getAssignee() == null || oleLicenseRequestBo.getAssignee().isEmpty()) {
199 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS,
200 OLEConstants.OleLicenseRequest.ERROR_REQUIRED, OLEConstants.OleLicenseRequest.DO_ASSIGNEE);
201 isFieldsValid &= false;
202 }
203 if (oleLicenseRequestBo.getLicenseRequestTypeId() == null ||
204 oleLicenseRequestBo.getLicenseRequestTypeId().isEmpty()) {
205 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OleLicenseRequest.ERROR_REQUIRED,
206 OLEConstants.OleLicenseRequest.DO_LICENSE_REQUEST_TYPE);
207 isFieldsValid &= false;
208 }
209 if (oleLicenseRequestBo.getAgreementMethodId() == null ||
210 oleLicenseRequestBo.getAgreementMethodId().isEmpty()) {
211 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OleLicenseRequest.ERROR_REQUIRED,
212 OLEConstants.OleLicenseRequest.DO_AGR_MTHD);
213 isFieldsValid &= false;
214 }
215 return isFieldsValid;
216 }
217
218
219
220
221 private String getLicenseRequestName(String licenseRequestCode) {
222 String licenseRequestStatusName = "";
223 Map criteria = new HashMap();
224 criteria.put("code", licenseRequestCode);
225 OleLicenseRequestStatus licenseRequestStatus = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleLicenseRequestStatus.class,
226 criteria);
227 licenseRequestStatusName = licenseRequestStatus.getName();
228 return licenseRequestStatusName;
229 }
230
231
232
233
234 private void assignActionRequests(String routeHeaderId, String selectedUserId) {
235 LOG.debug("Inside assignActionRequests of LicenseRequestDocument");
236 Timestamp currentTime = new Timestamp(System.currentTimeMillis());
237 ActionListService actionListSrv = KEWServiceLocator.getActionListService();
238 ActionRequestService actionReqSrv = KEWServiceLocator.getActionRequestService();
239 List<ActionRequestValue> actionReqValues = actionReqSrv.findAllPendingRequests(routeHeaderId);
240 for (ActionRequestValue actionRequest : actionReqValues) {
241 List<ActionItem> actionItems = actionRequest.getActionItems();
242 for (ActionItem actionItem : actionItems) {
243 actionItem.setPrincipalId(selectedUserId);
244 actionItem.setDateAssigned(currentTime);
245 actionListSrv.saveActionItem(actionItem);
246 }
247 actionRequest.setPrincipalId(selectedUserId);
248 actionRequest.setCreateDate(currentTime);
249
250 actionReqSrv.saveActionRequest(actionRequest);
251 }
252 LOG.debug("Leaving assignActionRequests of LicenseRequestDocument");
253 }
254
255
256
257
258
259
260
261
262 @RequestMapping(params = "methodToCall=approve")
263 public ModelAndView approve(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
264 HttpServletRequest request, HttpServletResponse response) throws Exception {
265 MaintenanceDocumentForm oldMaintenanceForm = (MaintenanceDocumentForm) form;
266 MaintenanceDocument oldDocument = (MaintenanceDocument) oldMaintenanceForm.getDocument();
267 OleLicenseRequestBo newLicenseRequestBo = (OleLicenseRequestBo) oldDocument.getNewMaintainableObject().getDataObject();
268 OleLicenseRequestBo oldLicenseRequestBo = (OleLicenseRequestBo) oldDocument.getOldMaintainableObject().getDataObject();
269 if (!validateFields(newLicenseRequestBo)) {
270 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OleLicenseRequest.ERROR_REQUIRED);
271 return getUIFModelAndView(form);
272 }
273 if (oldLicenseRequestBo.getLocationId() != null && newLicenseRequestBo.getLocationId() != null &&
274 !newLicenseRequestBo.getLocationId().equals(oldLicenseRequestBo.getLocationId())) {
275 updateEventLogForLocation(newLicenseRequestBo, "location", null);
276 }
277
278 processAgreementDocument(newLicenseRequestBo);
279
280 List<ActionRequest> oldActionRequests = oldDocument.getDocumentHeader().getWorkflowDocument().getDocumentDetail().getActionRequests();
281 String oldRoleName = "";
282 for (ActionRequest oldActionRequest : oldActionRequests) {
283 if (oldActionRequest.getStatus().getCode().equalsIgnoreCase("A")) {
284 oldRoleName = oldActionRequest.getQualifiedRoleNameLabel() != null ? oldActionRequest.getQualifiedRoleNameLabel() : "No Role";
285 }
286 }
287 boolean validRule = false;
288 EngineResults engineResult = executeEngineResults((OleLicenseRequestBo) oldDocument.getNewMaintainableObject().getDataObject());
289 List<ResultEvent> allResults = engineResult.getAllResults();
290 for (Iterator<ResultEvent> resultEventIterator = allResults.iterator(); resultEventIterator.hasNext(); ) {
291 ResultEvent resultEvent = resultEventIterator.next();
292 if (resultEvent.getType().equals(RULE_EVALUATED)) {
293 validRule |= resultEvent.getResult();
294 }
295 }
296 if (!validRule) {
297 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OleLicenseRequest.LICENSE_RULE_VAILDATIONS);
298 }
299 String currentUser = GlobalVariables.getUserSession().getPrincipalName();
300 boolean eventLogCheck = currentUser.equalsIgnoreCase(newLicenseRequestBo.getAssignee()) && newLicenseRequestBo.getEventLogs().size() < 1;
301 if (eventLogCheck) {
302 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OleLicenseRequest.LICENSE_EVENT_LOG_CHECK);
303 }
304 if (!validRule || eventLogCheck) {
305 return getUIFModelAndView(form);
306 }
307
308 performWorkflowAction(form, UifConstants.WorkflowAction.APPROVE, true);
309 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
310 MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
311 List<ActionRequest> actionRequests = document.getDocumentHeader().getWorkflowDocument().getDocumentDetail().getActionRequests();
312 OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo) document.getNewMaintainableObject().getDataObject();
313 for (ActionRequest actionRequest : actionRequests) {
314 String roleName = actionRequest != null && actionRequest.getQualifiedRoleNameLabel() != null ? actionRequest.getQualifiedRoleNameLabel() : "No Role";
315 if (roleName.equalsIgnoreCase(oldRoleName)) {
316 if (roleName.equalsIgnoreCase(OLEConstants.OleLicenseRequest.SIGNATORY_ROLE_NM) && actionRequest.getStatus().getCode().equalsIgnoreCase("D")) {
317 oleLicenseRequestBo.setLicenseRequestStatusCode(OLEConstants.OleLicenseRequest.SIGNATORY_COMPLETE);
318 document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(
319 getLicenseRequestName(oleLicenseRequestBo.getLicenseRequestStatusCode()));
320 form.setAnnotation(OLEConstants.OleLicenseRequest.SIGNATORY_ROLE_NM);
321 getDocumentService().saveDocument(document);
322 } else if (roleName.equalsIgnoreCase(OLEConstants.OleLicenseRequest.LICENSE_MNGR_ROLE_NM) && actionRequest.getStatus().getCode().equalsIgnoreCase("D")) {
323 if (newLicenseRequestBo.getLicenseRequestWorkflowTypeCode().equalsIgnoreCase(OLEConstants.OleLicenseRequest.SIGNATORY_ONLY)) {
324 oleLicenseRequestBo.setLicenseRequestStatusCode(OLEConstants.OleLicenseRequest.PENDING_SIGNATURE);
325 document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(
326 getLicenseRequestName(oleLicenseRequestBo.getLicenseRequestStatusCode()));
327 } else if (newLicenseRequestBo.getLicenseRequestWorkflowTypeCode().equalsIgnoreCase(OLEConstants.OleLicenseRequest.REVIEW_ONLY)) {
328 oleLicenseRequestBo.setLicenseRequestStatusCode(OLEConstants.OleLicenseRequest.PENDING_REVIEW);
329 document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(
330 getLicenseRequestName(oleLicenseRequestBo.getLicenseRequestStatusCode()));
331 } else if (newLicenseRequestBo.getLicenseRequestWorkflowTypeCode().equalsIgnoreCase(OLEConstants.OleLicenseRequest.APPROVE_ONLY)) {
332 oleLicenseRequestBo.setLicenseRequestStatusCode(OLEConstants.OleLicenseRequest.PENDING_APPROVAL);
333 document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(
334 getLicenseRequestName(oleLicenseRequestBo.getLicenseRequestStatusCode()));
335 } else {
336 oleLicenseRequestBo.setLicenseRequestStatusCode(OLEConstants.OleLicenseRequest.UNIVERSITY_COMPLETE);
337 document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(
338 getLicenseRequestName(oleLicenseRequestBo.getLicenseRequestStatusCode()));
339 }
340 form.setAnnotation(OLEConstants.OleLicenseRequest.LICENSE_MNGR_ROLE_NM);
341 getDocumentService().saveDocument(document);
342 } else if (roleName.equalsIgnoreCase(OLEConstants.OleLicenseRequest.REVIEWER_ROLE_NM) && actionRequest.getStatus().getCode().equalsIgnoreCase("D")) {
343 oleLicenseRequestBo.setLicenseRequestStatusCode(
344 newLicenseRequestBo.getLicenseRequestWorkflowTypeCode().equalsIgnoreCase(OLEConstants.OleLicenseRequest.FULL_APPROVAL) ?
345 OLEConstants.OleLicenseRequest.PENDING_UNIVERSITY : OLEConstants.OleLicenseRequest.REVIEW_COMPLETE);
346 document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(
347 getLicenseRequestName(oleLicenseRequestBo.getLicenseRequestStatusCode()));
348 form.setAnnotation(OLEConstants.OleLicenseRequest.REVIEWER_ROLE_NM);
349 getDocumentService().saveDocument(document);
350 } else if (roleName.equalsIgnoreCase(OLEConstants.OleLicenseRequest.APPROVER_ROLE_NM) && actionRequest.getStatus().getCode().equalsIgnoreCase("D")) {
351 oleLicenseRequestBo.setLicenseRequestStatusCode(OLEConstants.OleLicenseRequest.LICENSE_COMPLETE);
352 document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(
353 getLicenseRequestName(oleLicenseRequestBo.getLicenseRequestStatusCode()));
354 form.setAnnotation(OLEConstants.OleLicenseRequest.APPROVER_ROLE_NM);
355 getDocumentService().saveDocument(document);
356
357
358
359
360
361
362
363 }
364 }
365 }
366 return returnToPrevious(form);
367 }
368
369
370
371
372
373
374
375 @RequestMapping(params = "methodToCall=disapprove")
376 public ModelAndView disapprove(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
377 HttpServletRequest request, HttpServletResponse response) throws Exception {
378 LOG.debug("Inside disapprove method");
379 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
380 MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
381 OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo) document.getNewMaintainableObject().getDataObject();
382 if (oleLicenseRequestBo.getEventLogs().size() < 1) {
383 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OleLicenseRequest.LICENSE_DISAPPROVE_VALIDATIONS);
384 return getUIFModelAndView(form);
385 }
386 oleLicenseRequestBo.setLicenseRequestStatusCode(OLEConstants.OleLicenseRequest.NEGOTIATION_FAILED);
387 document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(
388 getLicenseRequestName(oleLicenseRequestBo.getLicenseRequestStatusCode()));
389 getDocumentService().saveDocument(document);
390 performWorkflowAction(form, UifConstants.WorkflowAction.DISAPPROVE, true);
391
392
393
394
395
396
397
398
399 updateEventLogForLocation(oleLicenseRequestBo, "system", "License Request Disapproved");
400 performWorkflowAction(form, UifConstants.WorkflowAction.SAVE, true);
401 return returnToPrevious(form);
402 }
403
404 public String getURL() {
405 return ConfigContext.getCurrentContextConfig().getProperty("oleRequisitionWebService.url");
406 }
407
408
409
410
411
412
413
414 private EngineResults executeEngineResults(OleLicenseRequestBo oleLicenseRequestBo) {
415 Engine engine = KrmsApiServiceLocator.getEngine();
416 EngineResults engineResult = null;
417 HashMap<String, Object> agendaValue = new HashMap<String, Object>();
418 agendaValue.put("nm", OLEConstants.OleLicenseRequest.LICENSE_AGENDA_NM);
419 List<AgendaBo> agendaBos = (List<AgendaBo>) KRADServiceLocator.getBusinessObjectService().findMatching(AgendaBo.class, agendaValue);
420 if (agendaBos != null && agendaBos.size() > 0) {
421 AgendaBo agendaBo = agendaBos.get(0);
422 HashMap<String, String> map = new HashMap<String, String>();
423 map.put("AGENDA_NAME", agendaBo.getName());
424 List<MatchBo> matchBos = (List<MatchBo>) KRADServiceLocator.getBusinessObjectService().findMatching(MatchBo.class, map);
425
426 SelectionCriteria selectionCriteria =
427 SelectionCriteria.createCriteria(null, getSelectionContext(agendaBo.getContext().getName()), getAgendaContext(OLEConstants.OleLicenseRequest.LICENSE_AGENDA_NM));
428
429 ExecutionOptions executionOptions = new ExecutionOptions();
430 executionOptions.setFlag(ExecutionFlag.LOG_EXECUTION, true);
431
432 Facts.Builder factBuilder = Facts.Builder.create();
433
434 String licenseType = getLicenseType(oleLicenseRequestBo.getLicenseRequestTypeId());
435 String workflowName = getWorkFlowName(oleLicenseRequestBo.getLicenseRequestWorkflowTypeCode());
436 String agreementMethod = getAgreementMethod(oleLicenseRequestBo.getAgreementMethodId());
437 HashMap<String, Object> termValues = new HashMap<String, Object>();
438
439 termValues.put("licenseType", licenseType);
440 termValues.put("agreementMethod", agreementMethod);
441 termValues.put("workflowName", workflowName);
442 for (Iterator<MatchBo> matchBoIterator = matchBos.iterator(); matchBoIterator.hasNext(); ) {
443 MatchBo matchBo = matchBoIterator.next();
444 factBuilder.addFact(matchBo.getTermName(), termValues.get((matchBo.getTermName())));
445 }
446 engineResult = engine.execute(selectionCriteria, factBuilder.build(), executionOptions);
447 }
448 return engineResult;
449 }
450
451 private String getAgreementMethod(String agreementMethodId) {
452 OleAgreementMethod oleAgreementMethod = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OleAgreementMethod.class, agreementMethodId);
453 return oleAgreementMethod.getAgreementMethodName();
454 }
455
456
457
458
459
460
461
462 protected Map<String, String> getSelectionContext(String contextName) {
463 Map<String, String> selector = new HashMap<String, String>();
464 selector.put(OLEConstants.NAMESPACE_CODE_SELECTOR, "OLE");
465 selector.put(OLEConstants.NAME_SELECTOR, contextName);
466 return selector;
467 }
468
469
470
471
472
473
474
475 protected Map<String, String> getAgendaContext(String agendaName) {
476 Map<String, String> selector = new HashMap<String, String>();
477 selector.put(OLEConstants.NAME_SELECTOR, agendaName);
478 return selector;
479 }
480
481
482
483
484
485
486
487 private String getLicenseType(String licenseRequestTypeId) {
488 OleLicenseRequestType oleLicenseRequestType = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OleLicenseRequestType.class, licenseRequestTypeId);
489 return oleLicenseRequestType.getName();
490 }
491
492
493
494
495
496
497
498 private String getWorkFlowName(String licenseRequestWorkflowTypeCode) {
499 OleLicenseRequestWorkflowType oleLicenseRequestWorkflowType = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OleLicenseRequestWorkflowType.class, licenseRequestWorkflowTypeCode);
500 return oleLicenseRequestWorkflowType.getName();
501 }
502
503
504
505
506
507
508
509
510 @RequestMapping(params = "methodToCall=insertAgreementDocument")
511 public ModelAndView insertAgreementDocument(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
512 HttpServletRequest request, HttpServletResponse response) {
513
514 MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
515 MultipartFile attachmentFile = form.getAttachmentFile();
516 if (attachmentFile.getOriginalFilename() != null && !attachmentFile.getOriginalFilename().isEmpty()) {
517 String selectedCollectionPath = form.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
518 CollectionGroup collectionGroup = form.getPostedView().getViewIndex().getCollectionGroupByPath(
519 selectedCollectionPath);
520 String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
521 Object eventObject = ObjectPropertyUtils.getPropertyValue(uifForm, addLinePath);
522 OleAgreementDocumentMetadata oleAgreementDocumentMetadata = (OleAgreementDocumentMetadata) eventObject;
523 oleAgreementDocumentMetadata.setCurrentTimeStamp();
524 String userName = GlobalVariables.getUserSession().getPrincipalName();
525 oleAgreementDocumentMetadata.setUploadedBy(userName);
526
527 if (attachmentFile != null && !StringUtils.isBlank(attachmentFile.getOriginalFilename())) {
528 if (attachmentFile.getSize() == 0) {
529 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS,
530 RiceKeyConstants.ERROR_UPLOADFILE_EMPTY, attachmentFile.getOriginalFilename());
531 return getUIFModelAndView(form);
532 } else {
533 try {
534 oleAgreementDocumentMetadata.setAgreementFileName(attachmentFile.getOriginalFilename());
535 oleAgreementDocumentMetadata.setAgreementMimeType(attachmentFile.getContentType());
536 storeAgreementAttachment(attachmentFile);
537 MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
538 OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo) document.getNewMaintainableObject().getDataObject();
539 updateEventLogForLocation(oleLicenseRequestBo, "file", "Agreement Document uploaded - " + attachmentFile.getOriginalFilename());
540 } catch (Exception e) {
541 LOG.error("Error while storing the Agreement Document");
542 }
543 }
544 }
545 } else {
546 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_MESSAGES, OLEConstants.OleLicenseRequest.ERROR_FILE_NOT_FOUND);
547 return getUIFModelAndView(form);
548 }
549
550 return addLine(form, result, request, response);
551 }
552
553
554
555
556
557
558
559 @RequestMapping(params = "methodToCall=deleteAgreementDocument")
560 public ModelAndView deleteAgreementDocument(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
561 HttpServletRequest request, HttpServletResponse response) {
562 MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
563 MultipartFile attachmentFile = form.getAttachmentFile();
564 String selectedLineIndex = uifForm.getActionParamaterValue("selectedLineIndex");
565 MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
566 OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo) document.getNewMaintainableObject().getDataObject();
567 OleAgreementDocumentMetadata oleAgreementDocumentMetadata = oleLicenseRequestBo.getAgreementDocumentMetadataList().get(Integer.parseInt(selectedLineIndex));
568 boolean isSuccesfull = false;
569 try {
570 if (oleAgreementDocumentMetadata.getAgreementUUID() != null) {
571 isSuccesfull = getOleLicenseRequestService().deleteAgreementDocument(oleAgreementDocumentMetadata);
572 } else {
573 isSuccesfull = deleteAgreementAttachment(attachmentFile);
574 }
575 if (isSuccesfull) {
576 updateEventLogForLocation(oleLicenseRequestBo, "file",
577 "Agreement Document deleted - " + oleAgreementDocumentMetadata.getAgreementFileName());
578 performWorkflowAction(form, UifConstants.WorkflowAction.SAVE, true);
579 }
580 } catch (Exception e) {
581 LOG.error("Error while deleting the Agreement Document");
582 }
583 return deleteLine(uifForm, result, request, response);
584 }
585
586
587
588
589
590
591
592
593 @RequestMapping(params = "methodToCall=downloadAgreement")
594 public ModelAndView downloadAgreement(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
595 HttpServletRequest request,
596 HttpServletResponse response) {
597 String selectedLineIndex = uifForm.getActionParamaterValue("selectedLineIndex");
598 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) uifForm;
599 MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
600 OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo) document.getNewMaintainableObject().getDataObject();
601
602 OleAgreementDocumentMetadata oleAgreementDocumentMetadata = oleLicenseRequestBo.getAgreementDocumentMetadataList().get(Integer.parseInt(selectedLineIndex));
603 File file;
604 try {
605 if (oleAgreementDocumentMetadata.getAgreementUUID() == null) {
606 file = new File(getKualiConfigurationService().getPropertyValueAsString(
607 KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY) + OLEConstants.OleLicenseRequest.AGREEMENT_TMP_LOCATION +
608 File.separator + oleAgreementDocumentMetadata.getAgreementFileName());
609
610 } else {
611 file = getOleLicenseRequestService().downloadAgreementDocumentFromDocstore(oleAgreementDocumentMetadata);
612
613 }
614 response.setContentType(oleAgreementDocumentMetadata.getAgreementMimeType());
615 response.setContentLength((int) file.length());
616 response.setHeader("Expires", "0");
617 response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
618 response.setHeader("Pragma", "public");
619 response.setHeader("Content-Disposition",
620 "attachment; filename=\"" + oleAgreementDocumentMetadata.getAgreementFileName() + "\"");
621 InputStream fis = new BufferedInputStream(new FileInputStream(file));
622 FileCopyUtils.copy(fis, response.getOutputStream());
623
624 } catch (Exception e) {
625 LOG.error("Error while retrieving the attachment");
626 } finally {
627 updateEventLogForLocation(oleLicenseRequestBo, "agreement document", "Agreement Document Downloaded");
628 performWorkflowAction(maintenanceForm, UifConstants.WorkflowAction.SAVE, true);
629 }
630
631 return null;
632 }
633
634
635
636
637
638
639
640 @RequestMapping(params = "methodToCall=viewRelatedDocument")
641 public ModelAndView viewRelatedDocument(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
642 HttpServletRequest request,
643 HttpServletResponse response) {
644 ModelAndView modelAndView = new ModelAndView();
645 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) uifForm;
646 String oleurl = ConfigContext.getCurrentContextConfig().getProperty("ole.url");
647 String olePortal = oleurl.substring(0, oleurl.indexOf("portal.jsp"));
648 String requisitionDocNumber = request.getParameter(OLEConstants.OleLicenseRequest.REQUISITION_DOC_NUM);
649 String redirectUrl = olePortal + KewApiConstants.Namespaces.MODULE_NAME + "/" + KewApiConstants.DOC_HANDLER_REDIRECT_PAGE + "?" + KewApiConstants.COMMAND_PARAMETER + "=" + KewApiConstants.DOCSEARCH_COMMAND + "&" + KewApiConstants.DOCUMENT_ID_PARAMETER + "=" + requisitionDocNumber;
650
651 modelAndView = new ModelAndView(REDIRECT_PREFIX + redirectUrl);
652
653 return modelAndView;
654 }
655
656
657
658
659
660
661 private void storeAgreementAttachment(MultipartFile agreementFile) throws IOException {
662 String location = "";
663 location = getKualiConfigurationService().getPropertyValueAsString(
664 KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY) + OLEConstants.OleLicenseRequest.AGREEMENT_TMP_LOCATION;
665
666 File dirLocation = new File(location);
667 if (!dirLocation.exists()) {
668 boolean success = dirLocation.mkdirs();
669 if (!success) {
670 LOG.error("Could not generate directory for File at: " + dirLocation.getAbsolutePath());
671 }
672 }
673 location = location + File.separator + agreementFile.getOriginalFilename();
674 InputStream fileContents = agreementFile.getInputStream();
675 File fileOut = new File(location);
676 FileOutputStream streamOut = null;
677 BufferedOutputStream bufferedStreamOut = null;
678 try {
679 streamOut = new FileOutputStream(fileOut);
680 bufferedStreamOut = new BufferedOutputStream(streamOut);
681 int c;
682 while ((c = fileContents.read()) != -1) {
683 bufferedStreamOut.write(c);
684 }
685 } finally {
686 bufferedStreamOut.close();
687 streamOut.close();
688 }
689
690 }
691
692
693
694
695
696
697
698 private boolean isNewAgreementDocument(OleAgreementDocumentMetadata agreementDocumentMetadata) {
699 return agreementDocumentMetadata.getAgreementUUID() == null;
700 }
701
702
703
704
705
706
707
708 @RequestMapping(params = "methodToCall=save")
709 public ModelAndView save(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
710 HttpServletRequest request, HttpServletResponse response) throws Exception {
711 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
712 MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
713 OleLicenseRequestBo newLicenseRequestBo = (OleLicenseRequestBo) document.getNewMaintainableObject().getDataObject();
714 OleLicenseRequestBo oldLicenseRequestBo = (OleLicenseRequestBo) document.getOldMaintainableObject().getDataObject();
715 if (oldLicenseRequestBo != null && newLicenseRequestBo.getLocationId() != null &&
716 newLicenseRequestBo.getLocationId().equals(OLEConstants.OleLicenseRequest.LICENSE_INITIAL_LOCATON) &&
717 !newLicenseRequestBo.getLocationId().equals(oldLicenseRequestBo.getLocationId())) {
718 updateEventLogForLocation(newLicenseRequestBo, "location", null);
719 }
720 processAgreementDocument(newLicenseRequestBo);
721 Map<String, String> licenses = getLicenseWorkFlows();
722 if (licenses.containsKey(newLicenseRequestBo.getLicenseRequestWorkflowTypeCode())) {
723 if (newLicenseRequestBo.getLicenseRequestWorkflowTypeCode().equals(OLEConstants.OleLicenseRequest.LICENSE_RECEIVED)) {
724 updateEventLogForLocation(newLicenseRequestBo, "license received", "status change");
725 }
726 newLicenseRequestBo.setLicenseRequestStatusCode(licenses.get(newLicenseRequestBo.getLicenseRequestWorkflowTypeCode()));
727 performWorkflowAction(form, UifConstants.WorkflowAction.SAVE, true);
728 }
729 return super.save(form, result, request, response);
730 }
731
732
733
734
735
736
737
738 /*
739 @RequestMapping(params = "methodToCall=viewAgreement")
740 public ModelAndView viewAgreement(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
741 HttpServletRequest request,
742 HttpServletResponse response) {
743 ModelAndView modelAndView = new ModelAndView();
744 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) uifForm;
745 MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
746 OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo)document.getNewMaintainableObject().getDataObject();
747 String url = PropertyUtil.getPropertyUtil().getProperty("docstore.url") ;
748 if(oleLicenseRequestBo != null) {
749 String redirectUrl = url+"?docAction=checkOut&uuid="+oleLicenseRequestBo.getAgreementId();
750
751 modelAndView = new ModelAndView(REDIRECT_PREFIX + redirectUrl);
752 }
753 else {
754 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OleLicenseRequest.AGREEMENT_ERROR);
755 }
756 return modelAndView;
757 }
758 */
759 private ConfigurationService getKualiConfigurationService() {
760 return KRADServiceLocator.getKualiConfigurationService();
761 }
762
763
764
765
766
767
768
769
770 private boolean deleteAgreementAttachment(MultipartFile agreementFile) throws IOException {
771 LOG.debug("Inside deleteAgreementAttachment method");
772 String location = "";
773 location = getKualiConfigurationService().getPropertyValueAsString(
774 KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY) + OLEConstants.OleLicenseRequest.AGREEMENT_TMP_LOCATION + File.separator + agreementFile.getOriginalFilename();
775 File attachmentFile = new File(location);
776 return attachmentFile.delete();
777 }
778
779
780
781
782
783
784
785
786 private void updateEventLogForLocation(OleLicenseRequestBo oleLicenseRequestBo, String eventType, String description) {
787 LOG.debug("Inside updateEventLogForLocation method");
788 OleEventLogBo oleEventLogBo = new OleEventLogBo();
789 oleEventLogBo.setCurrentTimeStamp();
790 oleEventLogBo.setCreatedBy(GlobalVariables.getUserSession().getPrincipalName());
791 if (eventType.equalsIgnoreCase("location")) {
792 Map criteria = new HashMap();
793 criteria.put("id", oleLicenseRequestBo.getLocationId());
794 OleLicenseRequestLocation location = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleLicenseRequestLocation.class,
795 criteria);
796 oleEventLogBo.setEventDescription(ROUTED_EXTERNAL);
797 oleEventLogBo.setEventType("location : " + location.getDescription());
798 } else {
799 oleEventLogBo.setEventDescription(description);
800 oleEventLogBo.setEventType(eventType);
801 }
802
803 oleLicenseRequestBo.getEventLogs().add(oleEventLogBo);
804 }
805
806
807
808
809
810
811 private void processAgreementDocument(OleLicenseRequestBo oleLicenseRequestBo) {
812 LOG.debug("Inside processAgreementDocument method");
813 List<OleAgreementDocumentMetadata> agreementDocuments = oleLicenseRequestBo.getAgreementDocumentMetadataList();
814 List<OleAgreementDocumentMetadata> newAgreementDocuments = new ArrayList<OleAgreementDocumentMetadata>();
815 List<OleAgreementDocumentMetadata> checkInAgreementDocuments = new ArrayList<OleAgreementDocumentMetadata>();
816 if (agreementDocuments.size() > 0) {
817 for (OleAgreementDocumentMetadata agreementDocument : agreementDocuments) {
818 if (isNewAgreementDocument(agreementDocument)) {
819 newAgreementDocuments.add(agreementDocument);
820 } else {
821 checkInAgreementDocuments.add(agreementDocument);
822 }
823 }
824 }
825 List<OleAgreementDocumentMetadata> agreementDocumentMetadataList = new ArrayList<OleAgreementDocumentMetadata>();
826 if (newAgreementDocuments.size() > 0) {
827 agreementDocumentMetadataList.addAll(getOleLicenseRequestService().processIngestAgreementDocuments(newAgreementDocuments));
828 }
829 if (checkInAgreementDocuments.size() > 0) {
830 agreementDocumentMetadataList.addAll(getOleLicenseRequestService().processCheckInAgreementDocuments(checkInAgreementDocuments));
831 }
832 oleLicenseRequestBo.setAgreementDocumentMetadataList(agreementDocumentMetadataList);
833 }
834
835
836
837
838
839
840 private Map<String, String> getLicenseWorkFlows() {
841 LOG.debug("Inside getLicenseWorkFlows method");
842 Map<String, String> licenseWorkFlows = new HashMap<String, String>();
843 licenseWorkFlows.put(OLEConstants.OleLicenseRequest.LICENSE_IN_NEGO, OLEConstants.OleLicenseRequest.LICENSE_IN_NEGO_VALUE);
844 licenseWorkFlows.put(OLEConstants.OleLicenseRequest.LICENSE_IN_PROCESS, OLEConstants.OleLicenseRequest.LICENSE_IN_PROCESS_VALUE);
845 licenseWorkFlows.put(OLEConstants.OleLicenseRequest.LICENSE_INITIAL_WORKFLOW, OLEConstants.OleLicenseRequest.LICENSE_NEEDED);
846 licenseWorkFlows.put(OLEConstants.OleLicenseRequest.LICENSE_RECEIVED,
847 OLEConstants.OleLicenseRequest.LICENSE_COMPLETE_RETURN);
848 licenseWorkFlows.put(OLEConstants.OleLicenseRequest.LICENSE_REQUESTED, OLEConstants.OleLicenseRequest.LICENSE_REQUESTED_VALUE);
849 return licenseWorkFlows;
850 }
851
852
853 }