001    /**
002     * Copyright 2005-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.rice.krad.dao.proxy;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.core.api.CoreApiServiceLocator;
020    import org.kuali.rice.core.api.config.ConfigurationException;
021    import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
022    import org.kuali.rice.krad.bo.ModuleConfiguration;
023    import org.kuali.rice.krad.dao.LookupDao;
024    import org.kuali.rice.krad.dao.impl.LookupDaoJpa;
025    import org.kuali.rice.krad.dao.impl.LookupDaoOjb;
026    import org.kuali.rice.krad.service.KRADServiceLocator;
027    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
028    import org.kuali.rice.krad.service.KualiModuleService;
029    import org.kuali.rice.krad.service.ModuleService;
030    import org.springframework.transaction.annotation.Transactional;
031    
032    import javax.persistence.EntityManager;
033    import java.util.Collection;
034    import java.util.Collections;
035    import java.util.HashMap;
036    import java.util.Map;
037    
038    @Transactional
039    public class LookupDaoProxy implements LookupDao {
040        
041            private LookupDao lookupDaoJpa;
042            private LookupDao lookupDaoOjb;
043        private static KualiModuleService kualiModuleService;
044        private static Map<String, LookupDao> lookupDaoValues = Collections.synchronizedMap(new HashMap<String, LookupDao>());
045            
046        public void setLookupDaoJpa(LookupDao lookupDaoJpa) {
047                    this.lookupDaoJpa = lookupDaoJpa;
048            }
049            
050            public void setLookupDaoOjb(LookupDao lookupDaoOjb) {
051                    this.lookupDaoOjb = lookupDaoOjb;
052            }
053            
054        private LookupDao getDao(Class clazz) {
055            ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
056            if (moduleService != null) {
057                ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
058                String dataSourceName = "";
059                EntityManager entityManager = null;
060                if (moduleConfig != null) {
061                    dataSourceName = moduleConfig.getDataSourceName();
062                    entityManager = moduleConfig.getEntityManager();
063                }
064    
065                if (StringUtils.isNotEmpty(dataSourceName)) {
066                    if (lookupDaoValues.get(dataSourceName) != null) {
067                        return lookupDaoValues.get(dataSourceName);
068                    } else {         
069                        if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
070                            //using JPA             
071                                LookupDaoJpa classSpecificLookupDaoJpa = new LookupDaoJpa();
072                                    if (entityManager != null) {
073                                            classSpecificLookupDaoJpa.setEntityManager(entityManager);
074                                            classSpecificLookupDaoJpa.setPersistenceStructureService(
075                                        KRADServiceLocator.getPersistenceStructureService());
076                                    classSpecificLookupDaoJpa.setDateTimeService(CoreApiServiceLocator.getDateTimeService());
077                                            lookupDaoValues.put(dataSourceName, classSpecificLookupDaoJpa);
078                                            return classSpecificLookupDaoJpa;
079                                    } else {
080                                            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).");
081                                    }
082                                            } else {
083                                                    LookupDaoOjb classSpecificLookupDaoOjb = new LookupDaoOjb();
084                            classSpecificLookupDaoOjb.setJcdAlias(dataSourceName);
085                            classSpecificLookupDaoOjb.setPersistenceStructureService(
086                                    KRADServiceLocator.getPersistenceStructureService());
087                            classSpecificLookupDaoOjb.setDateTimeService(CoreApiServiceLocator.getDateTimeService());
088                            classSpecificLookupDaoOjb.setDataDictionaryService(
089                                    KRADServiceLocatorWeb.getDataDictionaryService());
090                            lookupDaoValues.put(dataSourceName, classSpecificLookupDaoOjb);
091                            return classSpecificLookupDaoOjb;
092                        }
093                    }
094    
095                }
096            }
097            //return lookupDaoJpa;
098            return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? lookupDaoJpa : lookupDaoOjb;
099        }
100        
101            /**
102             * @see org.kuali.rice.krad.dao.LookupDao#createCriteria(java.lang.Object, java.lang.String, java.lang.String, java.lang.Object)
103             */
104            public boolean createCriteria(Object example, String searchValue, String propertyName, Object criteria) {
105                    return getDao(example.getClass()).createCriteria(example, searchValue, propertyName, criteria);
106            }
107    
108            /**
109             * @see org.kuali.rice.krad.dao.LookupDao#createCriteria(java.lang.Object, java.lang.String, java.lang.String, boolean, java.lang.Object)
110             */
111            public boolean createCriteria(Object example, String searchValue, String propertyName, boolean caseInsensitive, boolean treatWildcardsAndOperatorsAsLiteral, Object criteria) {
112                    return getDao(example.getClass()).createCriteria(example, searchValue, propertyName, caseInsensitive,
113                    treatWildcardsAndOperatorsAsLiteral, criteria);
114            }
115    
116            /**
117             * @see org.kuali.rice.krad.dao.LookupDao#findCollectionBySearchHelper(java.lang.Class, java.util.Map, boolean, boolean)
118             */
119            public Collection findCollectionBySearchHelper(Class example, Map formProps, boolean unbounded, boolean usePrimaryKeyValuesOnly) {
120                    return getDao(example).findCollectionBySearchHelper(example, formProps, unbounded, usePrimaryKeyValuesOnly);
121            }
122    
123            /**
124             * @see org.kuali.rice.krad.dao.LookupDao#findCountByMap(java.lang.Object, java.util.Map)
125             */
126            public Long findCountByMap(Object example, Map formProps) {
127                    return getDao(example.getClass()).findCountByMap(example, formProps);
128            }
129    
130            /**
131             * @see org.kuali.rice.krad.dao.LookupDao#findObjectByMap(java.lang.Object, java.util.Map)
132             */
133            public Object findObjectByMap(Object example, Map formProps) {
134                    return getDao(example.getClass()).findObjectByMap(example, formProps);
135            }
136    
137            private static KualiModuleService getKualiModuleService() {
138            if (kualiModuleService == null) {
139                kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
140            }
141            return kualiModuleService;
142        }
143    }