1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.social.service.persistence;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.orm.QueryPos;
19  import com.liferay.portal.kernel.dao.orm.QueryUtil;
20  import com.liferay.portal.kernel.dao.orm.SQLQuery;
21  import com.liferay.portal.kernel.dao.orm.Session;
22  import com.liferay.portal.kernel.dao.orm.Type;
23  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
24  import com.liferay.portlet.social.model.SocialActivity;
25  import com.liferay.portlet.social.model.impl.SocialActivityImpl;
26  import com.liferay.util.dao.orm.CustomSQLUtil;
27  
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /**
33   * <a href="SocialActivityFinderImpl.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class SocialActivityFinderImpl
38      extends BasePersistenceImpl<SocialActivity>
39      implements SocialActivityFinder {
40  
41      public static String COUNT_BY_GROUP_ID =
42          SocialActivityFinder.class.getName() + ".countByGroupId";
43  
44      public static String COUNT_BY_GROUP_USERS =
45          SocialActivityFinder.class.getName() + ".countByGroupUsers";
46  
47      public static String COUNT_BY_ORGANIZATION_ID =
48          SocialActivityFinder.class.getName() + ".countByOrganizationId";
49  
50      public static String COUNT_BY_ORGANIZATION_USERS =
51          SocialActivityFinder.class.getName() + ".countByOrganizationUsers";
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 COUNT_BY_USER_GROUPS =
60          SocialActivityFinder.class.getName() + ".countByUserGroups";
61  
62      public static String COUNT_BY_USER_GROUPS_AND_ORGANIZATIONS =
63          SocialActivityFinder.class.getName() +
64              ".countByUserGroupsAndOrganizations";
65  
66      public static String COUNT_BY_USER_ORGANIZATIONS =
67          SocialActivityFinder.class.getName() + ".countByUserOrganizations";
68  
69      public static String FIND_BY_GROUP_ID =
70          SocialActivityFinder.class.getName() + ".findByGroupId";
71  
72      public static String FIND_BY_GROUP_USERS =
73          SocialActivityFinder.class.getName() + ".findByGroupUsers";
74  
75      public static String FIND_BY_ORGANIZATION_ID =
76          SocialActivityFinder.class.getName() + ".findByOrganizationId";
77  
78      public static String FIND_BY_ORGANIZATION_USERS =
79          SocialActivityFinder.class.getName() + ".findByOrganizationUsers";
80  
81      public static String FIND_BY_RELATION =
82          SocialActivityFinder.class.getName() + ".findByRelation";
83  
84      public static String FIND_BY_RELATION_TYPE =
85          SocialActivityFinder.class.getName() + ".findByRelationType";
86  
87      public static String FIND_BY_USER_GROUPS =
88          SocialActivityFinder.class.getName() + ".findByUserGroups";
89  
90      public static String FIND_BY_USER_GROUPS_AND_ORGANIZATIONS =
91          SocialActivityFinder.class.getName() +
92              ".findByUserGroupsAndOrganizations";
93  
94      public static String FIND_BY_USER_ORGANIZATIONS =
95          SocialActivityFinder.class.getName() + ".findByUserOrganizations";
96  
97      public int countByGroupId(long groupId) throws SystemException {
98          Session session = null;
99  
100         try {
101             session = openSession();
102 
103             String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
104 
105             SQLQuery q = session.createSQLQuery(sql);
106 
107             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
108 
109             QueryPos qPos = QueryPos.getInstance(q);
110 
111             qPos.add(groupId);
112 
113             Iterator<Long> itr = q.list().iterator();
114 
115             if (itr.hasNext()) {
116                 Long count = itr.next();
117 
118                 if (count != null) {
119                     return count.intValue();
120                 }
121             }
122 
123             return 0;
124         }
125         catch (Exception e) {
126             throw new SystemException(e);
127         }
128         finally {
129             closeSession(session);
130         }
131     }
132 
133     public int countByGroupUsers(long groupId) throws SystemException {
134         Session session = null;
135 
136         try {
137             session = openSession();
138 
139             String sql = CustomSQLUtil.get(COUNT_BY_GROUP_USERS);
140 
141             SQLQuery q = session.createSQLQuery(sql);
142 
143             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
144 
145             QueryPos qPos = QueryPos.getInstance(q);
146 
147             qPos.add(groupId);
148 
149             Iterator<Long> itr = q.list().iterator();
150 
151             if (itr.hasNext()) {
152                 Long count = itr.next();
153 
154                 if (count != null) {
155                     return count.intValue();
156                 }
157             }
158 
159             return 0;
160         }
161         catch (Exception e) {
162             throw new SystemException(e);
163         }
164         finally {
165             closeSession(session);
166         }
167     }
168 
169     public int countByOrganizationId(long organizationId)
170         throws SystemException {
171 
172         Session session = null;
173 
174         try {
175             session = openSession();
176 
177             String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_ID);
178 
179             SQLQuery q = session.createSQLQuery(sql);
180 
181             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
182 
183             QueryPos qPos = QueryPos.getInstance(q);
184 
185             qPos.add(organizationId);
186 
187             Iterator<Long> itr = q.list().iterator();
188 
189             if (itr.hasNext()) {
190                 Long count = itr.next();
191 
192                 if (count != null) {
193                     return count.intValue();
194                 }
195             }
196 
197             return 0;
198         }
199         catch (Exception e) {
200             throw new SystemException(e);
201         }
202         finally {
203             closeSession(session);
204         }
205     }
206 
207     public int countByOrganizationUsers(long organizationId)
208         throws SystemException {
209 
210         Session session = null;
211 
212         try {
213             session = openSession();
214 
215             String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_USERS);
216 
217             SQLQuery q = session.createSQLQuery(sql);
218 
219             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
220 
221             QueryPos qPos = QueryPos.getInstance(q);
222 
223             qPos.add(organizationId);
224 
225             Iterator<Long> itr = q.list().iterator();
226 
227             if (itr.hasNext()) {
228                 Long count = itr.next();
229 
230                 if (count != null) {
231                     return count.intValue();
232                 }
233             }
234 
235             return 0;
236         }
237         catch (Exception e) {
238             throw new SystemException(e);
239         }
240         finally {
241             closeSession(session);
242         }
243     }
244 
245     public int countByRelation(long userId) throws SystemException {
246         Session session = null;
247 
248         try {
249             session = openSession();
250 
251             String sql = CustomSQLUtil.get(COUNT_BY_RELATION);
252 
253             SQLQuery q = session.createSQLQuery(sql);
254 
255             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
256 
257             QueryPos qPos = QueryPos.getInstance(q);
258 
259             qPos.add(userId);
260 
261             Iterator<Long> itr = q.list().iterator();
262 
263             if (itr.hasNext()) {
264                 Long count = itr.next();
265 
266                 if (count != null) {
267                     return count.intValue();
268                 }
269             }
270 
271             return 0;
272         }
273         catch (Exception e) {
274             throw new SystemException(e);
275         }
276         finally {
277             closeSession(session);
278         }
279     }
280 
281     public int countByRelationType(long userId, int type)
282         throws SystemException {
283 
284         Session session = null;
285 
286         try {
287             session = openSession();
288 
289             String sql = CustomSQLUtil.get(COUNT_BY_RELATION_TYPE);
290 
291             SQLQuery q = session.createSQLQuery(sql);
292 
293             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
294 
295             QueryPos qPos = QueryPos.getInstance(q);
296 
297             qPos.add(userId);
298             qPos.add(type);
299 
300             Iterator<Long> itr = q.list().iterator();
301 
302             if (itr.hasNext()) {
303                 Long count = itr.next();
304 
305                 if (count != null) {
306                     return count.intValue();
307                 }
308             }
309 
310             return 0;
311         }
312         catch (Exception e) {
313             throw new SystemException(e);
314         }
315         finally {
316             closeSession(session);
317         }
318     }
319 
320     public int countByUserGroups(long userId) throws SystemException {
321         Session session = null;
322 
323         try {
324             session = openSession();
325 
326             String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUPS);
327 
328             SQLQuery q = session.createSQLQuery(sql);
329 
330             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
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 int countByUserGroupsAndOrganizations(long userId)
357         throws SystemException {
358 
359         Session session = null;
360 
361         try {
362             session = openSession();
363 
364             String sql = CustomSQLUtil.get(
365                 COUNT_BY_USER_GROUPS_AND_ORGANIZATIONS);
366 
367             SQLQuery q = session.createSQLQuery(sql);
368 
369             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
370 
371             QueryPos qPos = QueryPos.getInstance(q);
372 
373             qPos.add(userId);
374             qPos.add(userId);
375 
376             int count = 0;
377 
378             Iterator<Long> itr = q.list().iterator();
379 
380             while (itr.hasNext()) {
381                 Long l = itr.next();
382 
383                 if (l != null) {
384                     count += l.intValue();
385                 }
386             }
387 
388             return count;
389         }
390         catch (Exception e) {
391             throw new SystemException(e);
392         }
393         finally {
394             closeSession(session);
395         }
396     }
397 
398     public int countByUserOrganizations(long userId) throws SystemException {
399         Session session = null;
400 
401         try {
402             session = openSession();
403 
404             String sql = CustomSQLUtil.get(COUNT_BY_USER_ORGANIZATIONS);
405 
406             SQLQuery q = session.createSQLQuery(sql);
407 
408             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
409 
410             QueryPos qPos = QueryPos.getInstance(q);
411 
412             qPos.add(userId);
413 
414             Iterator<Long> itr = q.list().iterator();
415 
416             if (itr.hasNext()) {
417                 Long count = itr.next();
418 
419                 if (count != null) {
420                     return count.intValue();
421                 }
422             }
423 
424             return 0;
425         }
426         catch (Exception e) {
427             throw new SystemException(e);
428         }
429         finally {
430             closeSession(session);
431         }
432     }
433 
434     public List<SocialActivity> findByGroupId(long groupId, int start, int end)
435         throws SystemException {
436 
437         Session session = null;
438 
439         try {
440             session = openSession();
441 
442             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
443 
444             SQLQuery q = session.createSQLQuery(sql);
445 
446             q.addEntity("SocialActivity", SocialActivityImpl.class);
447 
448             QueryPos qPos = QueryPos.getInstance(q);
449 
450             qPos.add(groupId);
451 
452             return (List<SocialActivity>)QueryUtil.list(
453                 q, getDialect(), start, end);
454         }
455         catch (Exception e) {
456             throw new SystemException(e);
457         }
458         finally {
459             closeSession(session);
460         }
461     }
462 
463     public List<SocialActivity> findByGroupUsers(
464             long groupId, int start, int end)
465         throws SystemException {
466 
467         Session session = null;
468 
469         try {
470             session = openSession();
471 
472             String sql = CustomSQLUtil.get(FIND_BY_GROUP_USERS);
473 
474             SQLQuery q = session.createSQLQuery(sql);
475 
476             q.addEntity("SocialActivity", SocialActivityImpl.class);
477 
478             QueryPos qPos = QueryPos.getInstance(q);
479 
480             qPos.add(groupId);
481 
482             return (List<SocialActivity>)QueryUtil.list(
483                 q, getDialect(), start, end);
484         }
485         catch (Exception e) {
486             throw new SystemException(e);
487         }
488         finally {
489             closeSession(session);
490         }
491     }
492 
493     public List<SocialActivity> findByOrganizationId(
494             long organizationId, int start, int end)
495         throws SystemException {
496 
497         Session session = null;
498 
499         try {
500             session = openSession();
501 
502             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_ID);
503 
504             SQLQuery q = session.createSQLQuery(sql);
505 
506             q.addEntity("SocialActivity", SocialActivityImpl.class);
507 
508             QueryPos qPos = QueryPos.getInstance(q);
509 
510             qPos.add(organizationId);
511 
512             return (List<SocialActivity>)QueryUtil.list(
513                 q, getDialect(), start, end);
514         }
515         catch (Exception e) {
516             throw new SystemException(e);
517         }
518         finally {
519             closeSession(session);
520         }
521     }
522 
523     public List<SocialActivity> findByOrganizationUsers(
524             long organizationId, int start, int end)
525         throws SystemException {
526 
527         Session session = null;
528 
529         try {
530             session = openSession();
531 
532             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_USERS);
533 
534             SQLQuery q = session.createSQLQuery(sql);
535 
536             q.addEntity("SocialActivity", SocialActivityImpl.class);
537 
538             QueryPos qPos = QueryPos.getInstance(q);
539 
540             qPos.add(organizationId);
541 
542             return (List<SocialActivity>)QueryUtil.list(
543                 q, getDialect(), start, end);
544         }
545         catch (Exception e) {
546             throw new SystemException(e);
547         }
548         finally {
549             closeSession(session);
550         }
551     }
552 
553     public List<SocialActivity> findByRelation(long userId, int start, int end)
554         throws SystemException {
555 
556         Session session = null;
557 
558         try {
559             session = openSession();
560 
561             String sql = CustomSQLUtil.get(FIND_BY_RELATION);
562 
563             SQLQuery q = session.createSQLQuery(sql);
564 
565             q.addEntity("SocialActivity", SocialActivityImpl.class);
566 
567             QueryPos qPos = QueryPos.getInstance(q);
568 
569             qPos.add(userId);
570 
571             return (List<SocialActivity>)QueryUtil.list(
572                 q, getDialect(), start, end);
573         }
574         catch (Exception e) {
575             throw new SystemException(e);
576         }
577         finally {
578             closeSession(session);
579         }
580     }
581 
582     public List<SocialActivity> findByRelationType(
583             long userId, int type, int start, int end)
584         throws SystemException {
585 
586         Session session = null;
587 
588         try {
589             session = openSession();
590 
591             String sql = CustomSQLUtil.get(FIND_BY_RELATION_TYPE);
592 
593             SQLQuery q = session.createSQLQuery(sql);
594 
595             q.addEntity("SocialActivity", SocialActivityImpl.class);
596 
597             QueryPos qPos = QueryPos.getInstance(q);
598 
599             qPos.add(userId);
600             qPos.add(type);
601 
602             return (List<SocialActivity>)QueryUtil.list(
603                 q, getDialect(), start, end);
604         }
605         catch (Exception e) {
606             throw new SystemException(e);
607         }
608         finally {
609             closeSession(session);
610         }
611     }
612 
613     public List<SocialActivity> findByUserGroups(
614             long userId, int start, int end)
615         throws SystemException {
616 
617         Session session = null;
618 
619         try {
620             session = openSession();
621 
622             String sql = CustomSQLUtil.get(FIND_BY_USER_GROUPS);
623 
624             SQLQuery q = session.createSQLQuery(sql);
625 
626             q.addEntity("SocialActivity", SocialActivityImpl.class);
627 
628             QueryPos qPos = QueryPos.getInstance(q);
629 
630             qPos.add(userId);
631 
632             return (List<SocialActivity>)QueryUtil.list(
633                 q, getDialect(), start, end);
634         }
635         catch (Exception e) {
636             throw new SystemException(e);
637         }
638         finally {
639             closeSession(session);
640         }
641     }
642 
643     public List<SocialActivity> findByUserGroupsAndOrganizations(
644             long userId, int start, int end)
645         throws SystemException {
646 
647         Session session = null;
648 
649         try {
650             session = openSession();
651 
652             String sql = CustomSQLUtil.get(
653                 FIND_BY_USER_GROUPS_AND_ORGANIZATIONS);
654 
655             SQLQuery q = session.createSQLQuery(sql);
656 
657             q.addScalar("activityId", Type.LONG);
658 
659             QueryPos qPos = QueryPos.getInstance(q);
660 
661             qPos.add(userId);
662             qPos.add(userId);
663 
664             List<SocialActivity> socialActivities =
665                 new ArrayList<SocialActivity>();
666 
667             Iterator<Long> itr = (Iterator<Long>)QueryUtil.iterate(
668                 q, getDialect(), start, end);
669 
670             while (itr.hasNext()) {
671                 Long activityId = itr.next();
672 
673                 SocialActivity socialActivity =
674                     SocialActivityUtil.findByPrimaryKey(activityId);
675 
676                 socialActivities.add(socialActivity);
677             }
678 
679             return socialActivities;
680         }
681         catch (Exception e) {
682             throw new SystemException(e);
683         }
684         finally {
685             closeSession(session);
686         }
687     }
688 
689     public List<SocialActivity> findByUserOrganizations(
690             long userId, int start, int end)
691         throws SystemException {
692 
693         Session session = null;
694 
695         try {
696             session = openSession();
697 
698             String sql = CustomSQLUtil.get(FIND_BY_USER_ORGANIZATIONS);
699 
700             SQLQuery q = session.createSQLQuery(sql);
701 
702             q.addEntity("SocialActivity", SocialActivityImpl.class);
703 
704             QueryPos qPos = QueryPos.getInstance(q);
705 
706             qPos.add(userId);
707 
708             return (List<SocialActivity>)QueryUtil.list(
709                 q, getDialect(), start, end);
710         }
711         catch (Exception e) {
712             throw new SystemException(e);
713         }
714         finally {
715             closeSession(session);
716         }
717     }
718 
719 }