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