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.documentlibrary.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.SQLQuery;
019    import com.liferay.portal.kernel.dao.orm.Session;
020    import com.liferay.portal.kernel.dao.orm.Type;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
023    import com.liferay.portlet.documentlibrary.model.DLFileRank;
024    import com.liferay.portlet.documentlibrary.model.impl.DLFileRankImpl;
025    import com.liferay.util.dao.orm.CustomSQLUtil;
026    
027    import java.util.List;
028    
029    /**
030     * @author Alexander Chow
031     */
032    public class DLFileRankFinderImpl
033            extends BasePersistenceImpl<DLFileRank> implements DLFileRankFinder {
034    
035            public static final String FIND_BY_STALE_RANKS =
036            DLFileRankFinder.class.getName() + ".findByStaleRanks";
037    
038            public static final String FIND_BY_FOLDER_ID =
039                    DLFileRankFinder.class.getName() + ".findByFolderId";
040    
041            @Override
042            public List<Object[]> findByStaleRanks(int count) throws SystemException {
043                    Session session = null;
044    
045                    try {
046                            session = openSession();
047    
048                            String sql = CustomSQLUtil.get(FIND_BY_STALE_RANKS);
049    
050                            SQLQuery q = session.createSQLQuery(sql);
051    
052                            q.addScalar("groupId", Type.LONG);
053                            q.addScalar("userId", Type.LONG);
054    
055                            QueryPos qPos = QueryPos.getInstance(q);
056    
057                            qPos.add(count);
058    
059                            return q.list(true);
060                    }
061                    catch (Exception e) {
062                            throw new SystemException(e);
063                    }
064                    finally {
065                            closeSession(session);
066                    }
067            }
068    
069            @Override
070            public List<DLFileRank> findByFolderId(long folderId)
071                    throws SystemException {
072    
073                    Session session = null;
074    
075                    try {
076                            session = openSession();
077    
078                            String sql = CustomSQLUtil.get(FIND_BY_FOLDER_ID);
079    
080                            SQLQuery q = session.createSQLQuery(sql);
081    
082                            q.addEntity("DLFileRank", DLFileRankImpl.class);
083    
084                            QueryPos qPos = QueryPos.getInstance(q);
085    
086                            qPos.add(folderId);
087    
088                            return q.list(true);
089                    }
090                    catch (Exception e) {
091                            throw new SystemException(e);
092                    }
093                    finally {
094                            closeSession(session);
095                    }
096            }
097    
098    }