001/**
002 * Copyright 2005-2018 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.rice.krad.dao.proxy;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.CoreApiServiceLocator;
020import org.kuali.rice.kns.service.KNSServiceLocator;
021import org.kuali.rice.krad.bo.ModuleConfiguration;
022import org.kuali.rice.krad.dao.LookupDao;
023import org.kuali.rice.krad.dao.impl.LookupDaoOjb;
024import org.kuali.rice.krad.service.KRADServiceLocator;
025import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
026import org.kuali.rice.krad.service.KualiModuleService;
027import org.kuali.rice.krad.service.ModuleService;
028import org.kuali.rice.krad.util.LegacyUtils;
029import org.springframework.transaction.annotation.Transactional;
030
031import java.util.Collection;
032import java.util.Collections;
033import java.util.HashMap;
034import java.util.Map;
035
036@Transactional
037public class LookupDaoProxy implements LookupDao {
038    
039        private LookupDao lookupDaoOjb;
040    private static KualiModuleService kualiModuleService;
041    private static Map<String, LookupDao> lookupDaoValues = Collections.synchronizedMap(new HashMap<String, LookupDao>());
042        
043        public void setLookupDaoOjb(LookupDao lookupDaoOjb) {
044                this.lookupDaoOjb = lookupDaoOjb;
045        }
046        
047    private LookupDao getDao(Class clazz) {
048        ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
049        if (moduleService != null) {
050            ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
051            String dataSourceName = "";
052            if (moduleConfig != null) {
053                dataSourceName = moduleConfig.getDataSourceName();
054            }
055
056            if (StringUtils.isNotEmpty(dataSourceName)) {
057                if (lookupDaoValues.get(dataSourceName) != null) {
058                    return lookupDaoValues.get(dataSourceName);
059                } else {
060                    LookupDaoOjb classSpecificLookupDaoOjb = new LookupDaoOjb();
061                    classSpecificLookupDaoOjb.setJcdAlias(dataSourceName);
062                    classSpecificLookupDaoOjb.setPersistenceStructureService(
063                            KNSServiceLocator.getPersistenceStructureService());
064                    classSpecificLookupDaoOjb.setDateTimeService(CoreApiServiceLocator.getDateTimeService());
065                    classSpecificLookupDaoOjb.setDataDictionaryService(
066                            KRADServiceLocatorWeb.getDataDictionaryService());
067                    lookupDaoValues.put(dataSourceName, classSpecificLookupDaoOjb);
068                    return classSpecificLookupDaoOjb;
069                }
070
071            }
072        }
073        return lookupDaoOjb;
074    }
075    
076        /**
077         * @see org.kuali.rice.krad.dao.LookupDao#createCriteria(java.lang.Object, java.lang.String, java.lang.String, java.lang.Object)
078         */
079        public boolean createCriteria(Object example, String searchValue, String propertyName, Object criteria) {
080                return getDao(example.getClass()).createCriteria(example, searchValue, propertyName, criteria);
081        }
082
083        /**
084         * @see org.kuali.rice.krad.dao.LookupDao#createCriteria(java.lang.Object, java.lang.String, java.lang.String, boolean, java.lang.Object)
085         */
086        public boolean createCriteria(Object example, String searchValue, String propertyName, boolean caseInsensitive, boolean treatWildcardsAndOperatorsAsLiteral, Object criteria) {
087                return getDao(example.getClass()).createCriteria(example, searchValue, propertyName, caseInsensitive,
088                treatWildcardsAndOperatorsAsLiteral, criteria);
089        }
090
091    /**
092     * Since 2.3
093     * This version of findCollectionBySearchHelper is needed for version compatibility.   It allows executeSearch
094     * to behave the same way as it did prior to 2.3. In the LookupDao, the value for searchResultsLimit will be
095     * retrieved from the KNS version of LookupUtils in the LookupDao.
096     *
097     * @see org.kuali.rice.krad.dao.LookupDao#findCollectionBySearchHelper(java.lang.Class, java.util.Map, boolean,
098     *      boolean)
099     */
100    public Collection findCollectionBySearchHelper(Class businessObjectClass, Map formProps, boolean unbounded,
101            boolean usePrimaryKeyValuesOnly) {
102        return getDao(businessObjectClass).findCollectionBySearchHelper(businessObjectClass, formProps, unbounded,
103                usePrimaryKeyValuesOnly);
104    }
105
106    /**
107     * @see org.kuali.rice.krad.dao.LookupDao#findCollectionBySearchHelper(java.lang.Class, java.util.Map, boolean,
108     *      boolean, Integer)
109     */
110    public Collection findCollectionBySearchHelper(Class businessObjectClass, Map formProps, boolean unbounded,
111            boolean usePrimaryKeyValuesOnly, Integer searchResultsLimit) {
112        return getDao(businessObjectClass).findCollectionBySearchHelper(businessObjectClass, formProps, unbounded,
113                usePrimaryKeyValuesOnly, searchResultsLimit);
114    }
115
116        /**
117         * @see org.kuali.rice.krad.dao.LookupDao#findCountByMap(java.lang.Object, java.util.Map)
118         */
119        public Long findCountByMap(Object example, Map formProps) {
120                return getDao(example.getClass()).findCountByMap(example, formProps);
121        }
122
123    @Override
124    public <T extends Object> T findObjectByMap(Class<T> type, Map<String, String> formProps) {
125        return getDao(type).findObjectByMap(type, formProps);
126    }
127
128        private static KualiModuleService getKualiModuleService() {
129        if (kualiModuleService == null) {
130            kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
131        }
132        return kualiModuleService;
133    }
134}