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