001    /**
002     * Copyright (c) 2000-2013 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;
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.util.dao.orm.CustomSQLUtil;
027    
028    import java.util.ArrayList;
029    import java.util.Iterator;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class SocialActivityFinderImpl
036            extends BasePersistenceImpl<SocialActivity>
037            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) throws SystemException {
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.createSQLQuery(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) throws SystemException {
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.createSQLQuery(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                    throws SystemException {
172    
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.createSQLQuery(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                    throws SystemException {
211    
212                    Session session = null;
213    
214                    try {
215                            session = openSession();
216    
217                            String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_USERS);
218    
219                            SQLQuery q = session.createSQLQuery(sql);
220    
221                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
222    
223                            QueryPos qPos = QueryPos.getInstance(q);
224    
225                            qPos.add(organizationId);
226    
227                            Iterator<Long> itr = q.iterate();
228    
229                            if (itr.hasNext()) {
230                                    Long count = itr.next();
231    
232                                    if (count != null) {
233                                            return count.intValue();
234                                    }
235                            }
236    
237                            return 0;
238                    }
239                    catch (Exception e) {
240                            throw new SystemException(e);
241                    }
242                    finally {
243                            closeSession(session);
244                    }
245            }
246    
247            @Override
248            public int countByRelation(long userId) throws SystemException {
249                    Session session = null;
250    
251                    try {
252                            session = openSession();
253    
254                            String sql = CustomSQLUtil.get(COUNT_BY_RELATION);
255    
256                            SQLQuery q = session.createSQLQuery(sql);
257    
258                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
259    
260                            QueryPos qPos = QueryPos.getInstance(q);
261    
262                            qPos.add(userId);
263    
264                            Iterator<Long> itr = q.iterate();
265    
266                            if (itr.hasNext()) {
267                                    Long count = itr.next();
268    
269                                    if (count != null) {
270                                            return count.intValue();
271                                    }
272                            }
273    
274                            return 0;
275                    }
276                    catch (Exception e) {
277                            throw new SystemException(e);
278                    }
279                    finally {
280                            closeSession(session);
281                    }
282            }
283    
284            @Override
285            public int countByRelationType(long userId, int type)
286                    throws SystemException {
287    
288                    Session session = null;
289    
290                    try {
291                            session = openSession();
292    
293                            String sql = CustomSQLUtil.get(COUNT_BY_RELATION_TYPE);
294    
295                            SQLQuery q = session.createSQLQuery(sql);
296    
297                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
298    
299                            QueryPos qPos = QueryPos.getInstance(q);
300    
301                            qPos.add(userId);
302                            qPos.add(type);
303    
304                            Iterator<Long> itr = q.iterate();
305    
306                            if (itr.hasNext()) {
307                                    Long count = itr.next();
308    
309                                    if (count != null) {
310                                            return count.intValue();
311                                    }
312                            }
313    
314                            return 0;
315                    }
316                    catch (Exception e) {
317                            throw new SystemException(e);
318                    }
319                    finally {
320                            closeSession(session);
321                    }
322            }
323    
324            @Override
325            public int countByUserGroups(long userId) throws SystemException {
326                    Session session = null;
327    
328                    try {
329                            session = openSession();
330    
331                            String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUPS);
332    
333                            SQLQuery q = session.createSQLQuery(sql);
334    
335                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
336    
337                            QueryPos qPos = QueryPos.getInstance(q);
338    
339                            qPos.add(userId);
340                            qPos.add(userId);
341                            qPos.add(userId);
342    
343                            Iterator<Long> itr = q.iterate();
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            @Override
364            public int countByUserGroupsAndOrganizations(long userId)
365                    throws SystemException {
366    
367                    Session session = null;
368    
369                    try {
370                            session = openSession();
371    
372                            String sql = CustomSQLUtil.get(
373                                    COUNT_BY_USER_GROUPS_AND_ORGANIZATIONS);
374    
375                            SQLQuery q = session.createSQLQuery(sql);
376    
377                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
378    
379                            QueryPos qPos = QueryPos.getInstance(q);
380    
381                            qPos.add(userId);
382                            qPos.add(userId);
383    
384                            int count = 0;
385    
386                            Iterator<Long> itr = q.iterate();
387    
388                            while (itr.hasNext()) {
389                                    Long l = itr.next();
390    
391                                    if (l != null) {
392                                            count += l.intValue();
393                                    }
394                            }
395    
396                            return count;
397                    }
398                    catch (Exception e) {
399                            throw new SystemException(e);
400                    }
401                    finally {
402                            closeSession(session);
403                    }
404            }
405    
406            @Override
407            public int countByUserOrganizations(long userId) throws SystemException {
408                    Session session = null;
409    
410                    try {
411                            session = openSession();
412    
413                            String sql = CustomSQLUtil.get(COUNT_BY_USER_ORGANIZATIONS);
414    
415                            SQLQuery q = session.createSQLQuery(sql);
416    
417                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
418    
419                            QueryPos qPos = QueryPos.getInstance(q);
420    
421                            qPos.add(userId);
422    
423                            Iterator<Long> itr = q.iterate();
424    
425                            if (itr.hasNext()) {
426                                    Long count = itr.next();
427    
428                                    if (count != null) {
429                                            return count.intValue();
430                                    }
431                            }
432    
433                            return 0;
434                    }
435                    catch (Exception e) {
436                            throw new SystemException(e);
437                    }
438                    finally {
439                            closeSession(session);
440                    }
441            }
442    
443            @Override
444            public List<SocialActivity> findByGroupId(long groupId, int start, int end)
445                    throws SystemException {
446    
447                    Session session = null;
448    
449                    try {
450                            session = openSession();
451    
452                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
453    
454                            SQLQuery q = session.createSQLQuery(sql);
455    
456                            q.addEntity("SocialActivity", SocialActivityImpl.class);
457    
458                            QueryPos qPos = QueryPos.getInstance(q);
459    
460                            qPos.add(groupId);
461    
462                            return (List<SocialActivity>)QueryUtil.list(
463                                    q, getDialect(), start, end);
464                    }
465                    catch (Exception e) {
466                            throw new SystemException(e);
467                    }
468                    finally {
469                            closeSession(session);
470                    }
471            }
472    
473            @Override
474            public List<SocialActivity> findByGroupUsers(
475                            long groupId, int start, int end)
476                    throws SystemException {
477    
478                    Session session = null;
479    
480                    try {
481                            session = openSession();
482    
483                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_USERS);
484    
485                            SQLQuery q = session.createSQLQuery(sql);
486    
487                            q.addEntity("SocialActivity", SocialActivityImpl.class);
488    
489                            QueryPos qPos = QueryPos.getInstance(q);
490    
491                            qPos.add(groupId);
492    
493                            return (List<SocialActivity>)QueryUtil.list(
494                                    q, getDialect(), start, end);
495                    }
496                    catch (Exception e) {
497                            throw new SystemException(e);
498                    }
499                    finally {
500                            closeSession(session);
501                    }
502            }
503    
504            @Override
505            public List<SocialActivity> findByOrganizationId(
506                            long organizationId, int start, int end)
507                    throws SystemException {
508    
509                    Session session = null;
510    
511                    try {
512                            session = openSession();
513    
514                            String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_ID);
515    
516                            SQLQuery q = session.createSQLQuery(sql);
517    
518                            q.addEntity("SocialActivity", SocialActivityImpl.class);
519    
520                            QueryPos qPos = QueryPos.getInstance(q);
521    
522                            qPos.add(organizationId);
523    
524                            return (List<SocialActivity>)QueryUtil.list(
525                                    q, getDialect(), start, end);
526                    }
527                    catch (Exception e) {
528                            throw new SystemException(e);
529                    }
530                    finally {
531                            closeSession(session);
532                    }
533            }
534    
535            @Override
536            public List<SocialActivity> findByOrganizationUsers(
537                            long organizationId, int start, int end)
538                    throws SystemException {
539    
540                    Session session = null;
541    
542                    try {
543                            session = openSession();
544    
545                            String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_USERS);
546    
547                            SQLQuery q = session.createSQLQuery(sql);
548    
549                            q.addEntity("SocialActivity", SocialActivityImpl.class);
550    
551                            QueryPos qPos = QueryPos.getInstance(q);
552    
553                            qPos.add(organizationId);
554    
555                            return (List<SocialActivity>)QueryUtil.list(
556                                    q, getDialect(), start, end);
557                    }
558                    catch (Exception e) {
559                            throw new SystemException(e);
560                    }
561                    finally {
562                            closeSession(session);
563                    }
564            }
565    
566            @Override
567            public List<SocialActivity> findByRelation(long userId, int start, int end)
568                    throws SystemException {
569    
570                    Session session = null;
571    
572                    try {
573                            session = openSession();
574    
575                            String sql = CustomSQLUtil.get(FIND_BY_RELATION);
576    
577                            SQLQuery q = session.createSQLQuery(sql);
578    
579                            q.addEntity("SocialActivity", SocialActivityImpl.class);
580    
581                            QueryPos qPos = QueryPos.getInstance(q);
582    
583                            qPos.add(userId);
584    
585                            return (List<SocialActivity>)QueryUtil.list(
586                                    q, getDialect(), start, end);
587                    }
588                    catch (Exception e) {
589                            throw new SystemException(e);
590                    }
591                    finally {
592                            closeSession(session);
593                    }
594            }
595    
596            @Override
597            public List<SocialActivity> findByRelationType(
598                            long userId, int type, int start, int end)
599                    throws SystemException {
600    
601                    Session session = null;
602    
603                    try {
604                            session = openSession();
605    
606                            String sql = CustomSQLUtil.get(FIND_BY_RELATION_TYPE);
607    
608                            SQLQuery q = session.createSQLQuery(sql);
609    
610                            q.addEntity("SocialActivity", SocialActivityImpl.class);
611    
612                            QueryPos qPos = QueryPos.getInstance(q);
613    
614                            qPos.add(userId);
615                            qPos.add(type);
616    
617                            return (List<SocialActivity>)QueryUtil.list(
618                                    q, getDialect(), start, end);
619                    }
620                    catch (Exception e) {
621                            throw new SystemException(e);
622                    }
623                    finally {
624                            closeSession(session);
625                    }
626            }
627    
628            @Override
629            public List<SocialActivity> findByUserGroups(
630                            long userId, int start, int end)
631                    throws SystemException {
632    
633                    Session session = null;
634    
635                    try {
636                            session = openSession();
637    
638                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUPS);
639    
640                            SQLQuery q = session.createSQLQuery(sql);
641    
642                            q.addEntity("SocialActivity", SocialActivityImpl.class);
643    
644                            QueryPos qPos = QueryPos.getInstance(q);
645    
646                            qPos.add(userId);
647                            qPos.add(userId);
648                            qPos.add(userId);
649    
650                            return (List<SocialActivity>)QueryUtil.list(
651                                    q, getDialect(), start, end);
652                    }
653                    catch (Exception e) {
654                            throw new SystemException(e);
655                    }
656                    finally {
657                            closeSession(session);
658                    }
659            }
660    
661            @Override
662            public List<SocialActivity> findByUserGroupsAndOrganizations(
663                            long userId, int start, int end)
664                    throws SystemException {
665    
666                    Session session = null;
667    
668                    try {
669                            session = openSession();
670    
671                            String sql = CustomSQLUtil.get(
672                                    FIND_BY_USER_GROUPS_AND_ORGANIZATIONS);
673    
674                            SQLQuery q = session.createSQLQuery(sql);
675    
676                            q.addScalar("activityId", Type.LONG);
677    
678                            QueryPos qPos = QueryPos.getInstance(q);
679    
680                            qPos.add(userId);
681                            qPos.add(userId);
682    
683                            List<SocialActivity> socialActivities =
684                                    new ArrayList<SocialActivity>();
685    
686                            Iterator<Long> itr = (Iterator<Long>)QueryUtil.iterate(
687                                    q, getDialect(), start, end);
688    
689                            while (itr.hasNext()) {
690                                    Long activityId = itr.next();
691    
692                                    SocialActivity socialActivity =
693                                            SocialActivityUtil.findByPrimaryKey(activityId);
694    
695                                    socialActivities.add(socialActivity);
696                            }
697    
698                            return socialActivities;
699                    }
700                    catch (Exception e) {
701                            throw new SystemException(e);
702                    }
703                    finally {
704                            closeSession(session);
705                    }
706            }
707    
708            @Override
709            public List<SocialActivity> findByUserOrganizations(
710                            long userId, int start, int end)
711                    throws SystemException {
712    
713                    Session session = null;
714    
715                    try {
716                            session = openSession();
717    
718                            String sql = CustomSQLUtil.get(FIND_BY_USER_ORGANIZATIONS);
719    
720                            SQLQuery q = session.createSQLQuery(sql);
721    
722                            q.addEntity("SocialActivity", SocialActivityImpl.class);
723    
724                            QueryPos qPos = QueryPos.getInstance(q);
725    
726                            qPos.add(userId);
727    
728                            return (List<SocialActivity>)QueryUtil.list(
729                                    q, getDialect(), start, end);
730                    }
731                    catch (Exception e) {
732                            throw new SystemException(e);
733                    }
734                    finally {
735                            closeSession(session);
736                    }
737            }
738    
739    }