001    /**
002     * Copyright (c) 2000-2010 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 String COUNT_BY_GROUP_ID =
040                    SocialActivityFinder.class.getName() + ".countByGroupId";
041    
042            public static String COUNT_BY_GROUP_USERS =
043                    SocialActivityFinder.class.getName() + ".countByGroupUsers";
044    
045            public static String COUNT_BY_ORGANIZATION_ID =
046                    SocialActivityFinder.class.getName() + ".countByOrganizationId";
047    
048            public static String COUNT_BY_ORGANIZATION_USERS =
049                    SocialActivityFinder.class.getName() + ".countByOrganizationUsers";
050    
051            public static String COUNT_BY_RELATION =
052                    SocialActivityFinder.class.getName() + ".countByRelation";
053    
054            public static String COUNT_BY_RELATION_TYPE =
055                    SocialActivityFinder.class.getName() + ".countByRelationType";
056    
057            public static String COUNT_BY_USER_GROUPS =
058                    SocialActivityFinder.class.getName() + ".countByUserGroups";
059    
060            public static String COUNT_BY_USER_GROUPS_AND_ORGANIZATIONS =
061                    SocialActivityFinder.class.getName() +
062                            ".countByUserGroupsAndOrganizations";
063    
064            public static String COUNT_BY_USER_ORGANIZATIONS =
065                    SocialActivityFinder.class.getName() + ".countByUserOrganizations";
066    
067            public static String FIND_BY_GROUP_ID =
068                    SocialActivityFinder.class.getName() + ".findByGroupId";
069    
070            public static String FIND_BY_GROUP_USERS =
071                    SocialActivityFinder.class.getName() + ".findByGroupUsers";
072    
073            public static String FIND_BY_ORGANIZATION_ID =
074                    SocialActivityFinder.class.getName() + ".findByOrganizationId";
075    
076            public static String FIND_BY_ORGANIZATION_USERS =
077                    SocialActivityFinder.class.getName() + ".findByOrganizationUsers";
078    
079            public static String FIND_BY_RELATION =
080                    SocialActivityFinder.class.getName() + ".findByRelation";
081    
082            public static String FIND_BY_RELATION_TYPE =
083                    SocialActivityFinder.class.getName() + ".findByRelationType";
084    
085            public static String FIND_BY_USER_GROUPS =
086                    SocialActivityFinder.class.getName() + ".findByUserGroups";
087    
088            public static String FIND_BY_USER_GROUPS_AND_ORGANIZATIONS =
089                    SocialActivityFinder.class.getName() +
090                            ".findByUserGroupsAndOrganizations";
091    
092            public static 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.list().iterator();
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.list().iterator();
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.list().iterator();
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.list().iterator();
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.list().iterator();
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.list().iterator();
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    
334                            Iterator<Long> itr = q.list().iterator();
335    
336                            if (itr.hasNext()) {
337                                    Long count = itr.next();
338    
339                                    if (count != null) {
340                                            return count.intValue();
341                                    }
342                            }
343    
344                            return 0;
345                    }
346                    catch (Exception e) {
347                            throw new SystemException(e);
348                    }
349                    finally {
350                            closeSession(session);
351                    }
352            }
353    
354            public int countByUserGroupsAndOrganizations(long userId)
355                    throws SystemException {
356    
357                    Session session = null;
358    
359                    try {
360                            session = openSession();
361    
362                            String sql = CustomSQLUtil.get(
363                                    COUNT_BY_USER_GROUPS_AND_ORGANIZATIONS);
364    
365                            SQLQuery q = session.createSQLQuery(sql);
366    
367                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
368    
369                            QueryPos qPos = QueryPos.getInstance(q);
370    
371                            qPos.add(userId);
372                            qPos.add(userId);
373    
374                            int count = 0;
375    
376                            Iterator<Long> itr = q.list().iterator();
377    
378                            while (itr.hasNext()) {
379                                    Long l = itr.next();
380    
381                                    if (l != null) {
382                                            count += l.intValue();
383                                    }
384                            }
385    
386                            return count;
387                    }
388                    catch (Exception e) {
389                            throw new SystemException(e);
390                    }
391                    finally {
392                            closeSession(session);
393                    }
394            }
395    
396            public int countByUserOrganizations(long userId) throws SystemException {
397                    Session session = null;
398    
399                    try {
400                            session = openSession();
401    
402                            String sql = CustomSQLUtil.get(COUNT_BY_USER_ORGANIZATIONS);
403    
404                            SQLQuery q = session.createSQLQuery(sql);
405    
406                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
407    
408                            QueryPos qPos = QueryPos.getInstance(q);
409    
410                            qPos.add(userId);
411    
412                            Iterator<Long> itr = q.list().iterator();
413    
414                            if (itr.hasNext()) {
415                                    Long count = itr.next();
416    
417                                    if (count != null) {
418                                            return count.intValue();
419                                    }
420                            }
421    
422                            return 0;
423                    }
424                    catch (Exception e) {
425                            throw new SystemException(e);
426                    }
427                    finally {
428                            closeSession(session);
429                    }
430            }
431    
432            public List<SocialActivity> findByGroupId(long groupId, int start, int end)
433                    throws SystemException {
434    
435                    Session session = null;
436    
437                    try {
438                            session = openSession();
439    
440                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
441    
442                            SQLQuery q = session.createSQLQuery(sql);
443    
444                            q.addEntity("SocialActivity", SocialActivityImpl.class);
445    
446                            QueryPos qPos = QueryPos.getInstance(q);
447    
448                            qPos.add(groupId);
449    
450                            return (List<SocialActivity>)QueryUtil.list(
451                                    q, getDialect(), start, end);
452                    }
453                    catch (Exception e) {
454                            throw new SystemException(e);
455                    }
456                    finally {
457                            closeSession(session);
458                    }
459            }
460    
461            public List<SocialActivity> findByGroupUsers(
462                            long groupId, int start, int end)
463                    throws SystemException {
464    
465                    Session session = null;
466    
467                    try {
468                            session = openSession();
469    
470                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_USERS);
471    
472                            SQLQuery q = session.createSQLQuery(sql);
473    
474                            q.addEntity("SocialActivity", SocialActivityImpl.class);
475    
476                            QueryPos qPos = QueryPos.getInstance(q);
477    
478                            qPos.add(groupId);
479    
480                            return (List<SocialActivity>)QueryUtil.list(
481                                    q, getDialect(), start, end);
482                    }
483                    catch (Exception e) {
484                            throw new SystemException(e);
485                    }
486                    finally {
487                            closeSession(session);
488                    }
489            }
490    
491            public List<SocialActivity> findByOrganizationId(
492                            long organizationId, int start, int end)
493                    throws SystemException {
494    
495                    Session session = null;
496    
497                    try {
498                            session = openSession();
499    
500                            String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_ID);
501    
502                            SQLQuery q = session.createSQLQuery(sql);
503    
504                            q.addEntity("SocialActivity", SocialActivityImpl.class);
505    
506                            QueryPos qPos = QueryPos.getInstance(q);
507    
508                            qPos.add(organizationId);
509    
510                            return (List<SocialActivity>)QueryUtil.list(
511                                    q, getDialect(), start, end);
512                    }
513                    catch (Exception e) {
514                            throw new SystemException(e);
515                    }
516                    finally {
517                            closeSession(session);
518                    }
519            }
520    
521            public List<SocialActivity> findByOrganizationUsers(
522                            long organizationId, int start, int end)
523                    throws SystemException {
524    
525                    Session session = null;
526    
527                    try {
528                            session = openSession();
529    
530                            String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_USERS);
531    
532                            SQLQuery q = session.createSQLQuery(sql);
533    
534                            q.addEntity("SocialActivity", SocialActivityImpl.class);
535    
536                            QueryPos qPos = QueryPos.getInstance(q);
537    
538                            qPos.add(organizationId);
539    
540                            return (List<SocialActivity>)QueryUtil.list(
541                                    q, getDialect(), start, end);
542                    }
543                    catch (Exception e) {
544                            throw new SystemException(e);
545                    }
546                    finally {
547                            closeSession(session);
548                    }
549            }
550    
551            public List<SocialActivity> findByRelation(long userId, int start, int end)
552                    throws SystemException {
553    
554                    Session session = null;
555    
556                    try {
557                            session = openSession();
558    
559                            String sql = CustomSQLUtil.get(FIND_BY_RELATION);
560    
561                            SQLQuery q = session.createSQLQuery(sql);
562    
563                            q.addEntity("SocialActivity", SocialActivityImpl.class);
564    
565                            QueryPos qPos = QueryPos.getInstance(q);
566    
567                            qPos.add(userId);
568    
569                            return (List<SocialActivity>)QueryUtil.list(
570                                    q, getDialect(), start, end);
571                    }
572                    catch (Exception e) {
573                            throw new SystemException(e);
574                    }
575                    finally {
576                            closeSession(session);
577                    }
578            }
579    
580            public List<SocialActivity> findByRelationType(
581                            long userId, int type, int start, int end)
582                    throws SystemException {
583    
584                    Session session = null;
585    
586                    try {
587                            session = openSession();
588    
589                            String sql = CustomSQLUtil.get(FIND_BY_RELATION_TYPE);
590    
591                            SQLQuery q = session.createSQLQuery(sql);
592    
593                            q.addEntity("SocialActivity", SocialActivityImpl.class);
594    
595                            QueryPos qPos = QueryPos.getInstance(q);
596    
597                            qPos.add(userId);
598                            qPos.add(type);
599    
600                            return (List<SocialActivity>)QueryUtil.list(
601                                    q, getDialect(), start, end);
602                    }
603                    catch (Exception e) {
604                            throw new SystemException(e);
605                    }
606                    finally {
607                            closeSession(session);
608                    }
609            }
610    
611            public List<SocialActivity> findByUserGroups(
612                            long userId, int start, int end)
613                    throws SystemException {
614    
615                    Session session = null;
616    
617                    try {
618                            session = openSession();
619    
620                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUPS);
621    
622                            SQLQuery q = session.createSQLQuery(sql);
623    
624                            q.addEntity("SocialActivity", SocialActivityImpl.class);
625    
626                            QueryPos qPos = QueryPos.getInstance(q);
627    
628                            qPos.add(userId);
629    
630                            return (List<SocialActivity>)QueryUtil.list(
631                                    q, getDialect(), start, end);
632                    }
633                    catch (Exception e) {
634                            throw new SystemException(e);
635                    }
636                    finally {
637                            closeSession(session);
638                    }
639            }
640    
641            public List<SocialActivity> findByUserGroupsAndOrganizations(
642                            long userId, int start, int end)
643                    throws SystemException {
644    
645                    Session session = null;
646    
647                    try {
648                            session = openSession();
649    
650                            String sql = CustomSQLUtil.get(
651                                    FIND_BY_USER_GROUPS_AND_ORGANIZATIONS);
652    
653                            SQLQuery q = session.createSQLQuery(sql);
654    
655                            q.addScalar("activityId", Type.LONG);
656    
657                            QueryPos qPos = QueryPos.getInstance(q);
658    
659                            qPos.add(userId);
660                            qPos.add(userId);
661    
662                            List<SocialActivity> socialActivities =
663                                    new ArrayList<SocialActivity>();
664    
665                            Iterator<Long> itr = (Iterator<Long>)QueryUtil.iterate(
666                                    q, getDialect(), start, end);
667    
668                            while (itr.hasNext()) {
669                                    Long activityId = itr.next();
670    
671                                    SocialActivity socialActivity =
672                                            SocialActivityUtil.findByPrimaryKey(activityId);
673    
674                                    socialActivities.add(socialActivity);
675                            }
676    
677                            return socialActivities;
678                    }
679                    catch (Exception e) {
680                            throw new SystemException(e);
681                    }
682                    finally {
683                            closeSession(session);
684                    }
685            }
686    
687            public List<SocialActivity> findByUserOrganizations(
688                            long userId, int start, int end)
689                    throws SystemException {
690    
691                    Session session = null;
692    
693                    try {
694                            session = openSession();
695    
696                            String sql = CustomSQLUtil.get(FIND_BY_USER_ORGANIZATIONS);
697    
698                            SQLQuery q = session.createSQLQuery(sql);
699    
700                            q.addEntity("SocialActivity", SocialActivityImpl.class);
701    
702                            QueryPos qPos = QueryPos.getInstance(q);
703    
704                            qPos.add(userId);
705    
706                            return (List<SocialActivity>)QueryUtil.list(
707                                    q, getDialect(), start, end);
708                    }
709                    catch (Exception e) {
710                            throw new SystemException(e);
711                    }
712                    finally {
713                            closeSession(session);
714                    }
715            }
716    
717    }