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