001/**
002 * Copyright 2005-2016 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.criteria;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.config.ConfigurationException;
020import org.kuali.rice.core.api.criteria.GenericQueryResults;
021import org.kuali.rice.core.api.criteria.LookupCustomizer;
022import org.kuali.rice.core.api.criteria.QueryByCriteria;
023import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
024import org.kuali.rice.krad.bo.ModuleConfiguration;
025import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
026import org.kuali.rice.krad.service.KualiModuleService;
027import org.kuali.rice.krad.service.ModuleService;
028
029import javax.persistence.EntityManager;
030import java.util.Collections;
031import java.util.HashMap;
032import java.util.Map;
033
034public class CriteriaLookupDaoProxy implements CriteriaLookupDao {
035    CriteriaLookupDao criteriaLookupDaoOjb;
036    CriteriaLookupDao criteriaLookupDaoJpa;
037    private static KualiModuleService kualiModuleService;
038    private static Map<String, CriteriaLookupDao> lookupDaoValues = Collections.synchronizedMap(new HashMap<String, CriteriaLookupDao>());
039
040    public void setCriteriaLookupDaoJpa(CriteriaLookupDao lookupDaoJpa) {
041                this.criteriaLookupDaoJpa = lookupDaoJpa;
042        }
043
044        public void setCriteriaLookupDaoOjb(CriteriaLookupDao lookupDaoOjb) {
045                this.criteriaLookupDaoOjb = lookupDaoOjb;
046        }
047
048    private CriteriaLookupDao getDao(Class clazz) {
049        ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
050        if (moduleService != null) {
051            ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
052            String dataSourceName = "";
053            EntityManager entityManager = null;
054            if (moduleConfig != null) {
055                dataSourceName = moduleConfig.getDataSourceName();
056                entityManager = moduleConfig.getEntityManager();
057            }
058
059            if (StringUtils.isNotEmpty(dataSourceName)) {
060                if (lookupDaoValues.get(dataSourceName) != null) {
061                    return lookupDaoValues.get(dataSourceName);
062                } else {
063                    if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
064                        //using JPA
065                            CriteriaLookupDaoJpa classSpecificLookupDaoJpa = new CriteriaLookupDaoJpa();
066                                if (entityManager != null) {
067                                        classSpecificLookupDaoJpa.setEntityManager(entityManager);
068                                        lookupDaoValues.put(dataSourceName, classSpecificLookupDaoJpa);
069                                        return classSpecificLookupDaoJpa;
070                                } else {
071                                        throw new ConfigurationException("EntityManager is null. EntityManager must be set in the Module Configuration bean in the appropriate spring beans xml. (see nested exception for details).");
072                                }
073                                        } else {
074                                                CriteriaLookupDaoOjb classSpecificLookupDaoOjb = new CriteriaLookupDaoOjb();
075                        classSpecificLookupDaoOjb.setJcdAlias(dataSourceName);
076                        lookupDaoValues.put(dataSourceName, classSpecificLookupDaoOjb);
077                        return classSpecificLookupDaoOjb;
078                    }
079                }
080
081            }
082        }
083        //return lookupDaoJpa;
084        return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? criteriaLookupDaoJpa : criteriaLookupDaoOjb;
085    }
086
087    @Override
088    public <T> GenericQueryResults<T> lookup(Class<T> queryClass, QueryByCriteria criteria) {
089        return getDao(queryClass).lookup(queryClass, criteria);
090    }
091
092    @Override
093    public <T> GenericQueryResults<T> lookup(Class<T> queryClass, QueryByCriteria criteria,
094            LookupCustomizer<T> customizer) {
095        return getDao(queryClass).lookup(queryClass, criteria, customizer);
096    }
097
098    private static KualiModuleService getKualiModuleService() {
099        if (kualiModuleService == null) {
100            kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
101        }
102        return kualiModuleService;
103    }
104}