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.util.ArrayList;
020    import java.util.Collection;
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import org.apache.commons.lang.StringUtils;
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.kuali.kfs.module.endow.EndowConstants;
029    import org.kuali.kfs.module.endow.EndowPropertyConstants;
030    import org.kuali.kfs.module.endow.businessobject.CurrentTaxLotBalance;
031    import org.kuali.kfs.module.endow.businessobject.FeeClassCode;
032    import org.kuali.kfs.module.endow.businessobject.FeeMethod;
033    import org.kuali.kfs.module.endow.businessobject.FeeSecurity;
034    import org.kuali.kfs.module.endow.businessobject.Security;
035    import org.kuali.kfs.module.endow.dataaccess.CurrentTaxLotBalanceDao;
036    import org.kuali.kfs.module.endow.dataaccess.SecurityDao;
037    import org.kuali.kfs.sys.context.SpringContext;
038    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
039    import org.kuali.rice.kns.service.DataDictionaryService;
040    
041    public class CurrentTaxLotBalanceDaoOjb extends PlatformAwareDaoBaseOjb implements CurrentTaxLotBalanceDao {
042        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CurrentTaxLotBalanceDaoOjb.class);
043    
044        protected SecurityDao securityDao;
045        
046        /**
047         * @see org.kuali.kfs.module.endow.dataaccess.CurrentTaxLotBalanceDao#getAllCurrentTaxLotBalanceEntriesForSecurity(java.lang.String)
048         */
049        public Collection<CurrentTaxLotBalance> getAllCurrentTaxLotBalanceEntriesForSecurity(String securityId) {
050            Criteria criteria = new Criteria();
051            criteria.addEqualTo(EndowPropertyConstants.CURRENT_TAX_LOT_SECURITY_ID, securityId);
052    
053            return (Collection<CurrentTaxLotBalance>) getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(CurrentTaxLotBalance.class, criteria));
054        }
055    
056        /**
057         * Prepares the criteria and selects the records from END_CRNT_TAX_LOT_BAL_T table
058         */
059        protected Collection<CurrentTaxLotBalance> getCurrentTaxLotBalances(FeeMethod feeMethod) {
060            Collection<CurrentTaxLotBalance> currentTaxLotBalance = new ArrayList(); 
061            
062            Collection incomePrincipalValues = new ArrayList();
063            incomePrincipalValues.add(EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_INCOME);
064            incomePrincipalValues.add(EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_PRINCIPAL);
065            
066            Criteria criteria = new Criteria();
067            
068            if (feeMethod.getFeeBaseCode().equalsIgnoreCase(EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_INCOME_AND_PRINCIPAL)) {
069                criteria.addIn(EndowPropertyConstants.CURRENT_TAX_LOT_BALANCE_INCOME_PRINCIPAL_INDICATOR, incomePrincipalValues);
070            }
071            else {
072                if (feeMethod.getFeeBaseCode().equalsIgnoreCase(EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_INCOME)) {
073                    criteria.addEqualTo(EndowPropertyConstants.CURRENT_TAX_LOT_BALANCE_INCOME_PRINCIPAL_INDICATOR, EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_INCOME);
074                }
075                
076                if (feeMethod.getFeeBaseCode().equalsIgnoreCase(EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_PRINCIPAL)) {
077                    criteria.addEqualTo(EndowPropertyConstants.CURRENT_TAX_LOT_BALANCE_INCOME_PRINCIPAL_INDICATOR, EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_PRINCIPAL);
078                }
079            }
080    
081            Collection securityClassCodes =  new ArrayList();
082            Collection securityIds = new ArrayList();
083            
084            if (feeMethod.getFeeByClassCode() && feeMethod.getFeeBySecurityCode()) {
085                securityClassCodes = getSecurityClassCodes(feeMethod.getCode());
086                securityIds = getSecurityIds(feeMethod.getCode());
087                
088                securityIds.addAll(securityClassCodes);
089                if (securityIds.size() > 0) {
090                   criteria.addIn(EndowPropertyConstants.CURRENT_TAX_LOT_BALANCE_SECURITY_ID, securityIds);
091                }
092            }
093            else {
094                if (feeMethod.getFeeByTransactionType()) {
095                    securityClassCodes = getSecurityClassCodes(feeMethod.getCode());
096                    if (securityClassCodes.size() > 0) {
097                       criteria.addIn(EndowPropertyConstants.CURRENT_TAX_LOT_BALANCE_SECURITY_ID, securityClassCodes);
098                    }
099                }
100                
101                if (feeMethod.getFeeByETranCode()) {
102                    securityIds = getSecurityIds(feeMethod.getCode());                
103                    if (securityIds.size() > 0) {
104                        criteria.addIn(EndowPropertyConstants.CURRENT_TAX_LOT_BALANCE_SECURITY_ID, securityIds);
105                    }
106                }
107            }
108            
109            QueryByCriteria query = QueryFactory.newQuery(CurrentTaxLotBalance.class, criteria);
110                
111            currentTaxLotBalance = getPersistenceBrokerTemplate().getCollectionByQuery(query);
112            
113            return currentTaxLotBalance;
114        }
115           
116        /**
117         * Gets the security codes for a given securityClassCode in END_FEE_CLS_CD_T table
118         * @feeMethodCode FEE_MTH
119         * @return securityCodes
120         */
121        protected Collection getSecurityClassCodes(String feeMethodCode) {
122            Collection securityClassCodes = new ArrayList();
123            Collection<FeeClassCode> feeClassCodes = new ArrayList();        
124    
125            if (StringUtils.isNotBlank(feeMethodCode)) {        
126                Map<String, String>  crit = new HashMap<String, String>();
127                
128                if (SpringContext.getBean(DataDictionaryService.class).getAttributeForceUppercase(FeeClassCode.class, EndowPropertyConstants.FEE_METHOD_CODE)) {
129                    feeMethodCode = feeMethodCode.toUpperCase();
130                }
131                
132                Criteria criteria = new Criteria();
133                criteria.addEqualTo(EndowPropertyConstants.FEE_METHOD_CODE, feeMethodCode);
134                criteria.addEqualTo(EndowPropertyConstants.FEE_CLASS_CODE_INCLUDE, EndowConstants.YES);
135                
136                QueryByCriteria query = QueryFactory.newQuery(FeeClassCode.class, criteria);
137                
138                feeClassCodes = getPersistenceBrokerTemplate().getCollectionByQuery(query);
139                for (FeeClassCode feeClassCode : feeClassCodes) {
140                    Collection <Security> securities = securityDao.getSecuritiesBySecurityClassCode(feeClassCode.getFeeClassCode());
141                    for (Security security : securities) {
142                        securityClassCodes.add(security.getId());
143                    }
144                }
145            }
146            
147            return securityClassCodes;
148        }
149        
150        /**
151         * Gets the security ids for a given securityClassCode in END_FEE_SEC_T table
152         * @feeMethodCode FEE_MTH
153         * @return securityIds
154         */
155        protected Collection getSecurityIds(String feeMethodCode) {
156            Collection securityIds = new ArrayList();
157            Collection<FeeSecurity> feeSecuritys = new ArrayList();        
158    
159            if (StringUtils.isNotBlank(feeMethodCode)) {        
160                Map<String, String>  crit = new HashMap<String, String>();
161                
162                if (SpringContext.getBean(DataDictionaryService.class).getAttributeForceUppercase(FeeSecurity.class, EndowPropertyConstants.FEE_METHOD_CODE)) {
163                    feeMethodCode = feeMethodCode.toUpperCase();
164                }
165                
166                Criteria criteria = new Criteria();
167                criteria.addEqualTo(EndowPropertyConstants.FEE_METHOD_CODE, feeMethodCode);
168                criteria.addEqualTo(EndowPropertyConstants.FEE_SECURITY_INCLUDE, EndowConstants.YES);
169                
170                QueryByCriteria query = QueryFactory.newQuery(FeeSecurity.class, criteria);
171                
172                feeSecuritys = getPersistenceBrokerTemplate().getCollectionByQuery(query);
173                for (FeeSecurity feeSecurity : feeSecuritys) {
174                    securityIds.add(feeSecurity.getSecurityCode());
175                }
176            }
177            
178            return securityIds;
179        }
180        
181        /**
182         * @see org.kuali.kfs.module.endow.dataaccess.CurrentTaxLotBalanceDao#getCurrentTaxLogBalanceTotalHoldingMarkeValue(FeeMethod)
183         */
184        public BigDecimal getCurrentTaxLotBalanceTotalHoldingUnits(FeeMethod feeMethod) {
185            BigDecimal totalHoldingUnits = new BigDecimal("0");
186            
187            Collection <CurrentTaxLotBalance> currentTaxLotBalanceRecords = getCurrentTaxLotBalances(feeMethod);
188            for (CurrentTaxLotBalance currentTaxLotBalance : currentTaxLotBalanceRecords) {
189                totalHoldingUnits = totalHoldingUnits.add(currentTaxLotBalance.getUnits());    
190            }
191            
192            return totalHoldingUnits;
193        }
194        
195        /**
196         * @see org.kuali.kfs.module.endow.dataaccess.CurrentTaxLotBalanceDao#getCurrentTaxLotBalanceTotalHoldingMarketValue(FeeMethod)
197         */
198        public BigDecimal getCurrentTaxLotBalanceTotalHoldingMarketValue(FeeMethod feeMethod) {
199            BigDecimal totalHoldingMarkteValue = new BigDecimal("0");
200            
201            Collection <CurrentTaxLotBalance> currentTaxLotBalanceRecords = getCurrentTaxLotBalances(feeMethod);
202            for (CurrentTaxLotBalance currentTaxLotBalance : currentTaxLotBalanceRecords) {
203                totalHoldingMarkteValue = totalHoldingMarkteValue.add(currentTaxLotBalance.getMarketValue());    
204            }
205            
206            return totalHoldingMarkteValue;
207        }
208    
209        /**
210         * Gets the securityDao attribute. 
211         * @return Returns the securityDao.
212         */
213        protected SecurityDao getSecurityDao() {
214            return securityDao;
215        }
216    
217        /**
218         * Sets the securityDao attribute value.
219         * @param securityDao The securityDao to set.
220         */
221        public void setSecurityDao(SecurityDao securityDao) {
222            this.securityDao = securityDao;
223        }
224    }