View Javadoc
1   /*
2    * Copyright 2006 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.fp.document.service.impl;
17  
18  import java.io.IOException;
19  import java.io.OutputStream;
20  
21  import org.kuali.ole.fp.businessobject.DisbursementVoucherDocumentationLocation;
22  import org.kuali.ole.fp.businessobject.PaymentReasonCode;
23  import org.kuali.ole.fp.businessobject.options.PaymentMethodValuesFinder;
24  import org.kuali.ole.fp.document.DisbursementVoucherConstants;
25  import org.kuali.ole.fp.document.DisbursementVoucherDocument;
26  import org.kuali.ole.fp.document.service.DisbursementVoucherCoverSheetService;
27  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
28  import org.kuali.rice.kew.api.WorkflowDocument;
29  import org.kuali.rice.krad.service.BusinessObjectService;
30  import org.kuali.rice.krad.service.PersistenceStructureService;
31  import org.kuali.rice.krad.util.ObjectUtils;
32  
33  import com.lowagie.text.DocumentException;
34  import com.lowagie.text.pdf.AcroFields;
35  import com.lowagie.text.pdf.PdfReader;
36  import com.lowagie.text.pdf.PdfStamper;
37  
38  /**
39   * This is the default implementation of the DisbursementVoucherCoverSheetService interface.
40   */
41  public class DisbursementVoucherCoverSheetServiceImpl implements DisbursementVoucherCoverSheetService {
42      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherCoverSheetServiceImpl.class);
43  
44      protected ParameterService parameterService;
45      protected BusinessObjectService businessObjectService;
46      protected PersistenceStructureService persistenceStructureService;
47  
48      /**
49       * This method uses the values provided to build and populate a cover sheet associated with a given DisbursementVoucher.
50       *
51       * @param templateDirectory The directory where the cover sheet template can be found.
52       * @param templateName The name of the cover sheet template to be used to build the cover sheet.
53       * @param document The DisbursementVoucher the cover sheet will be populated from.
54       * @param outputStream The stream the cover sheet file will be written to.
55       * @see org.kuali.ole.fp.document.service.DisbursementVoucherCoverSheetService#generateDisbursementVoucherCoverSheet(java.lang.String,
56       *      java.lang.String, org.kuali.ole.fp.document.DisbursementVoucherDocument, java.io.OutputStream)
57       */
58      @Override
59      public void generateDisbursementVoucherCoverSheet(String templateDirectory, String templateName, DisbursementVoucherDocument document, OutputStream outputStream) throws DocumentException, IOException {
60          if (this.isCoverSheetPrintable(document)) {
61              String attachment = "";
62              String handling = "";
63              String alien = "";
64              String lines = "";
65              String bar = "";
66              String rlines = "";
67  
68              String docNumber = document.getDocumentNumber();
69              String initiator = document.getDocumentHeader().getWorkflowDocument().getInitiatorPrincipalId();
70              String payee = document.getDvPayeeDetail().getDisbVchrPayeePersonName();
71  
72              String reason = businessObjectService.findBySinglePrimaryKey(PaymentReasonCode.class, document.getDvPayeeDetail().getDisbVchrPaymentReasonCode()).getName();
73              String check_total = document.getDisbVchrCheckTotalAmount().toString();
74  
75              String currency = new PaymentMethodValuesFinder().getKeyLabel(document.getDisbVchrPaymentMethodCode());
76  
77              String address = retrieveAddress(document.getDisbursementVoucherDocumentationLocationCode());
78  
79              // retrieve attachment label
80              if (document.isDisbVchrAttachmentCode()) {
81                  attachment = parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.DV_COVER_SHEET_TEMPLATE_ATTACHMENT_PARM_NM);
82              }
83              // retrieve handling label
84              if (document.isDisbVchrSpecialHandlingCode()) {
85                  handling = parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.DV_COVER_SHEET_TEMPLATE_HANDLING_PARM_NM);
86              }
87              // retrieve data for alien payment code
88              //Commented for the jira issue OLE-3415
89              /*if (document.getDvPayeeDetail().isDisbVchrAlienPaymentCode()) {
90                  String taxDocumentationLocationCode = parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.TAX_DOCUMENTATION_LOCATION_CODE_PARM_NM);
91  
92                  address = retrieveAddress(taxDocumentationLocationCode);
93                  alien = parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.DV_COVER_SHEET_TEMPLATE_ALIEN_PARM_NM);
94                  lines = parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.DV_COVER_SHEET_TEMPLATE_LINES_PARM_NM);
95              }*/
96  
97              // determine if non-employee travel payment reasons
98              String paymentReasonCode = document.getDvPayeeDetail().getDisbVchrPaymentReasonCode();
99            //Commented for the jira issue OLE-3415
100             /*ParameterEvaluator travelNonEmplPaymentReasonEvaluator = SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(DisbursementVoucherDocument.class, DisbursementVoucherConstants.NONEMPLOYEE_TRAVEL_PAY_REASONS_PARM_NM, paymentReasonCode);
101             boolean isTravelNonEmplPaymentReason = travelNonEmplPaymentReasonEvaluator.evaluationSucceeds();
102 
103             if (isTravelNonEmplPaymentReason) {
104                 bar = parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.DV_COVER_SHEET_TEMPLATE_BAR_PARM_NM);
105                 rlines = parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.DV_COVER_SHEET_TEMPLATE_RLINES_PARM_NM);
106             }*/
107 
108             try {
109                 PdfReader reader = new PdfReader(templateDirectory + templateName);
110 
111                 // populate form with document values
112                 PdfStamper stamper = new PdfStamper(reader, outputStream);
113 
114                 AcroFields populatedCoverSheet = stamper.getAcroFields();
115                 populatedCoverSheet.setField("initiator", initiator);
116                 populatedCoverSheet.setField("attachment", attachment);
117                 populatedCoverSheet.setField("currency", currency);
118                 populatedCoverSheet.setField("handling", handling);
119                 populatedCoverSheet.setField("alien", alien);
120                 populatedCoverSheet.setField("payee_name", payee);
121                 populatedCoverSheet.setField("check_total", check_total);
122                 populatedCoverSheet.setField("docNumber", docNumber);
123                 populatedCoverSheet.setField("payment_reason", reason);
124                 populatedCoverSheet.setField("destination_address", address);
125                 populatedCoverSheet.setField("lines", lines);
126                 populatedCoverSheet.setField("bar", bar);
127                 populatedCoverSheet.setField("rlines", rlines);
128 
129                 stamper.setFormFlattening(true);
130                 stamper.close();
131             }
132             catch (DocumentException e) {
133                 LOG.error("Error creating coversheet for: " + docNumber + ". ::" + e);
134                 throw e;
135             }
136             catch (IOException e) {
137                 LOG.error("Error creating coversheet for: " + docNumber + ". ::" + e);
138                 throw e;
139             }
140         }
141 
142     }
143 
144     /**
145      * @see org.kuali.ole.fp.document.service.DisbursementVoucherCoverSheetService#isCoverSheetPrintable(org.kuali.ole.fp.document.DisbursementVoucherDocument)
146      */
147     @Override
148     public boolean isCoverSheetPrintable(DisbursementVoucherDocument document) {
149         WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
150 
151         if(ObjectUtils.isNull(workflowDocument)){
152             return false;
153     }
154 
155         return !(workflowDocument.isCanceled() || workflowDocument.isInitiated() || workflowDocument.isDisapproved() || workflowDocument.isException() || workflowDocument.isDisapproved() || workflowDocument.isSaved());
156     }
157 
158     /**
159      * This method contains logic to determine the address the cover sheet should be sent to.
160      *
161      * @param docLocCd A key used to retrieve the document location.
162      * @return The address the cover sheet will be sent to or empty string if no location is found.
163      */
164     protected String retrieveAddress(String docLocCd) {
165         String address = "";
166         try {
167             address = businessObjectService.findBySinglePrimaryKey(DisbursementVoucherDocumentationLocation.class, docLocCd).getDisbursementVoucherDocumentationLocationAddress();
168         }
169         catch (NullPointerException e) {
170             // ignored
171         }
172 
173         return address;
174     }
175 
176     // spring injected services
177 
178     /**
179      * Sets the businessObjectService attribute value.
180      *
181      * @param businessObjectService The businessObjectService to set.
182      */
183     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
184         this.businessObjectService = businessObjectService;
185     }
186 
187     /**
188      * Sets the persistenceStructureService attribute value.
189      *
190      * @param persistenceStructureService The persistenceService to set.
191      */
192     public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
193         this.persistenceStructureService = persistenceStructureService;
194     }
195 
196     /**
197      * Sets the parameterService attribute value.
198      *
199      * @param parameterService The parameterService to set.
200      */
201     public void setParameterService(ParameterService parameterService) {
202         this.parameterService = parameterService;
203     }
204 }