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 = null;
224             if (blkRecDoc.getVendorDetail() != null && blkRecDoc.getVendorDetail().getVendorAddresses() != null && blkRecDoc.getVendorDetail().getVendorHeader() != null && blkRecDoc.getVendorDetail().getVendorHeader().getVendorType() != null && blkRecDoc.getVendorDetail().getVendorHeader().getVendorType().getAddressType() != null && blkRecDoc.getVendorDetail().getVendorHeader().getVendorType().getAddressType().getVendorAddressTypeCode() != null) {
225                 defaultAddress = SpringContext.getBean(VendorService.class).getVendorDefaultAddress(blkRecDoc.getVendorDetail().getVendorAddresses(), blkRecDoc.getVendorDetail().getVendorHeader().getVendorType().getAddressType().getVendorAddressTypeCode(), "");
226             }
227             if (ObjectUtils.isNotNull(defaultAddress)) {
228                 blkRecDoc.setVendorLine1Address(defaultAddress.getVendorLine1Address());
229                 blkRecDoc.setVendorLine2Address(defaultAddress.getVendorLine2Address());
230                 blkRecDoc.setVendorCityName(defaultAddress.getVendorCityName());
231                 blkRecDoc.setVendorStateCode(defaultAddress.getVendorStateCode());
232                 blkRecDoc.setVendorPostalCode(defaultAddress.getVendorZipCode());
233                 blkRecDoc.setVendorCountryCode(defaultAddress.getVendorCountryCode());
234             }
235         }
236 
237         // Refreshing the fields after returning from an address lookup in the vendor tab
238         if (StringUtils.equals(refreshCaller, VendorConstants.VENDOR_ADDRESS_LOOKUPABLE_IMPL)) {
239             if (StringUtils.isNotEmpty(request.getParameter(OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.VENDOR_ADDRESS_ID))) {
240                 // retrieve address based on selection from address lookup
241                 VendorAddress refreshVendorAddress = new VendorAddress();
242                 refreshVendorAddress.setVendorAddressGeneratedIdentifier(blkRecDoc.getVendorAddressGeneratedIdentifier());
243                 refreshVendorAddress = (VendorAddress) SpringContext.getBean(BusinessObjectService.class).retrieve(refreshVendorAddress);
244                 if (ObjectUtils.isNotNull(refreshVendorAddress)) {
245                     blkRecDoc.setVendorLine1Address(refreshVendorAddress.getVendorLine1Address());
246                     blkRecDoc.setVendorLine2Address(refreshVendorAddress.getVendorLine2Address());
247                     blkRecDoc.setVendorCityName(refreshVendorAddress.getVendorCityName());
248                     blkRecDoc.setVendorStateCode(refreshVendorAddress.getVendorStateCode());
249                     blkRecDoc.setVendorPostalCode(refreshVendorAddress.getVendorZipCode());
250                     blkRecDoc.setVendorCountryCode(refreshVendorAddress.getVendorCountryCode());
251                 }
252             }
253         }
254 
255         // Refreshing corresponding fields after returning from various kuali lookups
256         if (StringUtils.equals(refreshCaller, OLEConstants.KUALI_LOOKUPABLE_IMPL)) {
257             if (request.getParameter("document.deliveryCampusCode") != null) {
258                 // returning from a building or campus lookup on the delivery tab
259 
260                 if (request.getParameter("document.deliveryBuildingName") == null) {
261                     // came from campus lookup not building, so clear building
262                     blkRecDoc.setDeliveryBuildingCode("");
263                     blkRecDoc.setDeliveryBuildingLine1Address("");
264                     blkRecDoc.setDeliveryBuildingLine2Address("");
265                     blkRecDoc.setDeliveryBuildingRoomNumber("");
266                     blkRecDoc.setDeliveryCityName("");
267                     blkRecDoc.setDeliveryStateCode("");
268                     blkRecDoc.setDeliveryPostalCode("");
269                     blkRecDoc.setDeliveryCountryCode("");
270                 } else {
271                     // came from building lookup then turn off "OTHER" and clear room and line2address
272                     blkRecDoc.setDeliveryBuildingOtherIndicator(false);
273                     blkRecDoc.setDeliveryBuildingRoomNumber("");
274                     blkRecDoc.setDeliveryBuildingLine2Address("");
275                 }
276             }
277         }
278 
279         return super.refresh(mapping, form, request, response);
280     }
281 
282     /**
283      * Setup document to use "OTHER" building
284      *
285      * @param mapping  An ActionMapping
286      * @param form     An ActionForm
287      * @param request  A HttpServletRequest
288      * @param response A HttpServletResponse
289      * @return An ActionForward
290      * @throws Exception
291      */
292     public ActionForward useOtherDeliveryBuilding(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
293         BulkReceivingForm baseForm = (BulkReceivingForm) form;
294         BulkReceivingDocument document = (BulkReceivingDocument) baseForm.getDocument();
295 
296         document.setDeliveryBuildingOtherIndicator(true);
297         document.setDeliveryBuildingName("");
298         document.setDeliveryBuildingCode("");
299         document.setDeliveryBuildingLine1Address("");
300         document.setDeliveryBuildingLine2Address("");
301         document.setDeliveryBuildingRoomNumber("");
302         document.setDeliveryCityName("");
303         document.setDeliveryStateCode("");
304         document.setDeliveryCountryCode("");
305         document.setDeliveryPostalCode("");
306 
307         return mapping.findForward(OLEConstants.MAPPING_BASIC);
308     }
309 
310     @Override
311     protected void populateAdHocActionRequestCodes(KualiDocumentFormBase formBase) {
312         Document document = formBase.getDocument();
313         DocumentAuthorizer documentAuthorizer = getDocumentHelperService().getDocumentAuthorizer(document);
314         Map<String, String> adHocActionRequestCodes = new HashMap<String, String>();
315 
316         if (documentAuthorizer.canSendAdHocRequests(document, KewApiConstants.ACTION_REQUEST_FYI_REQ, GlobalVariables.getUserSession().getPerson())) {
317             adHocActionRequestCodes.put(KewApiConstants.ACTION_REQUEST_FYI_REQ, KewApiConstants.ACTION_REQUEST_FYI_REQ_LABEL);
318         }
319         if ((document.getDocumentHeader().getWorkflowDocument().isInitiated()
320                 || document.getDocumentHeader().getWorkflowDocument().isSaved()
321                 || document.getDocumentHeader().getWorkflowDocument().isEnroute()
322         ) && documentAuthorizer.canSendAdHocRequests(document, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, GlobalVariables.getUserSession().getPerson())) {
323             adHocActionRequestCodes.put(KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ_LABEL);
324         }
325         formBase.setAdHocActionRequestCodes(adHocActionRequestCodes);
326 
327     }
328 
329 }