1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.documentlibrary.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.QueryPos;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.kernel.dao.orm.SQLQuery;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.dao.orm.Type;
28  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
29  import com.liferay.portlet.documentlibrary.model.DLFileRank;
30  import com.liferay.portlet.documentlibrary.model.impl.DLFileRankImpl;
31  import com.liferay.util.dao.orm.CustomSQLUtil;
32  
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * <a href="DLFileRankFinderImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class DLFileRankFinderImpl
43      extends BasePersistenceImpl implements DLFileRankFinder {
44  
45      public static String COUNT_BY_G_U =
46          DLFileRankFinder.class.getName() + ".countByG_U";
47  
48      public static String FIND_BY_G_U =
49          DLFileRankFinder.class.getName() + ".findByG_U";
50  
51      public int countByG_U(long groupId, long userId) throws SystemException {
52          Session session = null;
53  
54          try {
55              session = openSession();
56  
57              String sql = CustomSQLUtil.get(COUNT_BY_G_U);
58  
59              SQLQuery q = session.createSQLQuery(sql);
60  
61              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
62  
63              QueryPos qPos = QueryPos.getInstance(q);
64  
65              qPos.add(groupId);
66              qPos.add(userId);
67  
68              Iterator<Long> itr = q.list().iterator();
69  
70              if (itr.hasNext()) {
71                  Long count = itr.next();
72  
73                  if (count != null) {
74                      return count.intValue();
75                  }
76              }
77  
78              return 0;
79          }
80          catch (Exception e) {
81              throw new SystemException(e);
82          }
83          finally {
84              closeSession(session);
85          }
86      }
87  
88      public List<DLFileRank> findByG_U(long groupId, long userId)
89          throws SystemException {
90  
91          return findByG_U(groupId, userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
92      }
93  
94      public List<DLFileRank> findByG_U(
95              long groupId, long userId, int start, int end)
96          throws SystemException {
97  
98          Session session = null;
99  
100         try {
101             session = openSession();
102 
103             String sql = CustomSQLUtil.get(FIND_BY_G_U);
104 
105             SQLQuery q = session.createSQLQuery(sql);
106 
107             q.addEntity("DLFileRank", DLFileRankImpl.class);
108 
109             QueryPos qPos = QueryPos.getInstance(q);
110 
111             qPos.add(groupId);
112             qPos.add(userId);
113 
114             return (List<DLFileRank>)QueryUtil.list(
115                 q, getDialect(), start, end);
116         }
117         catch (Exception e) {
118             throw new SystemException(e);
119         }
120         finally {
121             closeSession(session);
122         }
123     }
124 
125 }