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.sys.document.authorization;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.ole.coa.service.AccountService;
24  import org.kuali.ole.sys.OLEConstants;
25  import org.kuali.ole.sys.OLEConstants.RouteLevelNames;
26  import org.kuali.ole.sys.OleAuthorizationConstants;
27  import org.kuali.ole.sys.businessobject.AccountingLine;
28  import org.kuali.ole.sys.businessobject.FinancialSystemDocumentHeader;
29  import org.kuali.ole.sys.context.SpringContext;
30  import org.kuali.ole.sys.document.AccountingDocument;
31  import org.kuali.rice.kew.api.WorkflowDocument;
32  import org.kuali.rice.kim.api.identity.Person;
33  import org.kuali.rice.krad.document.Document;
34  import org.kuali.rice.krad.util.GlobalVariables;
35  import org.kuali.rice.krad.util.ObjectUtils;
36  
37  public class AccountingDocumentPresentationControllerBase extends LedgerPostingDocumentPresentationControllerBase {
38  
39      /**
40       * @see org.kuali.ole.sys.document.authorization.FinancialSystemTransactionalDocumentPresentationControllerBase#getEditModes(org.kuali.rice.krad.document.Document)
41       */
42      @Override
43      public Set<String> getEditModes(Document document) {
44          Set<String> editModes = super.getEditModes(document);
45  
46          this.addExpenseEntryEditMode(document, editModes);
47  
48          return editModes;
49      }
50  
51      // add expense entry edit mode when the given document is enroute for account review
52      protected void addExpenseEntryEditMode(Document document, Set<String> editModes) {
53          WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
54          Set<String> currentRouteLevels = workflowDocument.getCurrentNodeNames();
55  
56          if (workflowDocument.isEnroute() && currentRouteLevels.contains(OLEConstants.RouteLevelNames.ACCOUNT)) {
57              AccountingDocument accountingDocument = (AccountingDocument) document;
58  
59              List<AccountingLine> lineList = new ArrayList<AccountingLine>();
60              lineList.addAll(accountingDocument.getSourceAccountingLines());
61              lineList.addAll(accountingDocument.getTargetAccountingLines());
62  
63              Person currentUser = GlobalVariables.getUserSession().getPerson();
64              if (workflowDocument.isApprovalRequested() && userOwnsAnyAccountingLine(currentUser, lineList)) {
65                  editModes.add(OleAuthorizationConstants.TransactionalEditMode.EXPENSE_ENTRY);
66              }
67          }
68      }
69  
70      /**
71       * @see org.kuali.rice.krad.document.authorization.DocumentPresentationControllerBase#canEdit(org.kuali.rice.krad.document.Document)
72       */
73      @Override
74      public boolean canEdit(Document document) {
75          WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
76          FinancialSystemDocumentHeader documentheader = (FinancialSystemDocumentHeader) (document.getDocumentHeader());
77  
78          if (workflowDocument.isCanceled() || documentheader.getFinancialDocumentInErrorNumber() != null) {
79              return false;
80          }
81          else if (workflowDocument.isEnroute()) {
82              Set<String> currentRouteLevels = workflowDocument.getCurrentNodeNames();
83  
84              if (currentRouteLevels.contains(RouteLevelNames.ACCOUNTING_ORGANIZATION_HIERARCHY)) {
85                  return false;
86              }
87          }
88  
89          return super.canEdit(document);
90      }
91  
92      /**
93       * @param accountingLines
94       * @param user
95       * @return true if the given user is responsible for any accounting line of the given transactionalDocument
96       */
97      protected boolean userOwnsAnyAccountingLine(Person user, List<AccountingLine> accountingLines) {
98          for (AccountingLine accountingLine : accountingLines) {
99              if (StringUtils.isNotEmpty(accountingLine.getAccountNumber()) && ObjectUtils.isNotNull(accountingLine.getAccount()))
100             if (SpringContext.getBean(AccountService.class).hasResponsibilityOnAccount(user, accountingLine.getAccount())) {
101                 return true;
102             }
103         }
104         return false;
105     }
106 }