View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.document.web.struts;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.apache.struts.action.ActionForm;
21  import org.apache.struts.action.ActionForward;
22  import org.apache.struts.action.ActionMapping;
23  import org.kuali.ole.module.purap.PurapConstants;
24  import org.kuali.ole.module.purap.PurapPropertyConstants;
25  import org.kuali.ole.module.purap.document.BulkReceivingDocument;
26  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
27  import org.kuali.ole.module.purap.document.service.BulkReceivingService;
28  import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
29  import org.kuali.ole.sys.OLEConstants;
30  import org.kuali.ole.sys.OLEPropertyConstants;
31  import org.kuali.ole.sys.context.SpringContext;
32  import org.kuali.ole.vnd.VendorConstants;
33  import org.kuali.ole.vnd.businessobject.VendorAddress;
34  import org.kuali.ole.vnd.document.service.VendorService;
35  import org.kuali.ole.vnd.service.PhoneNumberService;
36  import org.kuali.rice.kew.api.KewApiConstants;
37  import org.kuali.rice.kew.api.exception.WorkflowException;
38  import org.kuali.rice.kim.api.KimConstants;
39  import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
40  import org.kuali.rice.kns.question.ConfirmationQuestion;
41  import org.kuali.rice.kns.service.DataDictionaryService;
42  import org.kuali.rice.kns.service.DocumentHelperService;
43  import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
44  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
45  import org.kuali.rice.krad.document.Document;
46  import org.kuali.rice.krad.service.BusinessObjectService;
47  import org.kuali.rice.krad.util.GlobalVariables;
48  import org.kuali.rice.krad.util.KRADConstants;
49  import org.kuali.rice.krad.util.ObjectUtils;
50  
51  import javax.servlet.ServletOutputStream;
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletResponse;
54  import java.io.ByteArrayOutputStream;
55  import java.util.HashMap;
56  import java.util.Map;
57  
58  public class BulkReceivingAction extends KualiTransactionalDocumentActionBase {
59      protected static final Logger LOG = Logger.getLogger(BulkReceivingAction.class);
60  
61      protected void createDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
62          super.createDocument(kualiDocumentFormBase);
63          BulkReceivingForm blkForm = (BulkReceivingForm) kualiDocumentFormBase;
64          BulkReceivingDocument blkRecDoc = (BulkReceivingDocument) blkForm.getDocument();
65  
66          blkRecDoc.setPurchaseOrderIdentifier(blkForm.getPurchaseOrderId());
67  
68          blkRecDoc.initiateDocument();
69      }
70  
71      public ActionForward continueBulkReceiving(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
72          BulkReceivingForm blkForm = (BulkReceivingForm) form;
73          BulkReceivingDocument blkRecDoc = (BulkReceivingDocument) blkForm.getDocument();
74  
75          PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(blkRecDoc.getPurchaseOrderIdentifier());
76          if (ObjectUtils.isNotNull(po)) {
77              // TODO figure out a more straightforward way to do this. ailish put this in so the link id would be set and the perm
78              // check would work
79              blkRecDoc.setAccountsPayablePurchasingDocumentLinkIdentifier(po.getAccountsPayablePurchasingDocumentLinkIdentifier());
80  
81              // TODO hjs-check to see if user is allowed to initiate doc based on PO sensitive data (add this to all other docs
82              // except acm doc)
83              if (!SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(blkRecDoc).isAuthorizedByTemplate(blkRecDoc, KRADConstants.KNS_NAMESPACE, KimConstants.PermissionTemplateNames.OPEN_DOCUMENT, GlobalVariables.getUserSession().getPrincipalId())) {
84                  throw buildAuthorizationException("initiate document", blkRecDoc);
85              }
86          }
87  
88          // perform duplicate check
89          ActionForward forward = isDuplicateDocumentEntry(mapping, form, request, response, blkRecDoc);
90          if (forward != null) {
91              return forward;
92          }
93  
94          // populate and save bulk Receiving Document from Purchase Order
95          SpringContext.getBean(BulkReceivingService.class).populateAndSaveBulkReceivingDocument(blkRecDoc);
96  
97          return mapping.findForward(OLEConstants.MAPPING_BASIC);
98      }
99  
100     public ActionForward clearInitFields(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
101         BulkReceivingForm blkRecForm = (BulkReceivingForm) form;
102         BulkReceivingDocument blkRecDoc = (BulkReceivingDocument) blkRecForm.getDocument();
103 
104         blkRecDoc.clearInitFields();
105 
106         return mapping.findForward(OLEConstants.MAPPING_BASIC);
107     }
108 
109     protected ActionForward isDuplicateDocumentEntry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, BulkReceivingDocument bulkReceivingDocument) throws Exception {
110         ActionForward forward = null;
111         HashMap<String, String> duplicateMessages = SpringContext.getBean(BulkReceivingService.class).bulkReceivingDuplicateMessages(bulkReceivingDocument);
112 
113         if (duplicateMessages != null && !duplicateMessages.isEmpty()) {
114             Object question = request.getParameter(OLEConstants.QUESTION_INST_ATTRIBUTE_NAME);
115             if (question == null) {
116 
117                 return this.performQuestionWithoutInput(mapping, form, request, response, PurapConstants.BulkReceivingDocumentStrings.DUPLICATE_BULK_RECEIVING_DOCUMENT_QUESTION, duplicateMessages.get(PurapConstants.BulkReceivingDocumentStrings.DUPLICATE_BULK_RECEIVING_DOCUMENT_QUESTION), OLEConstants.CONFIRMATION_QUESTION, OLEConstants.ROUTE_METHOD, "");
118             }
119 
120             Object buttonClicked = request.getParameter(OLEConstants.QUESTION_CLICKED_BUTTON);
121             if ((PurapConstants.BulkReceivingDocumentStrings.DUPLICATE_BULK_RECEIVING_DOCUMENT_QUESTION.equals(question)) && ConfirmationQuestion.NO.equals(buttonClicked)) {
122                 forward = mapping.findForward(OLEConstants.MAPPING_BASIC);
123             }
124         }
125 
126         return forward;
127     }
128 
129     public ActionForward printReceivingTicket(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
130         String blkDocId = request.getParameter("docId");
131         ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
132 
133         try {
134             // will throw validation exception if errors occur
135             SpringContext.getBean(BulkReceivingService.class).performPrintReceivingTicketPDF(blkDocId, baosPDF);
136 
137             response.setHeader("Cache-Control", "max-age=30");
138             response.setContentType("application/pdf");
139             StringBuffer sbContentDispValue = new StringBuffer();
140             String useJavascript = request.getParameter("useJavascript");
141             if (useJavascript == null || useJavascript.equalsIgnoreCase("false")) {
142                 sbContentDispValue.append("attachment");
143             } else {
144                 sbContentDispValue.append("inline");
145             }
146             StringBuffer sbFilename = new StringBuffer();
147             sbFilename.append("PURAP_RECEIVING_TICKET_");
148             sbFilename.append(blkDocId);
149             sbFilename.append("_");
150             sbFilename.append(System.currentTimeMillis());
151             sbFilename.append(".pdf");
152             sbContentDispValue.append("; filename=");
153             sbContentDispValue.append(sbFilename);
154 
155             response.setHeader("Content-disposition", sbContentDispValue.toString());
156 
157             response.setContentLength(baosPDF.size());
158 
159             ServletOutputStream sos = response.getOutputStream();
160             baosPDF.writeTo(sos);
161             sos.flush();
162 
163         } finally {
164             if (baosPDF != null) {
165                 baosPDF.reset();
166             }
167         }
168 
169         return null;
170     }
171 
172     public ActionForward printReceivingTicketPDF(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
173         BulkReceivingForm blkRecForm = (BulkReceivingForm) form;
174         BulkReceivingDocument blkRecDoc = (BulkReceivingDocument) blkRecForm.getDocument();
175 
176         String basePath = getApplicationBaseUrl();
177         String docId = blkRecDoc.getDocumentNumber();
178         String methodToCallPrintPurchaseOrderPDF = "printReceivingTicket";
179         String methodToCallDocHandler = "docHandler";
180         String printReceivingTicketPDFUrl = getUrlForPrintReceivingTicket(basePath, docId, methodToCallPrintPurchaseOrderPDF);
181         String displayReceivingDocTabbedPageUrl = getUrlForPrintReceivingTicket(basePath, docId, methodToCallDocHandler);
182         request.setAttribute("printReceivingTicketPDFUrl", printReceivingTicketPDFUrl);
183         request.setAttribute("displayReceivingDocTabbedPageUrl", displayReceivingDocTabbedPageUrl);
184         String label = SpringContext.getBean(DataDictionaryService.class).getDocumentLabelByTypeName(OLEConstants.FinancialDocumentTypeCodes.BULK_RECEIVING);
185         request.setAttribute("receivingDocLabel", label);
186 
187         return mapping.findForward("printReceivingTicketPDF");
188     }
189 
190     protected String getUrlForPrintReceivingTicket(String basePath, String docId, String methodToCall) {
191 
192         StringBuffer result = new StringBuffer(basePath);
193         result.append("/purapBulkReceiving.do?methodToCall=");
194         result.append(methodToCall);
195         result.append("&docId=");
196         result.append(docId);
197         result.append("&command=displayDocSearchView");
198 
199         return result.toString();
200     }
201 
202     @Override
203     public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
204         BulkReceivingForm blkRecForm = (BulkReceivingForm) form;
205 
206         BulkReceivingDocument blkRecDoc = (BulkReceivingDocument) blkRecForm.getDocument();
207         String refreshCaller = blkRecForm.getRefreshCaller();
208         PhoneNumberService phoneNumberService = SpringContext.getBean(PhoneNumberService.class);
209 
210         // Format phone numbers
211         blkRecDoc.setInstitutionContactPhoneNumber(phoneNumberService.formatNumberIfPossible(blkRecDoc.getInstitutionContactPhoneNumber()));
212         blkRecDoc.setRequestorPersonPhoneNumber(phoneNumberService.formatNumberIfPossible(blkRecDoc.getRequestorPersonPhoneNumber()));
213         blkRecDoc.setDeliveryToPhoneNumber(phoneNumberService.formatNumberIfPossible(blkRecDoc.getDeliveryToPhoneNumber()));
214 
215         // Refreshing the fields after returning from a vendor lookup in the vendor tab
216         if (StringUtils.equals(refreshCaller, VendorConstants.VENDOR_LOOKUPABLE_IMPL) && blkRecDoc.getVendorDetailAssignedIdentifier() != null && blkRecDoc.getVendorHeaderGeneratedIdentifier() != null) {
217 
218             // retrieve vendor based on selection from vendor lookup
219             blkRecDoc.refreshReferenceObject("vendorDetail");
220             blkRecDoc.setVendorName(blkRecDoc.getVendorDetail().getVendorName());
221 
222             // populate default address based on selected vendor
223             VendorAddress defaultAddress = SpringContext.getBean(VendorService.class).getVendorDefaultAddress(blkRecDoc.getVendorDetail().getVendorAddresses(), blkRecDoc.getVendorDetail().getVendorHeader().getVendorType().getAddressType().getVendorAddressTypeCode(), "");
224             if (ObjectUtils.isNotNull(defaultAddress)) {
225                 blkRecDoc.setVendorLine1Address(defaultAddress.getVendorLine1Address());
226                 blkRecDoc.setVendorLine2Address(defaultAddress.getVendorLine2Address());
227                 blkRecDoc.setVendorCityName(defaultAddress.getVendorCityName());
228                 blkRecDoc.setVendorStateCode(defaultAddress.getVendorStateCode());
229                 blkRecDoc.setVendorPostalCode(defaultAddress.getVendorZipCode());
230                 blkRecDoc.setVendorCountryCode(defaultAddress.getVendorCountryCode());
231             }
232         }
233 
234         // Refreshing the fields after returning from an address lookup in the vendor tab
235         if (StringUtils.equals(refreshCaller, VendorConstants.VENDOR_ADDRESS_LOOKUPABLE_IMPL)) {
236             if (StringUtils.isNotEmpty(request.getParameter(OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.VENDOR_ADDRESS_ID))) {
237                 // retrieve address based on selection from address lookup
238                 VendorAddress refreshVendorAddress = new VendorAddress();
239                 refreshVendorAddress.setVendorAddressGeneratedIdentifier(blkRecDoc.getVendorAddressGeneratedIdentifier());
240                 refreshVendorAddress = (VendorAddress) SpringContext.getBean(BusinessObjectService.class).retrieve(refreshVendorAddress);
241                 if (ObjectUtils.isNotNull(refreshVendorAddress)) {
242                     blkRecDoc.setVendorLine1Address(refreshVendorAddress.getVendorLine1Address());
243                     blkRecDoc.setVendorLine2Address(refreshVendorAddress.getVendorLine2Address());
244                     blkRecDoc.setVendorCityName(refreshVendorAddress.getVendorCityName());
245                     blkRecDoc.setVendorStateCode(refreshVendorAddress.getVendorStateCode());
246                     blkRecDoc.setVendorPostalCode(refreshVendorAddress.getVendorZipCode());
247                     blkRecDoc.setVendorCountryCode(refreshVendorAddress.getVendorCountryCode());
248                 }
249             }
250         }
251 
252         // Refreshing corresponding fields after returning from various kuali lookups
253         if (StringUtils.equals(refreshCaller, OLEConstants.KUALI_LOOKUPABLE_IMPL)) {
254             if (request.getParameter("document.deliveryCampusCode") != null) {
255                 // returning from a building or campus lookup on the delivery tab
256 
257                 if (request.getParameter("document.deliveryBuildingName") == null) {
258                     // came from campus lookup not building, so clear building
259                     blkRecDoc.setDeliveryBuildingCode("");
260                     blkRecDoc.setDeliveryBuildingLine1Address("");
261                     blkRecDoc.setDeliveryBuildingLine2Address("");
262                     blkRecDoc.setDeliveryBuildingRoomNumber("");
263                     blkRecDoc.setDeliveryCityName("");
264                     blkRecDoc.setDeliveryStateCode("");
265                     blkRecDoc.setDeliveryPostalCode("");
266                     blkRecDoc.setDeliveryCountryCode("");
267                 } else {
268                     // came from building lookup then turn off "OTHER" and clear room and line2address
269                     blkRecDoc.setDeliveryBuildingOtherIndicator(false);
270                     blkRecDoc.setDeliveryBuildingRoomNumber("");
271                     blkRecDoc.setDeliveryBuildingLine2Address("");
272                 }
273             }
274         }
275 
276         return super.refresh(mapping, form, request, response);
277     }
278 
279     /**
280      * Setup document to use "OTHER" building
281      *
282      * @param mapping  An ActionMapping
283      * @param form     An ActionForm
284      * @param request  A HttpServletRequest
285      * @param response A HttpServletResponse
286      * @return An ActionForward
287      * @throws Exception
288      */
289     public ActionForward useOtherDeliveryBuilding(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
290         BulkReceivingForm baseForm = (BulkReceivingForm) form;
291         BulkReceivingDocument document = (BulkReceivingDocument) baseForm.getDocument();
292 
293         document.setDeliveryBuildingOtherIndicator(true);
294         document.setDeliveryBuildingName("");
295         document.setDeliveryBuildingCode("");
296         document.setDeliveryBuildingLine1Address("");
297         document.setDeliveryBuildingLine2Address("");
298         document.setDeliveryBuildingRoomNumber("");
299         document.setDeliveryCityName("");
300         document.setDeliveryStateCode("");
301         document.setDeliveryCountryCode("");
302         document.setDeliveryPostalCode("");
303 
304         return mapping.findForward(OLEConstants.MAPPING_BASIC);
305     }
306 
307     @Override
308     protected void populateAdHocActionRequestCodes(KualiDocumentFormBase formBase) {
309         Document document = formBase.getDocument();
310         DocumentAuthorizer documentAuthorizer = getDocumentHelperService().getDocumentAuthorizer(document);
311         Map<String, String> adHocActionRequestCodes = new HashMap<String, String>();
312 
313         if (documentAuthorizer.canSendAdHocRequests(document, KewApiConstants.ACTION_REQUEST_FYI_REQ, GlobalVariables.getUserSession().getPerson())) {
314             adHocActionRequestCodes.put(KewApiConstants.ACTION_REQUEST_FYI_REQ, KewApiConstants.ACTION_REQUEST_FYI_REQ_LABEL);
315         }
316         if ((document.getDocumentHeader().getWorkflowDocument().isInitiated()
317                 || document.getDocumentHeader().getWorkflowDocument().isSaved()
318                 || document.getDocumentHeader().getWorkflowDocument().isEnroute()
319         ) && documentAuthorizer.canSendAdHocRequests(document, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, GlobalVariables.getUserSession().getPerson())) {
320             adHocActionRequestCodes.put(KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ_LABEL);
321         }
322         formBase.setAdHocActionRequestCodes(adHocActionRequestCodes);
323 
324     }
325 
326 }