1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.dao.orm.hibernate;
16  
17  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
18  import com.liferay.portal.kernel.dao.orm.DynamicQueryFactory;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.hibernate.criterion.DetachedCriteria;
26  
27  /**
28   * <a href="DynamicQueryFactoryImpl.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class DynamicQueryFactoryImpl implements DynamicQueryFactory {
33  
34      public DynamicQuery forClass(Class<?> clazz) {
35          clazz = getImplClass(clazz);
36  
37          return new DynamicQueryImpl(DetachedCriteria.forClass(clazz));
38      }
39  
40      public DynamicQuery forClass(Class<?> clazz, ClassLoader classLoader) {
41          clazz = getImplClass(clazz, classLoader);
42  
43          return new DynamicQueryImpl(DetachedCriteria.forClass(clazz));
44      }
45  
46      public DynamicQuery forClass(Class<?> clazz, String alias) {
47          clazz = getImplClass(clazz);
48  
49          return new DynamicQueryImpl(DetachedCriteria.forClass(clazz, alias));
50      }
51  
52      public DynamicQuery forClass(
53          Class<?> clazz, String alias, ClassLoader classLoader) {
54  
55          clazz = getImplClass(clazz, classLoader);
56  
57          return new DynamicQueryImpl(DetachedCriteria.forClass(clazz, alias));
58      }
59  
60      protected Class<?> getImplClass(Class<?> clazz) {
61          return getImplClass(clazz, null);
62      }
63  
64      protected Class<?> getImplClass(Class<?> clazz, ClassLoader classLoader) {
65          if (!clazz.getName().endsWith("Impl")) {
66              String implClassName =
67                  clazz.getPackage().getName() + ".impl." +
68                      clazz.getSimpleName() + "Impl";
69  
70              clazz = _classMap.get(implClassName);
71  
72              if (clazz == null) {
73                  try {
74                      if (classLoader == null) {
75                          Thread currentThread = Thread.currentThread();
76  
77                          classLoader = currentThread.getContextClassLoader();
78                      }
79  
80                      clazz = classLoader.loadClass(implClassName);
81  
82                      _classMap.put(implClassName, clazz);
83                  }
84                  catch (Exception e) {
85                      _log.error("Unable find model " + implClassName, e);
86                  }
87              }
88          }
89  
90          return clazz;
91      }
92  
93      private static Log _log = LogFactoryUtil.getLog(
94          DynamicQueryFactoryImpl.class);
95  
96      private Map<String, Class<?>> _classMap = new HashMap<String, Class<?>>();
97  
98  }