View Javadoc
1   /*
2    * Copyright 2009 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl1.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sec.businessobject.lookup;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.ole.coa.service.ObjectTypeService;
25  import org.kuali.ole.gl.GeneralLedgerConstants;
26  import org.kuali.ole.gl.businessobject.AccountBalance;
27  import org.kuali.ole.gl.businessobject.TransientBalanceInquiryAttributes;
28  import org.kuali.ole.gl.businessobject.lookup.AccountBalanceByConsolidationLookupableHelperServiceImpl;
29  import org.kuali.ole.gl.service.AccountBalanceService;
30  import org.kuali.ole.sec.SecKeyConstants;
31  import org.kuali.ole.sec.service.AccessSecurityService;
32  import org.kuali.ole.sec.util.SecUtil;
33  import org.kuali.ole.sys.OLEConstants;
34  import org.kuali.ole.sys.OLEKeyConstants;
35  import org.kuali.ole.sys.OLEPropertyConstants;
36  import org.kuali.ole.sys.context.SpringContext;
37  import org.kuali.rice.core.api.config.property.ConfigurationService;
38  import org.kuali.rice.krad.bo.BusinessObject;
39  import org.kuali.rice.krad.lookup.CollectionIncomplete;
40  import org.kuali.rice.krad.util.GlobalVariables;
41  
42  
43  /**
44   * Override of AccountBalanceByConsolidation lookup helper to integrate access security
45   */
46  public class AccessSecurityAccountBalanceByConsolidationLookupableHelperServiceImpl extends AccountBalanceByConsolidationLookupableHelperServiceImpl {
47      protected AccessSecurityService accessSecurityService;
48      protected ObjectTypeService objectTypeService;
49      protected ConfigurationService kualiConfigurationService;
50  
51      /**
52       * Checks security on the detail balance records, if user does not have access to view any of those records they are removed and total lines
53       * are updated
54       * 
55       * @see org.kuali.ole.gl.businessobject.lookup.AccountBalanceByConsolidationLookupableHelperServiceImpl#getSearchResults(java.util.Map)
56       */
57      @Override
58      public List<? extends BusinessObject> getSearchResults(Map fieldValues) {
59          AccountBalanceByConsolidationLookupableHelperServiceImpl helperServiceImpl = new AccountBalanceByConsolidationLookupableHelperServiceImpl();
60          helperServiceImpl.setAccountBalanceService(SpringContext.getBean(AccountBalanceService.class));
61          List<? extends BusinessObject> results = helperServiceImpl.getSearchResults(fieldValues);
62  
63          // first 7 items of results are total lines, so we need to check any detail lines after than
64          if (results.size() > 7) {
65              List details = results.subList(7, results.size());
66  
67              int resultSizeBeforeRestrictions = details.size();
68              accessSecurityService.applySecurityRestrictionsForGLInquiry(details, GlobalVariables.getUserSession().getPerson());
69  
70              accessSecurityService.compareListSizeAndAddMessageIfChanged(resultSizeBeforeRestrictions, details, SecKeyConstants.MESSAGE_BALANCE_INQUIRY_RESULTS_RESTRICTED);
71  
72              // if details have changed we need to update totals
73              if (resultSizeBeforeRestrictions != details.size()) {
74                  String subAccountNumber = (String) fieldValues.get(OLEPropertyConstants.SUB_ACCOUNT_NUMBER);
75                  String fiscalYear = (String) fieldValues.get(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR);
76                  Integer universityFiscalYear = new Integer(Integer.parseInt(fiscalYear));
77  
78                  // Dashes means no sub account number
79                  if (OLEConstants.getDashSubAccountNumber().equals(subAccountNumber)) {
80                      subAccountNumber = "";
81                  }
82  
83                  TransientBalanceInquiryAttributes dbo = ((AccountBalance) results.get(0)).getDummyBusinessObject();
84  
85                  List totals = buildAccountBalanceTotals(details, universityFiscalYear, subAccountNumber, dbo);
86                  totals.addAll(details);
87  
88                  return new CollectionIncomplete(totals, new Long(totals.size()));
89              }
90          }
91  
92          return results;
93      }
94  
95      /**
96       * Rebuilds the account balance total lines, logic mostly duplicated from AccountBalanceServiceImpl:findAccountBalanceByConsolidation
97       * 
98       * @param balanceDetails List of AccountBalance detail lines
99       * @param universityFiscalYear Fiscal Year being searched
100      * @param subAccountNumber Sub Account number being searched 
101      * @param dbo TransientBalanceInquiryAttributes object that will be set on total lines
102      * @return List of AccountBalance total lines
103      */
104     protected List buildAccountBalanceTotals(List balanceDetails, Integer universityFiscalYear, String subAccountNumber, TransientBalanceInquiryAttributes dbo) {
105         List totals = new ArrayList();
106 
107         List incomeObjectTypes = objectTypeService.getBasicIncomeObjectTypes(universityFiscalYear);
108         String incomeTransferObjectType = objectTypeService.getIncomeTransferObjectType(universityFiscalYear);
109         List expenseObjectTypes = objectTypeService.getBasicExpenseObjectTypes(universityFiscalYear);
110         String expenseTransferObjectType = objectTypeService.getExpenseTransferObjectType(universityFiscalYear);
111 
112         AccountBalance income = new AccountBalance(kualiConfigurationService.getPropertyValueAsString(OLEKeyConstants.AccountBalanceService.INCOME));
113         AccountBalance incomeTransfers = new AccountBalance(kualiConfigurationService.getPropertyValueAsString(OLEKeyConstants.AccountBalanceService.INCOME_FROM_TRANSFERS));
114         AccountBalance incomeTotal = new AccountBalance(kualiConfigurationService.getPropertyValueAsString(OLEKeyConstants.AccountBalanceService.INCOME_TOTAL));
115         AccountBalance expense = new AccountBalance(kualiConfigurationService.getPropertyValueAsString(OLEKeyConstants.AccountBalanceService.EXPENSE));
116         AccountBalance expenseTransfers = new AccountBalance(kualiConfigurationService.getPropertyValueAsString(OLEKeyConstants.AccountBalanceService.EXPENSE_FROM_TRANSFERS));
117         AccountBalance expenseTotal = new AccountBalance(kualiConfigurationService.getPropertyValueAsString(OLEKeyConstants.AccountBalanceService.EXPENSE_TOTAL));
118         AccountBalance total = new AccountBalance(kualiConfigurationService.getPropertyValueAsString(OLEKeyConstants.AccountBalanceService.TOTAL));
119 
120         totals.add(income);
121         totals.add(incomeTransfers);
122         totals.add(incomeTotal);
123         totals.add(expense);
124         totals.add(expenseTransfers);
125         totals.add(expenseTotal);
126         totals.add(total);
127 
128         // set the dummy business object that was built in super lookupable
129         for (Iterator iterator = totals.iterator(); iterator.hasNext();) {
130             AccountBalance totalBalance = (AccountBalance) iterator.next();
131             total.setDummyBusinessObject(dbo);
132         }
133 
134         boolean subAccountBlank = StringUtils.isBlank(subAccountNumber);
135 
136         // iterate over details and update total line based on object type
137         for (Iterator iterator = balanceDetails.iterator(); iterator.hasNext();) {
138             AccountBalance detail = (AccountBalance) iterator.next();
139             String objectType = detail.getFinancialObject().getFinancialObjectTypeCode();
140 
141             if (incomeObjectTypes.contains(objectType)) {
142                 String transferExpenseCode = detail.getFinancialObject().getFinancialObjectLevel().getFinancialConsolidationObject().getFinConsolidationObjectCode();
143                 if (!subAccountBlank && transferExpenseCode.equals(GeneralLedgerConstants.INCOME_OR_EXPENSE_TRANSFER_CONSOLIDATION_CODE)) {
144                     incomeTransfers.add(detail);
145                 }
146                 else {
147                     income.add(detail);
148                 }
149 
150                 incomeTotal.add(detail);
151             }
152 
153             if (incomeTransferObjectType.equals(objectType)) {
154                 incomeTransfers.add(detail);
155                 incomeTotal.add(detail);
156             }
157 
158             if (expenseObjectTypes.contains(objectType)) {
159                 String transferExpenseCode = detail.getFinancialObject().getFinancialObjectLevel().getFinancialConsolidationObject().getFinConsolidationObjectCode();
160                 if (!subAccountBlank && transferExpenseCode.equals(GeneralLedgerConstants.INCOME_OR_EXPENSE_TRANSFER_CONSOLIDATION_CODE)) {
161                     expenseTransfers.add(detail);
162                 }
163                 else {
164                     expense.add(detail);
165                 }
166 
167                 expenseTotal.add(detail);
168             }
169 
170             if (expenseTransferObjectType.equals(objectType)) {
171                 expenseTransfers.add(detail);
172                 expenseTransfers.add(detail);
173             }
174         }
175 
176         // Add up variances
177         income.getDummyBusinessObject().setGenericAmount(income.getAccountLineActualsBalanceAmount().add(income.getAccountLineEncumbranceBalanceAmount()).subtract(income.getCurrentBudgetLineBalanceAmount()));
178         incomeTransfers.getDummyBusinessObject().setGenericAmount(incomeTransfers.getAccountLineActualsBalanceAmount().add(incomeTransfers.getAccountLineEncumbranceBalanceAmount()).subtract(incomeTransfers.getCurrentBudgetLineBalanceAmount()));
179         incomeTotal.getDummyBusinessObject().setGenericAmount(income.getDummyBusinessObject().getGenericAmount().add(incomeTransfers.getDummyBusinessObject().getGenericAmount()));
180 
181         expense.getDummyBusinessObject().setGenericAmount(expense.getCurrentBudgetLineBalanceAmount().subtract(expense.getAccountLineActualsBalanceAmount()).subtract(expense.getAccountLineEncumbranceBalanceAmount()));
182         expenseTransfers.getDummyBusinessObject().setGenericAmount(expenseTransfers.getCurrentBudgetLineBalanceAmount().subtract(expenseTransfers.getAccountLineActualsBalanceAmount()).subtract(expenseTransfers.getAccountLineEncumbranceBalanceAmount()));
183         expenseTotal.getDummyBusinessObject().setGenericAmount(expense.getDummyBusinessObject().getGenericAmount().add(expenseTransfers.getDummyBusinessObject().getGenericAmount()));
184 
185         total.getDummyBusinessObject().setGenericAmount(incomeTotal.getDummyBusinessObject().getGenericAmount().add(expenseTotal.getDummyBusinessObject().getGenericAmount()));
186 
187         return totals;
188     }
189 
190     /**
191      * Sets the accessSecurityService attribute value.
192      * 
193      * @param accessSecurityService The accessSecurityService to set.
194      */
195     public void setAccessSecurityService(AccessSecurityService accessSecurityService) {
196         this.accessSecurityService = accessSecurityService;
197     }
198 
199     /**
200      * Sets the objectTypeService attribute value.
201      * 
202      * @param objectTypeService The objectTypeService to set.
203      */
204     public void setObjectTypeService(ObjectTypeService objectTypeService) {
205         this.objectTypeService = objectTypeService;
206     }
207 
208     /**
209      * Sets the kualiConfigurationService attribute value.
210      * 
211      * @param kualiConfigurationService The kualiConfigurationService to set.
212      */
213     public void setConfigurationService(ConfigurationService kualiConfigurationService) {
214         this.kualiConfigurationService = kualiConfigurationService;
215     }
216 
217 }