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.RatingsEntry;
025    import com.liferay.portlet.ratings.model.impl.RatingsEntryImpl;
026    import com.liferay.portlet.ratings.model.impl.RatingsEntryModelImpl;
027    import com.liferay.portlet.ratings.service.persistence.RatingsEntryFinder;
028    import com.liferay.portlet.ratings.service.persistence.RatingsEntryUtil;
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 RatingsEntryFinderImpl
042            extends RatingsEntryFinderBaseImpl implements RatingsEntryFinder {
043    
044            public static final String FIND_BY_U_C_C =
045                    RatingsEntryFinder.class.getName() + ".findByU_C_C";
046    
047            public static final FinderPath FINDER_PATH_FIND_BY_U_C_C = new FinderPath(
048                    RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
049                    RatingsEntryModelImpl.FINDER_CACHE_ENABLED, RatingsEntryImpl.class,
050                    RatingsEntryPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
051                    "findByU_C_C",
052                    new String[] {
053                            Long.class.getName(), Long.class.getName(), List.class.getName()
054                    });
055    
056            @Override
057            public Map<Serializable, RatingsEntry> fetchByPrimaryKeys(
058                    Set<Serializable> primaryKeys) {
059    
060                    return RatingsEntryUtil.fetchByPrimaryKeys(primaryKeys);
061            }
062    
063            @Override
064            public List<RatingsEntry> findByU_C_C(
065                    long userId, long classNameId, List<Long> classPKs) {
066    
067                    Object[] finderArgs = new Object[] {
068                            userId, classNameId,
069                            StringUtil.merge(classPKs.toArray(new Long[classPKs.size()]))
070                    };
071    
072                    List<RatingsEntry> list = (List<RatingsEntry>)FinderCacheUtil.getResult(
073                            FINDER_PATH_FIND_BY_U_C_C, finderArgs, this);
074    
075                    if ((list != null) && !list.isEmpty()) {
076                            for (RatingsEntry ratingsEntry : list) {
077                                    if ((userId != ratingsEntry.getUserId()) ||
078                                            (classNameId != ratingsEntry.getClassNameId()) ||
079                                            !classPKs.contains(ratingsEntry.getClassPK())) {
080    
081                                            list = null;
082    
083                                            break;
084                                    }
085                            }
086                    }
087    
088                    if (list != null) {
089                            return list;
090                    }
091    
092                    Session session = null;
093    
094                    try {
095                            session = openSession();
096    
097                            String sql = CustomSQLUtil.get(FIND_BY_U_C_C);
098    
099                            sql = StringUtil.replace(
100                                    sql, "[$CLASS_PKS$]", StringUtil.merge(classPKs));
101    
102                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
103    
104                            q.addEntity("RatingsEntry", RatingsEntryImpl.class);
105    
106                            QueryPos qPos = QueryPos.getInstance(q);
107    
108                            qPos.add(userId);
109                            qPos.add(classNameId);
110    
111                            list = q.list(true);
112                    }
113                    catch (Exception e) {
114                            throw new SystemException(e);
115                    }
116                    finally {
117                            if (list == null) {
118                                    FinderCacheUtil.removeResult(
119                                            FINDER_PATH_FIND_BY_U_C_C, finderArgs);
120                            }
121                            else {
122                                    FinderCacheUtil.putResult(
123                                            FINDER_PATH_FIND_BY_U_C_C, finderArgs, list);
124                            }
125    
126                            closeSession(session);
127                    }
128    
129                    return list;
130            }
131    
132    }