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.util.HashMap;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.kfs.module.endow.EndowConstants;
024    import org.kuali.kfs.module.endow.businessobject.ClassCode;
025    import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionCode;
026    import org.kuali.kfs.module.endow.businessobject.GLLink;
027    import org.kuali.kfs.module.endow.businessobject.KEMID;
028    import org.kuali.kfs.module.endow.businessobject.KemidGeneralLedgerAccount;
029    import org.kuali.kfs.module.endow.businessobject.Security;
030    import org.kuali.kfs.module.endow.businessobject.SecurityValuationMethod;
031    import org.kuali.kfs.module.endow.document.service.ClassCodeService;
032    import org.kuali.kfs.module.endow.document.service.EndowmentTransactionCodeService;
033    import org.kuali.kfs.module.endow.document.service.EndowmentTransactionDocumentService;
034    import org.kuali.kfs.module.endow.document.service.KEMIDService;
035    import org.kuali.kfs.module.endow.document.service.SecurityService;
036    import org.kuali.kfs.module.endow.document.service.SecurityValuationMethodService;
037    import org.kuali.kfs.sys.context.SpringContext;
038    import org.kuali.rice.kns.service.BusinessObjectService;
039    
040    /**
041     * This class is the service implementation for the EndowmentTransactionCodeService. This is the default, Kuali provided implementation.
042     */
043    public class EndowmentTransactionDocumentServiceImpl implements EndowmentTransactionDocumentService 
044    {
045        private BusinessObjectService businessObjectService;
046        private KEMIDService kemidService;
047        private EndowmentTransactionCodeService endowmentTransactionCodeService;
048    
049        public String[] getSecurity(String securityID)
050        {
051            Security security = SpringContext.getBean(SecurityService.class).getByPrimaryKey(securityID);
052            if(null == security)
053                return null;
054    
055            ClassCode classCode  = SpringContext.getBean(ClassCodeService.class).getByPrimaryKey(security.getSecurityClassCode());
056            if(null == classCode)
057                return null;
058            security.setClassCode(classCode);
059    
060            EndowmentTransactionCode tranCode  = SpringContext.getBean(EndowmentTransactionCodeService.class).getByPrimaryKey(classCode.getSecurityEndowmentTransactionCode());
061            if(null == tranCode)
062                return null;
063                   
064            String returnArray[] = new String[5];
065            returnArray[0] = security.getDescription(); 
066            returnArray[1] = security.getSecurityClassCode() + " - " + classCode.getName();
067            returnArray[2] = tranCode.getCode() + " - " + classCode.getName();
068            returnArray[3] = new Boolean( security.getClassCode().isTaxLotIndicator()).toString();
069            returnArray[4] = security.getId();
070            
071            
072            return returnArray;
073        }
074        
075        /**
076         * Gets the security, class and valuationMethod details based on the securityId.
077         * @see org.kuali.kfs.module.endow.document.service.EndowmentTransactionDocumentService#getSecurity(java.lang.String)
078         */
079        public String[] getSecurityForHoldingHistoryValueAdjustment(String securityId) {
080            Security security = SpringContext.getBean(SecurityService.class).getByPrimaryKey(securityId);
081            if(null == security)
082                return null;
083    
084            ClassCode classCode  = SpringContext.getBean(ClassCodeService.class).getByPrimaryKey(security.getSecurityClassCode());
085            if(null == classCode)
086                return null;
087    
088            SecurityValuationMethod securityValuation = SpringContext.getBean(SecurityValuationMethodService.class).getByPrimaryKey(classCode.getValuationMethod());
089            
090            if(null == securityValuation)
091                return null;
092                   
093            classCode.setSecurityValuationMethod(securityValuation);
094            security.setClassCode(classCode);
095            
096            String returnArray[] = new String[4];
097            returnArray[0] = security.getDescription(); 
098            returnArray[1] = classCode.getCodeAndDescription();
099            returnArray[2] = securityValuation.getCodeAndDescription();
100            returnArray[3] = security.getId();
101            
102            return returnArray;
103        }
104        
105        /**
106         * @see org.kuali.kfs.module.endow.document.service.EndowmentTransactionDocumentService#matchChartBetweenKEMIDAndETranCode(java.lang.String, java.lang.String, java.lang.String)
107         * 
108         * Check if the chart code matches based on the following rule:
109         * The ETRAN Code used must have an appropriately identified general ledger object code record; 
110         * one that matches the Chart for the KEMID associated general ledger account.   
111         * - If the END_TRAN_LN_T: TRAN_IP_IND_CD is equal to I, the chart must match the chart of the active END_KEMID_GL_LNK_T record where the IP_IND_CD is equal to I.
112         * - If the END_TRAN_LN_T: TRAN_IP_IND_CD is equal to P, the chart must match the chart of the active END_KEMID_GL_LNK_T record where the IP_IND_CD is equal to P.
113         * Assume that all inputs are valid.
114         */
115        public boolean matchChartBetweenKEMIDAndETranCode(String kemid, String etranCode, String ipIndicator){
116            boolean matchChartIndicator = false;
117            List<KemidGeneralLedgerAccount> kemidGeneralLedgerAccounts = null;
118            List<GLLink> glLinks = null;
119            String theChartCode = null;
120            
121            //Get the chart code, it can't be null because each KEMID always have one active income KemidGeneralLedgerAccount.
122            //Each KEMID always have one active principal KemidGeneralLedgerAccount if type code --> principal restriction code is not NA
123            //This will be valid before checking if the chart codes match.
124            kemidGeneralLedgerAccounts = kemidService.getByPrimaryKey(kemid).getKemidGeneralLedgerAccounts();
125            String theIpIndicator = null;
126            boolean activeIndicatorForKemidGLAccount = false;
127            
128            for (KemidGeneralLedgerAccount kemidGeneralLedgerAccount:kemidGeneralLedgerAccounts){
129               theIpIndicator = kemidGeneralLedgerAccount.getIncomePrincipalIndicatorCode();
130               activeIndicatorForKemidGLAccount = kemidGeneralLedgerAccount.isActive();
131               if (theIpIndicator.equalsIgnoreCase(ipIndicator) && activeIndicatorForKemidGLAccount ){
132                   theChartCode = kemidGeneralLedgerAccount.getChartCode();
133                   break;
134               }
135            }
136            glLinks = endowmentTransactionCodeService.getByPrimaryKey(etranCode).getGlLinks();
137            if (theChartCode != null){
138                for (GLLink glLink:glLinks){
139                    if (glLink.getChartCode().equalsIgnoreCase(theChartCode) && glLink.isActive()){
140                        matchChartIndicator = true;
141                        break;
142                    }
143                }
144            }
145            return matchChartIndicator;
146        }
147        
148        /**
149         * @see org.kuali.kfs.module.endow.document.service.EndowmentTransactionDocumentService#matchChartBetweenKEMIDAndETranCode(java.lang.String, java.lang.String, java.lang.String)
150         * 
151         * Check if the chart code matches based on the following rule:
152         * The Securities ETRAN Code used must have an appropriately identified general ledger object code record; 
153         * one that matches the Chart for the KEMID associated general ledger account.   
154         * - If the END_TRAN_LN_T: TRAN_IP_IND_CD is equal to I, the chart must match the chart of the active END_KEMID_GL_LNK_T record where the IP_IND_CD is equal to I.
155         * - If the END_TRAN_LN_T: TRAN_IP_IND_CD is equal to P, the chart must match the chart of the active END_KEMID_GL_LNK_T record where the IP_IND_CD is equal to P.
156         * Assume that all inputs are valid.
157         */
158        public boolean matchChartBetweenSecurityAndETranCode(Security security, String kemid, String ipIndicator){
159            boolean matchChartIndicator = false;
160            List<KemidGeneralLedgerAccount> kemidGeneralLedgerAccounts = null;
161            List<GLLink> glLinks = null;
162            String theChartCode = null;
163            
164            //Get the chart code, it can't be null because each KEMID always have one active income KemidGeneralLedgerAccount.
165            //Each KEMID always have one active principal KemidGeneralLedgerAccount if type code --> principal restriction code is not NA
166            //This will be valid before checking if the chart codes match.
167            kemidGeneralLedgerAccounts = kemidService.getByPrimaryKey(kemid).getKemidGeneralLedgerAccounts();
168            String theIpIndicator = null;
169            boolean activeIndicatorForKemidGLAccount = false;
170            
171            for (KemidGeneralLedgerAccount kemidGeneralLedgerAccount:kemidGeneralLedgerAccounts){
172               theIpIndicator = kemidGeneralLedgerAccount.getIncomePrincipalIndicatorCode();
173               activeIndicatorForKemidGLAccount = kemidGeneralLedgerAccount.isActive();
174               if (theIpIndicator.equalsIgnoreCase(ipIndicator) && activeIndicatorForKemidGLAccount ){
175                   theChartCode = kemidGeneralLedgerAccount.getChartCode();
176                   break;
177               }
178            }
179            
180            //Obtain Etran code from security
181            security.refreshNonUpdateableReferences();
182            //Obtain Security GL Links
183            glLinks = endowmentTransactionCodeService.getByPrimaryKey(security.getClassCode().getSecurityEndowmentTransactionCode()).getGlLinks();
184            
185            if (theChartCode != null){
186                for (GLLink glLink:glLinks){
187                    if (glLink.getChartCode().equalsIgnoreCase(theChartCode) && glLink.isActive()){
188                        matchChartIndicator = true;
189                        break;
190                    }
191                }
192            }
193            return matchChartIndicator;
194        }
195        /**
196         * Gets the businessObjectService.
197         * 
198         * @return businessObjectService
199         */
200        public BusinessObjectService getBusinessObjectService() {
201            return businessObjectService;
202        }
203    
204        /**
205         * Sets the businessObjectService
206         * 
207         * @param businessObjectService
208         */
209        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
210            this.businessObjectService = businessObjectService;
211        }
212        
213        /**
214         * Gets the kemidService.
215         * 
216         * @return kemidService
217         */
218        public KEMIDService getKemidService(){
219            return kemidService;
220        }
221        
222        /**
223         * Sets the kemidService.
224         * 
225         * @param kemidService
226         */
227        public void setKemidService (KEMIDService kemidService){
228            this.kemidService = kemidService; 
229        }
230        
231        /**
232         * Gets the endowmentTransactionCodeService.
233         * 
234         * @return endowmentTransactionCodeService
235         */
236        public EndowmentTransactionCodeService getEndowmentTransactionCodeService(){
237            return endowmentTransactionCodeService;
238        }
239        
240        /**
241         * Sets the endowmentTransactionCodeService.
242         * 
243         * @param endowmentTransactionCodeService
244         */
245        public void setEndowmentTransactionCodeService (EndowmentTransactionCodeService endowmentTransactionCodeService){
246            this.endowmentTransactionCodeService = endowmentTransactionCodeService; 
247        }
248    
249    }