001/*
002 * Copyright 2006 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */
016package org.kuali.ole.fp.document.service.impl;
017
018import java.io.IOException;
019import java.io.OutputStream;
020
021import org.kuali.ole.fp.businessobject.DisbursementVoucherDocumentationLocation;
022import org.kuali.ole.fp.businessobject.PaymentReasonCode;
023import org.kuali.ole.fp.businessobject.options.PaymentMethodValuesFinder;
024import org.kuali.ole.fp.document.DisbursementVoucherConstants;
025import org.kuali.ole.fp.document.DisbursementVoucherDocument;
026import org.kuali.ole.fp.document.service.DisbursementVoucherCoverSheetService;
027import org.kuali.rice.coreservice.framework.parameter.ParameterService;
028import org.kuali.rice.kew.api.WorkflowDocument;
029import org.kuali.rice.krad.service.BusinessObjectService;
030import org.kuali.rice.krad.service.PersistenceStructureService;
031import org.kuali.rice.krad.util.ObjectUtils;
032
033import com.lowagie.text.DocumentException;
034import com.lowagie.text.pdf.AcroFields;
035import com.lowagie.text.pdf.PdfReader;
036import com.lowagie.text.pdf.PdfStamper;
037
038/**
039 * This is the default implementation of the DisbursementVoucherCoverSheetService interface.
040 */
041public class DisbursementVoucherCoverSheetServiceImpl implements DisbursementVoucherCoverSheetService {
042    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherCoverSheetServiceImpl.class);
043
044    protected ParameterService parameterService;
045    protected BusinessObjectService businessObjectService;
046    protected PersistenceStructureService persistenceStructureService;
047
048    /**
049     * This method uses the values provided to build and populate a cover sheet associated with a given DisbursementVoucher.
050     *
051     * @param templateDirectory The directory where the cover sheet template can be found.
052     * @param templateName The name of the cover sheet template to be used to build the cover sheet.
053     * @param document The DisbursementVoucher the cover sheet will be populated from.
054     * @param outputStream The stream the cover sheet file will be written to.
055     * @see org.kuali.ole.fp.document.service.DisbursementVoucherCoverSheetService#generateDisbursementVoucherCoverSheet(java.lang.String,
056     *      java.lang.String, org.kuali.ole.fp.document.DisbursementVoucherDocument, java.io.OutputStream)
057     */
058    @Override
059    public void generateDisbursementVoucherCoverSheet(String templateDirectory, String templateName, DisbursementVoucherDocument document, OutputStream outputStream) throws DocumentException, IOException {
060        if (this.isCoverSheetPrintable(document)) {
061            String attachment = "";
062            String handling = "";
063            String alien = "";
064            String lines = "";
065            String bar = "";
066            String rlines = "";
067
068            String docNumber = document.getDocumentNumber();
069            String initiator = document.getDocumentHeader().getWorkflowDocument().getInitiatorPrincipalId();
070            String payee = document.getDvPayeeDetail().getDisbVchrPayeePersonName();
071
072            String reason = businessObjectService.findBySinglePrimaryKey(PaymentReasonCode.class, document.getDvPayeeDetail().getDisbVchrPaymentReasonCode()).getName();
073            String check_total = document.getDisbVchrCheckTotalAmount().toString();
074
075            String currency = new PaymentMethodValuesFinder().getKeyLabel(document.getDisbVchrPaymentMethodCode());
076
077            String address = retrieveAddress(document.getDisbursementVoucherDocumentationLocationCode());
078
079            // retrieve attachment label
080            if (document.isDisbVchrAttachmentCode()) {
081                attachment = parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.DV_COVER_SHEET_TEMPLATE_ATTACHMENT_PARM_NM);
082            }
083            // retrieve handling label
084            if (document.isDisbVchrSpecialHandlingCode()) {
085                handling = parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.DV_COVER_SHEET_TEMPLATE_HANDLING_PARM_NM);
086            }
087            // retrieve data for alien payment code
088            //Commented for the jira issue OLE-3415
089            /*if (document.getDvPayeeDetail().isDisbVchrAlienPaymentCode()) {
090                String taxDocumentationLocationCode = parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.TAX_DOCUMENTATION_LOCATION_CODE_PARM_NM);
091
092                address = retrieveAddress(taxDocumentationLocationCode);
093                alien = parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.DV_COVER_SHEET_TEMPLATE_ALIEN_PARM_NM);
094                lines = parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.DV_COVER_SHEET_TEMPLATE_LINES_PARM_NM);
095            }*/
096
097            // determine if non-employee travel payment reasons
098            String paymentReasonCode = document.getDvPayeeDetail().getDisbVchrPaymentReasonCode();
099          //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}