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