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.kernel.dao.orm.Type;
31  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
32  import com.liferay.portlet.social.model.SocialActivity;
33  import com.liferay.portlet.social.model.impl.SocialActivityImpl;
34  import com.liferay.util.dao.orm.CustomSQLUtil;
35  
36  import java.util.ArrayList;
37  import java.util.Iterator;
38  import java.util.List;
39  
40  /**
41   * <a href="SocialActivityFinderImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class SocialActivityFinderImpl
47      extends BasePersistenceImpl implements SocialActivityFinder {
48  
49      public static String COUNT_BY_GROUP_ID =
50          SocialActivityFinder.class.getName() + ".countByGroupId";
51  
52      public static String COUNT_BY_ORGANIZATION_ID =
53          SocialActivityFinder.class.getName() + ".countByOrganizationId";
54  
55      public static String COUNT_BY_RELATION =
56          SocialActivityFinder.class.getName() + ".countByRelation";
57  
58      public static String COUNT_BY_RELATION_TYPE =
59          SocialActivityFinder.class.getName() + ".countByRelationType";
60  
61      public static String COUNT_BY_USER_GROUPS =
62          SocialActivityFinder.class.getName() + ".countByUserGroups";
63  
64      public static String COUNT_BY_USER_GROUPS_AND_ORGANIZATIONS =
65          SocialActivityFinder.class.getName() +
66              ".countByUserGroupsAndOrganizations";
67  
68      public static String COUNT_BY_USER_ORGANIZATIONS =
69          SocialActivityFinder.class.getName() + ".countByUserOrganizations";
70  
71      public static String FIND_BY_GROUP_ID =
72          SocialActivityFinder.class.getName() + ".findByGroupId";
73  
74      public static String FIND_BY_ORGANIZATION_ID =
75          SocialActivityFinder.class.getName() + ".findByOrganizationId";
76  
77      public static String FIND_BY_RELATION =
78          SocialActivityFinder.class.getName() + ".findByRelation";
79  
80      public static String FIND_BY_RELATION_TYPE =
81          SocialActivityFinder.class.getName() + ".findByRelationType";
82  
83      public static String FIND_BY_USER_GROUPS =
84          SocialActivityFinder.class.getName() + ".findByUserGroups";
85  
86      public static String FIND_BY_USER_GROUPS_AND_ORGANIZATIONS =
87          SocialActivityFinder.class.getName() +
88              ".findByUserGroupsAndOrganizations";
89  
90      public static String FIND_BY_USER_ORGANIZATIONS =
91          SocialActivityFinder.class.getName() + ".findByUserOrganizations";
92  
93      public int countByGroupId(long groupId) throws SystemException {
94          Session session = null;
95  
96          try {
97              session = openSession();
98  
99              String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
100 
101             SQLQuery q = session.createSQLQuery(sql);
102 
103             q.addEntity("SocialActivity", SocialActivityImpl.class);
104 
105             QueryPos qPos = QueryPos.getInstance(q);
106 
107             qPos.add(groupId);
108 
109             Iterator<Long> itr = q.list().iterator();
110 
111             if (itr.hasNext()) {
112                 Long count = itr.next();
113 
114                 if (count != null) {
115                     return count.intValue();
116                 }
117             }
118 
119             return 0;
120         }
121         catch (Exception e) {
122             throw new SystemException(e);
123         }
124         finally {
125             closeSession(session);
126         }
127     }
128 
129     public int countByOrganizationId(long organizationId)
130         throws SystemException {
131 
132         Session session = null;
133 
134         try {
135             session = openSession();
136 
137             String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_ID);
138 
139             SQLQuery q = session.createSQLQuery(sql);
140 
141             q.addEntity("SocialActivity", SocialActivityImpl.class);
142 
143             QueryPos qPos = QueryPos.getInstance(q);
144 
145             qPos.add(organizationId);
146 
147             Iterator<Long> itr = q.list().iterator();
148 
149             if (itr.hasNext()) {
150                 Long count = itr.next();
151 
152                 if (count != null) {
153                     return count.intValue();
154                 }
155             }
156 
157             return 0;
158         }
159         catch (Exception e) {
160             throw new SystemException(e);
161         }
162         finally {
163             closeSession(session);
164         }
165     }
166 
167     public int countByRelation(long userId) throws SystemException {
168         Session session = null;
169 
170         try {
171             session = openSession();
172 
173             String sql = CustomSQLUtil.get(COUNT_BY_RELATION);
174 
175             SQLQuery q = session.createSQLQuery(sql);
176 
177             q.addEntity("SocialActivity", SocialActivityImpl.class);
178 
179             QueryPos qPos = QueryPos.getInstance(q);
180 
181             qPos.add(userId);
182 
183             Iterator<Long> itr = q.list().iterator();
184 
185             if (itr.hasNext()) {
186                 Long count = itr.next();
187 
188                 if (count != null) {
189                     return count.intValue();
190                 }
191             }
192 
193             return 0;
194         }
195         catch (Exception e) {
196             throw new SystemException(e);
197         }
198         finally {
199             closeSession(session);
200         }
201     }
202 
203     public int countByRelationType(long userId, int type)
204         throws SystemException {
205 
206         Session session = null;
207 
208         try {
209             session = openSession();
210 
211             String sql = CustomSQLUtil.get(COUNT_BY_RELATION_TYPE);
212 
213             SQLQuery q = session.createSQLQuery(sql);
214 
215             q.addEntity("SocialActivity", SocialActivityImpl.class);
216 
217             QueryPos qPos = QueryPos.getInstance(q);
218 
219             qPos.add(userId);
220             qPos.add(type);
221 
222             Iterator<Long> itr = q.list().iterator();
223 
224             if (itr.hasNext()) {
225                 Long count = itr.next();
226 
227                 if (count != null) {
228                     return count.intValue();
229                 }
230             }
231 
232             return 0;
233         }
234         catch (Exception e) {
235             throw new SystemException(e);
236         }
237         finally {
238             closeSession(session);
239         }
240     }
241 
242     public int countByUserGroups(long userId) throws SystemException {
243         Session session = null;
244 
245         try {
246             session = openSession();
247 
248             String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUPS);
249 
250             SQLQuery q = session.createSQLQuery(sql);
251 
252             q.addEntity("SocialActivity", SocialActivityImpl.class);
253 
254             QueryPos qPos = QueryPos.getInstance(q);
255 
256             qPos.add(userId);
257 
258             Iterator<Long> itr = q.list().iterator();
259 
260             if (itr.hasNext()) {
261                 Long count = itr.next();
262 
263                 if (count != null) {
264                     return count.intValue();
265                 }
266             }
267 
268             return 0;
269         }
270         catch (Exception e) {
271             throw new SystemException(e);
272         }
273         finally {
274             closeSession(session);
275         }
276     }
277 
278     public int countByUserGroupsAndOrganizations(long userId)
279         throws SystemException {
280 
281         Session session = null;
282 
283         try {
284             session = openSession();
285 
286             String sql = CustomSQLUtil.get(
287                 COUNT_BY_USER_GROUPS_AND_ORGANIZATIONS);
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             qPos.add(userId);
297 
298             int count = 0;
299 
300             Iterator<Long> itr = q.list().iterator();
301 
302             while (itr.hasNext()) {
303                 Long l = itr.next();
304 
305                 if (l != null) {
306                     count += l.intValue();
307                 }
308             }
309 
310             return count;
311         }
312         catch (Exception e) {
313             throw new SystemException(e);
314         }
315         finally {
316             closeSession(session);
317         }
318     }
319 
320     public int countByUserOrganizations(long userId) throws SystemException {
321         Session session = null;
322 
323         try {
324             session = openSession();
325 
326             String sql = CustomSQLUtil.get(COUNT_BY_USER_ORGANIZATIONS);
327 
328             SQLQuery q = session.createSQLQuery(sql);
329 
330             q.addEntity("SocialActivity", SocialActivityImpl.class);
331 
332             QueryPos qPos = QueryPos.getInstance(q);
333 
334             qPos.add(userId);
335 
336             Iterator<Long> itr = q.list().iterator();
337 
338             if (itr.hasNext()) {
339                 Long count = itr.next();
340 
341                 if (count != null) {
342                     return count.intValue();
343                 }
344             }
345 
346             return 0;
347         }
348         catch (Exception e) {
349             throw new SystemException(e);
350         }
351         finally {
352             closeSession(session);
353         }
354     }
355 
356     public List<SocialActivity> findByGroupId(long groupId, int start, int end)
357         throws SystemException {
358 
359         Session session = null;
360 
361         try {
362             session = openSession();
363 
364             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
365 
366             SQLQuery q = session.createSQLQuery(sql);
367 
368             q.addEntity("SocialActivity", SocialActivityImpl.class);
369 
370             QueryPos qPos = QueryPos.getInstance(q);
371 
372             qPos.add(groupId);
373 
374             return (List<SocialActivity>)QueryUtil.list(
375                 q, getDialect(), start, end);
376         }
377         catch (Exception e) {
378             throw new SystemException(e);
379         }
380         finally {
381             closeSession(session);
382         }
383     }
384 
385     public List<SocialActivity> findByOrganizationId(
386             long organizationId, int start, int end)
387         throws SystemException {
388 
389         Session session = null;
390 
391         try {
392             session = openSession();
393 
394             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_ID);
395 
396             SQLQuery q = session.createSQLQuery(sql);
397 
398             q.addEntity("SocialActivity", SocialActivityImpl.class);
399 
400             QueryPos qPos = QueryPos.getInstance(q);
401 
402             qPos.add(organizationId);
403 
404             return (List<SocialActivity>)QueryUtil.list(
405                 q, getDialect(), start, end);
406         }
407         catch (Exception e) {
408             throw new SystemException(e);
409         }
410         finally {
411             closeSession(session);
412         }
413     }
414 
415     public List<SocialActivity> findByRelation(long userId, int start, int end)
416         throws SystemException {
417 
418         Session session = null;
419 
420         try {
421             session = openSession();
422 
423             String sql = CustomSQLUtil.get(FIND_BY_RELATION);
424 
425             SQLQuery q = session.createSQLQuery(sql);
426 
427             q.addEntity("SocialActivity", SocialActivityImpl.class);
428 
429             QueryPos qPos = QueryPos.getInstance(q);
430 
431             qPos.add(userId);
432 
433             return (List<SocialActivity>)QueryUtil.list(
434                 q, getDialect(), start, end);
435         }
436         catch (Exception e) {
437             throw new SystemException(e);
438         }
439         finally {
440             closeSession(session);
441         }
442     }
443 
444     public List<SocialActivity> findByRelationType(
445             long userId, int type, int start, int end)
446         throws SystemException {
447 
448         Session session = null;
449 
450         try {
451             session = openSession();
452 
453             String sql = CustomSQLUtil.get(FIND_BY_RELATION_TYPE);
454 
455             SQLQuery q = session.createSQLQuery(sql);
456 
457             q.addEntity("SocialActivity", SocialActivityImpl.class);
458 
459             QueryPos qPos = QueryPos.getInstance(q);
460 
461             qPos.add(userId);
462             qPos.add(type);
463 
464             return (List<SocialActivity>)QueryUtil.list(
465                 q, getDialect(), start, end);
466         }
467         catch (Exception e) {
468             throw new SystemException(e);
469         }
470         finally {
471             closeSession(session);
472         }
473     }
474 
475     public List<SocialActivity> findByUserGroups(
476             long userId, int start, int end)
477         throws SystemException {
478 
479         Session session = null;
480 
481         try {
482             session = openSession();
483 
484             String sql = CustomSQLUtil.get(FIND_BY_USER_GROUPS);
485 
486             SQLQuery q = session.createSQLQuery(sql);
487 
488             q.addEntity("SocialActivity", SocialActivityImpl.class);
489 
490             QueryPos qPos = QueryPos.getInstance(q);
491 
492             qPos.add(userId);
493 
494             return (List<SocialActivity>)QueryUtil.list(
495                 q, getDialect(), start, end);
496         }
497         catch (Exception e) {
498             throw new SystemException(e);
499         }
500         finally {
501             closeSession(session);
502         }
503     }
504 
505     public List<SocialActivity> findByUserGroupsAndOrganizations(
506             long userId, int start, int end)
507         throws SystemException {
508 
509         Session session = null;
510 
511         try {
512             session = openSession();
513 
514             String sql = CustomSQLUtil.get(
515                 FIND_BY_USER_GROUPS_AND_ORGANIZATIONS);
516 
517             SQLQuery q = session.createSQLQuery(sql);
518 
519             q.addScalar("activityId", Type.LONG);
520 
521             QueryPos qPos = QueryPos.getInstance(q);
522 
523             qPos.add(userId);
524             qPos.add(userId);
525 
526             List<SocialActivity> socialActivities =
527                 new ArrayList<SocialActivity>();
528 
529             Iterator<Long> itr = (Iterator<Long>)QueryUtil.iterate(
530                 q, getDialect(), start, end);
531 
532             while (itr.hasNext()) {
533                 Long activityId = itr.next();
534 
535                 SocialActivity socialActivity =
536                     SocialActivityUtil.findByPrimaryKey(activityId);
537 
538                 socialActivities.add(socialActivity);
539             }
540 
541             return socialActivities;
542         }
543         catch (Exception e) {
544             throw new SystemException(e);
545         }
546         finally {
547             closeSession(session);
548         }
549     }
550 
551     public List<SocialActivity> findByUserOrganizations(
552             long userId, int start, int end)
553         throws SystemException {
554 
555         Session session = null;
556 
557         try {
558             session = openSession();
559 
560             String sql = CustomSQLUtil.get(FIND_BY_USER_ORGANIZATIONS);
561 
562             SQLQuery q = session.createSQLQuery(sql);
563 
564             q.addEntity("SocialActivity", SocialActivityImpl.class);
565 
566             QueryPos qPos = QueryPos.getInstance(q);
567 
568             qPos.add(userId);
569 
570             return (List<SocialActivity>)QueryUtil.list(
571                 q, getDialect(), start, end);
572         }
573         catch (Exception e) {
574             throw new SystemException(e);
575         }
576         finally {
577             closeSession(session);
578         }
579     }
580 
581 }