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
038            implements SocialActivityFinder {
039    
040            public static final String COUNT_BY_GROUP_ID =
041                    SocialActivityFinder.class.getName() + ".countByGroupId";
042    
043            public static final String COUNT_BY_GROUP_USERS =
044                    SocialActivityFinder.class.getName() + ".countByGroupUsers";
045    
046            public static final String COUNT_BY_ORGANIZATION_ID =
047                    SocialActivityFinder.class.getName() + ".countByOrganizationId";
048    
049            public static final String COUNT_BY_ORGANIZATION_USERS =
050                    SocialActivityFinder.class.getName() + ".countByOrganizationUsers";
051    
052            public static final String COUNT_BY_RELATION =
053                    SocialActivityFinder.class.getName() + ".countByRelation";
054    
055            public static final String COUNT_BY_RELATION_TYPE =
056                    SocialActivityFinder.class.getName() + ".countByRelationType";
057    
058            public static final String COUNT_BY_USER_GROUPS =
059                    SocialActivityFinder.class.getName() + ".countByUserGroups";
060    
061            public static final String COUNT_BY_USER_GROUPS_AND_ORGANIZATIONS =
062                    SocialActivityFinder.class.getName() +
063                            ".countByUserGroupsAndOrganizations";
064    
065            public static final String COUNT_BY_USER_ORGANIZATIONS =
066                    SocialActivityFinder.class.getName() + ".countByUserOrganizations";
067    
068            public static final String FIND_BY_GROUP_ID =
069                    SocialActivityFinder.class.getName() + ".findByGroupId";
070    
071            public static final String FIND_BY_GROUP_USERS =
072                    SocialActivityFinder.class.getName() + ".findByGroupUsers";
073    
074            public static final String FIND_BY_ORGANIZATION_ID =
075                    SocialActivityFinder.class.getName() + ".findByOrganizationId";
076    
077            public static final String FIND_BY_ORGANIZATION_USERS =
078                    SocialActivityFinder.class.getName() + ".findByOrganizationUsers";
079    
080            public static final String FIND_BY_RELATION =
081                    SocialActivityFinder.class.getName() + ".findByRelation";
082    
083            public static final String FIND_BY_RELATION_TYPE =
084                    SocialActivityFinder.class.getName() + ".findByRelationType";
085    
086            public static final String FIND_BY_USER_GROUPS =
087                    SocialActivityFinder.class.getName() + ".findByUserGroups";
088    
089            public static final String FIND_BY_USER_GROUPS_AND_ORGANIZATIONS =
090                    SocialActivityFinder.class.getName() +
091                            ".findByUserGroupsAndOrganizations";
092    
093            public static final String FIND_BY_USER_ORGANIZATIONS =
094                    SocialActivityFinder.class.getName() + ".findByUserOrganizations";
095    
096            @Override
097            public int countByGroupId(long groupId) {
098                    Session session = null;
099    
100                    try {
101                            session = openSession();
102    
103                            String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
104    
105                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
106    
107                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
108    
109                            QueryPos qPos = QueryPos.getInstance(q);
110    
111                            qPos.add(groupId);
112    
113                            Iterator<Long> itr = q.iterate();
114    
115                            if (itr.hasNext()) {
116                                    Long count = itr.next();
117    
118                                    if (count != null) {
119                                            return count.intValue();
120                                    }
121                            }
122    
123                            return 0;
124                    }
125                    catch (Exception e) {
126                            throw new SystemException(e);
127                    }
128                    finally {
129                            closeSession(session);
130                    }
131            }
132    
133            @Override
134            public int countByGroupUsers(long groupId) {
135                    Session session = null;
136    
137                    try {
138                            session = openSession();
139    
140                            String sql = CustomSQLUtil.get(COUNT_BY_GROUP_USERS);
141    
142                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
143    
144                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
145    
146                            QueryPos qPos = QueryPos.getInstance(q);
147    
148                            qPos.add(groupId);
149    
150                            Iterator<Long> itr = q.iterate();
151    
152                            if (itr.hasNext()) {
153                                    Long count = itr.next();
154    
155                                    if (count != null) {
156                                            return count.intValue();
157                                    }
158                            }
159    
160                            return 0;
161                    }
162                    catch (Exception e) {
163                            throw new SystemException(e);
164                    }
165                    finally {
166                            closeSession(session);
167                    }
168            }
169    
170            @Override
171            public int countByOrganizationId(long organizationId) {
172                    Session session = null;
173    
174                    try {
175                            session = openSession();
176    
177                            String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_ID);
178    
179                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
180    
181                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
182    
183                            QueryPos qPos = QueryPos.getInstance(q);
184    
185                            qPos.add(organizationId);
186    
187                            Iterator<Long> itr = q.iterate();
188    
189                            if (itr.hasNext()) {
190                                    Long count = itr.next();
191    
192                                    if (count != null) {
193                                            return count.intValue();
194                                    }
195                            }
196    
197                            return 0;
198                    }
199                    catch (Exception e) {
200                            throw new SystemException(e);
201                    }
202                    finally {
203                            closeSession(session);
204                    }
205            }
206    
207            @Override
208            public int countByOrganizationUsers(long organizationId) {
209                    Session session = null;
210    
211                    try {
212                            session = openSession();
213    
214                            String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_USERS);
215    
216                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
217    
218                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
219    
220                            QueryPos qPos = QueryPos.getInstance(q);
221    
222                            qPos.add(organizationId);
223    
224                            Iterator<Long> itr = q.iterate();
225    
226                            if (itr.hasNext()) {
227                                    Long count = itr.next();
228    
229                                    if (count != null) {
230                                            return count.intValue();
231                                    }
232                            }
233    
234                            return 0;
235                    }
236                    catch (Exception e) {
237                            throw new SystemException(e);
238                    }
239                    finally {
240                            closeSession(session);
241                    }
242            }
243    
244            @Override
245            public int countByRelation(long userId) {
246                    Session session = null;
247    
248                    try {
249                            session = openSession();
250    
251                            String sql = CustomSQLUtil.get(COUNT_BY_RELATION);
252    
253                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
254    
255                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
256    
257                            QueryPos qPos = QueryPos.getInstance(q);
258    
259                            qPos.add(userId);
260    
261                            Iterator<Long> itr = q.iterate();
262    
263                            if (itr.hasNext()) {
264                                    Long count = itr.next();
265    
266                                    if (count != null) {
267                                            return count.intValue();
268                                    }
269                            }
270    
271                            return 0;
272                    }
273                    catch (Exception e) {
274                            throw new SystemException(e);
275                    }
276                    finally {
277                            closeSession(session);
278                    }
279            }
280    
281            @Override
282            public int countByRelationType(long userId, int type) {
283                    Session session = null;
284    
285                    try {
286                            session = openSession();
287    
288                            String sql = CustomSQLUtil.get(COUNT_BY_RELATION_TYPE);
289    
290                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
291    
292                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
293    
294                            QueryPos qPos = QueryPos.getInstance(q);
295    
296                            qPos.add(userId);
297                            qPos.add(type);
298    
299                            Iterator<Long> itr = q.iterate();
300    
301                            if (itr.hasNext()) {
302                                    Long count = itr.next();
303    
304                                    if (count != null) {
305                                            return count.intValue();
306                                    }
307                            }
308    
309                            return 0;
310                    }
311                    catch (Exception e) {
312                            throw new SystemException(e);
313                    }
314                    finally {
315                            closeSession(session);
316                    }
317            }
318    
319            @Override
320            public int countByUserGroups(long userId) {
321                    Session session = null;
322    
323                    try {
324                            session = openSession();
325    
326                            String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUPS);
327    
328                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
329    
330                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
331    
332                            QueryPos qPos = QueryPos.getInstance(q);
333    
334                            qPos.add(userId);
335                            qPos.add(userId);
336                            qPos.add(userId);
337    
338                            Iterator<Long> itr = q.iterate();
339    
340                            if (itr.hasNext()) {
341                                    Long count = itr.next();
342    
343                                    if (count != null) {
344                                            return count.intValue();
345                                    }
346                            }
347    
348                            return 0;
349                    }
350                    catch (Exception e) {
351                            throw new SystemException(e);
352                    }
353                    finally {
354                            closeSession(session);
355                    }
356            }
357    
358            @Override
359            public int countByUserGroupsAndOrganizations(long userId) {
360                    Session session = null;
361    
362                    try {
363                            session = openSession();
364    
365                            String sql = CustomSQLUtil.get(
366                                    COUNT_BY_USER_GROUPS_AND_ORGANIZATIONS);
367    
368                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
369    
370                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
371    
372                            QueryPos qPos = QueryPos.getInstance(q);
373    
374                            qPos.add(userId);
375                            qPos.add(userId);
376    
377                            int count = 0;
378    
379                            Iterator<Long> itr = q.iterate();
380    
381                            while (itr.hasNext()) {
382                                    Long l = itr.next();
383    
384                                    if (l != null) {
385                                            count += l.intValue();
386                                    }
387                            }
388    
389                            return count;
390                    }
391                    catch (Exception e) {
392                            throw new SystemException(e);
393                    }
394                    finally {
395                            closeSession(session);
396                    }
397            }
398    
399            @Override
400            public int countByUserOrganizations(long userId) {
401                    Session session = null;
402    
403                    try {
404                            session = openSession();
405    
406                            String sql = CustomSQLUtil.get(COUNT_BY_USER_ORGANIZATIONS);
407    
408                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
409    
410                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
411    
412                            QueryPos qPos = QueryPos.getInstance(q);
413    
414                            qPos.add(userId);
415    
416                            Iterator<Long> itr = q.iterate();
417    
418                            if (itr.hasNext()) {
419                                    Long count = itr.next();
420    
421                                    if (count != null) {
422                                            return count.intValue();
423                                    }
424                            }
425    
426                            return 0;
427                    }
428                    catch (Exception e) {
429                            throw new SystemException(e);
430                    }
431                    finally {
432                            closeSession(session);
433                    }
434            }
435    
436            @Override
437            public List<SocialActivity> findByGroupId(
438                    long groupId, int start, int end) {
439    
440                    Session session = null;
441    
442                    try {
443                            session = openSession();
444    
445                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
446    
447                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
448    
449                            q.addEntity("SocialActivity", SocialActivityImpl.class);
450    
451                            QueryPos qPos = QueryPos.getInstance(q);
452    
453                            qPos.add(groupId);
454    
455                            return (List<SocialActivity>)QueryUtil.list(
456                                    q, getDialect(), start, end);
457                    }
458                    catch (Exception e) {
459                            throw new SystemException(e);
460                    }
461                    finally {
462                            closeSession(session);
463                    }
464            }
465    
466            @Override
467            public List<SocialActivity> findByGroupUsers(
468                    long groupId, int start, int end) {
469    
470                    Session session = null;
471    
472                    try {
473                            session = openSession();
474    
475                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_USERS);
476    
477                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
478    
479                            q.addEntity("SocialActivity", SocialActivityImpl.class);
480    
481                            QueryPos qPos = QueryPos.getInstance(q);
482    
483                            qPos.add(groupId);
484    
485                            return (List<SocialActivity>)QueryUtil.list(
486                                    q, getDialect(), start, end);
487                    }
488                    catch (Exception e) {
489                            throw new SystemException(e);
490                    }
491                    finally {
492                            closeSession(session);
493                    }
494            }
495    
496            @Override
497            public List<SocialActivity> findByOrganizationId(
498                    long organizationId, int start, int end) {
499    
500                    Session session = null;
501    
502                    try {
503                            session = openSession();
504    
505                            String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_ID);
506    
507                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
508    
509                            q.addEntity("SocialActivity", SocialActivityImpl.class);
510    
511                            QueryPos qPos = QueryPos.getInstance(q);
512    
513                            qPos.add(organizationId);
514    
515                            return (List<SocialActivity>)QueryUtil.list(
516                                    q, getDialect(), start, end);
517                    }
518                    catch (Exception e) {
519                            throw new SystemException(e);
520                    }
521                    finally {
522                            closeSession(session);
523                    }
524            }
525    
526            @Override
527            public List<SocialActivity> findByOrganizationUsers(
528                    long organizationId, int start, int end) {
529    
530                    Session session = null;
531    
532                    try {
533                            session = openSession();
534    
535                            String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_USERS);
536    
537                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
538    
539                            q.addEntity("SocialActivity", SocialActivityImpl.class);
540    
541                            QueryPos qPos = QueryPos.getInstance(q);
542    
543                            qPos.add(organizationId);
544    
545                            return (List<SocialActivity>)QueryUtil.list(
546                                    q, getDialect(), start, end);
547                    }
548                    catch (Exception e) {
549                            throw new SystemException(e);
550                    }
551                    finally {
552                            closeSession(session);
553                    }
554            }
555    
556            @Override
557            public List<SocialActivity> findByRelation(
558                    long userId, int start, int end) {
559    
560                    Session session = null;
561    
562                    try {
563                            session = openSession();
564    
565                            String sql = CustomSQLUtil.get(FIND_BY_RELATION);
566    
567                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
568    
569                            q.addEntity("SocialActivity", SocialActivityImpl.class);
570    
571                            QueryPos qPos = QueryPos.getInstance(q);
572    
573                            qPos.add(userId);
574    
575                            return (List<SocialActivity>)QueryUtil.list(
576                                    q, getDialect(), start, end);
577                    }
578                    catch (Exception e) {
579                            throw new SystemException(e);
580                    }
581                    finally {
582                            closeSession(session);
583                    }
584            }
585    
586            @Override
587            public List<SocialActivity> findByRelationType(
588                    long userId, int type, int start, int end) {
589    
590                    Session session = null;
591    
592                    try {
593                            session = openSession();
594    
595                            String sql = CustomSQLUtil.get(FIND_BY_RELATION_TYPE);
596    
597                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
598    
599                            q.addEntity("SocialActivity", SocialActivityImpl.class);
600    
601                            QueryPos qPos = QueryPos.getInstance(q);
602    
603                            qPos.add(userId);
604                            qPos.add(type);
605    
606                            return (List<SocialActivity>)QueryUtil.list(
607                                    q, getDialect(), start, end);
608                    }
609                    catch (Exception e) {
610                            throw new SystemException(e);
611                    }
612                    finally {
613                            closeSession(session);
614                    }
615            }
616    
617            @Override
618            public List<SocialActivity> findByUserGroups(
619                    long userId, int start, int end) {
620    
621                    Session session = null;
622    
623                    try {
624                            session = openSession();
625    
626                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUPS);
627    
628                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
629    
630                            q.addEntity("SocialActivity", SocialActivityImpl.class);
631    
632                            QueryPos qPos = QueryPos.getInstance(q);
633    
634                            qPos.add(userId);
635                            qPos.add(userId);
636                            qPos.add(userId);
637    
638                            return (List<SocialActivity>)QueryUtil.list(
639                                    q, getDialect(), start, end);
640                    }
641                    catch (Exception e) {
642                            throw new SystemException(e);
643                    }
644                    finally {
645                            closeSession(session);
646                    }
647            }
648    
649            @Override
650            public List<SocialActivity> findByUserGroupsAndOrganizations(
651                    long userId, int start, int end) {
652    
653                    Session session = null;
654    
655                    try {
656                            session = openSession();
657    
658                            String sql = CustomSQLUtil.get(
659                                    FIND_BY_USER_GROUPS_AND_ORGANIZATIONS);
660    
661                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
662    
663                            q.addScalar("activityId", Type.LONG);
664    
665                            QueryPos qPos = QueryPos.getInstance(q);
666    
667                            qPos.add(userId);
668                            qPos.add(userId);
669    
670                            List<SocialActivity> socialActivities = new ArrayList<>();
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            @Override
695            public List<SocialActivity> findByUserOrganizations(
696                    long userId, int start, int end) {
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.createSynchronizedSQLQuery(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    }