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