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.service.impl;
017    
018    import java.sql.Date;
019    import java.text.ParseException;
020    import java.util.ArrayList;
021    import java.util.Calendar;
022    import java.util.Collection;
023    import java.util.HashMap;
024    import java.util.List;
025    import java.util.Map;
026    
027    import org.apache.commons.lang.StringUtils;
028    import org.kuali.kfs.module.endow.EndowPropertyConstants;
029    import org.kuali.kfs.module.endow.businessobject.PooledFundControl;
030    import org.kuali.kfs.module.endow.businessobject.PooledFundValue;
031    import org.kuali.kfs.module.endow.dataaccess.PooledFundValueDao;
032    import org.kuali.kfs.module.endow.document.service.PooledFundValueService;
033    import org.kuali.kfs.sys.context.SpringContext;
034    import org.kuali.rice.kns.service.BusinessObjectService;
035    import org.kuali.rice.kns.service.DateTimeService;
036    import org.kuali.rice.kns.util.ObjectUtils;
037    import org.springframework.transaction.annotation.Transactional;
038    
039    @Transactional
040    public class PooledFundValueServiceImpl implements PooledFundValueService {
041        private BusinessObjectService businessObjectService;
042        private PooledFundValueDao pooledFundValueDao;
043    
044        /**
045         * @see org.kuali.kfs.module.endow.document.service.PooledFundControlService#getByPrimaryKey(java.lang.String)
046         */
047        public PooledFundValue getByPrimaryKey(String id) {
048            PooledFundValue pooledFundValue = null;
049            if (StringUtils.isNotBlank(id)) {
050                Map criteria = new HashMap();
051                criteria.put("pooledSecurityID", id);
052    
053                pooledFundValue = (PooledFundValue) businessObjectService.findByPrimaryKey(PooledFundValue.class, criteria);
054            }
055            return pooledFundValue;
056        }
057    
058        /**
059         * @see org.kuali.kfs.module.endow.document.service.PooledFundValueService#calculateValueEffectiveDateForAjax(java.lang.String,
060         *      java.lang.String)
061         */
062        public String calculateValueEffectiveDateForAjax(String valuationDate, String pooledSecurityID) {
063            DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
064            String valueEffectiveDate = null;
065    
066            try {
067                Date valDate = dateTimeService.convertToSqlDate(valuationDate);
068                Date computedValEffectiveDate = calculateValueEffectiveDate(valDate, pooledSecurityID);
069    
070                if (computedValEffectiveDate != null) {
071                    valueEffectiveDate = dateTimeService.toDateString(computedValEffectiveDate);
072                }
073            }
074            catch (ParseException e) {
075                // do nothing, the returned value will be empty string
076            }
077            return valueEffectiveDate;
078        }
079    
080        public Date calculateValueEffectiveDate(Date valuationDate, String pooledSecurityID) {
081            PooledFundControl pooledFundControl = null;
082            if (StringUtils.isNotBlank(pooledSecurityID)) {
083                Map criteria = new HashMap();
084                criteria.put("pooledSecurityID", pooledSecurityID);
085    
086                pooledFundControl = (PooledFundControl) businessObjectService.findByPrimaryKey(PooledFundControl.class, criteria);
087            }
088            if (ObjectUtils.isNotNull(pooledFundControl)) {
089                int incrementDays = pooledFundControl.getIncrementValuationDays().intValue();
090    
091                // Calculate the date
092                Calendar calendar = Calendar.getInstance();
093                calendar.setTime(valuationDate);
094                calendar.add(Calendar.DATE, incrementDays);
095                java.sql.Date valueEffectiveDate = new java.sql.Date(calendar.getTime().getTime());
096                return valueEffectiveDate;
097            }
098    
099            return null;
100        }
101    
102        public void setIncomeDistributionCompleted(List<PooledFundValue> pooledFundValueList, boolean completed) {
103            pooledFundValueDao.setIncomeDistributionCompleted(pooledFundValueList, completed);
104        }
105    
106        /**
107         * This method gets the businessObjectService.
108         * 
109         * @return businessObjectService
110         */
111        public BusinessObjectService getBusinessObjectService() {
112            return businessObjectService;
113        }
114    
115        /**
116         * This method sets the businessObjectService
117         * 
118         * @param businessObjectService
119         */
120        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
121            this.businessObjectService = businessObjectService;
122        }
123    
124        public boolean isValuationDateTheLatest(String pooledSecurityID, Date theValuationDate) {
125            boolean isLatest = true;
126    
127            Map<String, String> fieldValues = new HashMap<String, String>();
128            fieldValues.put(EndowPropertyConstants.POOL_SECURITY_ID, pooledSecurityID);
129    
130            BusinessObjectService businessObjectService = SpringContext.getBean(BusinessObjectService.class);
131            Collection<PooledFundValue> pooledFundValues = businessObjectService.findMatching(PooledFundValue.class, fieldValues);
132    
133            if (pooledFundValues.isEmpty())
134                return true;
135    
136            Calendar calendar = Calendar.getInstance();
137            Calendar theCalendar = Calendar.getInstance();
138            theCalendar.setTime(theValuationDate);
139    
140            for (PooledFundValue pooledFundValue : pooledFundValues) {
141                Date valuationDate = pooledFundValue.getValuationDate();
142                calendar.setTime(valuationDate);
143                if (theCalendar.before(calendar)) {
144                    isLatest = false;
145                    break;
146                }
147            }
148    
149            return isLatest;
150        }
151    
152        public Date getLatestValueEffectiveDate(String pooledSecurityID) {
153            Map<String, String> fieldValues = new HashMap<String, String>();
154            fieldValues.put(EndowPropertyConstants.POOL_SECURITY_ID, pooledSecurityID);
155    
156            BusinessObjectService businessObjectService = SpringContext.getBean(BusinessObjectService.class);
157            Collection<PooledFundValue> pooledFundValues = businessObjectService.findMatching(PooledFundValue.class, fieldValues);
158    
159            if (pooledFundValues.isEmpty())
160                return null;
161    
162            PooledFundValue thePooledFundValue = pooledFundValues.iterator().next();
163            Date theLastestValueEffectiveDate = thePooledFundValue.getValueEffectiveDate();
164    
165            Calendar calendar = Calendar.getInstance();
166            Calendar theLatestCalendar = Calendar.getInstance();
167            theLatestCalendar.setTime(theLastestValueEffectiveDate);
168            Date valueEffectiveDate = null;
169            for (PooledFundValue pooledFundValue : pooledFundValues) {
170                valueEffectiveDate = pooledFundValue.getValueEffectiveDate();
171                calendar.setTime(valueEffectiveDate);
172                if (theLatestCalendar.before(calendar)) {
173                    theLatestCalendar = calendar;
174                }
175            }
176    
177            return new java.sql.Date(theLatestCalendar.getTime().getTime());
178    
179        }
180    
181        /**
182         * @see org.kuali.kfs.module.endow.document.service.PooledFundValueService#getPooledFundValueWhereSTProcessOnDateIsCurrentDate()
183         */
184        public List<PooledFundValue> getPooledFundValueWhereSTProcessOnDateIsCurrentDate() {
185            // this is a list of pooled fund values sorted by security ID and descending by value effective date
186            List<PooledFundValue> fundValues = pooledFundValueDao.getPooledFundValueWhereSTProcessOnDateIsCurrentDate();
187            List<PooledFundValue> resultList = new ArrayList<PooledFundValue>();
188    
189            String currentSecurity = null;
190            // build a list of pooled fund values where we only get the entry with the most recent value effective date per security ID
191            if (fundValues != null && fundValues.size() > 0) {
192    
193                for (PooledFundValue pooledFundValue : fundValues) {
194    
195                    if (currentSecurity == null) {
196                        currentSecurity = pooledFundValue.getPooledSecurityID();
197                        resultList.add(pooledFundValue);
198                    }
199                    else {
200                        if (!currentSecurity.equalsIgnoreCase(pooledFundValue.getPooledSecurityID())) {
201                            currentSecurity = pooledFundValue.getPooledSecurityID();
202                            resultList.add(pooledFundValue);
203                        }
204                    }
205    
206                }
207            }
208    
209            return resultList;
210        }
211    
212        /**
213         * @see org.kuali.kfs.module.endow.document.service.PooledFundValueService#getPooledFundValueWhereLTProcessOnDateIsCurrentDate()
214         */
215        public List<PooledFundValue> getPooledFundValueWhereLTProcessOnDateIsCurrentDate() {
216    
217            // this is a list of pooled fund values sorted by security ID and descending by value effective date
218            List<PooledFundValue> fundValues = pooledFundValueDao.getPooledFundValueWhereLTProcessOnDateIsCurrentDate();
219            List<PooledFundValue> resultList = new ArrayList<PooledFundValue>();
220    
221            String currentSecurity = null;
222    
223            // build a list of pooled fund values where we only get the entry with the most recent value effective date per security ID
224            if (fundValues != null && fundValues.size() > 0) {
225    
226                for (PooledFundValue pooledFundValue : fundValues) {
227    
228                    if (currentSecurity == null) {
229                        currentSecurity = pooledFundValue.getPooledSecurityID();
230                        resultList.add(pooledFundValue);
231                    }
232                    else {
233                        if (!currentSecurity.equalsIgnoreCase(pooledFundValue.getPooledSecurityID())) {
234                            currentSecurity = pooledFundValue.getPooledSecurityID();
235                            resultList.add(pooledFundValue);
236                        }
237                    }
238    
239                }
240            }
241    
242            return resultList;
243        }
244    
245        /**
246         * @see org.kuali.kfs.module.endow.document.service.PooledFundValueService#getPooledFundValueWhereDistributionIncomeOnDateIsCurrentDate()
247         */
248        public List<PooledFundValue> getPooledFundValueWhereDistributionIncomeOnDateIsCurrentDate() {
249    
250            return pooledFundValueDao.getPooledFundValueWhereDistributionIncomeOnDateIsCurrentDate();
251        }
252    
253        /**
254         * Sets the pooledFundValueDao.
255         * 
256         * @param pooledFundValueDao
257         */
258        public void setPooledFundValueDao(PooledFundValueDao pooledFundValueDao) {
259            this.pooledFundValueDao = pooledFundValueDao;
260        }
261    }