001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.social.service.persistence.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portlet.social.model.SocialActivity;
024    import com.liferay.portlet.social.model.impl.SocialActivityImpl;
025    import com.liferay.portlet.social.service.persistence.SocialActivityFinder;
026    import com.liferay.portlet.social.service.persistence.SocialActivityUtil;
027    import com.liferay.util.dao.orm.CustomSQLUtil;
028    
029    import java.util.ArrayList;
030    import java.util.Iterator;
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class SocialActivityFinderImpl
037            extends SocialActivityFinderBaseImpl implements SocialActivityFinder {
038    
039            public static final String COUNT_BY_GROUP_ID =
040                    SocialActivityFinder.class.getName() + ".countByGroupId";
041    
042            public static final String COUNT_BY_GROUP_USERS =
043                    SocialActivityFinder.class.getName() + ".countByGroupUsers";
044    
045            public static final String COUNT_BY_ORGANIZATION_ID =
046                    SocialActivityFinder.class.getName() + ".countByOrganizationId";
047    
048            public static final String COUNT_BY_ORGANIZATION_USERS =
049                    SocialActivityFinder.class.getName() + ".countByOrganizationUsers";
050    
051            public static final String COUNT_BY_RELATION =
052                    SocialActivityFinder.class.getName() + ".countByRelation";
053    
054            public static final String COUNT_BY_RELATION_TYPE =
055                    SocialActivityFinder.class.getName() + ".countByRelationType";
056    
057            public static final String COUNT_BY_USER_GROUPS =
058                    SocialActivityFinder.class.getName() + ".countByUserGroups";
059    
060            public static final String COUNT_BY_USER_GROUPS_AND_ORGANIZATIONS =
061                    SocialActivityFinder.class.getName() +
062                            ".countByUserGroupsAndOrganizations";
063    
064            public static final String COUNT_BY_USER_ORGANIZATIONS =
065                    SocialActivityFinder.class.getName() + ".countByUserOrganizations";
066    
067            public static final String FIND_BY_GROUP_ID =
068                    SocialActivityFinder.class.getName() + ".findByGroupId";
069    
070            public static final String FIND_BY_GROUP_USERS =
071                    SocialActivityFinder.class.getName() + ".findByGroupUsers";
072    
073            public static final String FIND_BY_ORGANIZATION_ID =
074                    SocialActivityFinder.class.getName() + ".findByOrganizationId";
075    
076            public static final String FIND_BY_ORGANIZATION_USERS =
077                    SocialActivityFinder.class.getName() + ".findByOrganizationUsers";
078    
079            public static final String FIND_BY_RELATION =
080                    SocialActivityFinder.class.getName() + ".findByRelation";
081    
082            public static final String FIND_BY_RELATION_TYPE =
083                    SocialActivityFinder.class.getName() + ".findByRelationType";
084    
085            public static final String FIND_BY_USER_GROUPS =
086                    SocialActivityFinder.class.getName() + ".findByUserGroups";
087    
088            public static final String FIND_BY_USER_GROUPS_AND_ORGANIZATIONS =
089                    SocialActivityFinder.class.getName() +
090                            ".findByUserGroupsAndOrganizations";
091    
092            public static final String FIND_BY_USER_ORGANIZATIONS =
093                    SocialActivityFinder.class.getName() + ".findByUserOrganizations";
094    
095            @Override
096            public int countByGroupId(long groupId) {
097                    Session session = null;
098    
099                    try {
100                            session = openSession();
101    
102                            String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
103    
104                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
105    
106                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
107    
108                            QueryPos qPos = QueryPos.getInstance(q);
109    
110                            qPos.add(groupId);
111    
112                            Iterator<Long> itr = q.iterate();
113    
114                            if (itr.hasNext()) {
115                                    Long count = itr.next();
116    
117                                    if (count != null) {
118                                            return count.intValue();
119                                    }
120                            }
121    
122                            return 0;
123                    }
124                    catch (Exception e) {
125                            throw new SystemException(e);
126                    }
127                    finally {
128                            closeSession(session);
129                    }
130            }
131    
132            @Override
133            public int countByGroupUsers(long groupId) {
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.createSynchronizedSQLQuery(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.iterate();
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            @Override
170            public int countByOrganizationId(long organizationId) {
171                    Session session = null;
172    
173                    try {
174                            session = openSession();
175    
176                            String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_ID);
177    
178                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
179    
180                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
181    
182                            QueryPos qPos = QueryPos.getInstance(q);
183    
184                            qPos.add(organizationId);
185    
186                            Iterator<Long> itr = q.iterate();
187    
188                            if (itr.hasNext()) {
189                                    Long count = itr.next();
190    
191                                    if (count != null) {
192                                            return count.intValue();
193                                    }
194                            }
195    
196                            return 0;
197                    }
198                    catch (Exception e) {
199                            throw new SystemException(e);
200                    }
201                    finally {
202                            closeSession(session);
203                    }
204            }
205    
206            @Override
207            public int countByOrganizationUsers(long organizationId) {
208                    Session session = null;
209    
210                    try {
211                            session = openSession();
212    
213                            String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_USERS);
214    
215                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
216    
217                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
218    
219                            QueryPos qPos = QueryPos.getInstance(q);
220    
221                            qPos.add(organizationId);
222    
223                            Iterator<Long> itr = q.iterate();
224    
225                            if (itr.hasNext()) {
226                                    Long count = itr.next();
227    
228                                    if (count != null) {
229                                            return count.intValue();
230                                    }
231                            }
232    
233                            return 0;
234                    }
235                    catch (Exception e) {
236                            throw new SystemException(e);
237                    }
238                    finally {
239                            closeSession(session);
240                    }
241            }
242    
243            @Override
244            public int countByRelation(long userId) {
245                    Session session = null;
246    
247                    try {
248                            session = openSession();
249    
250                            String sql = CustomSQLUtil.get(COUNT_BY_RELATION);
251    
252                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
253    
254                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
255    
256                            QueryPos qPos = QueryPos.getInstance(q);
257    
258                            qPos.add(userId);
259    
260                            Iterator<Long> itr = q.iterate();
261    
262                            if (itr.hasNext()) {
263                                    Long count = itr.next();
264    
265                                    if (count != null) {
266                                            return count.intValue();
267                                    }
268                            }
269    
270                            return 0;
271                    }
272                    catch (Exception e) {
273                            throw new SystemException(e);
274                    }
275                    finally {
276                            closeSession(session);
277                    }
278            }
279    
280            @Override
281            public int countByRelationType(long userId, int type) {
282                    Session session = null;
283    
284                    try {
285                            session = openSession();
286    
287                            String sql = CustomSQLUtil.get(COUNT_BY_RELATION_TYPE);
288    
289                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
290    
291                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
292    
293                            QueryPos qPos = QueryPos.getInstance(q);
294    
295                            qPos.add(userId);
296                            qPos.add(type);
297    
298                            Iterator<Long> itr = q.iterate();
299    
300                            if (itr.hasNext()) {
301                                    Long count = itr.next();
302    
303                                    if (count != null) {
304                                            return count.intValue();
305                                    }
306                            }
307    
308                            return 0;
309                    }
310                    catch (Exception e) {
311                            throw new SystemException(e);
312                    }
313                    finally {
314                            closeSession(session);
315                    }
316            }
317    
318            @Override
319            public int countByUserGroups(long userId) {
320                    Session session = null;
321    
322                    try {
323                            session = openSession();
324    
325                            String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUPS);
326    
327                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
328    
329                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
330    
331                            QueryPos qPos = QueryPos.getInstance(q);
332    
333                            qPos.add(userId);
334                            qPos.add(userId);
335                            qPos.add(userId);
336    
337                            Iterator<Long> itr = q.iterate();
338    
339                            if (itr.hasNext()) {
340                                    Long count = itr.next();
341    
342                                    if (count != null) {
343                                            return count.intValue();
344                                    }
345                            }
346    
347                            return 0;
348                    }
349                    catch (Exception e) {
350                            throw new SystemException(e);
351                    }
352                    finally {
353                            closeSession(session);
354                    }
355            }
356    
357            @Override
358            public int countByUserGroupsAndOrganizations(long userId) {
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.createSynchronizedSQLQuery(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.iterate();
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            @Override
399            public int countByUserOrganizations(long userId) {
400                    Session session = null;
401    
402                    try {
403                            session = openSession();
404    
405                            String sql = CustomSQLUtil.get(COUNT_BY_USER_ORGANIZATIONS);
406    
407                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
408    
409                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
410    
411                            QueryPos qPos = QueryPos.getInstance(q);
412    
413                            qPos.add(userId);
414    
415                            Iterator<Long> itr = q.iterate();
416    
417                            if (itr.hasNext()) {
418                                    Long count = itr.next();
419    
420                                    if (count != null) {
421                                            return count.intValue();
422                                    }
423                            }
424    
425                            return 0;
426                    }
427                    catch (Exception e) {
428                            throw new SystemException(e);
429                    }
430                    finally {
431                            closeSession(session);
432                    }
433            }
434    
435            @Override
436            public List<SocialActivity> findByGroupId(
437                    long groupId, int start, int end) {
438    
439                    Session session = null;
440    
441                    try {
442                            session = openSession();
443    
444                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
445    
446                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
447    
448                            q.addEntity("SocialActivity", SocialActivityImpl.class);
449    
450                            QueryPos qPos = QueryPos.getInstance(q);
451    
452                            qPos.add(groupId);
453    
454                            return (List<SocialActivity>)QueryUtil.list(
455                                    q, getDialect(), start, end);
456                    }
457                    catch (Exception e) {
458                            throw new SystemException(e);
459                    }
460                    finally {
461                            closeSession(session);
462                    }
463            }
464    
465            @Override
466            public List<SocialActivity> findByGroupUsers(
467                    long groupId, int start, int end) {
468    
469                    Session session = null;
470    
471                    try {
472                            session = openSession();
473    
474                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_USERS);
475    
476                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
477    
478                            q.addEntity("SocialActivity", SocialActivityImpl.class);
479    
480                            QueryPos qPos = QueryPos.getInstance(q);
481    
482                            qPos.add(groupId);
483    
484                            return (List<SocialActivity>)QueryUtil.list(
485                                    q, getDialect(), start, end);
486                    }
487                    catch (Exception e) {
488                            throw new SystemException(e);
489                    }
490                    finally {
491                            closeSession(session);
492                    }
493            }
494    
495            @Override
496            public List<SocialActivity> findByOrganizationId(
497                    long organizationId, int start, int end) {
498    
499                    Session session = null;
500    
501                    try {
502                            session = openSession();
503    
504                            String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_ID);
505    
506                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
507    
508                            q.addEntity("SocialActivity", SocialActivityImpl.class);
509    
510                            QueryPos qPos = QueryPos.getInstance(q);
511    
512                            qPos.add(organizationId);
513    
514                            return (List<SocialActivity>)QueryUtil.list(
515                                    q, getDialect(), start, end);
516                    }
517                    catch (Exception e) {
518                            throw new SystemException(e);
519                    }
520                    finally {
521                            closeSession(session);
522                    }
523            }
524    
525            @Override
526            public List<SocialActivity> findByOrganizationUsers(
527                    long organizationId, int start, int end) {
528    
529                    Session session = null;
530    
531                    try {
532                            session = openSession();
533    
534                            String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_USERS);
535    
536                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
537    
538                            q.addEntity("SocialActivity", SocialActivityImpl.class);
539    
540                            QueryPos qPos = QueryPos.getInstance(q);
541    
542                            qPos.add(organizationId);
543    
544                            return (List<SocialActivity>)QueryUtil.list(
545                                    q, getDialect(), start, end);
546                    }
547                    catch (Exception e) {
548                            throw new SystemException(e);
549                    }
550                    finally {
551                            closeSession(session);
552                    }
553            }
554    
555            @Override
556            public List<SocialActivity> findByRelation(
557                    long userId, int start, int end) {
558    
559                    Session session = null;
560    
561                    try {
562                            session = openSession();
563    
564                            String sql = CustomSQLUtil.get(FIND_BY_RELATION);
565    
566                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
567    
568                            q.addEntity("SocialActivity", SocialActivityImpl.class);
569    
570                            QueryPos qPos = QueryPos.getInstance(q);
571    
572                            qPos.add(userId);
573    
574                            return (List<SocialActivity>)QueryUtil.list(
575                                    q, getDialect(), start, end);
576                    }
577                    catch (Exception e) {
578                            throw new SystemException(e);
579                    }
580                    finally {
581                            closeSession(session);
582                    }
583            }
584    
585            @Override
586            public List<SocialActivity> findByRelationType(
587                    long userId, int type, int start, int end) {
588    
589                    Session session = null;
590    
591                    try {
592                            session = openSession();
593    
594                            String sql = CustomSQLUtil.get(FIND_BY_RELATION_TYPE);
595    
596                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
597    
598                            q.addEntity("SocialActivity", SocialActivityImpl.class);
599    
600                            QueryPos qPos = QueryPos.getInstance(q);
601    
602                            qPos.add(userId);
603                            qPos.add(type);
604    
605                            return (List<SocialActivity>)QueryUtil.list(
606                                    q, getDialect(), start, end);
607                    }
608                    catch (Exception e) {
609                            throw new SystemException(e);
610                    }
611                    finally {
612                            closeSession(session);
613                    }
614            }
615    
616            @Override
617            public List<SocialActivity> findByUserGroups(
618                    long userId, int start, int end) {
619    
620                    Session session = null;
621    
622                    try {
623                            session = openSession();
624    
625                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUPS);
626    
627                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
628    
629                            q.addEntity("SocialActivity", SocialActivityImpl.class);
630    
631                            QueryPos qPos = QueryPos.getInstance(q);
632    
633                            qPos.add(userId);
634                            qPos.add(userId);
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            @Override
649            public List<SocialActivity> findByUserGroupsAndOrganizations(
650                    long userId, int start, int end) {
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.createSynchronizedSQLQuery(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 = new ArrayList<>();
670    
671                            Iterator<Long> itr = (Iterator<Long>)QueryUtil.iterate(
672                                    q, getDialect(), start, end);
673    
674                            while (itr.hasNext()) {
675                                    Long activityId = itr.next();
676    
677                                    SocialActivity socialActivity =
678                                            SocialActivityUtil.findByPrimaryKey(activityId);
679    
680                                    socialActivities.add(socialActivity);
681                            }
682    
683                            return socialActivities;
684                    }
685                    catch (Exception e) {
686                            throw new SystemException(e);
687                    }
688                    finally {
689                            closeSession(session);
690                    }
691            }
692    
693            @Override
694            public List<SocialActivity> findByUserOrganizations(
695                    long userId, int start, int end) {
696    
697                    Session session = null;
698    
699                    try {
700                            session = openSession();
701    
702                            String sql = CustomSQLUtil.get(FIND_BY_USER_ORGANIZATIONS);
703    
704                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
705    
706                            q.addEntity("SocialActivity", SocialActivityImpl.class);
707    
708                            QueryPos qPos = QueryPos.getInstance(q);
709    
710                            qPos.add(userId);
711    
712                            return (List<SocialActivity>)QueryUtil.list(
713                                    q, getDialect(), start, end);
714                    }
715                    catch (Exception e) {
716                            throw new SystemException(e);
717                    }
718                    finally {
719                            closeSession(session);
720                    }
721            }
722    
723    }