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            @Override
050            public List<RatingsStats> findByC_C(long classNameId, List<Long> classPKs)
051                    throws SystemException {
052    
053                    Object[] finderArgs = new Object[] {
054                            classNameId,
055                            StringUtil.merge(classPKs.toArray(new Long[classPKs.size()]))
056                    };
057    
058                    List<RatingsStats> list = (List<RatingsStats>)FinderCacheUtil.getResult(
059                            FINDER_PATH_FIND_BY_C_C, finderArgs, this);
060    
061                    if ((list != null) && !list.isEmpty()) {
062                            for (RatingsStats ratingsStats : list) {
063                                    if ((classNameId != ratingsStats.getClassNameId()) ||
064                                            !classPKs.contains(ratingsStats.getClassPK())) {
065    
066                                            list = null;
067    
068                                            break;
069                                    }
070                            }
071                    }
072    
073                    if (list != null) {
074                            return list;
075                    }
076    
077                    Session session = null;
078    
079                    try {
080                            session = openSession();
081    
082                            String sql = CustomSQLUtil.get(FIND_BY_C_C);
083    
084                            sql = StringUtil.replace(
085                                    sql, "[$CLASS_PKS$]", StringUtil.merge(classPKs));
086    
087                            SQLQuery q = session.createSQLQuery(sql);
088    
089                            q.addEntity("RatingsStats", RatingsStatsImpl.class);
090    
091                            QueryPos qPos = QueryPos.getInstance(q);
092    
093                            qPos.add(classNameId);
094    
095                            list = q.list(true);
096                    }
097                    catch (Exception e) {
098                            throw new SystemException(e);
099                    }
100                    finally {
101                            if (list == null) {
102                                    FinderCacheUtil.removeResult(
103                                            FINDER_PATH_FIND_BY_C_C, finderArgs);
104                            }
105                            else {
106                                    FinderCacheUtil.putResult(
107                                            FINDER_PATH_FIND_BY_C_C, finderArgs, list);
108                            }
109    
110                            closeSession(session);
111                    }
112    
113                    return list;
114            }
115    
116    }