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