001/*
002 * Copyright 2007 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.module.cg.document.validation.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.log4j.Logger;
020import org.kuali.ole.module.cg.businessobject.Proposal;
021import org.kuali.ole.module.cg.businessobject.ProposalOrganization;
022import org.kuali.ole.module.cg.businessobject.ProposalProjectDirector;
023import org.kuali.ole.module.cg.businessobject.ProposalSubcontractor;
024import org.kuali.ole.sys.OLEKeyConstants;
025import org.kuali.ole.sys.OLEPropertyConstants;
026import org.kuali.rice.kns.document.MaintenanceDocument;
027import org.kuali.rice.krad.bo.PersistableBusinessObject;
028import org.kuali.rice.krad.util.GlobalVariables;
029
030/**
031 * Rules for the Proposal maintenance document.
032 */
033public class ProposalRule extends CGMaintenanceDocumentRuleBase {
034    protected static Logger LOG = org.apache.log4j.Logger.getLogger(ProposalRule.class);
035
036    protected Proposal newProposalCopy;
037
038    @Override
039    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument documentCopy) {
040        LOG.debug("Entering ProposalRule.processCustomSaveDocumentBusinessRules");
041        processCustomRouteDocumentBusinessRules(documentCopy); // chain call but ignore success
042        LOG.info("Leaving ProposalRule.processCustomSaveDocumentBusinessRules");
043        return true; // save despite error messages
044    }
045
046    @Override
047    protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument documentCopy) {
048        LOG.debug("Entering ProposalRule.processCustomRouteDocumentBusinessRules");
049        boolean success = true;
050        success &= checkEndAfterBegin(newProposalCopy.getProposalBeginningDate(), newProposalCopy.getProposalEndingDate(), OLEPropertyConstants.PROPOSAL_ENDING_DATE);
051        success &= checkPrimary(newProposalCopy.getProposalOrganizations(), ProposalOrganization.class, OLEPropertyConstants.PROPOSAL_ORGANIZATIONS, Proposal.class);
052        success &= checkPrimary(newProposalCopy.getProposalProjectDirectors(), ProposalProjectDirector.class, OLEPropertyConstants.PROPOSAL_PROJECT_DIRECTORS, Proposal.class);
053        success &= checkProjectDirectorsAreDirectors(newProposalCopy.getProposalProjectDirectors(), ProposalProjectDirector.class, OLEPropertyConstants.PROPOSAL_PROJECT_DIRECTORS);
054        success &= checkProjectDirectorsExist(newProposalCopy.getProposalProjectDirectors(), ProposalProjectDirector.class, OLEPropertyConstants.PROPOSAL_PROJECT_DIRECTORS);
055        success &= checkProjectDirectorsStatuses(newProposalCopy.getProposalProjectDirectors(), ProposalProjectDirector.class, OLEPropertyConstants.PROPOSAL_PROJECT_DIRECTORS);
056        success &= checkFederalPassThrough(newProposalCopy.getProposalFederalPassThroughIndicator(), newProposalCopy.getAgency(), newProposalCopy.getFederalPassThroughAgencyNumber(), Proposal.class, OLEPropertyConstants.PROPOSAL_FEDERAL_PASS_THROUGH_INDICATOR);
057        success &= checkAgencyNotEqualToFederalPassThroughAgency(newProposalCopy.getAgency(), newProposalCopy.getFederalPassThroughAgency(), OLEPropertyConstants.AGENCY_NUMBER, OLEPropertyConstants.FEDERAL_PASS_THROUGH_AGENCY_NUMBER);
058        LOG.info("Leaving ProposalRule.processCustomRouteDocumentBusinessRules");
059        return success;
060    }
061
062    /**
063     * @return
064     */
065    @Override
066    public boolean processCustomAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName, PersistableBusinessObject line) {
067        if (LOG.isDebugEnabled()) {
068            LOG.debug("Entering ProposalRule.processCustomAddNewCollectionLineBusinessRules( " + collectionName + " )");
069        }
070        boolean success = true;
071        success &= validateAddSubcontractor(line);
072        if (LOG.isDebugEnabled()) {
073            LOG.debug("Leaving ProposalRule.processCustomAddNewCollectionLineBusinessRules( " + collectionName + " )");
074        }
075        return success;
076    }
077
078    /**
079     * This method takes a look at the new line being added and applies appropriate validation checks if the line is a new line for
080     * the {@link Subcontractor} collection. If the validation checks fail, an appropriate error message will be added to the global
081     * error map and the method will return a value of false.
082     * 
083     * @param addLine New business object values being added.
084     * @return True is the value being added passed all applicable validation rules.
085     */
086    protected boolean validateAddSubcontractor(PersistableBusinessObject addLine) {
087        boolean success = true;
088        if (addLine.getClass().isAssignableFrom(ProposalSubcontractor.class)) {
089            ProposalSubcontractor subc = (ProposalSubcontractor) addLine;
090            if (StringUtils.isBlank(subc.getSubcontractorNumber())) {
091                String propertyName = OLEPropertyConstants.SUBCONTRACTOR_NUMBER;
092                String errorKey = OLEKeyConstants.ERROR_PROPOSAL_SUBCONTRACTOR_NUMBER_REQUIRED_FOR_ADD;
093                GlobalVariables.getMessageMap().putError(propertyName, errorKey);
094                success = false;
095            }
096        }
097        return success;
098    }
099
100    /**
101     * Performs convenience cast for Maintenance framework. Note that the {@link MaintenanceDocumentRule} events provide only a deep
102     * copy of the document (from KualiDocumentEventBase), so these BOs are a copy too. The framework does this to prevent these
103     * rules from changing any data.
104     * 
105     * @see org.kuali.rice.kns.rules.MaintenanceDocumentRule#setupConvenienceObjects()
106     */
107    @Override
108    public void setupConvenienceObjects() {
109        // oldProposalCopy = (Proposal) super.getOldBo();
110        newProposalCopy = (Proposal) super.getNewBo();
111    }
112    
113    
114}