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