View Javadoc
1   /**
2    * Copyright 2005-2014 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.kns.service.KNSServiceLocator;
21  import org.kuali.rice.krad.bo.ModuleConfiguration;
22  import org.kuali.rice.krad.dao.LookupDao;
23  import org.kuali.rice.krad.dao.impl.LookupDaoOjb;
24  import org.kuali.rice.krad.service.KRADServiceLocator;
25  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
26  import org.kuali.rice.krad.service.KualiModuleService;
27  import org.kuali.rice.krad.service.ModuleService;
28  import org.kuali.rice.krad.util.LegacyUtils;
29  import org.springframework.transaction.annotation.Transactional;
30  
31  import java.util.Collection;
32  import java.util.Collections;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  @Transactional
37  public class LookupDaoProxy implements LookupDao {
38      
39  	private LookupDao lookupDaoOjb;
40      private static KualiModuleService kualiModuleService;
41      private static Map<String, LookupDao> lookupDaoValues = Collections.synchronizedMap(new HashMap<String, LookupDao>());
42  	
43  	public void setLookupDaoOjb(LookupDao lookupDaoOjb) {
44  		this.lookupDaoOjb = lookupDaoOjb;
45  	}
46  	
47      private LookupDao getDao(Class clazz) {
48          ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
49          if (moduleService != null) {
50              ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
51              String dataSourceName = "";
52              if (moduleConfig != null) {
53                  dataSourceName = moduleConfig.getDataSourceName();
54              }
55  
56              if (StringUtils.isNotEmpty(dataSourceName)) {
57                  if (lookupDaoValues.get(dataSourceName) != null) {
58                      return lookupDaoValues.get(dataSourceName);
59                  } else {
60                      LookupDaoOjb classSpecificLookupDaoOjb = new LookupDaoOjb();
61                      classSpecificLookupDaoOjb.setJcdAlias(dataSourceName);
62                      classSpecificLookupDaoOjb.setPersistenceStructureService(
63                              KNSServiceLocator.getPersistenceStructureService());
64                      classSpecificLookupDaoOjb.setDateTimeService(CoreApiServiceLocator.getDateTimeService());
65                      classSpecificLookupDaoOjb.setDataDictionaryService(
66                              KRADServiceLocatorWeb.getDataDictionaryService());
67                      lookupDaoValues.put(dataSourceName, classSpecificLookupDaoOjb);
68                      return classSpecificLookupDaoOjb;
69                  }
70  
71              }
72          }
73          return lookupDaoOjb;
74      }
75      
76  	/**
77  	 * @see org.kuali.rice.krad.dao.LookupDao#createCriteria(java.lang.Object, java.lang.String, java.lang.String, java.lang.Object)
78  	 */
79  	public boolean createCriteria(Object example, String searchValue, String propertyName, Object criteria) {
80  		return getDao(example.getClass()).createCriteria(example, searchValue, propertyName, criteria);
81  	}
82  
83  	/**
84  	 * @see org.kuali.rice.krad.dao.LookupDao#createCriteria(java.lang.Object, java.lang.String, java.lang.String, boolean, java.lang.Object)
85  	 */
86  	public boolean createCriteria(Object example, String searchValue, String propertyName, boolean caseInsensitive, boolean treatWildcardsAndOperatorsAsLiteral, Object criteria) {
87  		return getDao(example.getClass()).createCriteria(example, searchValue, propertyName, caseInsensitive,
88                  treatWildcardsAndOperatorsAsLiteral, criteria);
89  	}
90  
91      /**
92       * Since 2.3
93       * This version of findCollectionBySearchHelper is needed for version compatibility.   It allows executeSearch
94       * to behave the same way as it did prior to 2.3. In the LookupDao, the value for searchResultsLimit will be
95       * retrieved from the KNS version of LookupUtils in the LookupDao.
96       *
97       * @see org.kuali.rice.krad.dao.LookupDao#findCollectionBySearchHelper(java.lang.Class, java.util.Map, boolean,
98       *      boolean)
99       */
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 }