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