View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.dao.proxy;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreApiServiceLocator;
20  import org.kuali.rice.core.api.config.ConfigurationException;
21  import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
22  import org.kuali.rice.krad.bo.ModuleConfiguration;
23  import org.kuali.rice.krad.dao.LookupDao;
24  import org.kuali.rice.krad.dao.impl.LookupDaoJpa;
25  import org.kuali.rice.krad.dao.impl.LookupDaoOjb;
26  import org.kuali.rice.krad.service.KRADServiceLocator;
27  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
28  import org.kuali.rice.krad.service.KualiModuleService;
29  import org.kuali.rice.krad.service.ModuleService;
30  import org.springframework.transaction.annotation.Transactional;
31  
32  import javax.persistence.EntityManager;
33  import java.util.Collection;
34  import java.util.Collections;
35  import java.util.HashMap;
36  import java.util.Map;
37  
38  @Transactional
39  public class LookupDaoProxy implements LookupDao {
40      
41  	private LookupDao lookupDaoJpa;
42  	private LookupDao lookupDaoOjb;
43      private static KualiModuleService kualiModuleService;
44      private static Map<String, LookupDao> lookupDaoValues = Collections.synchronizedMap(new HashMap<String, LookupDao>());
45  	
46      public void setLookupDaoJpa(LookupDao lookupDaoJpa) {
47  		this.lookupDaoJpa = lookupDaoJpa;
48  	}
49  	
50  	public void setLookupDaoOjb(LookupDao lookupDaoOjb) {
51  		this.lookupDaoOjb = lookupDaoOjb;
52  	}
53  	
54      private LookupDao getDao(Class clazz) {
55          ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
56          if (moduleService != null) {
57              ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
58              String dataSourceName = "";
59              EntityManager entityManager = null;
60              if (moduleConfig != null) {
61                  dataSourceName = moduleConfig.getDataSourceName();
62                  entityManager = moduleConfig.getEntityManager();
63              }
64  
65              if (StringUtils.isNotEmpty(dataSourceName)) {
66                  if (lookupDaoValues.get(dataSourceName) != null) {
67                      return lookupDaoValues.get(dataSourceName);
68                  } else {         
69                      if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
70                          //using JPA       	
71                  	    LookupDaoJpa classSpecificLookupDaoJpa = new LookupDaoJpa();
72                  		if (entityManager != null) {
73                  			classSpecificLookupDaoJpa.setEntityManager(entityManager);
74                  			classSpecificLookupDaoJpa.setPersistenceStructureService(
75                                      KRADServiceLocator.getPersistenceStructureService());
76                          	classSpecificLookupDaoJpa.setDateTimeService(CoreApiServiceLocator.getDateTimeService());
77                  			lookupDaoValues.put(dataSourceName, classSpecificLookupDaoJpa);
78                  			return classSpecificLookupDaoJpa;
79                  		} else {
80                  			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).");
81                  		}
82  					} else {
83  						LookupDaoOjb classSpecificLookupDaoOjb = new LookupDaoOjb();
84                          classSpecificLookupDaoOjb.setJcdAlias(dataSourceName);
85                          classSpecificLookupDaoOjb.setPersistenceStructureService(
86                                  KRADServiceLocator.getPersistenceStructureService());
87                          classSpecificLookupDaoOjb.setDateTimeService(CoreApiServiceLocator.getDateTimeService());
88                          classSpecificLookupDaoOjb.setDataDictionaryService(
89                                  KRADServiceLocatorWeb.getDataDictionaryService());
90                          lookupDaoValues.put(dataSourceName, classSpecificLookupDaoOjb);
91                          return classSpecificLookupDaoOjb;
92                      }
93                  }
94  
95              }
96          }
97          //return lookupDaoJpa;
98          return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? lookupDaoJpa : lookupDaoOjb;
99      }
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      * Since 2.3
118      * This version of findCollectionBySearchHelper is needed for version compatibility.   It allows executeSearch
119      * to behave the same way as it did prior to 2.3. In the LookupDao, the value for searchResultsLimit will be
120      * retrieved from the KNS version of LookupUtils in the LookupDao.
121      *
122      * @see org.kuali.rice.krad.dao.LookupDao#findCollectionBySearchHelper(java.lang.Class, java.util.Map, boolean,
123      *      boolean)
124      */
125     public Collection findCollectionBySearchHelper(Class businessObjectClass, Map formProps, boolean unbounded,
126             boolean usePrimaryKeyValuesOnly) {
127         return getDao(businessObjectClass).findCollectionBySearchHelper(businessObjectClass, formProps, unbounded,
128                 usePrimaryKeyValuesOnly);
129     }
130 
131     /**
132      * @see org.kuali.rice.krad.dao.LookupDao#findCollectionBySearchHelper(java.lang.Class, java.util.Map, boolean,
133      *      boolean, Integer)
134      */
135     public Collection findCollectionBySearchHelper(Class businessObjectClass, Map formProps, boolean unbounded,
136             boolean usePrimaryKeyValuesOnly, Integer searchResultsLimit) {
137         return getDao(businessObjectClass).findCollectionBySearchHelper(businessObjectClass, formProps, unbounded,
138                 usePrimaryKeyValuesOnly, searchResultsLimit);
139     }
140 
141 	/**
142 	 * @see org.kuali.rice.krad.dao.LookupDao#findCountByMap(java.lang.Object, java.util.Map)
143 	 */
144 	public Long findCountByMap(Object example, Map formProps) {
145 		return getDao(example.getClass()).findCountByMap(example, formProps);
146 	}
147 
148 	/**
149 	 * @see org.kuali.rice.krad.dao.LookupDao#findObjectByMap(java.lang.Object, java.util.Map)
150 	 */
151 	public Object findObjectByMap(Object example, Map formProps) {
152 		return getDao(example.getClass()).findObjectByMap(example, formProps);
153 	}
154 
155 	private static KualiModuleService getKualiModuleService() {
156         if (kualiModuleService == null) {
157             kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
158         }
159         return kualiModuleService;
160     }
161 }