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