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