001/*
002 * Copyright 2007-2008 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 */
016package org.kuali.ole.sys.service.impl;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.commons.lang.ArrayUtils;
022import org.apache.commons.lang.StringUtils;
023import org.kuali.ole.sys.OLEParameterKeyConstants;
024import org.kuali.ole.sys.businessobject.Bank;
025import org.kuali.ole.sys.context.SpringContext;
026import org.kuali.ole.sys.service.BankService;
027import org.kuali.rice.core.api.parameter.ParameterEvaluator;
028import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
029import org.kuali.rice.coreservice.framework.parameter.ParameterService;
030import org.kuali.rice.kns.service.DataDictionaryService;
031import org.kuali.rice.krad.service.BusinessObjectService;
032import org.springframework.cache.annotation.Cacheable;
033
034/**
035 * Default implementation of the <code>BankService</code> interface.
036 *
037 * @see org.kuali.ole.fp.service.BankService
038 */
039public class BankServiceImpl implements BankService {
040    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BankServiceImpl.class);
041
042    protected BusinessObjectService businessObjectService;
043    protected DataDictionaryService dataDictionaryService;
044    protected ParameterService parameterService;
045    protected ParameterEvaluatorService parameterEvaluatorService;
046
047    /**
048     * @see org.kuali.ole.fp.service.BankService#getByPrimaryId(java.lang.String)
049     */
050    @Override
051    @Cacheable(value=Bank.CACHE_NAME, key="'bankCode='+#p0")
052    public Bank getByPrimaryId(String bankCode) {
053        if ( StringUtils.isBlank(bankCode) ) {
054            return null;
055        }
056        return businessObjectService.findBySinglePrimaryKey(Bank.class, bankCode);
057    }
058
059    /**
060     * @see org.kuali.ole.sys.service.BankService#getDefaultBankByDocType(java.lang.String)
061     */
062    @Override
063    @Cacheable(value=Bank.CACHE_NAME, key="'DefaultByDocType-'+#p0")
064    public Bank getDefaultBankByDocType(String documentTypeCode) {
065        if (parameterService.parameterExists(Bank.class, OLEParameterKeyConstants.DEFAULT_BANK_BY_DOCUMENT_TYPE)) {
066            List<String> parmValues = new ArrayList<String>( parameterService.getSubParameterValuesAsString(Bank.class, OLEParameterKeyConstants.DEFAULT_BANK_BY_DOCUMENT_TYPE, documentTypeCode) );
067
068            if (parmValues != null && !parmValues.isEmpty()) {
069                String defaultBankCode = parmValues.get(0);
070
071                Bank defaultBank = getByPrimaryId(defaultBankCode);
072
073                // check active status, if not return continuation bank if active
074                if ( defaultBank != null && !defaultBank.isActive() && defaultBank.getContinuationBank() != null && defaultBank.getContinuationBank().isActive()) {
075                    return defaultBank.getContinuationBank();
076                }
077
078                return defaultBank;
079            }
080        }
081
082        return null;
083    }
084
085    /**
086     * @see org.kuali.ole.sys.service.BankService#getDefaultBankByDocType(java.lang.Class)
087     */
088    @Override
089    @Cacheable(value=Bank.CACHE_NAME, key="'DefaultByDocClass-'+#p0")
090    public Bank getDefaultBankByDocType(Class<?> documentClass) {
091        String documentTypeCode = dataDictionaryService.getDocumentTypeNameByClass(documentClass);
092
093        if (StringUtils.isBlank(documentTypeCode)) {
094            throw new RuntimeException("Document type not found for document class: " + documentClass.getName());
095        }
096        return getDefaultBankByDocType(documentTypeCode);
097    }
098
099    /**
100     * @see org.kuali.ole.sys.service.BankService#isBankSpecificationEnabled()
101     */
102    @Override
103    @Cacheable(value=Bank.CACHE_NAME, key="'isBankSpecificationEnabled'")
104    public boolean isBankSpecificationEnabled() {
105        return parameterService.getParameterValueAsBoolean(Bank.class, OLEParameterKeyConstants.ENABLE_BANK_SPECIFICATION_IND);
106    }
107
108    /**
109     * @see org.kuali.ole.sys.service.BankService#isBankSpecificationEnabledForDocument(java.lang.Class)
110     */
111    @Override
112    @Cacheable(value=Bank.CACHE_NAME, key="'isBankSpecificationEnabled'+#p0")
113    public boolean isBankSpecificationEnabledForDocument(Class<?> documentClass) {
114        String documentTypeCode = dataDictionaryService.getDocumentTypeNameByClass(documentClass);
115        if (ArrayUtils.contains(PERMANENT_BANK_SPECIFICATION_ENABLED_DOCUMENT_TYPES, documentTypeCode)) {
116            return true;
117        }
118        ParameterEvaluator evaluator = SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(Bank.class, OLEParameterKeyConstants.BANK_CODE_DOCUMENT_TYPES, documentTypeCode);
119        return evaluator.evaluationSucceeds();
120    }
121
122    public void setBusinessObjectService(BusinessObjectService businessObjectService) {
123        this.businessObjectService = businessObjectService;
124    }
125
126    public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
127        this.dataDictionaryService = dataDictionaryService;
128    }
129
130    public void setParameterService(ParameterService parameterService) {
131        this.parameterService = parameterService;
132    }
133
134    public void setParameterEvaluatorService(ParameterEvaluatorService parameterEvaluatorService) {
135        this.parameterEvaluatorService = parameterEvaluatorService;
136    }
137}