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