1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.fp.batch.service.impl;
17
18 import java.util.List;
19
20 import org.apache.log4j.Logger;
21 import org.kuali.ole.fp.document.DistributionOfIncomeAndExpenseDocument;
22 import org.kuali.ole.sys.OLEConstants;
23 import org.kuali.ole.sys.businessobject.ElectronicPaymentClaim;
24 import org.kuali.ole.sys.businessobject.SourceAccountingLine;
25 import org.kuali.ole.sys.context.SpringContext;
26 import org.kuali.ole.sys.service.ElectronicPaymentClaimingDocumentGenerationStrategy;
27 import org.kuali.ole.sys.service.ElectronicPaymentClaimingService;
28 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
29 import org.kuali.rice.kew.api.KewApiConstants;
30 import org.kuali.rice.kew.api.doctype.DocumentTypeService;
31 import org.kuali.rice.kew.api.exception.WorkflowException;
32 import org.kuali.rice.kim.api.identity.Person;
33 import org.kuali.rice.krad.bo.Note;
34 import org.kuali.rice.krad.service.DocumentService;
35
36 public class DistributionOfIncomeAndExpenseElectronicPaymentClaimingHelperStrategyImpl implements ElectronicPaymentClaimingDocumentGenerationStrategy {
37 private static final Logger LOG = Logger.getLogger(DistributionOfIncomeAndExpenseElectronicPaymentClaimingHelperStrategyImpl.class);
38
39 protected DocumentService documentService;
40 protected ElectronicPaymentClaimingService electronicPaymentClaimingService;
41 protected ParameterService parameterService;
42
43
44
45
46
47 protected final static String DOCUMENT_DESCRIPTION_PARAM_NAME = "ELECTRONIC_FUNDS_DOCUMENT_DESCRIPTION";
48 protected final static String URL_PREFIX = "financial";
49 protected final static String URL_MIDDLE = ".do?methodToCall=docHandler&command=";
50 protected final static String URL_SUFFIX = "&docId=";
51 protected final static String URL_DOC_TYPE = "DistributionOfIncomeAndExpense";
52
53
54
55
56 public String createDocumentFromElectronicPayments(List<ElectronicPaymentClaim> electronicPayments, Person user) {
57 DistributionOfIncomeAndExpenseDocument document = null;
58 try {
59 document = (DistributionOfIncomeAndExpenseDocument) documentService.getNewDocument(getClaimingDocumentWorkflowDocumentType());
60 addAccountingLinesToDocument(document, electronicPayments);
61 addDescriptionToDocument(document);
62 addNotesToDocument(document, electronicPayments, user);
63 documentService.saveDocument(document);
64 electronicPaymentClaimingService.claimElectronicPayments(electronicPayments, document.getDocumentNumber());
65 }
66 catch (WorkflowException we) {
67 throw new RuntimeException("WorkflowException while creating a DistributionOfIncomeAndExpenseDocument to claim ElectronicPaymentClaim records.", we);
68 }
69
70 return getURLForDocument(document);
71 }
72
73
74
75
76
77
78
79 protected String getURLForDocument(DistributionOfIncomeAndExpenseDocument doc) {
80 StringBuilder url = new StringBuilder();
81 url.append(URL_PREFIX);
82 url.append(getUrlDocType());
83 url.append(URL_MIDDLE);
84 url.append(KewApiConstants.ACTIONLIST_COMMAND);
85 url.append(URL_SUFFIX);
86 url.append(doc.getDocumentNumber());
87 return url.toString();
88 }
89
90
91
92
93
94
95
96
97 protected void addNotesToDocument(DistributionOfIncomeAndExpenseDocument claimingDoc, List<ElectronicPaymentClaim> claims, Person user) {
98 for (String noteText : electronicPaymentClaimingService.constructNoteTextsForClaims(claims)) {
99 try {
100 Note note = documentService.createNoteFromDocument(claimingDoc, noteText);
101 claimingDoc.addNote(note);
102 }
103 catch (Exception e) {
104 LOG.error("Exception while attempting to create or add note: ", e);
105 }
106 }
107 }
108
109
110
111
112
113
114
115 protected void addAccountingLinesToDocument(DistributionOfIncomeAndExpenseDocument document, List<ElectronicPaymentClaim> electronicPayments) {
116 for (ElectronicPaymentClaim payment : electronicPayments) {
117 SourceAccountingLine claimingAccountingLine = copyAccountingLineToNew(payment.getGeneratingAccountingLine(), createNewAccountingLineForDocument(document));
118 document.addSourceAccountingLine(claimingAccountingLine);
119 }
120 }
121
122
123
124
125
126
127 protected void addDescriptionToDocument(DistributionOfIncomeAndExpenseDocument document) {
128 String description = parameterService.getParameterValueAsString(DistributionOfIncomeAndExpenseDocument.class, DistributionOfIncomeAndExpenseElectronicPaymentClaimingHelperStrategyImpl.DOCUMENT_DESCRIPTION_PARAM_NAME);
129 if (description != null) {
130 document.getDocumentHeader().setDocumentDescription(description);
131 }
132 else {
133 throw new RuntimeException("There is evidently no value for Parameter OLE-FP / Distribution of Income and Expense / " + DistributionOfIncomeAndExpenseElectronicPaymentClaimingHelperStrategyImpl.DOCUMENT_DESCRIPTION_PARAM_NAME + "; please set a value before claiming Electronic Payments");
134 }
135 }
136
137
138
139
140
141
142
143 protected SourceAccountingLine createNewAccountingLineForDocument(DistributionOfIncomeAndExpenseDocument document) {
144 try {
145 Class<? extends SourceAccountingLine> accountingLineClass = document.getSourceAccountingLineClass();
146 return accountingLineClass.newInstance();
147 }
148 catch (Exception ex) {
149 throw new RuntimeException( "Unable to create source accounting line for document: " + document, ex);
150 }
151 }
152
153
154
155
156
157
158
159 protected SourceAccountingLine copyAccountingLineToNew(SourceAccountingLine line, SourceAccountingLine newLine) {
160 newLine.setChartOfAccountsCode(line.getChartOfAccountsCode());
161 newLine.setAccountNumber(line.getAccountNumber());
162 newLine.setSubAccountNumber(line.getSubAccountNumber());
163 newLine.setFinancialObjectCode(line.getFinancialObjectCode());
164 newLine.setFinancialSubObjectCode(line.getFinancialSubObjectCode());
165 newLine.setProjectCode(line.getProjectCode());
166 newLine.setOrganizationReferenceId(line.getOrganizationReferenceId());
167 newLine.setAmount(line.getAmount());
168 return newLine;
169 }
170
171
172
173
174
175 public String getClaimingDocumentWorkflowDocumentType() {
176 return OLEConstants.FinancialDocumentTypeCodes.DISTRIBUTION_OF_INCOME_AND_EXPENSE;
177 }
178
179
180
181
182 protected String getUrlDocType() {
183 return DistributionOfIncomeAndExpenseElectronicPaymentClaimingHelperStrategyImpl.URL_DOC_TYPE;
184 }
185
186
187
188
189
190
191 public String getDocumentLabel() {
192 try {
193 return SpringContext.getBean(DocumentTypeService.class).getDocumentTypeByName(getClaimingDocumentWorkflowDocumentType()).getLabel();
194 } catch (Exception e) {
195 LOG.error("Caught Exception trying to get Workflow Document Type" + e);
196 }
197 return "DI";
198 }
199
200
201
202
203
204
205 public boolean userMayUseToClaim(Person claimingUser) {
206 final String documentTypeName = getClaimingDocumentWorkflowDocumentType();
207
208 final boolean canClaim = electronicPaymentClaimingService.isAuthorizedForClaimingElectronicPayment(claimingUser, documentTypeName)
209 || electronicPaymentClaimingService.isAuthorizedForClaimingElectronicPayment(claimingUser, null);
210
211 return canClaim;
212 }
213
214
215
216
217 public boolean isDocumentReferenceValid(String referenceDocumentNumber) {
218 boolean valid = false;
219 try {
220 long docNumberAsLong = Long.parseLong(referenceDocumentNumber);
221 if (docNumberAsLong > 0L) {
222 valid = documentService.documentExists(referenceDocumentNumber);
223 }
224 }
225 catch (NumberFormatException nfe) {
226
227 LOG.warn( "Unable to parse referenceDocumentNumber (" + referenceDocumentNumber + ") into a number: " + nfe.getMessage() );
228 }
229 return valid;
230 }
231
232
233
234
235
236
237 public void setDocumentService(DocumentService documentService) {
238 this.documentService = documentService;
239 }
240
241
242
243
244
245
246 public void setElectronicPaymentClaimingService(ElectronicPaymentClaimingService electronicPaymentClaimingService) {
247 this.electronicPaymentClaimingService = electronicPaymentClaimingService;
248 }
249
250
251
252
253
254
255 public void setParameterService(ParameterService parameterService) {
256 this.parameterService = parameterService;
257 }
258
259
260 }