001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.orm.hibernate;
016    
017    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQueryFactory;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    import org.hibernate.criterion.DetachedCriteria;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class DynamicQueryFactoryImpl implements DynamicQueryFactory {
031    
032            public DynamicQuery forClass(Class<?> clazz) {
033                    clazz = getImplClass(clazz);
034    
035                    return new DynamicQueryImpl(DetachedCriteria.forClass(clazz));
036            }
037    
038            public DynamicQuery forClass(Class<?> clazz, ClassLoader classLoader) {
039                    clazz = getImplClass(clazz, classLoader);
040    
041                    return new DynamicQueryImpl(DetachedCriteria.forClass(clazz));
042            }
043    
044            public DynamicQuery forClass(Class<?> clazz, String alias) {
045                    clazz = getImplClass(clazz);
046    
047                    return new DynamicQueryImpl(DetachedCriteria.forClass(clazz, alias));
048            }
049    
050            public DynamicQuery forClass(
051                    Class<?> clazz, String alias, ClassLoader classLoader) {
052    
053                    clazz = getImplClass(clazz, classLoader);
054    
055                    return new DynamicQueryImpl(DetachedCriteria.forClass(clazz, alias));
056            }
057    
058            protected Class<?> getImplClass(Class<?> clazz) {
059                    return getImplClass(clazz, null);
060            }
061    
062            protected Class<?> getImplClass(Class<?> clazz, ClassLoader classLoader) {
063                    if (!clazz.getName().endsWith("Impl")) {
064                            String implClassName =
065                                    clazz.getPackage().getName() + ".impl." +
066                                            clazz.getSimpleName() + "Impl";
067    
068                            clazz = _classMap.get(implClassName);
069    
070                            if (clazz == null) {
071                                    try {
072                                            if (classLoader == null) {
073                                                    Thread currentThread = Thread.currentThread();
074    
075                                                    classLoader = currentThread.getContextClassLoader();
076                                            }
077    
078                                            clazz = classLoader.loadClass(implClassName);
079    
080                                            _classMap.put(implClassName, clazz);
081                                    }
082                                    catch (Exception e) {
083                                            _log.error("Unable find model " + implClassName, e);
084                                    }
085                            }
086                    }
087    
088                    return clazz;
089            }
090    
091            private static Log _log = LogFactoryUtil.getLog(
092                    DynamicQueryFactoryImpl.class);
093    
094            private Map<String, Class<?>> _classMap = new HashMap<String, Class<?>>();
095    
096    }