001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.social.service.persistence.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portlet.social.model.SocialActivitySet;
024    import com.liferay.portlet.social.model.impl.SocialActivitySetImpl;
025    import com.liferay.portlet.social.service.persistence.SocialActivitySetFinder;
026    import com.liferay.util.dao.orm.CustomSQLUtil;
027    
028    import java.util.Iterator;
029    import java.util.List;
030    
031    /**
032     * @author Jonathan Lee
033     */
034    public class SocialActivitySetFinderImpl
035            extends SocialActivitySetFinderBaseImpl implements SocialActivitySetFinder {
036    
037            public static final String COUNT_BY_RELATION =
038                    SocialActivitySetFinder.class.getName() + ".countByRelation";
039    
040            public static final String COUNT_BY_RELATION_TYPE =
041                    SocialActivitySetFinder.class.getName() + ".countByRelationType";
042    
043            public static final String COUNT_BY_USER =
044                    SocialActivitySetFinder.class.getName() + ".countByUser";
045    
046            public static final String COUNT_BY_USER_GROUPS =
047                    SocialActivitySetFinder.class.getName() + ".countByUserGroups";
048    
049            public static final String FIND_BY_RELATION =
050                    SocialActivitySetFinder.class.getName() + ".findByRelation";
051    
052            public static final String FIND_BY_RELATION_TYPE =
053                    SocialActivitySetFinder.class.getName() + ".findByRelationType";
054    
055            public static final String FIND_BY_USER =
056                    SocialActivitySetFinder.class.getName() + ".findByUser";
057    
058            public static final String FIND_BY_USER_GROUPS =
059                    SocialActivitySetFinder.class.getName() + ".findByUserGroups";
060    
061            @Override
062            public int countByRelation(long userId) {
063                    Session session = null;
064    
065                    try {
066                            session = openSession();
067    
068                            String sql = CustomSQLUtil.get(COUNT_BY_RELATION);
069    
070                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
071    
072                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
073    
074                            QueryPos qPos = QueryPos.getInstance(q);
075    
076                            qPos.add(userId);
077    
078                            Iterator<Long> itr = q.iterate();
079    
080                            if (itr.hasNext()) {
081                                    Long count = itr.next();
082    
083                                    if (count != null) {
084                                            return count.intValue();
085                                    }
086                            }
087    
088                            return 0;
089                    }
090                    catch (Exception e) {
091                            throw new SystemException(e);
092                    }
093                    finally {
094                            closeSession(session);
095                    }
096            }
097    
098            @Override
099            public int countByRelationType(long userId, int type) {
100                    Session session = null;
101    
102                    try {
103                            session = openSession();
104    
105                            String sql = CustomSQLUtil.get(COUNT_BY_RELATION_TYPE);
106    
107                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
108    
109                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
110    
111                            QueryPos qPos = QueryPos.getInstance(q);
112    
113                            qPos.add(userId);
114                            qPos.add(type);
115    
116                            Iterator<Long> itr = q.iterate();
117    
118                            if (itr.hasNext()) {
119                                    Long count = itr.next();
120    
121                                    if (count != null) {
122                                            return count.intValue();
123                                    }
124                            }
125    
126                            return 0;
127                    }
128                    catch (Exception e) {
129                            throw new SystemException(e);
130                    }
131                    finally {
132                            closeSession(session);
133                    }
134            }
135    
136            @Override
137            public int countByUser(long userId) {
138                    Session session = null;
139    
140                    try {
141                            session = openSession();
142    
143                            String sql = CustomSQLUtil.get(COUNT_BY_USER);
144    
145                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
146    
147                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
148    
149                            QueryPos qPos = QueryPos.getInstance(q);
150    
151                            qPos.add(userId);
152                            qPos.add(userId);
153                            qPos.add(userId);
154                            qPos.add(userId);
155                            qPos.add(userId);
156    
157                            Iterator<Long> itr = q.iterate();
158    
159                            if (itr.hasNext()) {
160                                    Long count = itr.next();
161    
162                                    if (count != null) {
163                                            return count.intValue();
164                                    }
165                            }
166    
167                            return 0;
168                    }
169                    catch (Exception e) {
170                            throw new SystemException(e);
171                    }
172                    finally {
173                            closeSession(session);
174                    }
175            }
176    
177            @Override
178            public int countByUserGroups(long userId) {
179                    Session session = null;
180    
181                    try {
182                            session = openSession();
183    
184                            String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUPS);
185    
186                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
187    
188                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
189    
190                            QueryPos qPos = QueryPos.getInstance(q);
191    
192                            qPos.add(userId);
193                            qPos.add(userId);
194                            qPos.add(userId);
195    
196                            Iterator<Long> itr = q.iterate();
197    
198                            if (itr.hasNext()) {
199                                    Long count = itr.next();
200    
201                                    if (count != null) {
202                                            return count.intValue();
203                                    }
204                            }
205    
206                            return 0;
207                    }
208                    catch (Exception e) {
209                            throw new SystemException(e);
210                    }
211                    finally {
212                            closeSession(session);
213                    }
214            }
215    
216            @Override
217            public List<SocialActivitySet> findByRelation(
218                    long userId, int start, int end) {
219    
220                    Session session = null;
221    
222                    try {
223                            session = openSession();
224    
225                            String sql = CustomSQLUtil.get(FIND_BY_RELATION);
226    
227                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
228    
229                            q.addEntity("SocialActivitySet", SocialActivitySetImpl.class);
230    
231                            QueryPos qPos = QueryPos.getInstance(q);
232    
233                            qPos.add(userId);
234    
235                            return (List<SocialActivitySet>)QueryUtil.list(
236                                    q, getDialect(), start, end);
237                    }
238                    catch (Exception e) {
239                            throw new SystemException(e);
240                    }
241                    finally {
242                            closeSession(session);
243                    }
244            }
245    
246            @Override
247            public List<SocialActivitySet> findByRelationType(
248                    long userId, int type, int start, int end) {
249    
250                    Session session = null;
251    
252                    try {
253                            session = openSession();
254    
255                            String sql = CustomSQLUtil.get(FIND_BY_RELATION_TYPE);
256    
257                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
258    
259                            q.addEntity("SocialActivitySet", SocialActivitySetImpl.class);
260    
261                            QueryPos qPos = QueryPos.getInstance(q);
262    
263                            qPos.add(userId);
264                            qPos.add(type);
265    
266                            return (List<SocialActivitySet>)QueryUtil.list(
267                                    q, getDialect(), start, end);
268                    }
269                    catch (Exception e) {
270                            throw new SystemException(e);
271                    }
272                    finally {
273                            closeSession(session);
274                    }
275            }
276    
277            @Override
278            public List<SocialActivitySet> findByUser(long userId, int start, int end) {
279                    Session session = null;
280    
281                    try {
282                            session = openSession();
283    
284                            String sql = CustomSQLUtil.get(FIND_BY_USER);
285    
286                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
287    
288                            q.addEntity("SocialActivitySet", SocialActivitySetImpl.class);
289    
290                            QueryPos qPos = QueryPos.getInstance(q);
291    
292                            qPos.add(userId);
293                            qPos.add(userId);
294                            qPos.add(userId);
295                            qPos.add(userId);
296                            qPos.add(userId);
297    
298                            return (List<SocialActivitySet>)QueryUtil.list(
299                                    q, getDialect(), start, end);
300                    }
301                    catch (Exception e) {
302                            throw new SystemException(e);
303                    }
304                    finally {
305                            closeSession(session);
306                    }
307            }
308    
309            @Override
310            public List<SocialActivitySet> findByUserGroups(
311                    long userId, int start, int end) {
312    
313                    Session session = null;
314    
315                    try {
316                            session = openSession();
317    
318                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUPS);
319    
320                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
321    
322                            q.addEntity("SocialActivitySet", SocialActivitySetImpl.class);
323    
324                            QueryPos qPos = QueryPos.getInstance(q);
325    
326                            qPos.add(userId);
327                            qPos.add(userId);
328                            qPos.add(userId);
329    
330                            return (List<SocialActivitySet>)QueryUtil.list(
331                                    q, getDialect(), start, end);
332                    }
333                    catch (Exception e) {
334                            throw new SystemException(e);
335                    }
336                    finally {
337                            closeSession(session);
338                    }
339            }
340    
341    }