1 package org.kuali.ole.select.service.impl;
2
3 import org.kuali.ole.module.purap.PurapConstants.LicenseRequestStatus;
4 import org.kuali.ole.select.businessobject.OleRequisitionItem;
5 import org.kuali.ole.select.document.OleRequisitionDocument;
6 import org.kuali.ole.select.service.OleRequisitionWebService;
7 import org.kuali.ole.sys.OLEConstants;
8 import org.kuali.ole.sys.OLEKeyConstants;
9 import org.kuali.ole.sys.context.SpringContext;
10 import org.kuali.rice.core.api.datetime.DateTimeService;
11 import org.kuali.rice.kew.actionitem.ActionItem;
12 import org.kuali.rice.kew.actionlist.service.ActionListService;
13 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
14 import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
15 import org.kuali.rice.kew.api.WorkflowDocument;
16 import org.kuali.rice.kew.api.action.ActionRequestType;
17 import org.kuali.rice.kew.api.document.DocumentStatus;
18 import org.kuali.rice.kew.api.exception.WorkflowException;
19 import org.kuali.rice.kew.service.KEWServiceLocator;
20 import org.kuali.rice.kim.api.identity.Person;
21 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
22 import org.kuali.rice.krad.service.DocumentService;
23 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
24 import org.kuali.rice.krad.util.GlobalVariables;
25
26 import java.sql.Timestamp;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Set;
30
31
32
33
34
35
36
37
38
39 public class OleRequisitionWebServiceImpl implements OleRequisitionWebService {
40 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleRequisitionWebServiceImpl.class);
41 private DocumentService documentService;
42 public List<OleRequisitionItem> requisitionItems = new ArrayList<OleRequisitionItem>();
43
44 @Override
45 public void updateRequisitionStatus(String documentNumber, String reqStatus) {
46 try {
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 ActionListService actionListSrv = KEWServiceLocator.getActionListService();
108 ActionRequestService actionReqSrv = KEWServiceLocator.getActionRequestService();
109 OleRequisitionDocument requisitionDocument = (OleRequisitionDocument)
110 SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(documentNumber);
111 Person person = KimApiServiceLocator.getPersonService().getPerson(requisitionDocument.getDocumentHeader().getWorkflowDocument().getInitiatorPrincipalId());
112
113 try {
114 WorkflowDocument workflowDocument = KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(requisitionDocument.getDocumentNumber(), person);
115 if (DocumentStatus.ENROUTE.equals(workflowDocument.getStatus())) {
116 List<ActionRequestValue> actionReqValues = actionReqSrv.findAllPendingRequests(workflowDocument.getDocumentId());
117 for (ActionRequestValue actionRequest : actionReqValues) {
118 if (ActionRequestType.APPROVE.getCode().equals(actionRequest.getActionRequested())) {
119 Timestamp currentTime = SpringContext.getBean(DateTimeService.class).getCurrentTimestamp();
120 List<ActionItem> actionItems = actionRequest.getActionItems();
121 for (ActionItem actionItem : actionItems) {
122 if (ActionRequestType.APPROVE.getLabel().equals(actionItem.getActionRequestLabel())) {
123 actionItem.setPrincipalId(person.getPrincipalId());
124 actionItem.setDateAssigned(currentTime);
125 actionListSrv.saveActionItem(actionItem);
126 }
127 }
128 actionRequest.setPrincipalId(person.getPrincipalId());
129 actionRequest.setCreateDate(currentTime);
130 actionReqSrv.saveActionRequest(actionRequest);
131 if (reqStatus.equals(LicenseRequestStatus.LICENSE_REQUEST_COMPLETE)) {
132 boolean documentApproved = approveDocument(requisitionDocument, requisitionDocument.getDocumentNumber(), actionRequest.getAnnotation(), person, reqStatus);
133 } else if (reqStatus.equals(LicenseRequestStatus.NEGOTIATION_FAILED)) {
134 boolean documentCanceled = cancelDocument(requisitionDocument, requisitionDocument.getDocumentNumber(), actionRequest.getAnnotation(), person, reqStatus);
135 }
136 }
137 }
138 }
139 } catch (Exception e) {
140 LOG.error("Exception while updateRequisitionStatus"+e.getMessage());
141 throw new RuntimeException(e);
142 }
143 } catch (Exception e) {
144 LOG.error("Exception while updateRequisitionStatus"+e.getMessage());
145 throw new RuntimeException(e);
146 }
147 }
148
149 private boolean approveDocument(OleRequisitionDocument requisitionDocument, String documentNumber, String annotation, Person principalPerson, String reqStatus) {
150 LOG.debug("Inside approveDocument of OleOrderQueueDocument");
151 requisitionDocument.setDocumentHeader(KRADServiceLocatorWeb.getDocumentHeaderService().getDocumentHeaderById(documentNumber));
152
153 try {
154 requisitionDocument.getDocumentHeader().setWorkflowDocument(KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(documentNumber, principalPerson));
155 KRADServiceLocatorWeb.getDocumentService().approveDocument(requisitionDocument, principalPerson.getName() + OLEConstants.LicenseRequest.APPROVE_ANNOTATION + annotation, null);
156 return true;
157 } catch (WorkflowException wfe) {
158 GlobalVariables.getMessageMap().putError(OLEConstants.LicenseRequest.REQUISITIONS, OLEKeyConstants.ERROR_REQUISITION_APPROVE_WFE, new String[]{documentNumber, wfe.getMessage()});
159 return false;
160 }
161 }
162
163 private boolean cancelDocument(OleRequisitionDocument requisitionDocument, String documentNumber, String annotation, Person principalPerson, String reqStatus) {
164 LOG.debug("Inside cancelDocument of OleOrderQueueDocument");
165 requisitionDocument.setDocumentHeader(KRADServiceLocatorWeb.getDocumentHeaderService().getDocumentHeaderById(documentNumber));
166
167 try {
168 requisitionDocument.getDocumentHeader().setWorkflowDocument(KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(documentNumber, principalPerson));
169 KRADServiceLocatorWeb.getDocumentService().cancelDocument(requisitionDocument, principalPerson.getName() + OLEConstants.LicenseRequest.CANCEL_ANNOTATION + annotation);
170 return true;
171 } catch (WorkflowException wfe) {
172 GlobalVariables.getMessageMap().putError(OLEConstants.LicenseRequest.REQUISITIONS, OLEKeyConstants.ERROR_REQUISITION_CANCELED_WFE, new String[]{documentNumber, wfe.getMessage()});
173 return false;
174 }
175 }
176
177
178 public void setDocumentService(DocumentService documentService) {
179 this.documentService = documentService;
180 }
181
182 protected String getCurrentRouteNodeName(WorkflowDocument wd) throws WorkflowException {
183 Set<String> nodeNames = wd.getCurrentNodeNames();
184 if ((nodeNames == null) || (nodeNames.isEmpty())) {
185 return null;
186 } else {
187 return nodeNames.iterator().next();
188 }
189 }
190
191 }