View Javadoc

1   /**
2    * Copyright 2005-2013 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                      if (!LegacyUtils.useLegacy(clazz)) {
56                          throw new IllegalStateException(this.getClass() + " called with non-legacy class: " + clazz);
57  					} else {
58  						CriteriaLookupDaoOjb classSpecificLookupDaoOjb = new CriteriaLookupDaoOjb();
59                          classSpecificLookupDaoOjb.setJcdAlias(dataSourceName);
60                          lookupDaoValues.put(dataSourceName, classSpecificLookupDaoOjb);
61                          return classSpecificLookupDaoOjb;
62                      }
63                  }
64  
65              }
66          }
67          //return lookupDaoJpa;
68  
69          if (!LegacyUtils.useLegacy(clazz)) {
70              throw new IllegalStateException(this.getClass() + " called with non-legacy class: " + clazz);
71          }
72          return criteriaLookupDaoOjb;
73      }
74  
75      @Override
76      public <T> GenericQueryResults<T> lookup(Class<T> queryClass, QueryByCriteria criteria) {
77          return getDao(queryClass).lookup(queryClass, criteria);
78      }
79  
80      @Override
81      public <T> GenericQueryResults<T> lookup(Class<T> queryClass, QueryByCriteria criteria,
82              LookupCustomizer<T> customizer) {
83          return getDao(queryClass).lookup(queryClass, criteria, customizer);
84      }
85  
86      private static KualiModuleService getKualiModuleService() {
87          if (kualiModuleService == null) {
88              kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
89          }
90          return kualiModuleService;
91      }
92  }