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.businessobject.lookup;
017
018 import java.math.BigDecimal;
019 import java.util.HashMap;
020 import java.util.List;
021 import java.util.Map;
022
023 import org.kuali.kfs.module.endow.EndowConstants;
024 import org.kuali.kfs.module.endow.EndowPropertyConstants;
025 import org.kuali.kfs.module.endow.businessobject.IncomePrincipalIndicator;
026 import org.kuali.kfs.module.endow.businessobject.KEMIDCurrentBalanceDetail;
027 import org.kuali.kfs.module.endow.businessobject.KemidCurrentCash;
028 import org.kuali.kfs.module.endow.document.service.impl.KemidCurrentCashServiceImpl;
029 import org.kuali.kfs.sys.context.SpringContext;
030 import org.kuali.rice.kns.bo.BusinessObject;
031 import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
032 import org.kuali.rice.kns.util.ObjectUtils;
033
034 public class KEMIDCurrentBalanceDetailLookupableHelperService extends KualiLookupableHelperServiceImpl {
035
036 /**
037 * @see org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl#getSearchResults(java.util.Map)
038 */
039 public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
040 List<KEMIDCurrentBalanceDetail> results = (List<KEMIDCurrentBalanceDetail>) super.getSearchResults(fieldValues);
041
042 // check if there are entries with a CSHEQ reporting group in the results list
043 boolean incCashEq = false;
044 boolean princCashEq = false;
045
046 for (KEMIDCurrentBalanceDetail balanceDetail : results) {
047 if (EndowConstants.SecurityReportingGroups.CASH_EQUIVALENTS.equalsIgnoreCase(balanceDetail.getReportingGroupCode())) {
048
049 if (EndowConstants.IncomePrincipalIndicator.INCOME.equalsIgnoreCase(balanceDetail.getIncomePrincipalIndicator())) {
050 incCashEq = true;
051 }
052 else if (EndowConstants.IncomePrincipalIndicator.PRINCIPAL.equalsIgnoreCase(balanceDetail.getIncomePrincipalIndicator())) {
053 princCashEq = true;
054 }
055
056 if (incCashEq && princCashEq) {
057 break;
058 }
059 }
060 }
061
062 if (!incCashEq || !princCashEq) {
063 String kemid = fieldValues.get(EndowPropertyConstants.KEMID);
064 // check if there is current income/principal cash
065 KemidCurrentCashServiceImpl kemidCurrentCashService = SpringContext.getBean(KemidCurrentCashServiceImpl.class);
066 KemidCurrentCash currentCash = kemidCurrentCashService.getByPrimaryKey(kemid);
067
068 if (ObjectUtils.isNotNull(currentCash)) {
069 // if there is an entry in the current cash for the kemid, and there is income current cash but there were no
070 // holdings for this kemid for CSHEQ reporting group add a new entry in the results list to show the current income
071 // cash
072 if (!incCashEq && currentCash.getCurrentIncomeCash().isNonZero()) {
073 KEMIDCurrentBalanceDetail balanceDetail = createCurrentBalanceDetailForCSHEQ(kemid, EndowConstants.IncomePrincipalIndicator.INCOME, currentCash);
074
075 results.add(balanceDetail);
076 }
077
078 // if there is an entry in the current cash for the kemid, and there is principal current cash but there were no
079 // holdings for this kemid for CSHEQ reporting group add a new entry in the results list to show the current
080 // principal cash
081 if (!princCashEq && currentCash.getCurrentPrincipalCash().isNonZero()) {
082 KEMIDCurrentBalanceDetail balanceDetail = createCurrentBalanceDetailForCSHEQ(kemid, EndowConstants.IncomePrincipalIndicator.PRINCIPAL, currentCash);
083
084 results.add(balanceDetail);
085 }
086 }
087 }
088
089 return results;
090 }
091
092 /**
093 * Creates a new KEMIDCurrentBalanceDetail for CSHEQ reporting group. This method is used when there is an entry in the current
094 * cash for the kemid but there are no holdings.
095 *
096 * @param kemid
097 * @param incomeOrPrincipal
098 * @param currentCash
099 * @return a new KEMIDCurrentBalanceDetail
100 */
101 private KEMIDCurrentBalanceDetail createCurrentBalanceDetailForCSHEQ(String kemid, String incomeOrPrincipal, KemidCurrentCash currentCash) {
102
103 KEMIDCurrentBalanceDetail balanceDetail = new KEMIDCurrentBalanceDetail();
104
105 balanceDetail.setKemid(kemid);
106 balanceDetail.setReportingGroupCode(EndowConstants.SecurityReportingGroups.CASH_EQUIVALENTS);
107 balanceDetail.setIncomePrincipalIndicator(incomeOrPrincipal);
108
109
110 if (EndowConstants.IncomePrincipalIndicator.INCOME.equalsIgnoreCase(incomeOrPrincipal)) {
111 balanceDetail.setValueAtMarket(currentCash.getCurrentIncomeCash().bigDecimalValue());
112 }
113 else {
114 balanceDetail.setValueAtMarket(currentCash.getCurrentPrincipalCash().bigDecimalValue());
115 }
116
117 Map<String, String> keys = new HashMap<String, String>();
118 keys.put(EndowPropertyConstants.KUALICODEBASE_CODE, incomeOrPrincipal);
119 IncomePrincipalIndicator incomePrincipalIndicator = (IncomePrincipalIndicator) businessObjectService.findByPrimaryKey(IncomePrincipalIndicator.class, keys);
120 balanceDetail.setIpIndicator(incomePrincipalIndicator);
121
122 balanceDetail.setAnnualEstimatedIncome(BigDecimal.ZERO.setScale(2));
123 balanceDetail.setNextFYEstimatedIncome(BigDecimal.ZERO.setScale(2));
124 balanceDetail.setRemainderOfFYEstimatedIncome(BigDecimal.ZERO.setScale(2));
125
126 balanceDetail.setNoDrillDownOnMarketVal(true);
127
128 return balanceDetail;
129 }
130 }