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            public int countByGroupId(long groupId) throws SystemException {
096                    Session session = null;
097    
098                    try {
099                            session = openSession();
100    
101                            String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
102    
103                            SQLQuery q = session.createSQLQuery(sql);
104    
105                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
106    
107                            QueryPos qPos = QueryPos.getInstance(q);
108    
109                            qPos.add(groupId);
110    
111                            Iterator<Long> itr = q.iterate();
112    
113                            if (itr.hasNext()) {
114                                    Long count = itr.next();
115    
116                                    if (count != null) {
117                                            return count.intValue();
118                                    }
119                            }
120    
121                            return 0;
122                    }
123                    catch (Exception e) {
124                            throw new SystemException(e);
125                    }
126                    finally {
127                            closeSession(session);
128                    }
129            }
130    
131            public int countByGroupUsers(long groupId) throws SystemException {
132                    Session session = null;
133    
134                    try {
135                            session = openSession();
136    
137                            String sql = CustomSQLUtil.get(COUNT_BY_GROUP_USERS);
138    
139                            SQLQuery q = session.createSQLQuery(sql);
140    
141                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
142    
143                            QueryPos qPos = QueryPos.getInstance(q);
144    
145                            qPos.add(groupId);
146    
147                            Iterator<Long> itr = q.iterate();
148    
149                            if (itr.hasNext()) {
150                                    Long count = itr.next();
151    
152                                    if (count != null) {
153                                            return count.intValue();
154                                    }
155                            }
156    
157                            return 0;
158                    }
159                    catch (Exception e) {
160                            throw new SystemException(e);
161                    }
162                    finally {
163                            closeSession(session);
164                    }
165            }
166    
167            public int countByOrganizationId(long organizationId)
168                    throws SystemException {
169    
170                    Session session = null;
171    
172                    try {
173                            session = openSession();
174    
175                            String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_ID);
176    
177                            SQLQuery q = session.createSQLQuery(sql);
178    
179                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
180    
181                            QueryPos qPos = QueryPos.getInstance(q);
182    
183                            qPos.add(organizationId);
184    
185                            Iterator<Long> itr = q.iterate();
186    
187                            if (itr.hasNext()) {
188                                    Long count = itr.next();
189    
190                                    if (count != null) {
191                                            return count.intValue();
192                                    }
193                            }
194    
195                            return 0;
196                    }
197                    catch (Exception e) {
198                            throw new SystemException(e);
199                    }
200                    finally {
201                            closeSession(session);
202                    }
203            }
204    
205            public int countByOrganizationUsers(long organizationId)
206                    throws SystemException {
207    
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.createSQLQuery(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            public int countByRelation(long userId) throws SystemException {
244                    Session session = null;
245    
246                    try {
247                            session = openSession();
248    
249                            String sql = CustomSQLUtil.get(COUNT_BY_RELATION);
250    
251                            SQLQuery q = session.createSQLQuery(sql);
252    
253                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
254    
255                            QueryPos qPos = QueryPos.getInstance(q);
256    
257                            qPos.add(userId);
258    
259                            Iterator<Long> itr = q.iterate();
260    
261                            if (itr.hasNext()) {
262                                    Long count = itr.next();
263    
264                                    if (count != null) {
265                                            return count.intValue();
266                                    }
267                            }
268    
269                            return 0;
270                    }
271                    catch (Exception e) {
272                            throw new SystemException(e);
273                    }
274                    finally {
275                            closeSession(session);
276                    }
277            }
278    
279            public int countByRelationType(long userId, int type)
280                    throws SystemException {
281    
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.createSQLQuery(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            public int countByUserGroups(long userId) throws SystemException {
319                    Session session = null;
320    
321                    try {
322                            session = openSession();
323    
324                            String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUPS);
325    
326                            SQLQuery q = session.createSQLQuery(sql);
327    
328                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
329    
330                            QueryPos qPos = QueryPos.getInstance(q);
331    
332                            qPos.add(userId);
333                            qPos.add(userId);
334                            qPos.add(userId);
335    
336                            Iterator<Long> itr = q.iterate();
337    
338                            if (itr.hasNext()) {
339                                    Long count = itr.next();
340    
341                                    if (count != null) {
342                                            return count.intValue();
343                                    }
344                            }
345    
346                            return 0;
347                    }
348                    catch (Exception e) {
349                            throw new SystemException(e);
350                    }
351                    finally {
352                            closeSession(session);
353                    }
354            }
355    
356            public int countByUserGroupsAndOrganizations(long userId)
357                    throws SystemException {
358    
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.createSQLQuery(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            public int countByUserOrganizations(long userId) throws SystemException {
399                    Session session = null;
400    
401                    try {
402                            session = openSession();
403    
404                            String sql = CustomSQLUtil.get(COUNT_BY_USER_ORGANIZATIONS);
405    
406                            SQLQuery q = session.createSQLQuery(sql);
407    
408                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
409    
410                            QueryPos qPos = QueryPos.getInstance(q);
411    
412                            qPos.add(userId);
413    
414                            Iterator<Long> itr = q.iterate();
415    
416                            if (itr.hasNext()) {
417                                    Long count = itr.next();
418    
419                                    if (count != null) {
420                                            return count.intValue();
421                                    }
422                            }
423    
424                            return 0;
425                    }
426                    catch (Exception e) {
427                            throw new SystemException(e);
428                    }
429                    finally {
430                            closeSession(session);
431                    }
432            }
433    
434            public List<SocialActivity> findByGroupId(long groupId, int start, int end)
435                    throws SystemException {
436    
437                    Session session = null;
438    
439                    try {
440                            session = openSession();
441    
442                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
443    
444                            SQLQuery q = session.createSQLQuery(sql);
445    
446                            q.addEntity("SocialActivity", SocialActivityImpl.class);
447    
448                            QueryPos qPos = QueryPos.getInstance(q);
449    
450                            qPos.add(groupId);
451    
452                            return (List<SocialActivity>)QueryUtil.list(
453                                    q, getDialect(), start, end);
454                    }
455                    catch (Exception e) {
456                            throw new SystemException(e);
457                    }
458                    finally {
459                            closeSession(session);
460                    }
461            }
462    
463            public List<SocialActivity> findByGroupUsers(
464                            long groupId, int start, int end)
465                    throws SystemException {
466    
467                    Session session = null;
468    
469                    try {
470                            session = openSession();
471    
472                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_USERS);
473    
474                            SQLQuery q = session.createSQLQuery(sql);
475    
476                            q.addEntity("SocialActivity", SocialActivityImpl.class);
477    
478                            QueryPos qPos = QueryPos.getInstance(q);
479    
480                            qPos.add(groupId);
481    
482                            return (List<SocialActivity>)QueryUtil.list(
483                                    q, getDialect(), start, end);
484                    }
485                    catch (Exception e) {
486                            throw new SystemException(e);
487                    }
488                    finally {
489                            closeSession(session);
490                    }
491            }
492    
493            public List<SocialActivity> findByOrganizationId(
494                            long organizationId, int start, int end)
495                    throws SystemException {
496    
497                    Session session = null;
498    
499                    try {
500                            session = openSession();
501    
502                            String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_ID);
503    
504                            SQLQuery q = session.createSQLQuery(sql);
505    
506                            q.addEntity("SocialActivity", SocialActivityImpl.class);
507    
508                            QueryPos qPos = QueryPos.getInstance(q);
509    
510                            qPos.add(organizationId);
511    
512                            return (List<SocialActivity>)QueryUtil.list(
513                                    q, getDialect(), start, end);
514                    }
515                    catch (Exception e) {
516                            throw new SystemException(e);
517                    }
518                    finally {
519                            closeSession(session);
520                    }
521            }
522    
523            public List<SocialActivity> findByOrganizationUsers(
524                            long organizationId, int start, int end)
525                    throws SystemException {
526    
527                    Session session = null;
528    
529                    try {
530                            session = openSession();
531    
532                            String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_USERS);
533    
534                            SQLQuery q = session.createSQLQuery(sql);
535    
536                            q.addEntity("SocialActivity", SocialActivityImpl.class);
537    
538                            QueryPos qPos = QueryPos.getInstance(q);
539    
540                            qPos.add(organizationId);
541    
542                            return (List<SocialActivity>)QueryUtil.list(
543                                    q, getDialect(), start, end);
544                    }
545                    catch (Exception e) {
546                            throw new SystemException(e);
547                    }
548                    finally {
549                            closeSession(session);
550                    }
551            }
552    
553            public List<SocialActivity> findByRelation(long userId, int start, int end)
554                    throws SystemException {
555    
556                    Session session = null;
557    
558                    try {
559                            session = openSession();
560    
561                            String sql = CustomSQLUtil.get(FIND_BY_RELATION);
562    
563                            SQLQuery q = session.createSQLQuery(sql);
564    
565                            q.addEntity("SocialActivity", SocialActivityImpl.class);
566    
567                            QueryPos qPos = QueryPos.getInstance(q);
568    
569                            qPos.add(userId);
570    
571                            return (List<SocialActivity>)QueryUtil.list(
572                                    q, getDialect(), start, end);
573                    }
574                    catch (Exception e) {
575                            throw new SystemException(e);
576                    }
577                    finally {
578                            closeSession(session);
579                    }
580            }
581    
582            public List<SocialActivity> findByRelationType(
583                            long userId, int type, int start, int end)
584                    throws SystemException {
585    
586                    Session session = null;
587    
588                    try {
589                            session = openSession();
590    
591                            String sql = CustomSQLUtil.get(FIND_BY_RELATION_TYPE);
592    
593                            SQLQuery q = session.createSQLQuery(sql);
594    
595                            q.addEntity("SocialActivity", SocialActivityImpl.class);
596    
597                            QueryPos qPos = QueryPos.getInstance(q);
598    
599                            qPos.add(userId);
600                            qPos.add(type);
601    
602                            return (List<SocialActivity>)QueryUtil.list(
603                                    q, getDialect(), start, end);
604                    }
605                    catch (Exception e) {
606                            throw new SystemException(e);
607                    }
608                    finally {
609                            closeSession(session);
610                    }
611            }
612    
613            public List<SocialActivity> findByUserGroups(
614                            long userId, int start, int end)
615                    throws SystemException {
616    
617                    Session session = null;
618    
619                    try {
620                            session = openSession();
621    
622                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUPS);
623    
624                            SQLQuery q = session.createSQLQuery(sql);
625    
626                            q.addEntity("SocialActivity", SocialActivityImpl.class);
627    
628                            QueryPos qPos = QueryPos.getInstance(q);
629    
630                            qPos.add(userId);
631                            qPos.add(userId);
632                            qPos.add(userId);
633    
634                            return (List<SocialActivity>)QueryUtil.list(
635                                    q, getDialect(), start, end);
636                    }
637                    catch (Exception e) {
638                            throw new SystemException(e);
639                    }
640                    finally {
641                            closeSession(session);
642                    }
643            }
644    
645            public List<SocialActivity> findByUserGroupsAndOrganizations(
646                            long userId, int start, int end)
647                    throws SystemException {
648    
649                    Session session = null;
650    
651                    try {
652                            session = openSession();
653    
654                            String sql = CustomSQLUtil.get(
655                                    FIND_BY_USER_GROUPS_AND_ORGANIZATIONS);
656    
657                            SQLQuery q = session.createSQLQuery(sql);
658    
659                            q.addScalar("activityId", Type.LONG);
660    
661                            QueryPos qPos = QueryPos.getInstance(q);
662    
663                            qPos.add(userId);
664                            qPos.add(userId);
665    
666                            List<SocialActivity> socialActivities =
667                                    new ArrayList<SocialActivity>();
668    
669                            Iterator<Long> itr = (Iterator<Long>)QueryUtil.iterate(
670                                    q, getDialect(), start, end);
671    
672                            while (itr.hasNext()) {
673                                    Long activityId = itr.next();
674    
675                                    SocialActivity socialActivity =
676                                            SocialActivityUtil.findByPrimaryKey(activityId);
677    
678                                    socialActivities.add(socialActivity);
679                            }
680    
681                            return socialActivities;
682                    }
683                    catch (Exception e) {
684                            throw new SystemException(e);
685                    }
686                    finally {
687                            closeSession(session);
688                    }
689            }
690    
691            public List<SocialActivity> findByUserOrganizations(
692                            long userId, int start, int end)
693                    throws SystemException {
694    
695                    Session session = null;
696    
697                    try {
698                            session = openSession();
699    
700                            String sql = CustomSQLUtil.get(FIND_BY_USER_ORGANIZATIONS);
701    
702                            SQLQuery q = session.createSQLQuery(sql);
703    
704                            q.addEntity("SocialActivity", SocialActivityImpl.class);
705    
706                            QueryPos qPos = QueryPos.getInstance(q);
707    
708                            qPos.add(userId);
709    
710                            return (List<SocialActivity>)QueryUtil.list(
711                                    q, getDialect(), start, end);
712                    }
713                    catch (Exception e) {
714                            throw new SystemException(e);
715                    }
716                    finally {
717                            closeSession(session);
718                    }
719            }
720    
721    }