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.cache.MultiVMPoolUtil;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.kernel.dao.orm.QueryPos;
020    import com.liferay.portal.kernel.dao.orm.QueryUtil;
021    import com.liferay.portal.kernel.dao.orm.SQLQuery;
022    import com.liferay.portal.kernel.dao.orm.Session;
023    import com.liferay.portal.kernel.dao.orm.Type;
024    import com.liferay.portal.kernel.exception.SystemException;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.model.User;
030    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portlet.social.model.SocialActivityCounter;
033    import com.liferay.portlet.social.model.impl.SocialActivityCounterImpl;
034    import com.liferay.portlet.social.util.SocialCounterPeriodUtil;
035    import com.liferay.util.dao.orm.CustomSQLUtil;
036    
037    import java.util.ArrayList;
038    import java.util.Iterator;
039    import java.util.List;
040    
041    /**
042     * @author Zsolt Berentey
043     */
044    public class SocialActivityCounterFinderImpl
045            extends BasePersistenceImpl<SocialActivityCounter>
046            implements SocialActivityCounterFinder {
047    
048            public static final String COUNT_U_BY_G_C_N_S_E =
049                    SocialActivityCounterFinder.class.getName() + ".countU_ByG_C_N_S_E";
050    
051            public static final String FIND_AC_BY_G_N_S_E_1 =
052                    SocialActivityCounterFinder.class.getName() + ".findAC_ByG_N_S_E_1";
053    
054            public static final String FIND_AC_BY_G_N_S_E_2 =
055                    SocialActivityCounterFinder.class.getName() + ".findAC_ByG_N_S_E_2";
056    
057            public static final String FIND_AC_BY_G_C_C_N_S_E =
058                    SocialActivityCounterFinder.class.getName() + ".findAC_By_G_C_C_N_S_E";
059    
060            public static final String FIND_U_BY_G_C_N_S_E =
061                    SocialActivityCounterFinder.class.getName() + ".findU_ByG_C_N_S_E";
062    
063            @Override
064            public int countU_ByG_N(long groupId, String[] names)
065                    throws SystemException {
066    
067                    Session session = null;
068    
069                    try {
070                            session = openSession();
071    
072                            String sql = CustomSQLUtil.get(COUNT_U_BY_G_C_N_S_E);
073    
074                            sql = StringUtil.replace(sql, "[$NAME$]", getNames(names));
075    
076                            SQLQuery q = session.createSQLQuery(sql);
077    
078                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
079    
080                            QueryPos qPos = QueryPos.getInstance(q);
081    
082                            qPos.add(groupId);
083                            qPos.add(PortalUtil.getClassNameId(User.class.getName()));
084    
085                            setNames(qPos, names);
086    
087                            qPos.add(SocialCounterPeriodUtil.getPeriodLength());
088                            qPos.add(SocialCounterPeriodUtil.getActivityDay());
089    
090                            Iterator<Long> itr = q.iterate();
091    
092                            if (itr.hasNext()) {
093                                    Long count = itr.next();
094    
095                                    if (count != null) {
096                                            return count.intValue();
097                                    }
098                            }
099    
100                            return 0;
101                    }
102                    catch (Exception e) {
103                            throw new SystemException(e);
104                    }
105                    finally {
106                            closeSession(session);
107                    }
108            }
109    
110            @Override
111            public List<SocialActivityCounter> findAC_ByG_N_S_E_1(
112                            long groupId, String name, int startPeriod, int endPeriod,
113                            int periodLength)
114                    throws SystemException {
115    
116                    StringBundler sb = new StringBundler(9);
117    
118                    sb.append(groupId);
119                    sb.append(StringPool.POUND);
120                    sb.append(name);
121                    sb.append(StringPool.POUND);
122                    sb.append(startPeriod);
123                    sb.append(StringPool.POUND);
124                    sb.append(endPeriod);
125                    sb.append(StringPool.POUND);
126                    sb.append(periodLength);
127    
128                    String key = sb.toString();
129    
130                    List<SocialActivityCounter> activityCounters = null;
131    
132                    if (endPeriod < SocialCounterPeriodUtil.getActivityDay()) {
133                            activityCounters =
134                                    (List<SocialActivityCounter>)_activityCounters.get(key);
135                    }
136    
137                    if (activityCounters != null) {
138                            return activityCounters;
139                    }
140    
141                    Session session = null;
142    
143                    try {
144                            session = openSession();
145    
146                            String sql = CustomSQLUtil.get(FIND_AC_BY_G_N_S_E_1);
147    
148                            SQLQuery q = session.createSQLQuery(sql);
149    
150                            QueryPos qPos = QueryPos.getInstance(q);
151    
152                            qPos.add(groupId);
153                            qPos.add(name);
154                            qPos.add(startPeriod);
155                            qPos.add(endPeriod);
156                            qPos.add(periodLength);
157                            qPos.add(endPeriod);
158    
159                            activityCounters = new ArrayList<SocialActivityCounter>();
160    
161                            Iterator<Object[]> itr = q.iterate();
162    
163                            while (itr.hasNext()) {
164                                    Object[] array = itr.next();
165    
166                                    SocialActivityCounter activityCounter =
167                                            new SocialActivityCounterImpl();
168    
169                                    activityCounter.setName(GetterUtil.getString(array[0]));
170                                    activityCounter.setCurrentValue(
171                                            GetterUtil.getInteger(array[1]));
172                                    activityCounter.setStartPeriod(GetterUtil.getInteger(array[2]));
173                                    activityCounter.setEndPeriod(GetterUtil.getInteger(array[3]));
174    
175                                    activityCounters.add(activityCounter);
176                            }
177                    }
178                    catch (Exception e) {
179                            throw new SystemException(e);
180                    }
181                    finally {
182                            if (activityCounters == null) {
183                                    _activityCounters.remove(key);
184                            }
185                            else {
186                                    if (endPeriod < SocialCounterPeriodUtil.getActivityDay()) {
187                                            _activityCounters.put(key, activityCounters);
188                                    }
189                            }
190    
191                            closeSession(session);
192                    }
193    
194                    return activityCounters;
195            }
196    
197            @Override
198            public List<SocialActivityCounter> findAC_ByG_N_S_E_2(
199                            long groupId, String counterName, int startPeriod, int endPeriod,
200                            int periodLength)
201                    throws SystemException {
202    
203                    Session session = null;
204    
205                    try {
206                            session = openSession();
207    
208                            String sql = CustomSQLUtil.get(FIND_AC_BY_G_N_S_E_2);
209    
210                            SQLQuery q = session.createSQLQuery(sql);
211    
212                            QueryPos qPos = QueryPos.getInstance(q);
213    
214                            qPos.add(groupId);
215                            qPos.add(counterName);
216                            qPos.add(startPeriod);
217                            qPos.add(endPeriod);
218                            qPos.add(periodLength);
219                            qPos.add(endPeriod);
220    
221                            List<SocialActivityCounter> activityCounters =
222                                    new ArrayList<SocialActivityCounter>();
223    
224                            Iterator<Object[]> itr = q.iterate();
225    
226                            while (itr.hasNext()) {
227                                    Object[] array = itr.next();
228    
229                                    SocialActivityCounter activityCounter =
230                                            new SocialActivityCounterImpl();
231    
232                                    activityCounter.setClassNameId(GetterUtil.getLong(array[0]));
233                                    activityCounter.setName(GetterUtil.getString(array[1]));
234                                    activityCounter.setCurrentValue(
235                                            GetterUtil.getInteger(array[2]));
236    
237                                    activityCounters.add(activityCounter);
238                            }
239    
240                            return activityCounters;
241                    }
242                    catch (Exception e) {
243                            throw new SystemException(e);
244                    }
245                    finally {
246                            closeSession(session);
247                    }
248            }
249    
250            @Override
251            public List<SocialActivityCounter> findAC_By_G_C_C_N_S_E(
252                            long groupId, List<Long> userIds, String[] names, int start,
253                            int end)
254                    throws SystemException {
255    
256                    if (names.length == 0) {
257                            return null;
258                    }
259    
260                    Session session = null;
261    
262                    try {
263                            session = openSession();
264    
265                            String sql = CustomSQLUtil.get(FIND_AC_BY_G_C_C_N_S_E);
266    
267                            sql = StringUtil.replace(
268                                    sql, new String[] {"[$CLASS_PK$]", "[$NAME$]"},
269                                    new String[] {StringUtil.merge(userIds), getNames(names)});
270    
271                            SQLQuery q = session.createSQLQuery(sql);
272    
273                            q.addEntity(
274                                    "SocialActivityCounter", SocialActivityCounterImpl.class);
275    
276                            QueryPos qPos = QueryPos.getInstance(q);
277    
278                            qPos.add(groupId);
279                            qPos.add(PortalUtil.getClassNameId(User.class.getName()));
280    
281                            setNames(qPos, names);
282    
283                            return (List<SocialActivityCounter>)QueryUtil.list(
284                                    q, getDialect(), start, end);
285                    }
286                    catch (Exception e) {
287                            throw new SystemException(e);
288                    }
289                    finally {
290                            closeSession(session);
291                    }
292            }
293    
294            @Override
295            public List<Long> findU_ByG_N(
296                            long groupId, String[] names, int start, int end)
297                    throws SystemException {
298    
299                    if (names.length == 0) {
300                            return null;
301                    }
302    
303                    Session session = null;
304    
305                    try {
306                            session = openSession();
307    
308                            String sql = CustomSQLUtil.get(FIND_U_BY_G_C_N_S_E);
309    
310                            sql = StringUtil.replace(sql, "[$NAME$]", getNames(names));
311    
312                            SQLQuery q = session.createSQLQuery(sql);
313    
314                            q.addScalar("classPK", Type.LONG);
315    
316                            QueryPos qPos = QueryPos.getInstance(q);
317    
318                            qPos.add(groupId);
319                            qPos.add(PortalUtil.getClassNameId(User.class.getName()));
320    
321                            setNames(qPos, names);
322    
323                            qPos.add(SocialCounterPeriodUtil.getStartPeriod());
324    
325                            return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
326                    }
327                    catch (Exception e) {
328                            throw new SystemException(e);
329                    }
330                    finally {
331                            closeSession(session);
332                    }
333            }
334    
335            protected String getNames(String[] names) {
336                    if (names.length == 0) {
337                            return StringPool.BLANK;
338                    }
339    
340                    StringBundler sb = new StringBundler(names.length * 2 - 1);
341    
342                    for (int i = 0; i < names.length; i++) {
343                            sb.append(StringPool.QUESTION);
344    
345                            if ((i + 1) < names.length) {
346                                    sb.append(StringPool.COMMA);
347                            }
348                    }
349    
350                    return sb.toString();
351            }
352    
353            protected void setNames(QueryPos qPos, String[] names) {
354                    if ((names != null) && (names.length > 0)) {
355                            for (String name : names) {
356                                    qPos.add(name);
357                            }
358                    }
359            }
360    
361            private static PortalCache _activityCounters = MultiVMPoolUtil.getCache(
362                    SocialActivityCounterFinder.class.getName());
363    
364    }