001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.kfs.sys.document.authorization;
017
018 import java.util.ArrayList;
019 import java.util.Arrays;
020 import java.util.List;
021 import java.util.Set;
022
023 import org.kuali.kfs.coa.service.AccountService;
024 import org.kuali.kfs.sys.KFSConstants;
025 import org.kuali.kfs.sys.KfsAuthorizationConstants;
026 import org.kuali.kfs.sys.KFSConstants.RouteLevelNames;
027 import org.kuali.kfs.sys.businessobject.AccountingLine;
028 import org.kuali.kfs.sys.businessobject.FinancialSystemDocumentHeader;
029 import org.kuali.kfs.sys.context.SpringContext;
030 import org.kuali.kfs.sys.document.AccountingDocument;
031 import org.kuali.rice.kew.exception.WorkflowException;
032 import org.kuali.rice.kim.bo.Person;
033 import org.kuali.rice.kns.document.Document;
034 import org.kuali.rice.kns.util.GlobalVariables;
035 import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
036
037 public class AccountingDocumentPresentationControllerBase extends LedgerPostingDocumentPresentationControllerBase {
038
039 /**
040 * @see org.kuali.kfs.sys.document.authorization.FinancialSystemTransactionalDocumentPresentationControllerBase#getEditModes(org.kuali.rice.kns.document.Document)
041 */
042 @Override
043 public Set<String> getEditModes(Document document) {
044 Set<String> editModes = super.getEditModes(document);
045
046 this.addExpenseEntryEditMode(document, editModes);
047
048 return editModes;
049 }
050
051 // add expense entry edit mode when the given document is enroute for account review
052 protected void addExpenseEntryEditMode(Document document, Set<String> editModes) {
053 KualiWorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
054 List<String> currentRouteLevels = getCurrentRouteLevels(workflowDocument);
055
056 if (workflowDocument.stateIsEnroute() && currentRouteLevels.contains(KFSConstants.RouteLevelNames.ACCOUNT)) {
057 AccountingDocument accountingDocument = (AccountingDocument) document;
058
059 List<AccountingLine> lineList = new ArrayList<AccountingLine>();
060 lineList.addAll(accountingDocument.getSourceAccountingLines());
061 lineList.addAll(accountingDocument.getTargetAccountingLines());
062
063 Person currentUser = GlobalVariables.getUserSession().getPerson();
064 if (workflowDocument.isApprovalRequested() && userOwnsAnyAccountingLine(currentUser, lineList)) {
065 editModes.add(KfsAuthorizationConstants.TransactionalEditMode.EXPENSE_ENTRY);
066 }
067 }
068 }
069
070 /**
071 * @see org.kuali.rice.kns.document.authorization.DocumentPresentationControllerBase#canEdit(org.kuali.rice.kns.document.Document)
072 */
073 @Override
074 protected boolean canEdit(Document document) {
075 KualiWorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
076 FinancialSystemDocumentHeader documentheader = (FinancialSystemDocumentHeader) (document.getDocumentHeader());
077
078 if (workflowDocument.stateIsCanceled() || documentheader.getFinancialDocumentInErrorNumber() != null) {
079 return false;
080 }
081 else if (workflowDocument.stateIsEnroute()) {
082 List<String> currentRouteLevels = getCurrentRouteLevels(workflowDocument);
083
084 if (currentRouteLevels.contains(RouteLevelNames.ACCOUNTING_ORGANIZATION_HIERARCHY)) {
085 return false;
086 }
087 }
088
089 return super.canEdit(document);
090 }
091
092 /**
093 * A helper method for determining the route levels for a given document.
094 *
095 * @param workflowDocument
096 * @return List
097 */
098 protected List<String> getCurrentRouteLevels(KualiWorkflowDocument workflowDocument) {
099 try {
100 return Arrays.asList(workflowDocument.getNodeNames());
101 }
102 catch (WorkflowException e) {
103 throw new RuntimeException(e);
104 }
105 }
106
107 /**
108 * @param accountingLines
109 * @param user
110 * @return true if the given user is responsible for any accounting line of the given transactionalDocument
111 */
112 protected boolean userOwnsAnyAccountingLine(Person user, List<AccountingLine> accountingLines) {
113 for (AccountingLine accountingLine : accountingLines) {
114 if (SpringContext.getBean(AccountService.class).hasResponsibilityOnAccount(user, accountingLine.getAccount())) {
115 return true;
116 }
117 }
118 return false;
119 }
120 }