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