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.dataaccess.impl;
017    
018    import java.math.BigDecimal;
019    import java.sql.Date;
020    import java.text.DateFormat;
021    import java.text.SimpleDateFormat;
022    import java.util.Iterator;
023    import java.util.List;
024    
025    import org.apache.ojb.broker.query.Criteria;
026    import org.apache.ojb.broker.query.QueryByCriteria;
027    import org.apache.ojb.broker.query.QueryFactory;
028    import org.apache.ojb.broker.query.ReportQueryByCriteria;
029    import org.kuali.kfs.module.endow.EndowPropertyConstants;
030    import org.kuali.kfs.module.endow.businessobject.ClassCode;
031    import org.kuali.kfs.module.endow.businessobject.HoldingTaxLot;
032    import org.kuali.kfs.module.endow.businessobject.KemidPayoutInstruction;
033    import org.kuali.kfs.module.endow.businessobject.PooledFundValue;
034    import org.kuali.kfs.module.endow.businessobject.Security;
035    import org.kuali.kfs.module.endow.dataaccess.IncomeDistributionForPooledFundDao;
036    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
037    
038    public class IncomeDistributionForPooledFundDaoOjb extends PlatformAwareDaoBaseOjb implements IncomeDistributionForPooledFundDao {
039    
040        /**
041         * @see org.kuali.kfs.module.endow.dataaccess.IncomeDistributionForPooledFundDao#getIncomeEntraCode(java.lang.String)
042         */
043        public String getIncomeEntraCode(String securityId) {
044            // get the security
045            Security security = (Security) getPersistenceBrokerTemplate().getObjectById(Security.class, securityId);        
046            // get the transaction post code, using security
047            ClassCode classCode = (ClassCode) getPersistenceBrokerTemplate().getObjectById(ClassCode.class, security.getSecurityClassCode());
048    
049            return classCode.getSecurityIncomeEndowmentTransactionPostCode();
050        }
051         
052        /**
053         * @see org.kuali.kfs.module.endow.dataaccess.IncomeDistributionForPooledFundDao#getSecurityForIncomeDistribution()
054         */
055        public List<PooledFundValue> getPooledFundValueForIncomeDistribution(Date currentDate) {
056            
057            // get pooledFundValue query for each security id with DSTRB_PROC_ON_DT = current date, DSTRB_PROC_CMPLT = 'N', and the most recent value effective date
058            Criteria subCrit = new Criteria();
059            subCrit.addEqualTo(EndowPropertyConstants.INCOME_DISTRIBUTION_COMPLETE, false);
060            subCrit.addEqualTo(EndowPropertyConstants.DISTRIBUTE_INCOME_ON_DATE, currentDate);
061            ReportQueryByCriteria subQuery = QueryFactory.newReportQuery(PooledFundValue.class, subCrit);        
062            subQuery.setAttributes(new String[] {EndowPropertyConstants.POOL_SECURITY_ID, "max(" + EndowPropertyConstants.VALUE_EFFECTIVE_DATE + ")"});
063            subQuery.addGroupBy(EndowPropertyConstants.POOL_SECURITY_ID);      
064            Iterator<?> result = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(subQuery);        
065            if (!result.hasNext()) {
066                return null;
067            }
068            
069            Criteria crit = new Criteria();
070            while (result.hasNext()) {
071                Criteria c = new Criteria();
072                Object[] data = (Object[]) result.next();
073                String securityId = data[0].toString();
074                String effectiveDate = data[1].toString();
075                c.addEqualTo(EndowPropertyConstants.POOL_SECURITY_ID, securityId);
076                try {
077                    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
078                    java.util.Date date = df.parse(effectiveDate);
079                    c.addEqualTo(EndowPropertyConstants.VALUE_EFFECTIVE_DATE, new Date(date.getTime()));
080                } catch (Exception e) {
081                    continue;
082                }            
083                crit.addOrCriteria(c);
084            }
085            
086            return (List<PooledFundValue>) getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(PooledFundValue.class, crit));
087        }     
088    
089        /**
090         * @see org.kuali.kfs.module.endow.dataaccess.IncomeDistributionForPooledFundDao#getKemidPayoutInstructionForECT(java.lang.String)
091         */
092        public List<KemidPayoutInstruction> getKemidPayoutInstructionForECT(String kemid, Date currentDate) {
093            Criteria crit = new Criteria();
094            Criteria crit2 = new Criteria();
095            Criteria crit21 = new Criteria();
096            
097            crit.addEqualTo(EndowPropertyConstants.KEMID_PAY_INC_KEMID, kemid);
098            crit.addNotEqualTo(EndowPropertyConstants.KEMID_PAY_INC_TO_KEMID, kemid);
099            crit.addLessOrEqualThan(EndowPropertyConstants.KEMID_PAY_INC_START_DATE, currentDate);
100            crit2.addGreaterThan(EndowPropertyConstants.KEMID_PAY_INC_END_DATE, currentDate);        
101            crit21.addIsNull(EndowPropertyConstants.KEMID_PAY_INC_END_DATE);                
102            crit2.addOrCriteria(crit21);        
103            crit.addAndCriteria(crit2);
104                            
105            return (List<KemidPayoutInstruction>) getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(KemidPayoutInstruction.class, crit));
106        }
107      
108    }