View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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          //Unassign historical expense.
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       * Gets the travelReimbursementService attribute.
86       *
87       * @return Returns the travelReimbursementService.
88       */
89      protected TravelDocumentService getTravelDocumentService() {
90          return SpringContext.getBean(TravelDocumentService.class);
91      }
92  
93  
94      /**
95       * Gets the kualiRulesService attribute.
96       *
97       * @return Returns the kualiRuleseService.
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 }