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.fp.document.authorization;
017    
018    import java.util.List;
019    
020    import org.kuali.kfs.fp.document.DistributionOfIncomeAndExpenseDocument;
021    import org.kuali.kfs.sys.KFSPropertyConstants;
022    import org.kuali.kfs.sys.businessobject.AccountingLine;
023    import org.kuali.kfs.sys.businessobject.ElectronicPaymentClaim;
024    import org.kuali.kfs.sys.document.AccountingDocument;
025    import org.kuali.rice.kns.util.ObjectUtils;
026    
027    /**
028     * Authorizer which deals with financial processing document issues, specifically sales tax lines on documents This class utilizes
029     * the new accountingLine model.
030     */
031    public class DistributionOfIncomeAndExpenseAccountingLineAuthorizer extends FinancialProcessingAccountingLineAuthorizer {
032    
033        /**
034         * This method determines if the current accounting line is editable based upon if electronic claims exists on the DI document.
035         * 
036         * @see org.kuali.kfs.sys.document.authorization.AccountingLineAuthorizerBase#determineFieldModifyability(org.kuali.kfs.sys.document.AccountingDocument,
037         *      org.kuali.kfs.sys.businessobject.AccountingLine, org.kuali.kfs.sys.document.web.AccountingLineViewField, java.util.Map)
038         */
039        @Override
040        public boolean determineEditPermissionOnField(AccountingDocument accountingDocument, AccountingLine accountingLine, String accountingLineCollectionProperty, String fieldName, boolean editablePage) {
041            final boolean canModify = super.determineEditPermissionOnField(accountingDocument, accountingLine, accountingLineCollectionProperty, fieldName, editablePage);
042            if (canModify && accountingLine.isSourceAccountingLine()) {
043                return !hasElectronicPaymentClaims(accountingDocument);
044            }
045            
046            return canModify;
047        }
048    
049        /**
050         * Don't render a new line if this is the source group and there's electronic payment claims
051         * @see org.kuali.kfs.sys.document.authorization.AccountingLineAuthorizerBase#renderNewLine(org.kuali.kfs.sys.document.AccountingDocument, java.lang.String)
052         */
053        @Override
054        public boolean renderNewLine(AccountingDocument accountingDocument, String accountingGroupProperty) {
055            final boolean shouldRender = super.renderNewLine(accountingDocument, accountingGroupProperty);
056            if (shouldRender && accountingGroupProperty.contains("source")) {
057                return !hasElectronicPaymentClaims(accountingDocument);
058            }
059            return shouldRender;
060        }
061    
062        /**
063         * There's no edit permission on lines in the source group on documents claiming electronic payments
064         * @see org.kuali.kfs.sys.document.authorization.AccountingLineAuthorizerBase#determineEditPermissionOnLine(org.kuali.kfs.sys.document.AccountingDocument, org.kuali.kfs.sys.businessobject.AccountingLine, java.lang.String)
065         */
066        @Override
067        public boolean determineEditPermissionOnLine(AccountingDocument accountingDocument, AccountingLine accountingLine, String accountingLineCollectionProperty, boolean currentUserIsDocumentInitiator, boolean pageIsEditable) {
068            final boolean hasEditPermOnLine = super.determineEditPermissionOnLine(accountingDocument, accountingLine, accountingLineCollectionProperty, currentUserIsDocumentInitiator, pageIsEditable);
069            if (hasEditPermOnLine && accountingLineCollectionProperty.contains("source")) {
070                return !hasElectronicPaymentClaims(accountingDocument);
071            }
072            return hasEditPermOnLine;
073        }
074    
075        /**
076         * Determines if the DI document has electronic payment claims associated with it
077         * @param accountingDocument a DI document
078         * @return true if there are electronic payment claims, false otherwise
079         */
080        protected boolean hasElectronicPaymentClaims(AccountingDocument accountingDocument) {
081            DistributionOfIncomeAndExpenseDocument diDoc = (DistributionOfIncomeAndExpenseDocument) accountingDocument;
082    
083            List<ElectronicPaymentClaim> epcs = diDoc.getElectronicPaymentClaims();
084    
085            if (epcs == null) {
086                diDoc.refreshReferenceObject(KFSPropertyConstants.ELECTRONIC_PAYMENT_CLAIMS);
087                epcs = diDoc.getElectronicPaymentClaims();
088            }
089    
090            return (!ObjectUtils.isNull(epcs) && epcs.size() > 0);
091        }
092    
093    }