001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.ratings.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
018    import com.liferay.portal.kernel.dao.orm.FinderPath;
019    import com.liferay.portal.kernel.dao.orm.QueryPos;
020    import com.liferay.portal.kernel.dao.orm.SQLQuery;
021    import com.liferay.portal.kernel.dao.orm.Session;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
025    import com.liferay.portlet.ratings.model.RatingsEntry;
026    import com.liferay.portlet.ratings.model.impl.RatingsEntryImpl;
027    import com.liferay.portlet.ratings.model.impl.RatingsEntryModelImpl;
028    import com.liferay.util.dao.orm.CustomSQLUtil;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    
033    /**
034     * @author Shuyang Zhou
035     * @author Brian Wing Shun Chan
036     */
037    public class RatingsEntryFinderImpl
038            extends BasePersistenceImpl<RatingsEntry> implements RatingsEntryFinder {
039    
040            public static String FIND_BY_U_C_C =
041                    RatingsEntryFinder.class.getName() + ".findByU_C_C";
042    
043            public static final FinderPath FINDER_PATH_FIND_BY_U_C_C = new FinderPath(
044                    RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
045                    RatingsEntryModelImpl.FINDER_CACHE_ENABLED, RatingsEntryImpl.class,
046                    RatingsEntryPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
047                    "findByU_C_C",
048                    new String[] {
049                            Long.class.getName(), Long.class.getName(), List.class.getName()
050                    });
051    
052            public List<RatingsEntry> findByU_C_C(
053                            long userId, long classNameId, List<Long> classPKs)
054                    throws SystemException {
055    
056                    Object[] finderArgs = new Object[] {
057                            userId, classNameId,
058                            StringUtil.merge(classPKs.toArray(new Long[classPKs.size()]))
059                    };
060    
061                    List<RatingsEntry> list = (List<RatingsEntry>)FinderCacheUtil.getResult(
062                            FINDER_PATH_FIND_BY_U_C_C, finderArgs, this);
063    
064                    if (list != null) {
065                            return list;
066                    }
067    
068                    Session session = null;
069    
070                    try {
071                            session = openSession();
072    
073                            String sql = CustomSQLUtil.get(FIND_BY_U_C_C);
074    
075                            sql = StringUtil.replace(
076                                    sql, "[$CLASS_PKS$]", StringUtil.merge(classPKs));
077    
078                            SQLQuery q = session.createSQLQuery(sql);
079    
080                            q.addEntity("RatingsEntry", RatingsEntryImpl.class);
081    
082                            QueryPos qPos = QueryPos.getInstance(q);
083    
084                            qPos.add(userId);
085                            qPos.add(classNameId);
086    
087                            list = q.list(true);
088                    }
089                    catch (Exception e) {
090                            throw new SystemException(e);
091                    }
092                    finally {
093                            if (list == null) {
094                                    list = new ArrayList<RatingsEntry>();
095                            }
096    
097                            FinderCacheUtil.putResult(
098                                    FINDER_PATH_FIND_BY_U_C_C, finderArgs, list);
099    
100                            closeSession(session);
101                    }
102    
103                    return list;
104            }
105    
106    }