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