View Javadoc

1   /*
2    * Copyright 2005-2008 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.kns.dao.proxy;
17  
18  import java.util.Collection;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.persistence.EntityManager;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.rice.core.config.ConfigurationException;
27  import org.kuali.rice.core.util.OrmUtils;
28  import org.kuali.rice.kns.bo.ModuleConfiguration;
29  import org.kuali.rice.kns.dao.LookupDao;
30  import org.kuali.rice.kns.dao.impl.LookupDaoJpa;
31  import org.kuali.rice.kns.dao.impl.LookupDaoOjb;
32  import org.kuali.rice.kns.service.KNSServiceLocator;
33  import org.kuali.rice.kns.service.KualiModuleService;
34  import org.kuali.rice.kns.service.ModuleService;
35  import org.springframework.transaction.annotation.Transactional;
36  
37  @Transactional
38  public class LookupDaoProxy implements LookupDao {
39      
40  	private LookupDao lookupDaoJpa;
41  	private LookupDao lookupDaoOjb;
42      private static KualiModuleService kualiModuleService;
43      private static Map<String, LookupDao> lookupDaoValues = Collections.synchronizedMap(new HashMap<String, LookupDao>());
44  	
45      public void setLookupDaoJpa(LookupDao lookupDaoJpa) {
46  		this.lookupDaoJpa = lookupDaoJpa;
47  	}
48  
49  	public void setLookupDaoOjb(LookupDao lookupDaoOjb) {
50  		this.lookupDaoOjb = lookupDaoOjb;
51  	}
52  	
53      private LookupDao getDao(Class clazz) {
54          ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
55          if (moduleService != null) {
56              ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
57              String dataSourceName = "";
58              EntityManager entityManager = null;
59              if (moduleConfig != null) {
60                  dataSourceName = moduleConfig.getDataSourceName();
61                  entityManager = moduleConfig.getEntityManager();
62              }
63  
64              if (StringUtils.isNotEmpty(dataSourceName)) {
65                  if (lookupDaoValues.get(dataSourceName) != null) {
66                      return lookupDaoValues.get(dataSourceName);
67                  } else {
68                      if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
69                          //using JPA
70                          LookupDaoJpa classSpecificLookupDaoJpa = new LookupDaoJpa();
71                          if (entityManager != null) {
72                          	classSpecificLookupDaoJpa.setEntityManager(entityManager);
73                          	classSpecificLookupDaoJpa.setPersistenceStructureService(KNSServiceLocator.getPersistenceStructureService());
74                          	classSpecificLookupDaoJpa.setDateTimeService(KNSServiceLocator.getDateTimeService());
75                              lookupDaoValues.put(dataSourceName, classSpecificLookupDaoJpa);
76                              return classSpecificLookupDaoJpa;
77                          } else {
78                              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).");
79                          }
80  
81                      } else {
82                          //using OJB
83                          LookupDaoOjb classSpecificLookupDaoOjb = new LookupDaoOjb();
84                          classSpecificLookupDaoOjb.setJcdAlias(dataSourceName);
85                          classSpecificLookupDaoOjb.setPersistenceStructureService(KNSServiceLocator.getPersistenceStructureService());
86                          classSpecificLookupDaoOjb.setDateTimeService(KNSServiceLocator.getDateTimeService());
87                          classSpecificLookupDaoOjb.setBusinessObjectDictionaryService(KNSServiceLocator.getBusinessObjectDictionaryService());
88                          lookupDaoValues.put(dataSourceName, classSpecificLookupDaoOjb);
89                          return classSpecificLookupDaoOjb;
90                      }
91  
92                  }
93  
94              }
95          }
96          return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? lookupDaoJpa : lookupDaoOjb;
97      }
98      
99  	/**
100 	 * @see org.kuali.rice.kns.dao.LookupDao#createCriteria(java.lang.Object, java.lang.String, java.lang.String, java.lang.Object)
101 	 */
102 	public boolean createCriteria(Object example, String searchValue, String propertyName, Object criteria) {
103 		return getDao(example.getClass()).createCriteria(example, searchValue, propertyName, criteria);
104 	}
105 
106 	/**
107 	 * @see org.kuali.rice.kns.dao.LookupDao#createCriteria(java.lang.Object, java.lang.String, java.lang.String, boolean, java.lang.Object)
108 	 */
109 	public boolean createCriteria(Object example, String searchValue, String propertyName, boolean caseInsensitive, boolean treatWildcardsAndOperatorsAsLiteral, Object criteria) {
110 		return getDao(example.getClass()).createCriteria(example, searchValue, propertyName, caseInsensitive, treatWildcardsAndOperatorsAsLiteral, criteria);
111 	}
112 
113 	/**
114 	 * @see org.kuali.rice.kns.dao.LookupDao#findCollectionBySearchHelper(java.lang.Class, java.util.Map, boolean, boolean)
115 	 */
116 	public Collection findCollectionBySearchHelper(Class example, Map formProps, boolean unbounded, boolean usePrimaryKeyValuesOnly) {
117 		return getDao(example).findCollectionBySearchHelper(example, formProps, unbounded, usePrimaryKeyValuesOnly);
118 	}
119 
120 	/**
121 	 * @see org.kuali.rice.kns.dao.LookupDao#findCollectionBySearchHelper(java.lang.Class, java.util.Map, boolean, boolean, java.lang.Object)
122 	 */
123 	public Collection findCollectionBySearchHelper(Class example, Map formProps, boolean unbounded, boolean usePrimaryKeyValuesOnly, Object additionalCriteria) {
124 		return getDao(example).findCollectionBySearchHelper(example, formProps, unbounded, usePrimaryKeyValuesOnly, additionalCriteria);
125 	}
126 
127 	/**
128 	 * @see org.kuali.rice.kns.dao.LookupDao#findCountByMap(java.lang.Object, java.util.Map)
129 	 */
130 	public Long findCountByMap(Object example, Map formProps) {
131 		return getDao(example.getClass()).findCountByMap(example, formProps);
132 	}
133 
134 	/**
135 	 * @see org.kuali.rice.kns.dao.LookupDao#findObjectByMap(java.lang.Object, java.util.Map)
136 	 */
137 	public Object findObjectByMap(Object example, Map formProps) {
138 		return getDao(example.getClass()).findObjectByMap(example, formProps);
139 	}
140 
141 	private static KualiModuleService getKualiModuleService() {
142         if (kualiModuleService == null) {
143             kualiModuleService = KNSServiceLocator.getKualiModuleService();
144         }
145         return kualiModuleService;
146     }
147 }