1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.blogs.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryPos;
18  import com.liferay.portal.kernel.dao.orm.QueryUtil;
19  import com.liferay.portal.kernel.dao.orm.SQLQuery;
20  import com.liferay.portal.kernel.dao.orm.Session;
21  import com.liferay.portal.kernel.dao.orm.Type;
22  import com.liferay.portal.kernel.exception.SystemException;
23  import com.liferay.portal.kernel.util.OrderByComparator;
24  import com.liferay.portal.kernel.util.StringBundler;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
28  import com.liferay.portlet.blogs.model.BlogsStatsUser;
29  import com.liferay.portlet.blogs.model.impl.BlogsStatsUserImpl;
30  import com.liferay.util.dao.orm.CustomSQLUtil;
31  
32  import java.util.ArrayList;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * <a href="BlogsStatsUserFinderImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class BlogsStatsUserFinderImpl
42      extends BasePersistenceImpl<BlogsStatsUser>
43      implements BlogsStatsUserFinder {
44  
45      public static String COUNT_BY_ORGANIZATION_IDS =
46          BlogsStatsUserFinder.class.getName() + ".countByOrganizationIds";
47  
48      public static String FIND_BY_GROUP_IDS =
49          BlogsStatsUserFinder.class.getName() + ".findByGroupIds";
50  
51      public static String FIND_BY_ORGANIZATION_IDS =
52          BlogsStatsUserFinder.class.getName() + ".findByOrganizationIds";
53  
54      public int countByOrganizationId(long organizationId)
55          throws SystemException {
56  
57          List<Long> organizationIds = new ArrayList<Long>();
58  
59          organizationIds.add(organizationId);
60  
61          return countByOrganizationIds(organizationIds);
62      }
63  
64      public int countByOrganizationIds(List<Long> organizationIds)
65          throws SystemException {
66  
67          Session session = null;
68  
69          try {
70              session = openSession();
71  
72              String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
73  
74              sql = StringUtil.replace(
75                  sql, "[$ORGANIZATION_ID$]",
76                  getOrganizationIds(organizationIds));
77  
78              SQLQuery q = session.createSQLQuery(sql);
79  
80              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
81  
82              QueryPos qPos = QueryPos.getInstance(q);
83  
84              for (int i = 0; i < organizationIds.size(); i++) {
85                  Long organizationId = organizationIds.get(i);
86  
87                  qPos.add(organizationId);
88              }
89  
90              Iterator<Long> itr = q.list().iterator();
91  
92              if (itr.hasNext()) {
93                  Long count = itr.next();
94  
95                  if (count != null) {
96                      return count.intValue();
97                  }
98              }
99  
100             return 0;
101         }
102         catch (Exception e) {
103             throw new SystemException(e);
104         }
105         finally {
106             closeSession(session);
107         }
108     }
109 
110     public List<BlogsStatsUser> findByGroupIds(
111             long companyId, long groupId, int start, int end)
112         throws SystemException {
113 
114         Session session = null;
115 
116         try {
117             session = openSession();
118 
119             String sql = CustomSQLUtil.get(FIND_BY_GROUP_IDS);
120 
121             SQLQuery q = session.createSQLQuery(sql);
122 
123             q.addEntity("BlogsStatsUser", BlogsStatsUserImpl.class);
124 
125             QueryPos qPos = QueryPos.getInstance(q);
126 
127             qPos.add(companyId);
128             qPos.add(groupId);
129             qPos.add(groupId);
130 
131             return (List<BlogsStatsUser>)QueryUtil.list(
132                 q, getDialect(), start, end);
133         }
134         catch (Exception e) {
135             throw new SystemException(e);
136         }
137         finally {
138             closeSession(session);
139         }
140     }
141 
142     public List<BlogsStatsUser> findByOrganizationId(
143             long organizationId, int start, int end, OrderByComparator obc)
144         throws SystemException {
145 
146         List<Long> organizationIds = new ArrayList<Long>();
147 
148         organizationIds.add(organizationId);
149 
150         return findByOrganizationIds(organizationIds, start, end, obc);
151     }
152 
153     public List<BlogsStatsUser> findByOrganizationIds(
154             List<Long> organizationIds, int start, int end,
155             OrderByComparator obc)
156         throws SystemException {
157 
158         Session session = null;
159 
160         try {
161             session = openSession();
162 
163             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
164 
165             sql = StringUtil.replace(
166                 sql, "[$ORGANIZATION_ID$]",
167                 getOrganizationIds(organizationIds));
168             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
169 
170             SQLQuery q = session.createSQLQuery(sql);
171 
172             q.addEntity("BlogsStatsUser", BlogsStatsUserImpl.class);
173 
174             QueryPos qPos = QueryPos.getInstance(q);
175 
176             for (int i = 0; i < organizationIds.size(); i++) {
177                 Long organizationId = organizationIds.get(i);
178 
179                 qPos.add(organizationId);
180             }
181 
182             return (List<BlogsStatsUser>)QueryUtil.list(
183                 q, getDialect(), start, end);
184         }
185         catch (Exception e) {
186             throw new SystemException(e);
187         }
188         finally {
189             closeSession(session);
190         }
191     }
192 
193     protected String getOrganizationIds(List<Long> organizationIds) {
194         if (organizationIds.isEmpty()) {
195             return StringPool.BLANK;
196         }
197 
198         StringBundler sb = new StringBundler(organizationIds.size() * 2 - 1);
199 
200         for (int i = 0; i < organizationIds.size(); i++) {
201             sb.append("Users_Orgs.organizationId = ? ");
202 
203             if ((i + 1) != organizationIds.size()) {
204                 sb.append("OR ");
205             }
206         }
207 
208         return sb.toString();
209     }
210 
211 }