001    /**
002     * Copyright (c) 2000-2013 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.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.RatingsStats;
026    import com.liferay.portlet.ratings.model.impl.RatingsStatsImpl;
027    import com.liferay.portlet.ratings.model.impl.RatingsStatsModelImpl;
028    import com.liferay.util.dao.orm.CustomSQLUtil;
029    
030    import java.util.List;
031    
032    /**
033     * @author Shuyang Zhou
034     * @author Brian Wing Shun Chan
035     */
036    public class RatingsStatsFinderImpl
037            extends BasePersistenceImpl<RatingsStats> implements RatingsStatsFinder {
038    
039            public static final String FIND_BY_C_C =
040                    RatingsStatsFinder.class.getName() + ".findByC_C";
041    
042            public static final FinderPath FINDER_PATH_FIND_BY_C_C = new FinderPath(
043                    RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
044                    RatingsStatsModelImpl.FINDER_CACHE_ENABLED, RatingsStatsImpl.class,
045                    RatingsStatsPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
046                    "findByC_C",
047                    new String[] {Long.class.getName(), List.class.getName()});
048    
049            public List<RatingsStats> findByC_C(long classNameId, List<Long> classPKs)
050                    throws SystemException {
051    
052                    Object[] finderArgs = new Object[] {
053                            classNameId,
054                            StringUtil.merge(classPKs.toArray(new Long[classPKs.size()]))
055                    };
056    
057                    List<RatingsStats> list = (List<RatingsStats>)FinderCacheUtil.getResult(
058                            FINDER_PATH_FIND_BY_C_C, finderArgs, this);
059    
060                    if ((list != null) && !list.isEmpty()) {
061                            for (RatingsStats ratingsStats : list) {
062                                    if ((classNameId != ratingsStats.getClassNameId()) ||
063                                            !classPKs.contains(ratingsStats.getClassPK())) {
064    
065                                            list = null;
066    
067                                            break;
068                                    }
069                            }
070                    }
071    
072                    if (list != null) {
073                            return list;
074                    }
075    
076                    Session session = null;
077    
078                    try {
079                            session = openSession();
080    
081                            String sql = CustomSQLUtil.get(FIND_BY_C_C);
082    
083                            sql = StringUtil.replace(
084                                    sql, "[$CLASS_PKS$]", StringUtil.merge(classPKs));
085    
086                            SQLQuery q = session.createSQLQuery(sql);
087    
088                            q.addEntity("RatingsStats", RatingsStatsImpl.class);
089    
090                            QueryPos qPos = QueryPos.getInstance(q);
091    
092                            qPos.add(classNameId);
093    
094                            list = q.list(true);
095                    }
096                    catch (Exception e) {
097                            throw new SystemException(e);
098                    }
099                    finally {
100                            if (list == null) {
101                                    FinderCacheUtil.removeResult(
102                                            FINDER_PATH_FIND_BY_C_C, finderArgs);
103                            }
104                            else {
105                                    FinderCacheUtil.putResult(
106                                            FINDER_PATH_FIND_BY_C_C, finderArgs, list);
107                            }
108    
109                            closeSession(session);
110                    }
111    
112                    return list;
113            }
114    
115    }