View Javadoc
1   /*
2    * Copyright 2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.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/ecl2.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.fp.document.authorization;
17  
18  import java.util.List;
19  import java.util.Map;
20  import java.util.Set;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.ole.fp.document.DisbursementVoucherConstants;
24  import org.kuali.ole.fp.document.DisbursementVoucherDocument;
25  import org.kuali.ole.sys.businessobject.AccountingLine;
26  import org.kuali.ole.sys.document.authorization.AccountingDocumentAuthorizerBase;
27  import org.kuali.ole.sys.identity.OleKimAttributes;
28  import org.kuali.rice.kew.api.WorkflowDocument;
29  import org.kuali.rice.kim.api.KimConstants;
30  import org.kuali.rice.kim.api.identity.Person;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  import org.kuali.rice.krad.util.ObjectUtils;
33  
34  /**
35   * Adds extra role qualifiers for funky travel edit mode permission
36   */
37  public class DisbursementVoucherDocumentAuthorizer extends AccountingDocumentAuthorizerBase {
38  
39      /**
40       * Adds chart codes and account numbers for accounting lines if we're at Account level, so that the fiscal officer gets travel edit mode
41       * @see org.kuali.ole.sys.document.authorization.AccountingDocumentAuthorizerBase#addRoleQualification(org.kuali.rice.krad.bo.BusinessObject, java.util.Map)
42       */
43      @Override
44      protected void addRoleQualification(Object dataObject, Map<String, String> attributes) {
45          super.addRoleQualification(dataObject, attributes);
46          final DisbursementVoucherDocument disbursementVoucherDocument = (DisbursementVoucherDocument)dataObject;
47          
48          // are we add Account level?  Then let's add our qualifiers
49          if (isAtAccountLevel(disbursementVoucherDocument)) {
50              addAccountQualification(getAccountingLines(disbursementVoucherDocument), attributes);
51          }
52  
53          // add campus code if we have one
54          if (!StringUtils.isBlank(disbursementVoucherDocument.getCampusCode())) {
55              attributes.put(KimConstants.AttributeConstants.CAMPUS_CODE, disbursementVoucherDocument.getCampusCode());
56          }
57      }
58      
59      /**
60       * Finds the source accounting lines in the given business object
61       * @param disbursementVoucherDocument a document to get accounting lines from
62       * @return a List of accounting lines
63       */
64      protected List<? extends AccountingLine> getAccountingLines(DisbursementVoucherDocument disbursementVoucherDocument) {
65          return disbursementVoucherDocument.getSourceAccountingLines();
66      }
67  
68      /**
69       * Goes through the given List of accounting lines and fines one line where the current user is the fiscal officer; it uses that line to put chart of accounts
70       * code and account number qualifications into the given Map of attributes for role qualification
71       * @param accountingLines a List of AccountingLines
72       * @param attributes a Map of role qualification attributes
73       */
74      protected void addAccountQualification(List<? extends AccountingLine> accountingLines, Map<String, String> attributes) {
75          final Person currentUser = GlobalVariables.getUserSession().getPerson();
76          boolean foundQualification = false;
77          int count = 0;
78          while (!foundQualification && count < accountingLines.size()) {
79              AccountingLine accountingLine = accountingLines.get(count);
80              if (ObjectUtils.isNull(accountingLine.getAccount())) {
81                  accountingLine.refreshReferenceObject("account");
82              }
83              if (!ObjectUtils.isNull(accountingLine.getAccount()) && currentUser.getPrincipalId().equalsIgnoreCase(accountingLine.getAccount().getAccountFiscalOfficerSystemIdentifier())) {
84                  attributes.put(OleKimAttributes.CHART_OF_ACCOUNTS_CODE, accountingLine.getChartOfAccountsCode());
85                  attributes.put(OleKimAttributes.ACCOUNT_NUMBER, accountingLine.getAccountNumber());
86                  foundQualification = true;
87              }
88              count += 1;
89          }
90      }
91      
92      /**
93       * A helper method for determining the route levels for a given document.
94       * 
95       * @param workflowDocument
96       * @return List
97       */
98      protected Set<String> getCurrentRouteLevels(WorkflowDocument workflowDocument) {
99          return workflowDocument.getCurrentNodeNames();
100     }
101     
102     /**
103      * Determines if the document is at the Account route level
104      * @param disbursementVoucherDocument the Disbursement Voucher document to determine the account level of
105      * @return true if the document is at the account level, false otherwise
106      */
107     protected boolean isAtAccountLevel(DisbursementVoucherDocument disbursementVoucherDocument) {
108         final WorkflowDocument workflowDocument = disbursementVoucherDocument.getDocumentHeader().getWorkflowDocument();
109         
110         return getCurrentRouteLevels(workflowDocument).contains(DisbursementVoucherConstants.RouteLevelNames.ACCOUNT);
111     }
112 }