1   /**
2    * Copyright (c) 2000-2008 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.social.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.SQLQuery;
29  import com.liferay.portal.kernel.dao.orm.Session;
30  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
31  import com.liferay.portlet.social.model.SocialActivity;
32  import com.liferay.portlet.social.model.impl.SocialActivityImpl;
33  import com.liferay.util.dao.orm.CustomSQLUtil;
34  
35  import java.util.Iterator;
36  import java.util.List;
37  
38  /**
39   * <a href="SocialActivityFinderImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class SocialActivityFinderImpl
45      extends BasePersistenceImpl implements SocialActivityFinder {
46  
47      public static String COUNT_BY_GROUP_ID =
48          SocialActivityFinder.class.getName() + ".countByGroupId";
49  
50      public static String COUNT_BY_ORGANIZATION_ID =
51          SocialActivityFinder.class.getName() + ".countByOrganizationId";
52  
53      public static String COUNT_BY_RELATION =
54          SocialActivityFinder.class.getName() + ".countByRelation";
55  
56      public static String COUNT_BY_RELATION_TYPE =
57          SocialActivityFinder.class.getName() + ".countByRelationType";
58  
59      public static String FIND_BY_GROUP_ID =
60          SocialActivityFinder.class.getName() + ".findByGroupId";
61  
62      public static String FIND_BY_ORGANIZATION_ID =
63          SocialActivityFinder.class.getName() + ".findByOrganizationId";
64  
65      public static String FIND_BY_RELATION =
66          SocialActivityFinder.class.getName() + ".findByRelation";
67  
68      public static String FIND_BY_RELATION_TYPE =
69          SocialActivityFinder.class.getName() + ".findByRelationType";
70  
71      public int countByGroupId(long groupId) throws SystemException {
72          Session session = null;
73  
74          try {
75              session = openSession();
76  
77              String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
78  
79              SQLQuery q = session.createSQLQuery(sql);
80  
81              q.addEntity("SocialActivity", SocialActivityImpl.class);
82  
83              QueryPos qPos = QueryPos.getInstance(q);
84  
85              qPos.add(groupId);
86  
87              Iterator<Long> itr = q.list().iterator();
88  
89              if (itr.hasNext()) {
90                  Long count = itr.next();
91  
92                  if (count != null) {
93                      return count.intValue();
94                  }
95              }
96  
97              return 0;
98          }
99          catch (Exception e) {
100             throw new SystemException(e);
101         }
102         finally {
103             closeSession(session);
104         }
105     }
106 
107     public int countByOrganizationId(long organizationId)
108         throws SystemException {
109 
110         Session session = null;
111 
112         try {
113             session = openSession();
114 
115             String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_ID);
116 
117             SQLQuery q = session.createSQLQuery(sql);
118 
119             q.addEntity("SocialActivity", SocialActivityImpl.class);
120 
121             QueryPos qPos = QueryPos.getInstance(q);
122 
123             qPos.add(organizationId);
124 
125             Iterator<Long> itr = q.list().iterator();
126 
127             if (itr.hasNext()) {
128                 Long count = itr.next();
129 
130                 if (count != null) {
131                     return count.intValue();
132                 }
133             }
134 
135             return 0;
136         }
137         catch (Exception e) {
138             throw new SystemException(e);
139         }
140         finally {
141             closeSession(session);
142         }
143     }
144 
145     public int countByRelation(long userId) throws SystemException {
146         Session session = null;
147 
148         try {
149             session = openSession();
150 
151             String sql = CustomSQLUtil.get(COUNT_BY_RELATION);
152 
153             SQLQuery q = session.createSQLQuery(sql);
154 
155             q.addEntity("SocialActivity", SocialActivityImpl.class);
156 
157             QueryPos qPos = QueryPos.getInstance(q);
158 
159             qPos.add(userId);
160 
161             Iterator<Long> itr = q.list().iterator();
162 
163             if (itr.hasNext()) {
164                 Long count = itr.next();
165 
166                 if (count != null) {
167                     return count.intValue();
168                 }
169             }
170 
171             return 0;
172         }
173         catch (Exception e) {
174             throw new SystemException(e);
175         }
176         finally {
177             closeSession(session);
178         }
179     }
180 
181     public int countByRelationType(long userId, int type)
182         throws SystemException {
183 
184         Session session = null;
185 
186         try {
187             session = openSession();
188 
189             String sql = CustomSQLUtil.get(COUNT_BY_RELATION_TYPE);
190 
191             SQLQuery q = session.createSQLQuery(sql);
192 
193             q.addEntity("SocialActivity", SocialActivityImpl.class);
194 
195             QueryPos qPos = QueryPos.getInstance(q);
196 
197             qPos.add(userId);
198             qPos.add(type);
199 
200             Iterator<Long> itr = q.list().iterator();
201 
202             if (itr.hasNext()) {
203                 Long count = itr.next();
204 
205                 if (count != null) {
206                     return count.intValue();
207                 }
208             }
209 
210             return 0;
211         }
212         catch (Exception e) {
213             throw new SystemException(e);
214         }
215         finally {
216             closeSession(session);
217         }
218     }
219 
220     public List<SocialActivity> findByGroupId(long groupId, int start, int end)
221         throws SystemException {
222 
223         Session session = null;
224 
225         try {
226             session = openSession();
227 
228             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
229 
230             SQLQuery q = session.createSQLQuery(sql);
231 
232             q.addEntity("SocialActivity", SocialActivityImpl.class);
233 
234             QueryPos qPos = QueryPos.getInstance(q);
235 
236             qPos.add(groupId);
237 
238             return (List<SocialActivity>)QueryUtil.list(
239                 q, getDialect(), start, end);
240         }
241         catch (Exception e) {
242             throw new SystemException(e);
243         }
244         finally {
245             closeSession(session);
246         }
247     }
248 
249     public List<SocialActivity> findByOrganizationId(
250             long organizationId, int start, int end)
251         throws SystemException {
252 
253         Session session = null;
254 
255         try {
256             session = openSession();
257 
258             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_ID);
259 
260             SQLQuery q = session.createSQLQuery(sql);
261 
262             q.addEntity("SocialActivity", SocialActivityImpl.class);
263 
264             QueryPos qPos = QueryPos.getInstance(q);
265 
266             qPos.add(organizationId);
267 
268             return (List<SocialActivity>)QueryUtil.list(
269                 q, getDialect(), start, end);
270         }
271         catch (Exception e) {
272             throw new SystemException(e);
273         }
274         finally {
275             closeSession(session);
276         }
277     }
278 
279     public List<SocialActivity> findByRelation(long userId, int start, int end)
280         throws SystemException {
281 
282         Session session = null;
283 
284         try {
285             session = openSession();
286 
287             String sql = CustomSQLUtil.get(FIND_BY_RELATION);
288 
289             SQLQuery q = session.createSQLQuery(sql);
290 
291             q.addEntity("SocialActivity", SocialActivityImpl.class);
292 
293             QueryPos qPos = QueryPos.getInstance(q);
294 
295             qPos.add(userId);
296 
297             return (List<SocialActivity>)QueryUtil.list(
298                 q, getDialect(), start, end);
299         }
300         catch (Exception e) {
301             throw new SystemException(e);
302         }
303         finally {
304             closeSession(session);
305         }
306     }
307 
308     public List<SocialActivity> findByRelationType(
309             long userId, int type, int start, int end)
310         throws SystemException {
311 
312         Session session = null;
313 
314         try {
315             session = openSession();
316 
317             String sql = CustomSQLUtil.get(FIND_BY_RELATION_TYPE);
318 
319             SQLQuery q = session.createSQLQuery(sql);
320 
321             q.addEntity("SocialActivity", SocialActivityImpl.class);
322 
323             QueryPos qPos = QueryPos.getInstance(q);
324 
325             qPos.add(userId);
326             qPos.add(type);
327 
328             return (List<SocialActivity>)QueryUtil.list(
329                 q, getDialect(), start, end);
330         }
331         catch (Exception e) {
332             throw new SystemException(e);
333         }
334         finally {
335             closeSession(session);
336         }
337     }
338 
339 }