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.validation.impl;
017    
018    import org.kuali.kfs.fp.businessobject.DisbursementVoucherPayeeDetail;
019    import org.kuali.kfs.fp.document.DisbursementVoucherConstants;
020    import org.kuali.kfs.fp.document.DisbursementVoucherDocument;
021    import org.kuali.kfs.sys.KFSConstants;
022    import org.kuali.kfs.sys.KFSPropertyConstants;
023    import org.kuali.kfs.sys.businessobject.ChartOrgHolder;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.kfs.sys.document.AccountingDocument;
026    import org.kuali.kfs.sys.document.validation.GenericValidation;
027    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
028    import org.kuali.kfs.sys.service.FinancialSystemUserService;
029    import org.kuali.rice.kim.bo.Person;
030    import org.kuali.rice.kns.service.ParameterEvaluator;
031    import org.kuali.rice.kns.service.ParameterService;
032    import org.kuali.rice.kns.util.GlobalVariables;
033    import org.kuali.rice.kns.util.MessageMap;
034    import org.kuali.rice.kns.util.ObjectUtils;
035    
036    public class DisbursementVoucherDocumentLocationValidation extends GenericValidation implements DisbursementVoucherConstants {
037        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherDocumentLocationValidation.class);
038    
039        private ParameterService parameterService;
040        private AccountingDocument accountingDocumentForValidation;
041    
042        /**
043         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
044         */
045        public boolean validate(AttributedDocumentEvent event) {
046            LOG.debug("validate start");
047            boolean isValid = true;
048            
049            DisbursementVoucherDocument document = (DisbursementVoucherDocument) accountingDocumentForValidation;
050            DisbursementVoucherPayeeDetail payeeDetail = document.getDvPayeeDetail();
051            String documentationLocationCode = document.getDisbursementVoucherDocumentationLocationCode();
052    
053            MessageMap errors = GlobalVariables.getMessageMap();
054            int initialErrorCount = errors.getErrorCount();
055            errors.addToErrorPath(KFSPropertyConstants.DOCUMENT);
056    
057            // payment reason restrictions
058            if (ObjectUtils.isNotNull(payeeDetail.getDisbVchrPaymentReasonCode())) {
059                ParameterEvaluator parameterEvaluator = parameterService.getParameterEvaluator(DisbursementVoucherDocument.class, DisbursementVoucherConstants.VALID_DOC_LOC_BY_PAYMENT_REASON_PARM, DisbursementVoucherConstants.INVALID_DOC_LOC_BY_PAYMENT_REASON_PARM, payeeDetail.getDisbVchrPaymentReasonCode(), documentationLocationCode);
060                parameterEvaluator.evaluateAndAddError(document.getClass(), KFSPropertyConstants.DISBURSEMENT_VOUCHER_DOCUMENTATION_LOCATION_CODE);
061            }
062    
063            // alien indicator restrictions
064            if (payeeDetail.isDisbVchrAlienPaymentCode()) {
065                ParameterEvaluator parameterEvaluator = parameterService.getParameterEvaluator(DisbursementVoucherDocument.class, ALIEN_INDICATOR_CHECKED_PARM_NM, documentationLocationCode);
066                parameterEvaluator.evaluateAndAddError(document.getClass(), KFSPropertyConstants.DISBURSEMENT_VOUCHER_DOCUMENTATION_LOCATION_CODE);
067            }
068    
069            Person initiator = getInitiator(document);
070            ChartOrgHolder chartOrg = SpringContext.getBean(FinancialSystemUserService.class).getPrimaryOrganization(initiator, KFSConstants.ParameterNamespaces.FINANCIAL);
071            String locationCode = (chartOrg == null || chartOrg.getOrganization() == null) ? null : chartOrg.getOrganization().getOrganizationPhysicalCampusCode();
072    
073            // initiator campus code restrictions
074            ParameterEvaluator parameterEvaluator = parameterService.getParameterEvaluator(DisbursementVoucherDocument.class, DisbursementVoucherConstants.VALID_DOC_LOC_BY_CAMPUS_PARM, DisbursementVoucherConstants.INVALID_DOC_LOC_BY_CAMPUS_PARM, locationCode, documentationLocationCode);
075            parameterEvaluator.evaluateAndAddError(document.getClass(), KFSPropertyConstants.DISBURSEMENT_VOUCHER_DOCUMENTATION_LOCATION_CODE);
076    
077            errors.removeFromErrorPath(KFSPropertyConstants.DOCUMENT);  
078            
079            isValid = initialErrorCount == errors.getErrorCount();        
080            return isValid;
081        }
082    
083        /**
084         * Returns the initiator of the document as a KualiUser
085         * 
086         * @param document submitted document
087         * @return <code>KualiUser</code>
088         */
089        protected Person getInitiator(AccountingDocument document) {
090            Person initUser = SpringContext.getBean(org.kuali.rice.kim.service.PersonService.class).getPersonByPrincipalName(document.getDocumentHeader().getWorkflowDocument().getInitiatorNetworkId());
091            if (initUser == null) {
092                throw new RuntimeException("Document Initiator not found ");
093            }
094    
095            return initUser;
096        }
097    
098        /**
099         * Sets the accountingDocumentForValidation attribute value.
100         * 
101         * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
102         */
103        public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
104            this.accountingDocumentForValidation = accountingDocumentForValidation;
105        }
106    
107        /**
108         * Sets the parameterService attribute value.
109         * @param parameterService The parameterService to set.
110         */
111        public void setParameterService(ParameterService parameterService) {
112            this.parameterService = parameterService;
113        }
114    
115        /**
116         * Gets the accountingDocumentForValidation attribute. 
117         * @return Returns the accountingDocumentForValidation.
118         */
119        public AccountingDocument getAccountingDocumentForValidation() {
120            return accountingDocumentForValidation;
121        }
122    }
123