1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
50
51
52
53
54
55
56
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
80 if (document.isDisbVchrAttachmentCode()) {
81 attachment = parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.DV_COVER_SHEET_TEMPLATE_ATTACHMENT_PARM_NM);
82 }
83
84 if (document.isDisbVchrSpecialHandlingCode()) {
85 handling = parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.DV_COVER_SHEET_TEMPLATE_HANDLING_PARM_NM);
86 }
87
88
89
90
91
92
93
94
95
96
97
98 String paymentReasonCode = document.getDvPayeeDetail().getDisbVchrPaymentReasonCode();
99
100
101
102
103
104
105
106
107
108 try {
109 PdfReader reader = new PdfReader(templateDirectory + templateName);
110
111
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
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
160
161
162
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
171 }
172
173 return address;
174 }
175
176
177
178
179
180
181
182
183 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
184 this.businessObjectService = businessObjectService;
185 }
186
187
188
189
190
191
192 public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
193 this.persistenceStructureService = persistenceStructureService;
194 }
195
196
197
198
199
200
201 public void setParameterService(ParameterService parameterService) {
202 this.parameterService = parameterService;
203 }
204 }