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.SocialActivitySet;
025    import com.liferay.portlet.social.model.impl.SocialActivitySetImpl;
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 BasePersistenceImpl<SocialActivitySet>
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) throws SystemException {
064                    Session session = null;
065    
066                    try {
067                            session = openSession();
068    
069                            String sql = CustomSQLUtil.get(COUNT_BY_RELATION);
070    
071                            SQLQuery q = session.createSQLQuery(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                    throws SystemException {
102    
103                    Session session = null;
104    
105                    try {
106                            session = openSession();
107    
108                            String sql = CustomSQLUtil.get(COUNT_BY_RELATION_TYPE);
109    
110                            SQLQuery q = session.createSQLQuery(sql);
111    
112                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
113    
114                            QueryPos qPos = QueryPos.getInstance(q);
115    
116                            qPos.add(userId);
117                            qPos.add(type);
118    
119                            Iterator<Long> itr = q.iterate();
120    
121                            if (itr.hasNext()) {
122                                    Long count = itr.next();
123    
124                                    if (count != null) {
125                                            return count.intValue();
126                                    }
127                            }
128    
129                            return 0;
130                    }
131                    catch (Exception e) {
132                            throw new SystemException(e);
133                    }
134                    finally {
135                            closeSession(session);
136                    }
137            }
138    
139            @Override
140            public int countByUser(long userId) throws SystemException {
141                    Session session = null;
142    
143                    try {
144                            session = openSession();
145    
146                            String sql = CustomSQLUtil.get(COUNT_BY_USER);
147    
148                            SQLQuery q = session.createSQLQuery(sql);
149    
150                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
151    
152                            QueryPos qPos = QueryPos.getInstance(q);
153    
154                            qPos.add(userId);
155                            qPos.add(userId);
156                            qPos.add(userId);
157                            qPos.add(userId);
158                            qPos.add(userId);
159    
160                            Iterator<Long> itr = q.iterate();
161    
162                            if (itr.hasNext()) {
163                                    Long count = itr.next();
164    
165                                    if (count != null) {
166                                            return count.intValue();
167                                    }
168                            }
169    
170                            return 0;
171                    }
172                    catch (Exception e) {
173                            throw new SystemException(e);
174                    }
175                    finally {
176                            closeSession(session);
177                    }
178            }
179    
180            @Override
181            public int countByUserGroups(long userId) throws SystemException {
182                    Session session = null;
183    
184                    try {
185                            session = openSession();
186    
187                            String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUPS);
188    
189                            SQLQuery q = session.createSQLQuery(sql);
190    
191                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
192    
193                            QueryPos qPos = QueryPos.getInstance(q);
194    
195                            qPos.add(userId);
196                            qPos.add(userId);
197                            qPos.add(userId);
198    
199                            Iterator<Long> itr = q.iterate();
200    
201                            if (itr.hasNext()) {
202                                    Long count = itr.next();
203    
204                                    if (count != null) {
205                                            return count.intValue();
206                                    }
207                            }
208    
209                            return 0;
210                    }
211                    catch (Exception e) {
212                            throw new SystemException(e);
213                    }
214                    finally {
215                            closeSession(session);
216                    }
217            }
218    
219            @Override
220            public List<SocialActivitySet> findByRelation(
221                            long userId, int start, int end)
222                    throws SystemException {
223    
224                    Session session = null;
225    
226                    try {
227                            session = openSession();
228    
229                            String sql = CustomSQLUtil.get(FIND_BY_RELATION);
230    
231                            SQLQuery q = session.createSQLQuery(sql);
232    
233                            q.addEntity("SocialActivitySet", SocialActivitySetImpl.class);
234    
235                            QueryPos qPos = QueryPos.getInstance(q);
236    
237                            qPos.add(userId);
238    
239                            return (List<SocialActivitySet>)QueryUtil.list(
240                                    q, getDialect(), start, end);
241                    }
242                    catch (Exception e) {
243                            throw new SystemException(e);
244                    }
245                    finally {
246                            closeSession(session);
247                    }
248            }
249    
250            @Override
251            public List<SocialActivitySet> findByRelationType(
252                            long userId, int type, int start, int end)
253                    throws SystemException {
254    
255                    Session session = null;
256    
257                    try {
258                            session = openSession();
259    
260                            String sql = CustomSQLUtil.get(FIND_BY_RELATION_TYPE);
261    
262                            SQLQuery q = session.createSQLQuery(sql);
263    
264                            q.addEntity("SocialActivitySet", SocialActivitySetImpl.class);
265    
266                            QueryPos qPos = QueryPos.getInstance(q);
267    
268                            qPos.add(userId);
269                            qPos.add(type);
270    
271                            return (List<SocialActivitySet>)QueryUtil.list(
272                                    q, getDialect(), start, end);
273                    }
274                    catch (Exception e) {
275                            throw new SystemException(e);
276                    }
277                    finally {
278                            closeSession(session);
279                    }
280            }
281    
282            @Override
283            public List<SocialActivitySet> findByUser(long userId, int start, int end)
284                    throws SystemException {
285    
286                    Session session = null;
287    
288                    try {
289                            session = openSession();
290    
291                            String sql = CustomSQLUtil.get(FIND_BY_USER);
292    
293                            SQLQuery q = session.createSQLQuery(sql);
294    
295                            q.addEntity("SocialActivitySet", SocialActivitySetImpl.class);
296    
297                            QueryPos qPos = QueryPos.getInstance(q);
298    
299                            qPos.add(userId);
300                            qPos.add(userId);
301                            qPos.add(userId);
302                            qPos.add(userId);
303                            qPos.add(userId);
304    
305                            return (List<SocialActivitySet>)QueryUtil.list(
306                                    q, getDialect(), start, end);
307                    }
308                    catch (Exception e) {
309                            throw new SystemException(e);
310                    }
311                    finally {
312                            closeSession(session);
313                    }
314            }
315    
316            @Override
317            public List<SocialActivitySet> findByUserGroups(
318                            long userId, int start, int end)
319                    throws SystemException {
320    
321                    Session session = null;
322    
323                    try {
324                            session = openSession();
325    
326                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUPS);
327    
328                            SQLQuery q = session.createSQLQuery(sql);
329    
330                            q.addEntity("SocialActivitySet", SocialActivitySetImpl.class);
331    
332                            QueryPos qPos = QueryPos.getInstance(q);
333    
334                            qPos.add(userId);
335                            qPos.add(userId);
336                            qPos.add(userId);
337    
338                            return (List<SocialActivitySet>)QueryUtil.list(
339                                    q, getDialect(), start, end);
340                    }
341                    catch (Exception e) {
342                            throw new SystemException(e);
343                    }
344                    finally {
345                            closeSession(session);
346                    }
347            }
348    
349    }