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.math.BigDecimal; 019 import java.text.ParseException; 020 import java.text.SimpleDateFormat; 021 import java.util.Calendar; 022 import java.util.Date; 023 import java.util.GregorianCalendar; 024 import java.util.HashMap; 025 import java.util.Map; 026 027 import org.apache.log4j.Logger; 028 import org.kuali.kfs.module.endow.EndowConstants; 029 import org.kuali.kfs.module.endow.EndowParameterKeyConstants; 030 import org.kuali.kfs.module.endow.batch.AvailableCashUpdateStep; 031 import org.kuali.kfs.module.endow.businessobject.CurrentTaxLotBalance; 032 import org.kuali.kfs.module.endow.businessobject.PooledFundValue; 033 import org.kuali.kfs.module.endow.document.service.CurrentTaxLotService; 034 import org.kuali.kfs.module.endow.document.service.KEMService; 035 import org.kuali.kfs.pdp.businessobject.Batch; 036 import org.kuali.kfs.sys.KFSConstants; 037 import org.kuali.kfs.sys.context.SpringContext; 038 import org.kuali.kfs.sys.service.impl.KfsParameterConstants; 039 import org.kuali.rice.kns.service.DateTimeService; 040 import org.kuali.rice.kns.service.ParameterService; 041 import org.kuali.rice.kns.util.KualiInteger; 042 import org.kuali.rice.kns.util.ObjectUtils; 043 044 public class KEMServiceImpl implements KEMService { 045 046 private DateTimeService dateTimeService; 047 private ParameterService parameterService; 048 private CurrentTaxLotService currentTaxLotService; 049 050 private static Logger log = org.apache.log4j.Logger.getLogger(KEMServiceImpl.class); 051 052 /** 053 * @see org.kuali.kfs.module.endow.document.service.KEMService#getMarketValue(java.lang.String) 054 */ 055 public BigDecimal getMarketValue(String securityId) { 056 057 BigDecimal marketValue = currentTaxLotService.getHoldingMarketValueSumForSecurity(securityId); 058 059 return marketValue; 060 } 061 062 /** 063 * @see org.kuali.kfs.module.endow.document.service.KEMService#getMarketValue(java.lang.String, java.lang.String, 064 * java.lang.String, org.kuali.rice.kns.util.KualiInteger, java.lang.String) 065 */ 066 public BigDecimal getMarketValue(String kemid, String securityId, String registrationCode, KualiInteger lotNumber, String ipIndicator) { 067 BigDecimal marketValue = BigDecimal.ZERO; 068 069 CurrentTaxLotBalance currentTaxLotBalance = currentTaxLotService.getByPrimaryKey(kemid, securityId, registrationCode, lotNumber, ipIndicator); 070 071 if (ObjectUtils.isNotNull(currentTaxLotBalance) && currentTaxLotBalance.getHoldingMarketValue() != null) { 072 marketValue = currentTaxLotBalance.getHoldingMarketValue(); 073 } 074 return marketValue; 075 } 076 077 /** 078 * @see org.kuali.kfs.module.endow.document.service.KEMService#mod10(java.lang.String) 079 */ 080 public String mod10(String prefix) { 081 Map<String, Integer> mod10 = new HashMap<String, Integer>(); 082 char[] userInputChars = prefix.toCharArray(); 083 int theNinthDigit = 0; 084 085 for (int i = 0; i < userInputChars.length; i++) { 086 087 int mod10MappingForCurrentChar = mapChar(userInputChars[i]); 088 089 if (i % 2 != 0) { 090 091 mod10MappingForCurrentChar *= 2; 092 } 093 094 if (mod10MappingForCurrentChar > 9) { 095 mod10MappingForCurrentChar = (mod10MappingForCurrentChar % 10) + (mod10MappingForCurrentChar / 10); 096 } 097 098 theNinthDigit += mod10MappingForCurrentChar; 099 } 100 101 theNinthDigit = (10 - (theNinthDigit % 10)) % 10; 102 103 return String.valueOf(theNinthDigit); 104 } 105 106 /** 107 * Maps a char to an integer value 108 * 109 * @param c the character to be mapped 110 * @return the mapped value 111 */ 112 private static int mapChar(char c) { 113 114 if (c >= '0' && c <= '9') 115 return c - '0'; 116 117 if (c == '*') 118 return 36; 119 if (c == '#') 120 return 37; 121 if (c == '@') 122 return 38; 123 124 return c - 'A' + 10; 125 } 126 127 /** 128 * @see org.kuali.kfs.module.endow.document.service.KEMService#getCurrentSystemProcessDate() 129 */ 130 public String getCurrentSystemProcessDate() { 131 132 ParameterService parameterService = SpringContext.getBean(ParameterService.class); 133 134 String curentSystemProcessDate = parameterService.getParameterValue(KfsParameterConstants.ENDOWMENT_ALL.class, EndowParameterKeyConstants.CURRENT_PROCESS_DATE); 135 136 return curentSystemProcessDate; 137 } 138 139 /** 140 * @see org.kuali.kfs.module.endow.document.service.KEMService#getCurrentSystemProcessDate() 141 */ 142 public String getCurrentSystemProcessDateFormated() throws Exception { 143 return getDateTimeService().toDateString(getCurrentDate()); 144 } 145 146 public Date getCurrentSystemProcessDateObject() { 147 Date date = null; 148 149 try { 150 String systemDateString = getCurrentSystemProcessDate(); 151 SimpleDateFormat sdfSource = new SimpleDateFormat("dd-MMM-yy"); 152 date = sdfSource.parse(systemDateString); 153 154 } 155 catch (Exception e) { 156 log.debug("Issue obtaining System Date", e); 157 date = new Date(); 158 } 159 160 return date; 161 } 162 163 /** 164 * @see org.kuali.kfs.module.endow.document.service.KEMService#getCurrentDate() 165 */ 166 public java.sql.Date getCurrentDate() { 167 java.sql.Date currentDate = null; 168 String useProcessDate = parameterService.getParameterValue(KfsParameterConstants.ENDOWMENT_ALL.class, EndowParameterKeyConstants.USE_PROCESS_DATE); 169 170 if (KFSConstants.ParameterValues.YES.equalsIgnoreCase(useProcessDate)) { 171 currentDate = getCurrentProcessDate(); 172 } 173 else { 174 currentDate = dateTimeService.getCurrentSqlDate(); 175 } 176 return currentDate; 177 } 178 179 /** 180 * @see org.kuali.kfs.module.endow.document.service.KEMService#getCurrentProcessDate() 181 */ 182 public java.sql.Date getCurrentProcessDate() { 183 java.sql.Date date = null; 184 185 String currentProcessDate = getCurrentSystemProcessDate(); 186 try { 187 date = dateTimeService.convertToSqlDate(currentProcessDate); 188 } 189 catch (ParseException ex) { 190 throw new RuntimeException("Invalid value for " + EndowParameterKeyConstants.CURRENT_PROCESS_DATE + " system parameter: " + currentProcessDate + ".\n" + ex.getMessage()); 191 } 192 193 return date; 194 } 195 196 /** 197 * @see org.kuali.kfs.module.endow.document.service.KEMService#getAvailableCashPercent() Gets the AVAILABLE_CASH_PERCENT system 198 * parameter 199 * @return AVAILABLE_CASH_PERCENT value 200 */ 201 public BigDecimal getAvailableCashPercent() { 202 BigDecimal availableCashPercent = BigDecimal.ZERO; 203 204 ParameterService parameterService = SpringContext.getBean(ParameterService.class); 205 String systemParameterAvailablePercent = parameterService.getParameterValue(AvailableCashUpdateStep.class, EndowParameterKeyConstants.AvailableCashUpdateConstants.AVAILABLE_CASH_PERCENT); 206 availableCashPercent = new BigDecimal(systemParameterAvailablePercent); 207 208 return availableCashPercent; 209 } 210 211 /** 212 * @see org.kuali.kfs.module.endow.document.service.KEMService#getFiscalYearEndDayAndMonth() Gets the 213 * FISCAL_YEAR_END_DAY_AND_MONTH system parameter 214 * @return FISCAL_YEAR_END_DAY_AND_MONTH value 215 */ 216 public java.sql.Date getFiscalYearEndDayAndMonth() { 217 java.sql.Date fiscalDate = null; 218 219 ParameterService parameterService = SpringContext.getBean(ParameterService.class); 220 String yearEndDateAndMonth = parameterService.getParameterValue(KfsParameterConstants.ENDOWMENT_BATCH.class, EndowParameterKeyConstants.FISCAL_YEAR_END_DAY_AND_MONTH); 221 222 Calendar calendar = Calendar.getInstance(); 223 224 yearEndDateAndMonth = yearEndDateAndMonth.substring(0, 2).concat("/").concat(yearEndDateAndMonth.substring(2, 4)).concat("/") + calendar.get(Calendar.YEAR); 225 226 try { 227 fiscalDate = getDateTimeService().convertToSqlDate(yearEndDateAndMonth); 228 } 229 catch (ParseException pe) { 230 return null; 231 } 232 233 return fiscalDate; 234 } 235 236 /** 237 * @see org.kuali.kfs.module.endow.document.service.org.kuali.kfs.module.endow.document.service.KEMService#getTotalNumberOfPaymentsForFiscalYear() 238 */ 239 public long getTotalNumberOfPaymentsForFiscalYear() { 240 long totalNumberOfPayments = 2; 241 242 ParameterService parameterService = SpringContext.getBean(ParameterService.class); 243 String totalPayments = parameterService.getParameterValue(PooledFundValue.class, EndowParameterKeyConstants.DISTRIBUTION_TIMES_PER_YEAR); 244 245 try { 246 totalNumberOfPayments = Long.parseLong(totalPayments); 247 } catch (NumberFormatException nfe) { 248 log.info("Unable to convert the value of DISTRIBUTION_TIMES_PER_YEAR system parameter."); 249 } 250 251 return totalNumberOfPayments; 252 } 253 254 /** 255 * @see org.kuali.kfs.module.endow.document.service.KEMService#getNumberOfDaysInCalendarYear() 256 */ 257 public int getNumberOfDaysInCalendarYear() { 258 Date currentDate = getCurrentDate(); 259 Calendar calendar = Calendar.getInstance(); 260 261 GregorianCalendar gregorianCalendar = new GregorianCalendar(); 262 calendar.setTime(currentDate); 263 gregorianCalendar.setTime(currentDate); 264 265 if (gregorianCalendar.isLeapYear(calendar.YEAR)) { 266 return 366; 267 } 268 else 269 return 365; 270 } 271 272 /** 273 * Gets the first day after the fiscal year end day and month as set in the system parameter. 274 * @see org.kuali.kfs.module.endow.document.service.KEMService#getFirstDayAfterFiscalYearEndDayAndMonth() 275 * @return Date 276 */ 277 public java.sql.Date getFirstDayAfterFiscalYearEndDayAndMonth() { 278 Date fiscalYearEndDate = getFiscalYearEndDayAndMonth(); 279 280 Calendar calendar = Calendar.getInstance(); 281 calendar.setTime(fiscalYearEndDate); 282 calendar.add(Calendar.DAY_OF_MONTH, 1); 283 calendar.set(Calendar.HOUR, 0); 284 calendar.set(Calendar.MINUTE, 0); 285 calendar.set(Calendar.SECOND, 0); 286 calendar.set(Calendar.MILLISECOND, 0); 287 return new java.sql.Date(calendar.getTimeInMillis()); 288 } 289 290 /** 291 * @see org.kuali.kfs.module.endow.document.service.KEMService#getMaxNumberOfTransactionLinesPerDocument() 292 * @return maximumNumberOfTransactionLines 293 */ 294 public int getMaxNumberOfTransactionLinesPerDocument() { 295 int maximumNumberOfTransactionLines = EndowConstants.MAXIMUM_NUMBER_OF_LINES_PER_EDOC; 296 297 try { 298 maximumNumberOfTransactionLines = Integer.parseInt(parameterService.getParameterValue(KfsParameterConstants.ENDOWMENT_BATCH.class, EndowParameterKeyConstants.MAXIMUM_TRANSACTION_LINES)); 299 } 300 catch (NumberFormatException ex) { 301 return maximumNumberOfTransactionLines; 302 } 303 304 return maximumNumberOfTransactionLines; 305 } 306 307 /** 308 * Gets the dateTimeService. 309 * 310 * @return dateTimeService 311 */ 312 public DateTimeService getDateTimeService() { 313 return dateTimeService; 314 } 315 316 /** 317 * Sets dateTimeService. 318 * 319 * @param dateTimeService 320 */ 321 public void setDateTimeService(DateTimeService dateTimeService) { 322 this.dateTimeService = dateTimeService; 323 } 324 325 /** 326 * Gets the parameterService. 327 * 328 * @return parameterService 329 */ 330 public ParameterService getParameterService() { 331 return parameterService; 332 } 333 334 /** 335 * Sets the parameterService. 336 * 337 * @param parameterService 338 */ 339 public void setParameterService(ParameterService parameterService) { 340 this.parameterService = parameterService; 341 } 342 343 /** 344 * Gets the currentTaxLotService. 345 * 346 * @param currentTaxLotService 347 */ 348 public void setCurrentTaxLotService(CurrentTaxLotService currentTaxLotService) { 349 this.currentTaxLotService = currentTaxLotService; 350 } 351 352 }