001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.financial.service.impl;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.io.OutputStream;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.apache.commons.lang.StringUtils;
027: import org.kuali.core.bo.PersistableBusinessObject;
028: import org.kuali.core.lookup.keyvalues.KeyValuesFinder;
029: import org.kuali.core.service.BusinessObjectService;
030: import org.kuali.core.service.KualiRuleService;
031: import org.kuali.core.service.PersistenceStructureService;
032: import org.kuali.core.web.ui.KeyLabelPair;
033: import org.kuali.kfs.context.SpringContext;
034: import org.kuali.kfs.service.ParameterService;
035: import org.kuali.module.financial.bo.DisbursementVoucherDocumentationLocation;
036: import org.kuali.module.financial.bo.PaymentReasonCode;
037: import org.kuali.module.financial.document.DisbursementVoucherDocument;
038: import org.kuali.module.financial.lookup.keyvalues.PaymentMethodValuesFinder;
039: import org.kuali.module.financial.rules.DisbursementVoucherDocumentRule;
040: import org.kuali.module.financial.rules.DisbursementVoucherRuleConstants;
041: import org.kuali.module.financial.service.DisbursementVoucherCoverSheetService;
042:
043: import com.lowagie.text.DocumentException;
044: import com.lowagie.text.pdf.AcroFields;
045: import com.lowagie.text.pdf.PdfReader;
046: import com.lowagie.text.pdf.PdfStamper;
047:
048: /**
049: * This is the default implementation of the DisbursementVoucherCoverSheetService interface.
050: */
051: public class DisbursementVoucherCoverSheetServiceImpl implements
052: DisbursementVoucherCoverSheetService {
053: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
054: .getLogger(DisbursementVoucherCoverSheetServiceImpl.class);
055:
056: public static final String DV_COVERSHEET_TEMPLATE_NM = "disbursementVoucherCoverSheetTemplate.pdf";
057:
058: public static final String DV_COVER_SHEET_TEMPLATE_LINES_PARM_NM = "COVER_SHEET_TEMPLATE_LINES";
059: public static final String DV_COVER_SHEET_TEMPLATE_RLINES_PARM_NM = "COVER_SHEET_TEMPLATE_RLINES";
060: public static final String DV_COVER_SHEET_TEMPLATE_ALIEN_PARM_NM = "COVER_SHEET_TEMPLATE_NON_RESIDENT_ALIEN";
061: public static final String DV_COVER_SHEET_TEMPLATE_ATTACHMENT_PARM_NM = "COVER_SHEET_TEMPLATE_ATTACHMENT";
062: public static final String DV_COVER_SHEET_TEMPLATE_HANDLING_PARM_NM = "COVER_SHEET_TEMPLATE_HANDLING";
063: public static final String DV_COVER_SHEET_TEMPLATE_BAR_PARM_NM = "COVER_SHEET_TEMPLATE_BAR";
064:
065: private ParameterService parameterService;
066: private BusinessObjectService businessObjectService;
067: private PersistenceStructureService persistenceStructureService;
068:
069: /**
070: * This method uses the values provided to build and populate a cover sheet associated with a given DisbursementVoucher.
071: *
072: * @param templateDirectory The directory where the cover sheet template can be found.
073: * @param templateName The name of the cover sheet template to be used to build the cover sheet.
074: * @param document The DisbursementVoucher the cover sheet will be populated from.
075: * @param outputStream The stream the cover sheet file will be written to.
076: *
077: * @see org.kuali.module.financial.service.DisbursementVoucherCoverSheetService#generateDisbursementVoucherCoverSheet(java.lang.String,
078: * java.lang.String, org.kuali.module.financial.document.DisbursementVoucherDocument, java.io.OutputStream)
079: */
080: public void generateDisbursementVoucherCoverSheet(
081: String templateDirectory, String templateName,
082: DisbursementVoucherDocument document,
083: OutputStream outputStream) throws DocumentException,
084: IOException {
085: DisbursementVoucherDocumentRule documentRule = new DisbursementVoucherDocumentRule();
086: if (documentRule.isCoverSheetPrintable(document)) {
087: String attachment = "";
088: String handling = "";
089: String alien = "";
090: String lines = "";
091: String bar = "";
092: String rlines = "";
093:
094: String docNumber = document.getDocumentNumber();
095: String initiator = document.getDocumentHeader()
096: .getWorkflowDocument().getInitiatorNetworkId();
097: String payee = document.getDvPayeeDetail()
098: .getDisbVchrPayeePersonName();
099:
100: String reason = ((PaymentReasonCode) retrieveObjectByKey(
101: PaymentReasonCode.class, document
102: .getDvPayeeDetail()
103: .getDisbVchrPaymentReasonCode())).getName();
104: String check_total = document.getDisbVchrCheckTotalAmount()
105: .toString();
106:
107: String currency = getValueForKey(
108: new PaymentMethodValuesFinder(), document
109: .getDisbVchrPaymentMethodCode());
110:
111: String address = retrieveAddress(document
112: .getDisbursementVoucherDocumentationLocationCode());
113:
114: // retrieve attachment label
115: if (document.isDisbVchrAttachmentCode()) {
116: attachment = parameterService.getParameterValue(
117: DisbursementVoucherDocument.class,
118: DV_COVER_SHEET_TEMPLATE_ATTACHMENT_PARM_NM);
119: }
120: // retrieve handling label
121: if (document.isDisbVchrSpecialHandlingCode()) {
122: handling = parameterService.getParameterValue(
123: DisbursementVoucherDocument.class,
124: DV_COVER_SHEET_TEMPLATE_HANDLING_PARM_NM);
125: }
126: // retrieve data for alien payment code
127: if (document.getDvPayeeDetail()
128: .isDisbVchrAlienPaymentCode()) {
129: String taxDocumentationLocationCode = parameterService
130: .getParameterValue(
131: DisbursementVoucherDocument.class,
132: DisbursementVoucherRuleConstants.TAX_DOCUMENTATION_LOCATION_CODE_PARM_NM);
133:
134: address = retrieveAddress(taxDocumentationLocationCode);
135: alien = parameterService.getParameterValue(
136: DisbursementVoucherDocument.class,
137: DV_COVER_SHEET_TEMPLATE_ALIEN_PARM_NM);
138: lines = parameterService.getParameterValue(
139: DisbursementVoucherDocument.class,
140: DV_COVER_SHEET_TEMPLATE_LINES_PARM_NM);
141: }
142: // determine if non-employee travel payment reasons
143: DisbursementVoucherDocumentRule dvDocRule = (DisbursementVoucherDocumentRule) SpringContext
144: .getBean(KualiRuleService.class)
145: .getBusinessRulesInstance(document,
146: DisbursementVoucherDocumentRule.class);
147: if (dvDocRule.isTravelNonEmplPaymentReason(document)) {
148: bar = parameterService.getParameterValue(
149: DisbursementVoucherDocument.class,
150: DV_COVER_SHEET_TEMPLATE_BAR_PARM_NM);
151: rlines = parameterService.getParameterValue(
152: DisbursementVoucherDocument.class,
153: DV_COVER_SHEET_TEMPLATE_RLINES_PARM_NM);
154: }
155:
156: try {
157: PdfReader reader = new PdfReader(templateDirectory
158: + File.separator + templateName);
159:
160: // populate form with document values
161: PdfStamper stamper = new PdfStamper(reader,
162: outputStream);
163:
164: AcroFields populatedCoverSheet = stamper
165: .getAcroFields();
166: populatedCoverSheet.setField("initiator", initiator);
167: populatedCoverSheet.setField("attachment", attachment);
168: populatedCoverSheet.setField("currency", currency);
169: populatedCoverSheet.setField("handling", handling);
170: populatedCoverSheet.setField("alien", alien);
171: populatedCoverSheet.setField("payee_name", payee);
172: populatedCoverSheet
173: .setField("check_total", check_total);
174: populatedCoverSheet.setField("docNumber", docNumber);
175: populatedCoverSheet.setField("payment_reason", reason);
176: populatedCoverSheet.setField("destination_address",
177: address);
178: populatedCoverSheet.setField("lines", lines);
179: populatedCoverSheet.setField("bar", bar);
180: populatedCoverSheet.setField("rlines", rlines);
181:
182: stamper.setFormFlattening(true);
183: stamper.close();
184: } catch (DocumentException e) {
185: LOG.error("Error creating coversheet for: " + docNumber
186: + ". ::" + e);
187: throw e;
188: } catch (IOException e) {
189: LOG.error("Error creating coversheet for: " + docNumber
190: + ". ::" + e);
191: throw e;
192: }
193: }
194:
195: }
196:
197: /**
198: * This method is used to retrieve business objects that have a single primary key field without hard-coding
199: * the key field name.
200: *
201: * @param clazz The class type that will be used to retrieve the primary key field names.
202: * @param keyValue The primary key value to be used to lookup the object by.
203: * @return An instance of a business object matching the class type and primary key value given.
204: */
205: private PersistableBusinessObject retrieveObjectByKey(Class clazz,
206: String keyValue) {
207: List primaryKeyFields = persistenceStructureService
208: .listPrimaryKeyFieldNames(clazz);
209: if (primaryKeyFields.size() != 1) {
210: throw new IllegalArgumentException(
211: "multi-part key found. expecting single key field for "
212: + clazz.getName());
213: }
214: Map primaryKeys = new HashMap();
215: primaryKeys.put(primaryKeyFields.get(0), keyValue);
216: PersistableBusinessObject b = businessObjectService
217: .findByPrimaryKey(clazz, primaryKeys);
218:
219: return b;
220: }
221:
222: /**
223: * This method is a helper method to retrieve values from a list based on a primary key provided.
224: *
225: * @param keyValuesFinder KeyValuesFinder that the value will be retrieved from.
226: * @param key The key to the value being retrieved.
227: * @return The value associated with the key provided, or empty string if no value is found.
228: */
229: private String getValueForKey(KeyValuesFinder keyValuesFinder,
230: String key) {
231: for (Iterator i = keyValuesFinder.getKeyValues().iterator(); i
232: .hasNext();) {
233: KeyLabelPair pair = (KeyLabelPair) i.next();
234: if (StringUtils.equals((String) pair.getKey(), key)) {
235: return pair.getLabel();
236: }
237: }
238: return "";
239: }
240:
241: /**
242: * This method contains logic to determine the address the cover sheet should be sent to.
243: *
244: * @param docLocCd A key used to retrieve the document location.
245: * @return The address the cover sheet will be sent to or empty string if no location is found.
246: */
247: private String retrieveAddress(String docLocCd) {
248: String address = "";
249: try {
250: address = ((DisbursementVoucherDocumentationLocation) retrieveObjectByKey(
251: DisbursementVoucherDocumentationLocation.class,
252: docLocCd))
253: .getDisbursementVoucherDocumentationLocationAddress();
254: } catch (NullPointerException e) {
255: // ignored
256: }
257:
258: return address;
259: }
260:
261: // spring injected services
262:
263: /**
264: * Sets the businessObjectService attribute value.
265: * @param businessObjectService The businessObjectService to set.
266: */
267: public void setBusinessObjectService(
268: BusinessObjectService businessObjectService) {
269: this .businessObjectService = businessObjectService;
270: }
271:
272: /**
273: * Sets the persistenceStructureService attribute value.
274: * @param persistenceStructureService The persistenceService to set.
275: */
276: public void setPersistenceStructureService(
277: PersistenceStructureService persistenceStructureService) {
278: this .persistenceStructureService = persistenceStructureService;
279: }
280:
281: /**
282: * Sets the parameterService attribute value.
283: * @param parameterService The parameterService to set.
284: */
285: public void setParameterService(ParameterService parameterService) {
286: this.parameterService = parameterService;
287: }
288: }
|