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