1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.blogs.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.util.OrderByComparator;
27  import com.liferay.portal.kernel.util.StringMaker;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
30  import com.liferay.portal.spring.hibernate.HibernateUtil;
31  import com.liferay.portlet.blogs.model.impl.BlogsStatsUserImpl;
32  import com.liferay.util.dao.hibernate.QueryPos;
33  import com.liferay.util.dao.hibernate.QueryUtil;
34  
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.List;
38  
39  import org.hibernate.Hibernate;
40  import org.hibernate.SQLQuery;
41  import org.hibernate.Session;
42  
43  /**
44   * <a href="BlogsStatsUserFinder.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class BlogsStatsUserFinder {
50  
51      public static String COUNT_BY_ORGANIZATION_IDS =
52          BlogsStatsUserFinder.class.getName() + ".countByOrganizationIds";
53  
54      public static String FIND_BY_ORGANIZATION_IDS =
55          BlogsStatsUserFinder.class.getName() + ".findByOrganizationIds";
56  
57      public static int countByOrganizationId(long organizationId)
58          throws SystemException {
59  
60          List organizationIds = new ArrayList();
61  
62          organizationIds.add(new Long(organizationId));
63  
64          return countByOrganizationIds(organizationIds);
65      }
66  
67      public static int countByOrganizationIds(List organizationIds)
68          throws SystemException {
69  
70          Session session = null;
71  
72          try {
73              session = HibernateUtil.openSession();
74  
75              String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
76  
77              sql = StringUtil.replace(
78                  sql, "[$ORGANIZATION_ID$]",
79                  _getOrganizationIds(organizationIds));
80  
81              SQLQuery q = session.createSQLQuery(sql);
82  
83              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
84  
85              QueryPos qPos = QueryPos.getInstance(q);
86  
87              for (int i = 0; i < organizationIds.size(); i++) {
88                  Long organizationId = (Long)organizationIds.get(i);
89  
90                  qPos.add(organizationId);
91              }
92  
93              Iterator itr = q.list().iterator();
94  
95              if (itr.hasNext()) {
96                  Long count = (Long)itr.next();
97  
98                  if (count != null) {
99                      return count.intValue();
100                 }
101             }
102 
103             return 0;
104         }
105         catch (Exception e) {
106             throw new SystemException(e);
107         }
108         finally {
109             HibernateUtil.closeSession(session);
110         }
111     }
112 
113     public static List findByOrganizationId(
114             long organizationId, int begin, int end, OrderByComparator obc)
115         throws SystemException {
116 
117         List organizationIds = new ArrayList();
118 
119         organizationIds.add(new Long(organizationId));
120 
121         return findByOrganizationIds(organizationIds, begin, end, obc);
122     }
123 
124     public static List findByOrganizationIds(
125             List organizationIds, int begin, int end, OrderByComparator obc)
126         throws SystemException {
127 
128         Session session = null;
129 
130         try {
131             session = HibernateUtil.openSession();
132 
133             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
134 
135             sql = StringUtil.replace(
136                 sql, "[$ORGANIZATION_ID$]",
137                 _getOrganizationIds(organizationIds));
138             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
139 
140             SQLQuery q = session.createSQLQuery(sql);
141 
142             q.addEntity("BlogsStatsUser", BlogsStatsUserImpl.class);
143 
144             QueryPos qPos = QueryPos.getInstance(q);
145 
146             for (int i = 0; i < organizationIds.size(); i++) {
147                 Long organizationId = (Long)organizationIds.get(i);
148 
149                 qPos.add(organizationId);
150             }
151 
152             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
153         }
154         catch (Exception e) {
155             throw new SystemException(e);
156         }
157         finally {
158             HibernateUtil.closeSession(session);
159         }
160     }
161 
162     private static String _getOrganizationIds(List organizationIds) {
163         StringMaker sm = new StringMaker();
164 
165         for (int i = 0; i < organizationIds.size(); i++) {
166             sm.append("Users_Orgs.organizationId = ? ");
167 
168             if ((i + 1) != organizationIds.size()) {
169                 sm.append("OR ");
170             }
171         }
172 
173         return sm.toString();
174     }
175 
176 }