001/*
002 * Copyright 2009 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 */
016package org.kuali.ole.sys.document.authorization;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Set;
021
022import org.apache.commons.lang.StringUtils;
023import org.kuali.ole.coa.service.AccountService;
024import org.kuali.ole.sys.OLEConstants;
025import org.kuali.ole.sys.OLEConstants.RouteLevelNames;
026import org.kuali.ole.sys.OleAuthorizationConstants;
027import org.kuali.ole.sys.businessobject.AccountingLine;
028import org.kuali.ole.sys.businessobject.FinancialSystemDocumentHeader;
029import org.kuali.ole.sys.context.SpringContext;
030import org.kuali.ole.sys.document.AccountingDocument;
031import org.kuali.rice.kew.api.WorkflowDocument;
032import org.kuali.rice.kim.api.identity.Person;
033import org.kuali.rice.krad.document.Document;
034import org.kuali.rice.krad.util.GlobalVariables;
035import org.kuali.rice.krad.util.ObjectUtils;
036
037public class AccountingDocumentPresentationControllerBase extends LedgerPostingDocumentPresentationControllerBase {
038
039    /**
040     * @see org.kuali.ole.sys.document.authorization.FinancialSystemTransactionalDocumentPresentationControllerBase#getEditModes(org.kuali.rice.krad.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        WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
054        Set<String> currentRouteLevels = workflowDocument.getCurrentNodeNames();
055
056        if (workflowDocument.isEnroute() && currentRouteLevels.contains(OLEConstants.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(OleAuthorizationConstants.TransactionalEditMode.EXPENSE_ENTRY);
066            }
067        }
068    }
069
070    /**
071     * @see org.kuali.rice.krad.document.authorization.DocumentPresentationControllerBase#canEdit(org.kuali.rice.krad.document.Document)
072     */
073    @Override
074    public boolean canEdit(Document document) {
075        WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
076        FinancialSystemDocumentHeader documentheader = (FinancialSystemDocumentHeader) (document.getDocumentHeader());
077
078        if (workflowDocument.isCanceled() || documentheader.getFinancialDocumentInErrorNumber() != null) {
079            return false;
080        }
081        else if (workflowDocument.isEnroute()) {
082            Set<String> currentRouteLevels = workflowDocument.getCurrentNodeNames();
083
084            if (currentRouteLevels.contains(RouteLevelNames.ACCOUNTING_ORGANIZATION_HIERARCHY)) {
085                return false;
086            }
087        }
088
089        return super.canEdit(document);
090    }
091
092    /**
093     * @param accountingLines
094     * @param user
095     * @return true if the given user is responsible for any accounting line of the given transactionalDocument
096     */
097    protected boolean userOwnsAnyAccountingLine(Person user, List<AccountingLine> accountingLines) {
098        for (AccountingLine accountingLine : accountingLines) {
099            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}