001/*
002 * Copyright 2008 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.fp.document.authorization;
017
018import java.util.Map;
019import java.util.Set;
020
021import org.apache.commons.lang.StringUtils;
022import org.kuali.ole.sys.OLEConstants;
023import org.kuali.ole.sys.OLEKeyConstants;
024import org.kuali.ole.sys.businessobject.AccountingLine;
025import org.kuali.ole.sys.context.SpringContext;
026import org.kuali.ole.sys.document.AccountingDocument;
027import org.kuali.ole.sys.document.authorization.AccountingLineAuthorizerBase;
028import org.kuali.ole.sys.document.validation.impl.AccountingDocumentRuleBaseConstants.APPLICATION_PARAMETER;
029import org.kuali.ole.sys.document.web.AccountingLineRenderingContext;
030import org.kuali.ole.sys.document.web.AccountingLineViewAction;
031import org.kuali.ole.sys.service.impl.OleParameterConstants;
032import org.kuali.rice.core.api.parameter.ParameterEvaluator;
033import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
034import org.kuali.rice.coreservice.framework.parameter.ParameterService;
035import org.kuali.rice.kim.api.identity.Person;
036import org.kuali.rice.kns.service.DataDictionaryService;
037
038/**
039 * Authorizer which deals with financial processing document issues, specifically sales tax lines on documents
040 */
041public class FinancialProcessingAccountingLineAuthorizer extends AccountingLineAuthorizerBase {
042    private final static String SALES_TAX_DOCUMENT_TYPES_PARAMETER_NAME = "SALES_TAX_APPLICABLE_DOCUMENT_TYPES";
043    private final static String SALES_TAX_LINE_ACCOUNT_OBJECT_CODES_PARAMETER_NAME = "SALES_TAX_APPLICABLE_ACCOUNTS_AND_OBJECT_CODES";
044
045    /**
046     * @see org.kuali.ole.sys.document.authorization.AccountingLineAuthorizerBase#getUnviewableBlocks(org.kuali.ole.sys.document.AccountingDocument, org.kuali.ole.sys.businessobject.AccountingLine, boolean, org.kuali.rice.kim.api.identity.Person)
047     */
048    @Override
049    public Set<String> getUnviewableBlocks(AccountingDocument accountingDocument, AccountingLine accountingLine, boolean newLine, Person currentUser) {
050        Set unviewableBlocks = super.getUnviewableBlocks(accountingDocument, accountingLine, newLine, currentUser);
051        if (salesTaxUnviewable(accountingDocument, accountingLine)) {
052            unviewableBlocks.add(OLEConstants.AccountingLineViewStandardBlockNames.SALES_TAX_BLOCK);
053        }
054        return unviewableBlocks;
055    }
056
057    /**
058     * Determines if the given line on the given document should not show any sales tax block it has
059     * @param document the document the line lives on (or will live on)
060     * @param line the accounting line which perhaps should be hiding any sales tax information
061     * @return true if sales tax should not be seen for the line, false otherwise
062     */
063    protected boolean salesTaxUnviewable(AccountingDocument document, AccountingLine line) {
064        ParameterService parameterService = SpringContext.getBean(ParameterService.class);
065        String docTypeCode = SpringContext.getBean(DataDictionaryService.class).getDocumentTypeNameByClass(document.getClass());
066        ParameterEvaluator docTypeEvaluator = /*REFACTORME*/SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(OleParameterConstants.FINANCIAL_PROCESSING_DOCUMENT.class, FinancialProcessingAccountingLineAuthorizer.SALES_TAX_DOCUMENT_TYPES_PARAMETER_NAME, docTypeCode);
067        if (!docTypeEvaluator.evaluationSucceeds()) return true;
068        if (!StringUtils.isEmpty(line.getFinancialObjectCode()) && !StringUtils.isEmpty(line.getAccountNumber())) {
069            String compare = line.getAccountNumber() + ":" + line.getFinancialObjectCode();
070            ParameterEvaluator salesTaxApplicableAccountAndObjectEvaluator = /*REFACTORME*/SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(OleParameterConstants.FINANCIAL_PROCESSING_DOCUMENT.class, APPLICATION_PARAMETER.SALES_TAX_APPLICABLE_ACCOUNTS_AND_OBJECT_CODES, compare);
071            if (!salesTaxApplicableAccountAndObjectEvaluator.evaluationSucceeds()) return true;
072            return false;
073        }
074        return true;
075    }
076    
077    /**
078     * adds refresh method to the action map.
079     * @see org.kuali.ole.sys.document.authorization.AccountingLineAuthorizerBase#getActionMap(org.kuali.ole.sys.document.web.AccountingLineRenderingContext, java.lang.String, java.lang.Integer, java.lang.String)
080     */
081    @Override
082    protected Map<String, AccountingLineViewAction> getActionMap(AccountingLineRenderingContext accountingLineRenderingContext, String accountingLinePropertyName, Integer accountingLineIndex, String groupTitle) {
083    
084        Map<String, AccountingLineViewAction> actionMap = super.getActionMap(accountingLineRenderingContext, accountingLinePropertyName, accountingLineIndex, groupTitle);
085
086        if (accountingLineIndex != null) {
087            AccountingLineViewAction refreshAction = this.getRefreshAction(accountingLineRenderingContext.getAccountingLine(), accountingLinePropertyName, accountingLineIndex, groupTitle);
088            actionMap.put(OLEConstants.RETURN_METHOD_TO_CALL, refreshAction);
089        }
090        
091        return actionMap;
092    }
093    
094    /**
095     * constructs a refresh action image and action
096     * 
097     * @param accountingLine
098     * @param accountingLinePropertyName
099     * @param accountingLineIndex
100     * @param groupTitle
101     * @return
102     */
103    protected AccountingLineViewAction getRefreshAction(AccountingLine accountingLine, String accountingLinePropertyName, Integer accountingLineIndex, String groupTitle) {
104        String actionMethod = this.getRefreshLineMethod(accountingLine, accountingLinePropertyName, accountingLineIndex);
105        String actionLabel = getActionLabel(OLEKeyConstants.AccountingLineViewRendering.ACCOUNTING_LINE_REFRESH_ACTION_LABEL, groupTitle, accountingLineIndex + 1);
106
107        String actionImageName = getRiceImagePath() + "tinybutton-refresh.gif";
108
109        return new AccountingLineViewAction(actionMethod, actionLabel, actionImageName);
110    }
111    
112    /**
113     * constructs a refresh line method
114     * 
115     * @param accountingLine
116     * @param accountingLineProperty
117     * @param accountingLineIndex
118     * @return
119     */
120    protected String getRefreshLineMethod(AccountingLine accountingLine, String accountingLineProperty, Integer accountingLineIndex) {
121        final String infix = getActionInfixForExtantAccountingLine(accountingLine, accountingLineProperty);
122        return "refresh.line" + accountingLineIndex + ".anchoraccounting" + infix + "Anchor";
123    }
124}
125