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