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.sql.Date;
019    
020    import org.kuali.kfs.coa.document.validation.impl.MaintenancePreRulesBase;
021    import org.kuali.kfs.module.endow.businessobject.PooledFundControl;
022    import org.kuali.kfs.module.endow.businessobject.PooledFundValue;
023    import org.kuali.kfs.module.endow.document.service.PooledFundControlService;
024    import org.kuali.kfs.module.endow.document.service.PooledFundValueService;
025    import org.kuali.kfs.sys.context.SpringContext;
026    import org.kuali.rice.kns.document.MaintenanceDocument;
027    import org.kuali.rice.kns.util.ObjectUtils;
028    
029    
030    public class PooledFundValuePreRule extends MaintenancePreRulesBase {
031    
032        private PooledFundValue newPooledFundValue;
033        private PooledFundControl pooledFundControl;
034    
035        /**
036         * Set value for newCashSweepModel.
037         * 
038         * @param document the CashSweepModel Maintenance document
039         */
040        private void setupConvenienceObjects(MaintenanceDocument document) {
041    
042            // setup newCashSweepModel convenience objects, make sure all possible sub-objects are populated
043            newPooledFundValue = (PooledFundValue) document.getNewMaintainableObject().getBusinessObject();
044            newPooledFundValue.refreshNonUpdateableReferences();
045            pooledFundControl = newPooledFundValue.getPooledFundControl();
046            if (!ObjectUtils.isNotNull(pooledFundControl)) {
047                PooledFundControlService pooledFundControlService = SpringContext.getBean(PooledFundControlService.class);
048                pooledFundControl = pooledFundControlService.getByPrimaryKey(newPooledFundValue.getPooledSecurityID());
049            }
050        }
051    
052        /**
053         * @see org.kuali.kfs.coa.document.validation.impl.MaintenancePreRulesBase#doCustomPreRules(org.kuali.rice.kns.document.MaintenanceDocument)
054         */
055        @Override
056        protected boolean doCustomPreRules(MaintenanceDocument maintenanceDocument) {
057    
058            setupConvenienceObjects(maintenanceDocument);
059            if (ObjectUtils.isNotNull(pooledFundControl)) {
060                // Set the date of last sweep model change -- the date is the time when the maintenance doc is submitted, not
061                // the time when that maintenance doc gets approved.
062                PooledFundValueService pooledFundValueService = SpringContext.getBean(PooledFundValueService.class);
063                Date valuationDate = newPooledFundValue.getValuationDate();
064                String pooledSecurityID = newPooledFundValue.getPooledSecurityID();
065    
066                if (valuationDate != null && pooledSecurityID != null) {
067    
068                    Date valueEffectiveDate = pooledFundValueService.calculateValueEffectiveDate(valuationDate, pooledSecurityID);
069                    newPooledFundValue.setValueEffectiveDate(valueEffectiveDate);
070                    return true;
071                }
072            }
073    
074            return true;
075        }
076    
077    }