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.criteria;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.criteria.GenericQueryResults;
20  import org.kuali.rice.core.api.criteria.LookupCustomizer;
21  import org.kuali.rice.core.api.criteria.QueryByCriteria;
22  import org.kuali.rice.krad.bo.ModuleConfiguration;
23  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
24  import org.kuali.rice.krad.service.KualiModuleService;
25  import org.kuali.rice.krad.service.ModuleService;
26  import org.kuali.rice.krad.util.LegacyUtils;
27  
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  public class CriteriaLookupDaoProxy implements CriteriaLookupDao {
33      CriteriaLookupDao criteriaLookupDaoOjb;
34  
35      private static KualiModuleService kualiModuleService;
36      private static Map<String, CriteriaLookupDao> lookupDaoValues = Collections.synchronizedMap(new HashMap<String, CriteriaLookupDao>());
37  
38  	public void setCriteriaLookupDaoOjb(CriteriaLookupDao lookupDaoOjb) {
39  		this.criteriaLookupDaoOjb = lookupDaoOjb;
40  	}
41  
42      private CriteriaLookupDao getDao(Class clazz) {
43          ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
44          if (moduleService != null) {
45              ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
46              String dataSourceName = "";
47              if (moduleConfig != null) {
48                  dataSourceName = moduleConfig.getDataSourceName();
49              }
50  
51              if (StringUtils.isNotEmpty(dataSourceName)) {
52                  if (lookupDaoValues.get(dataSourceName) != null) {
53                      return lookupDaoValues.get(dataSourceName);
54                  } else {
55          			CriteriaLookupDaoOjb classSpecificLookupDaoOjb = new CriteriaLookupDaoOjb();
56                      classSpecificLookupDaoOjb.setJcdAlias(dataSourceName);
57                      lookupDaoValues.put(dataSourceName, classSpecificLookupDaoOjb);
58                      return classSpecificLookupDaoOjb;
59                  }
60  
61              }
62          }
63          return criteriaLookupDaoOjb;
64      }
65  
66      @Override
67      public <T> GenericQueryResults<T> lookup(Class<T> queryClass, QueryByCriteria criteria) {
68          return getDao(queryClass).lookup(queryClass, criteria);
69      }
70  
71      @Override
72      public <T> GenericQueryResults<T> lookup(Class<T> queryClass, QueryByCriteria criteria,
73              LookupCustomizer<T> customizer) {
74          return getDao(queryClass).lookup(queryClass, criteria, customizer);
75      }
76  
77      private static KualiModuleService getKualiModuleService() {
78          if (kualiModuleService == null) {
79              kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
80          }
81          return kualiModuleService;
82      }
83  }