1 package org.kuali.ole.sys.form;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.apache.struts.upload.FormFile;
5 import org.kuali.ole.coa.businessobject.Account;
6 import org.kuali.ole.coa.businessobject.ObjectCode;
7 import org.kuali.ole.coa.businessobject.SubAccount;
8 import org.kuali.ole.coa.businessobject.SubObjectCode;
9 import org.kuali.ole.olekrad.form.OLEKRADTransactionalForm;
10 import org.kuali.ole.sys.OLEConstants;
11 import org.kuali.ole.sys.OLEPropertyConstants;
12 import org.kuali.ole.sys.businessobject.*;
13 import org.kuali.ole.sys.context.SpringContext;
14 import org.kuali.ole.sys.document.AccountingDocument;
15 import org.kuali.ole.sys.document.OLEAccountingDocument;
16 import org.kuali.ole.sys.document.web.form.OLEFinancialSystemTransactionalDocumentFormBase;
17 import org.kuali.ole.sys.service.impl.OleParameterConstants;
18 import org.kuali.rice.core.api.config.property.ConfigurationService;
19 import org.kuali.rice.core.web.format.CurrencyFormatter;
20 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
21 import org.kuali.rice.kns.service.BusinessObjectDictionaryService;
22 import org.kuali.rice.krad.service.DataDictionaryService;
23 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
24 import org.kuali.rice.krad.util.KRADConstants;
25 import org.kuali.rice.krad.util.ObjectUtils;
26 import org.kuali.rice.krad.web.form.TransactionalDocumentFormBase;
27 import org.springframework.web.multipart.commons.CommonsMultipartResolver;
28
29 import javax.servlet.http.HttpServletRequest;
30 import java.util.*;
31
32
33
34
35 public class OLEKualiAccountingDocumentFormBase extends OLEFinancialSystemTransactionalDocumentFormBase {
36 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OLEKualiAccountingDocumentFormBase.class);
37
38 protected SourceAccountingLine newSourceLine;
39 protected TargetAccountingLine newTargetLine;
40
41
42
43 protected Map editableAccounts;
44 protected Map forcedLookupOptionalFields;
45
46
47 protected FormFile sourceFile;
48 protected FormFile targetFile;
49 protected boolean hideDetails = false;
50
51
52
53
54 public OLEKualiAccountingDocumentFormBase() {
55 super();
56
57
58 editableAccounts = new HashMap();
59
60 forcedLookupOptionalFields = new HashMap();
61 }
62
63
64
65
66
67
68
69
70
71 public void populate(HttpServletRequest request) {
72
73 final String methodToCall = this.getMethodToCall();
74 final Map parameterMap = request.getParameterMap();
75
76 populateAccountingLinesForResponse(methodToCall, parameterMap);
77
78 setDocTypeName(((DataDictionaryService) KRADServiceLocatorWeb.getDataDictionaryService()).getDataDictionary().getDocumentEntry(getDocument().getClass().getName()).getDocumentTypeName());
79 }
80
81
82
83
84
85
86 protected void populateAccountingLinesForResponse(String methodToCall, Map parameterMap) {
87 populateSourceAccountingLine(getNewSourceLine(), OLEPropertyConstants.NEW_SOURCE_LINE, parameterMap);
88 populateTargetAccountingLine(getNewTargetLine(), OLEPropertyConstants.NEW_TARGET_LINE, parameterMap);
89
90
91
92 if (!StringUtils.equals(methodToCall, OLEConstants.COPY_METHOD) && !StringUtils.equals(methodToCall, OLEConstants.ERRORCORRECT_METHOD)) {
93 populateAccountingLines(parameterMap);
94 }
95 }
96
97
98
99
100
101
102
103
104 protected void populateAccountingLines(Map parameterMap) {
105 Iterator sourceLines = getFinancialDocument().getSourceAccountingLines().iterator();
106 int count = 0;
107 while (sourceLines.hasNext()) {
108 SourceAccountingLine sourceLine = (SourceAccountingLine) sourceLines.next();
109 populateSourceAccountingLine(sourceLine, OLEPropertyConstants.DOCUMENT+"."+OLEPropertyConstants.SOURCE_ACCOUNTING_LINE+"["+count+"]", parameterMap);
110 count += 1;
111 }
112
113 Iterator targetLines = getFinancialDocument().getTargetAccountingLines().iterator();
114 count = 0;
115 while (targetLines.hasNext()) {
116 TargetAccountingLine targetLine = (TargetAccountingLine) targetLines.next();
117 populateTargetAccountingLine(targetLine, OLEPropertyConstants.DOCUMENT+"."+OLEPropertyConstants.TARGET_ACCOUNTING_LINE+"["+count+"]", parameterMap);
118 count += 1;
119 }
120 }
121
122
123
124
125
126
127
128
129
130
131 public void populateSourceAccountingLine(SourceAccountingLine sourceLine, String accountingLinePropertyName, Map parameterMap) {
132 populateAccountingLine(sourceLine, accountingLinePropertyName, parameterMap);
133 }
134
135
136
137
138
139
140
141
142
143
144 public void populateTargetAccountingLine(TargetAccountingLine targetLine, String accountingLinePropertyName, Map parameterMap) {
145 populateAccountingLine(targetLine, accountingLinePropertyName, parameterMap);
146 }
147
148
149
150
151
152
153
154
155 @SuppressWarnings("deprecation")
156 protected void populateAccountingLine(AccountingLine line, String accountingLinePropertyName, Map parameterMap) {
157 SpringContext.getBean(BusinessObjectDictionaryService.class).performForceUppercase(line);
158
159 line.setDocumentNumber(getDocument().getDocumentNumber());
160
161 if (ObjectUtils.isNull(line.getAccount())) {
162 line.setAccount(new Account());
163 }
164 line.getAccount().setChartOfAccountsCode(line.getChartOfAccountsCode());
165
166 if (ObjectUtils.isNull(line.getObjectCode())) {
167 line.setObjectCode(new ObjectCode());
168 }
169 line.getObjectCode().setUniversityFiscalYear(getFinancialDocument().getPostingYear());
170 line.getObjectCode().setChartOfAccountsCode(line.getChartOfAccountsCode());
171
172 if (ObjectUtils.isNull(line.getSubAccount())) {
173 line.setSubAccount(new SubAccount());
174 }
175 line.getSubAccount().setChartOfAccountsCode(line.getChartOfAccountsCode());
176 line.getSubAccount().setAccountNumber(line.getAccountNumber());
177
178 if (ObjectUtils.isNull(line.getSubObjectCode())) {
179 line.setSubObjectCode(new SubObjectCode());
180 }
181 line.getSubObjectCode().setChartOfAccountsCode(line.getChartOfAccountsCode());
182 line.getSubObjectCode().setAccountNumber(line.getAccountNumber());
183 line.getSubObjectCode().setFinancialObjectCode(line.getFinancialObjectCode());
184 line.getSubObjectCode().setUniversityFiscalYear(getFinancialDocument().getPostingYear());
185
186 repopulateOverrides(line, accountingLinePropertyName, parameterMap);
187
188 AccountingLineOverride.populateFromInput(line);
189 }
190
191
192
193
194
195
196
197 protected void repopulateOverrides(AccountingLine line, String accountingLinePropertyName, Map parameterMap) {
198 OLEAccountingLineOverride.determineNeededOverrides(getFinancialDocument(), line);
199 if (line.getAccountExpiredOverrideNeeded()) {
200 if (LOG.isDebugEnabled()) {
201 StringUtils.join(parameterMap.keySet(), "\n");
202 }
203 if (parameterMap.containsKey(accountingLinePropertyName+".accountExpiredOverride.present")) {
204 line.setAccountExpiredOverride(parameterMap.containsKey(accountingLinePropertyName+".accountExpiredOverride"));
205 }
206 } else {
207 line.setAccountExpiredOverride(false);
208 }
209 if (line.isObjectBudgetOverrideNeeded()) {
210 if (parameterMap.containsKey(accountingLinePropertyName+".objectBudgetOverride.present")) {
211 line.setObjectBudgetOverride(parameterMap.containsKey(accountingLinePropertyName+".objectBudgetOverride"));
212 }
213 } else {
214 line.setObjectBudgetOverride(false);
215 }
216 }
217
218
219
220
221
222
223 public OLEAccountingDocument getFinancialDocument() {
224 return (OLEAccountingDocument) getDocument();
225 }
226
227
228
229
230 public TargetAccountingLine getNewTargetLine() {
231 if (newTargetLine == null) {
232 newTargetLine = createNewTargetAccountingLine(getFinancialDocument());
233 }
234 return newTargetLine;
235 }
236
237
238
239
240 public void setNewTargetLine(TargetAccountingLine newExpenseLine) {
241 this.newTargetLine = newExpenseLine;
242 }
243
244
245
246
247 public SourceAccountingLine getNewSourceLine() {
248 if (newSourceLine == null) {
249 newSourceLine = createNewSourceAccountingLine(getFinancialDocument());
250 }
251 return newSourceLine;
252 }
253
254
255
256
257 public void setNewSourceLine(SourceAccountingLine newIncomeLine) {
258 this.newSourceLine = newIncomeLine;
259 }
260
261
262
263
264 public FormFile getSourceFile() {
265 return sourceFile;
266 }
267
268
269
270
271 public void setSourceFile(FormFile sourceFile) {
272 this.sourceFile = sourceFile;
273 }
274
275
276
277
278 public FormFile getTargetFile() {
279 return targetFile;
280 }
281
282
283
284
285 public void setTargetFile(FormFile targetFile) {
286 this.targetFile = targetFile;
287 }
288
289
290
291
292
293 public Map getEditableAccounts() {
294 return editableAccounts;
295 }
296
297
298
299
300 public void setEditableAccounts(Map editableAccounts) {
301 this.editableAccounts = editableAccounts;
302 }
303
304
305
306
307 public boolean isHideDetails() {
308 return hideDetails;
309 }
310
311
312
313
314
315 public boolean getHideDetails() {
316 return isHideDetails();
317 }
318
319
320
321
322 public void setHideDetails(boolean hideDetails) {
323 this.hideDetails = hideDetails;
324 }
325
326
327
328
329
330
331 public String getCurrencyFormattedSourceTotal() {
332 return (String) new CurrencyFormatter().format(getFinancialDocument().getSourceTotal());
333 }
334
335
336
337
338
339
340 public String getCurrencyFormattedTargetTotal() {
341 return (String) new CurrencyFormatter().format(getFinancialDocument().getTargetTotal());
342 }
343
344
345
346
347 public String getAccountingLineImportInstructionsUrl() {
348 return SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.EXTERNALIZABLE_HELP_URL_KEY) + SpringContext.getBean(ParameterService.class).getParameterValueAsString(OleParameterConstants.FINANCIAL_SYSTEM_DOCUMENT.class, OLEConstants.FinancialApcParms.ACCOUNTING_LINE_IMPORT_HELP);
349 }
350
351
352
353
354
355 protected SourceAccountingLine createNewSourceAccountingLine(OLEAccountingDocument financialDocument) {
356 if (financialDocument == null) {
357 throw new IllegalArgumentException("invalid (null) document");
358 }
359 try {
360 return (SourceAccountingLine) financialDocument.getSourceAccountingLineClass().newInstance();
361 }
362 catch (Exception e) {
363 throw new IllegalArgumentException("unable to create a new source accounting line", e);
364 }
365 }
366
367
368
369
370
371 protected TargetAccountingLine createNewTargetAccountingLine(OLEAccountingDocument financialDocument) {
372 if (financialDocument == null) {
373 throw new IllegalArgumentException("invalid (null) document");
374 }
375 try {
376 return (TargetAccountingLine) financialDocument.getTargetAccountingLineClass().newInstance();
377 }
378 catch (Exception e) {
379 throw new IllegalArgumentException("unable to create a new target accounting line", e);
380 }
381 }
382
383
384
385
386
387
388
389
390 protected List<AccountingLine> harvestAccountingLines(List lines) {
391 List<AccountingLine> accountingLines = new ArrayList<AccountingLine>();
392 for (Object o : lines) {
393 if (o instanceof AccountingLine) {
394 accountingLines.add((AccountingLine) o);
395 }
396 }
397 return accountingLines;
398 }
399
400
401
402
403
404
405 public void setForcedLookupOptionalFields(Map fieldMap) {
406 forcedLookupOptionalFields = fieldMap;
407 }
408
409
410
411
412
413
414 public Map getForcedLookupOptionalFields() {
415 return forcedLookupOptionalFields;
416 }
417
418
419
420
421
422
423
424 protected void customInitMaxUploadSizes() {
425 long maxUploadSize = ((CommonsMultipartResolver)SpringContext.getBean("multipartResolver")).getFileUpload().getSizeMax();
426
427 }
428
429
430
431
432
433 public boolean shouldMethodToCallParameterBeUsed(String methodToCallParameterName, String methodToCallParameterValue, HttpServletRequest request) {
434 if(StringUtils.equals(methodToCallParameterName, KRADConstants.DISPATCH_REQUEST_PARAMETER)) {
435 if(this.getExcludedmethodToCall().contains(methodToCallParameterValue)) {
436 return true;
437 }
438 }
439
440 return false;
441 }
442
443
444
445
446
447 protected List<String> getExcludedmethodToCall() {
448 return new ArrayList<String>();
449 }
450 }