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.endow.document.validation.impl;
017    
018    import java.math.BigDecimal;
019    
020    import org.apache.log4j.Logger;
021    import org.kuali.kfs.module.endow.EndowKeyConstants;
022    import org.kuali.kfs.module.endow.EndowPropertyConstants;
023    import org.kuali.kfs.module.endow.businessobject.AutomatedCashInvestmentModel;
024    import org.kuali.kfs.sys.KFSConstants;
025    import org.kuali.rice.kns.document.MaintenanceDocument;
026    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
027    import org.kuali.rice.kns.util.GlobalVariables;
028    
029    
030    public class ACIModelRule extends MaintenanceDocumentRuleBase{
031        protected static Logger LOG = org.apache.log4j.Logger.getLogger(SecurityRule.class);
032        private AutomatedCashInvestmentModel newACIModel;
033        
034        /**
035         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
036         */
037        @Override
038        protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
039            
040            boolean isValid = true;
041            isValid &= super.processCustomRouteDocumentBusinessRules(document);
042            AutomatedCashInvestmentModel newACIModel = (AutomatedCashInvestmentModel) document.getNewMaintainableObject().getBusinessObject();
043            isValid = checkTotalPercentageEqualOne(newACIModel);        
044            return isValid;
045        }
046    
047        protected boolean checkTotalPercentageEqualOne(AutomatedCashInvestmentModel newACIModel){
048            boolean isTotalPercentageEqualOne = true; 
049            
050    //      BigDecimal investment1Percent = newACIModel.getInvestment1Percent().bigDecimalValue();
051            BigDecimal investment1Percent = newACIModel.getInvestment1Percent();
052            BigDecimal investment2Percent = newACIModel.getInvestment2Percent();
053            BigDecimal investment3Percent = newACIModel.getInvestment3Percent();
054            BigDecimal investment4Percent = newACIModel.getInvestment4Percent();
055         
056            BigDecimal total= new BigDecimal(0.0000);
057            if ((newACIModel.getInvestment1SecurityID()!= null) && (investment1Percent != null)){
058                total = total.add(investment1Percent);
059            }
060            if ((newACIModel.getInvestment2SecurityID()!= null) && (investment2Percent) != null){
061                total = total.add(investment2Percent);
062            }
063            if ((newACIModel.getInvestment3SecurityID()!= null) && (investment3Percent != null)){
064                total = total.add(investment3Percent);
065            }
066            if ((newACIModel.getInvestment4SecurityID()!= null) && (investment4Percent !=null)){
067                total = total.add(investment4Percent);  
068            }
069                    
070            if ((total.compareTo(new BigDecimal(1.00)))!=0) {
071                putFieldError(EndowPropertyConstants.INVESTMENT_1_PERCENT, EndowKeyConstants.ACIModelConstants.ERROR_TOTAL_OF_ALL_PERCENTAGES_NOT_EQUAL_ONE);
072                isTotalPercentageEqualOne = false;
073            }
074            
075            return isTotalPercentageEqualOne;
076        }
077    }