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
036            implements SocialActivitySetFinder {
037    
038            public static final String COUNT_BY_RELATION =
039                    SocialActivitySetFinder.class.getName() + ".countByRelation";
040    
041            public static final String COUNT_BY_RELATION_TYPE =
042                    SocialActivitySetFinder.class.getName() + ".countByRelationType";
043    
044            public static final String COUNT_BY_USER =
045                    SocialActivitySetFinder.class.getName() + ".countByUser";
046    
047            public static final String COUNT_BY_USER_GROUPS =
048                    SocialActivitySetFinder.class.getName() + ".countByUserGroups";
049    
050            public static final String FIND_BY_RELATION =
051                    SocialActivitySetFinder.class.getName() + ".findByRelation";
052    
053            public static final String FIND_BY_RELATION_TYPE =
054                    SocialActivitySetFinder.class.getName() + ".findByRelationType";
055    
056            public static final String FIND_BY_USER =
057                    SocialActivitySetFinder.class.getName() + ".findByUser";
058    
059            public static final String FIND_BY_USER_GROUPS =
060                    SocialActivitySetFinder.class.getName() + ".findByUserGroups";
061    
062            @Override
063            public int countByRelation(long userId) {
064                    Session session = null;
065    
066                    try {
067                            session = openSession();
068    
069                            String sql = CustomSQLUtil.get(COUNT_BY_RELATION);
070    
071                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
072    
073                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
074    
075                            QueryPos qPos = QueryPos.getInstance(q);
076    
077                            qPos.add(userId);
078    
079                            Iterator<Long> itr = q.iterate();
080    
081                            if (itr.hasNext()) {
082                                    Long count = itr.next();
083    
084                                    if (count != null) {
085                                            return count.intValue();
086                                    }
087                            }
088    
089                            return 0;
090                    }
091                    catch (Exception e) {
092                            throw new SystemException(e);
093                    }
094                    finally {
095                            closeSession(session);
096                    }
097            }
098    
099            @Override
100            public int countByRelationType(long userId, int type) {
101                    Session session = null;
102    
103                    try {
104                            session = openSession();
105    
106                            String sql = CustomSQLUtil.get(COUNT_BY_RELATION_TYPE);
107    
108                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
109    
110                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
111    
112                            QueryPos qPos = QueryPos.getInstance(q);
113    
114                            qPos.add(userId);
115                            qPos.add(type);
116    
117                            Iterator<Long> itr = q.iterate();
118    
119                            if (itr.hasNext()) {
120                                    Long count = itr.next();
121    
122                                    if (count != null) {
123                                            return count.intValue();
124                                    }
125                            }
126    
127                            return 0;
128                    }
129                    catch (Exception e) {
130                            throw new SystemException(e);
131                    }
132                    finally {
133                            closeSession(session);
134                    }
135            }
136    
137            @Override
138            public int countByUser(long userId) {
139                    Session session = null;
140    
141                    try {
142                            session = openSession();
143    
144                            String sql = CustomSQLUtil.get(COUNT_BY_USER);
145    
146                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
147    
148                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
149    
150                            QueryPos qPos = QueryPos.getInstance(q);
151    
152                            qPos.add(userId);
153                            qPos.add(userId);
154                            qPos.add(userId);
155                            qPos.add(userId);
156                            qPos.add(userId);
157    
158                            Iterator<Long> itr = q.iterate();
159    
160                            if (itr.hasNext()) {
161                                    Long count = itr.next();
162    
163                                    if (count != null) {
164                                            return count.intValue();
165                                    }
166                            }
167    
168                            return 0;
169                    }
170                    catch (Exception e) {
171                            throw new SystemException(e);
172                    }
173                    finally {
174                            closeSession(session);
175                    }
176            }
177    
178            @Override
179            public int countByUserGroups(long userId) {
180                    Session session = null;
181    
182                    try {
183                            session = openSession();
184    
185                            String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUPS);
186    
187                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
188    
189                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
190    
191                            QueryPos qPos = QueryPos.getInstance(q);
192    
193                            qPos.add(userId);
194                            qPos.add(userId);
195                            qPos.add(userId);
196    
197                            Iterator<Long> itr = q.iterate();
198    
199                            if (itr.hasNext()) {
200                                    Long count = itr.next();
201    
202                                    if (count != null) {
203                                            return count.intValue();
204                                    }
205                            }
206    
207                            return 0;
208                    }
209                    catch (Exception e) {
210                            throw new SystemException(e);
211                    }
212                    finally {
213                            closeSession(session);
214                    }
215            }
216    
217            @Override
218            public List<SocialActivitySet> findByRelation(
219                    long userId, int start, int end) {
220    
221                    Session session = null;
222    
223                    try {
224                            session = openSession();
225    
226                            String sql = CustomSQLUtil.get(FIND_BY_RELATION);
227    
228                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
229    
230                            q.addEntity("SocialActivitySet", SocialActivitySetImpl.class);
231    
232                            QueryPos qPos = QueryPos.getInstance(q);
233    
234                            qPos.add(userId);
235    
236                            return (List<SocialActivitySet>)QueryUtil.list(
237                                    q, getDialect(), start, end);
238                    }
239                    catch (Exception e) {
240                            throw new SystemException(e);
241                    }
242                    finally {
243                            closeSession(session);
244                    }
245            }
246    
247            @Override
248            public List<SocialActivitySet> findByRelationType(
249                    long userId, int type, int start, int end) {
250    
251                    Session session = null;
252    
253                    try {
254                            session = openSession();
255    
256                            String sql = CustomSQLUtil.get(FIND_BY_RELATION_TYPE);
257    
258                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
259    
260                            q.addEntity("SocialActivitySet", SocialActivitySetImpl.class);
261    
262                            QueryPos qPos = QueryPos.getInstance(q);
263    
264                            qPos.add(userId);
265                            qPos.add(type);
266    
267                            return (List<SocialActivitySet>)QueryUtil.list(
268                                    q, getDialect(), start, end);
269                    }
270                    catch (Exception e) {
271                            throw new SystemException(e);
272                    }
273                    finally {
274                            closeSession(session);
275                    }
276            }
277    
278            @Override
279            public List<SocialActivitySet> findByUser(long userId, int start, int end) {
280                    Session session = null;
281    
282                    try {
283                            session = openSession();
284    
285                            String sql = CustomSQLUtil.get(FIND_BY_USER);
286    
287                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
288    
289                            q.addEntity("SocialActivitySet", SocialActivitySetImpl.class);
290    
291                            QueryPos qPos = QueryPos.getInstance(q);
292    
293                            qPos.add(userId);
294                            qPos.add(userId);
295                            qPos.add(userId);
296                            qPos.add(userId);
297                            qPos.add(userId);
298    
299                            return (List<SocialActivitySet>)QueryUtil.list(
300                                    q, getDialect(), start, end);
301                    }
302                    catch (Exception e) {
303                            throw new SystemException(e);
304                    }
305                    finally {
306                            closeSession(session);
307                    }
308            }
309    
310            @Override
311            public List<SocialActivitySet> findByUserGroups(
312                    long userId, int start, int end) {
313    
314                    Session session = null;
315    
316                    try {
317                            session = openSession();
318    
319                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUPS);
320    
321                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
322    
323                            q.addEntity("SocialActivitySet", SocialActivitySetImpl.class);
324    
325                            QueryPos qPos = QueryPos.getInstance(q);
326    
327                            qPos.add(userId);
328                            qPos.add(userId);
329                            qPos.add(userId);
330    
331                            return (List<SocialActivitySet>)QueryUtil.list(
332                                    q, getDialect(), start, end);
333                    }
334                    catch (Exception e) {
335                            throw new SystemException(e);
336                    }
337                    finally {
338                            closeSession(session);
339                    }
340            }
341    
342    }