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.module.purap.document.validation.impl;
017    
018    import org.kuali.kfs.coa.businessobject.Organization;
019    import org.kuali.kfs.coa.service.ChartService;
020    import org.kuali.kfs.coa.service.OrganizationService;
021    import org.kuali.kfs.module.purap.PurapKeyConstants;
022    import org.kuali.kfs.module.purap.document.PurchasingDocumentBase;
023    import org.kuali.kfs.sys.document.validation.GenericValidation;
024    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
025    import org.kuali.rice.kns.util.GlobalVariables;
026    
027    public class PurchasingChartOrgValidation extends GenericValidation
028    {
029        private OrganizationService organizationService;
030        private ChartService chartService;
031        
032        /**
033         * Validate the chart and organization code prior to submitting the document.
034         * This has to be validated to protect against a person with an invalid
035         * primary department code.
036         * 
037         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
038         */
039        public boolean validate(AttributedDocumentEvent event) {
040    
041            //GlobalVariables.getMessageMap().addToErrorPath("document");
042            
043            // Initialize the valid flag to true (we assume everything will be ok).
044            boolean valid = true;
045            
046            // Get the document.
047            PurchasingDocumentBase purapDoc = (PurchasingDocumentBase) event.getDocument();
048            
049            // Get the chart of accounts and organization code from the document.
050            String chartOfAccountsCode = purapDoc.getChartOfAccountsCode();
051            String organizationCode    = purapDoc.getOrganizationCode();
052            
053            // Get organization business object from DB.  If a valid object is
054            // returned, we know that the COA and Org codes are valid; otherwise,
055            // display and error message to the user.
056            Organization organization = organizationService.getByPrimaryId(
057                    chartOfAccountsCode, 
058                    organizationCode);
059            
060            if (organization == null) {
061                GlobalVariables.getMessageMap().putError(
062                        "document.documentHeader.*",
063                        PurapKeyConstants.ERROR_INVALID_COA_ORG_CODE);
064                
065                valid = false;
066            }
067            
068            return valid;
069        }
070    
071        /**
072         * Sets the organizationService attribute value.
073         * @param organizationService The organizationService to set.
074         */
075        public void setOrganizationService(OrganizationService organizationService) {
076            this.organizationService = organizationService;
077        }
078    
079        /**
080         * Sets the chartService attribute value.
081         * @param chartService The chartService to set.
082         */
083        public void setChartService(ChartService chartService) {
084            this.chartService = chartService;
085        }
086    
087    }