1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.kuali.kfs.module.tem.document.web.struts;
20
21 import java.util.List;
22 import java.util.Observable;
23 import java.util.Observer;
24
25 import org.apache.log4j.Logger;
26 import org.kuali.kfs.module.tem.businessobject.ImportedExpense;
27 import org.kuali.kfs.module.tem.document.TravelDocument;
28 import org.kuali.kfs.module.tem.document.service.TravelDocumentService;
29 import org.kuali.kfs.module.tem.document.web.bean.TravelMvcWrapperBean;
30 import org.kuali.kfs.module.tem.service.AccountingDistributionService;
31 import org.kuali.kfs.module.tem.util.ExpenseUtils;
32 import org.kuali.kfs.sys.context.SpringContext;
33 import org.kuali.rice.krad.service.KualiRuleService;
34 import org.kuali.rice.krad.util.ObjectUtils;
35
36 public class RemoveImportedExpenseEvent implements Observer {
37
38 public static Logger LOG = Logger.getLogger(RemoveImportedExpenseEvent.class);
39
40 private static final int WRAPPER_ARG_IDX = 0;
41 private static final int SELECTED_LINE_ARG_IDX = 1;
42
43 @Override
44 public void update(Observable arg0, Object arg1) {
45 if (!(arg1 instanceof Object[])) {
46 return;
47 }
48 final Object[] args = (Object[]) arg1;
49 LOG.debug(args[WRAPPER_ARG_IDX]);
50 if (!(args[WRAPPER_ARG_IDX] instanceof TravelMvcWrapperBean)) {
51 return;
52 }
53 final TravelMvcWrapperBean wrapper = (TravelMvcWrapperBean) args[WRAPPER_ARG_IDX];
54
55 final TravelDocument document = wrapper.getTravelDocument();
56 final Integer deleteIndex = (Integer) args[SELECTED_LINE_ARG_IDX];
57
58 ImportedExpense line = document.getImportedExpenses().get(deleteIndex);
59 document.removeExpense(line, deleteIndex);
60
61
62 ExpenseUtils.assignExpense(line.getHistoricalTravelExpenseId(), null,null,null, false);
63 List<ImportedExpense> importedExpenses = wrapper.getNewImportedExpenseLines();
64
65
66 if (wrapper.getNewImportedExpenseLines().size() > deleteIndex.intValue()){
67 wrapper.getNewImportedExpenseLines().remove(deleteIndex.intValue());
68 }
69 wrapper.setDistribution(getAccountingDistributionService().buildDistributionFrom(document));
70
71 }
72
73 private int getNextDetailIndex(List<ImportedExpense> importedExpenses, Long id){
74 int index = 0;
75 for(ImportedExpense detailLine: importedExpenses){
76 if(ObjectUtils.isNotNull(detailLine.getExpenseParentId()) && detailLine.getExpenseParentId().equals(id)){
77 return index;
78 }
79 index++;
80 }
81 return -1;
82 }
83
84
85
86
87
88
89 protected TravelDocumentService getTravelDocumentService() {
90 return SpringContext.getBean(TravelDocumentService.class);
91 }
92
93
94
95
96
97
98
99 protected KualiRuleService getRuleService() {
100 return SpringContext.getBean(KualiRuleService.class);
101 }
102
103 protected AccountingDistributionService getAccountingDistributionService() {
104 return SpringContext.getBean(AccountingDistributionService.class);
105 }
106 }