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.io.Serializable;
038    
039    import java.util.ArrayList;
040    import java.util.Iterator;
041    import java.util.List;
042    
043    /**
044     * @author Zsolt Berentey
045     */
046    public class SocialActivityCounterFinderImpl
047            extends BasePersistenceImpl<SocialActivityCounter>
048            implements SocialActivityCounterFinder {
049    
050            public static final String COUNT_U_BY_G_C_N_S_E =
051                    SocialActivityCounterFinder.class.getName() + ".countU_ByG_C_N_S_E";
052    
053            public static final String FIND_AC_BY_G_N_S_E_1 =
054                    SocialActivityCounterFinder.class.getName() + ".findAC_ByG_N_S_E_1";
055    
056            public static final String FIND_AC_BY_G_N_S_E_2 =
057                    SocialActivityCounterFinder.class.getName() + ".findAC_ByG_N_S_E_2";
058    
059            public static final String FIND_AC_BY_G_C_C_N_S_E =
060                    SocialActivityCounterFinder.class.getName() + ".findAC_By_G_C_C_N_S_E";
061    
062            public static final String FIND_U_BY_G_C_N_S_E =
063                    SocialActivityCounterFinder.class.getName() + ".findU_ByG_C_N_S_E";
064    
065            public int countU_ByG_N(long groupId, String[] names)
066                    throws SystemException {
067    
068                    Session session = null;
069    
070                    try {
071                            session = openSession();
072    
073                            String sql = CustomSQLUtil.get(COUNT_U_BY_G_C_N_S_E);
074    
075                            sql = StringUtil.replace(sql, "[$NAME$]", getNames(names));
076    
077                            SQLQuery q = session.createSQLQuery(sql);
078    
079                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
080    
081                            QueryPos qPos = QueryPos.getInstance(q);
082    
083                            qPos.add(groupId);
084                            qPos.add(PortalUtil.getClassNameId(User.class.getName()));
085    
086                            setNames(qPos, names);
087    
088                            qPos.add(SocialCounterPeriodUtil.getPeriodLength());
089                            qPos.add(SocialCounterPeriodUtil.getActivityDay());
090    
091                            Iterator<Long> itr = q.iterate();
092    
093                            if (itr.hasNext()) {
094                                    Long count = itr.next();
095    
096                                    if (count != null) {
097                                            return count.intValue();
098                                    }
099                            }
100    
101                            return 0;
102                    }
103                    catch (Exception e) {
104                            throw new SystemException(e);
105                    }
106                    finally {
107                            closeSession(session);
108                    }
109            }
110    
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, (Serializable)activityCounters);
188                                    }
189                            }
190    
191                            closeSession(session);
192                    }
193    
194                    return activityCounters;
195            }
196    
197            public List<SocialActivityCounter> findAC_ByG_N_S_E_2(
198                            long groupId, String counterName, int startPeriod, int endPeriod,
199                            int periodLength)
200                    throws SystemException {
201    
202                    Session session = null;
203    
204                    try {
205                            session = openSession();
206    
207                            String sql = CustomSQLUtil.get(FIND_AC_BY_G_N_S_E_2);
208    
209                            SQLQuery q = session.createSQLQuery(sql);
210    
211                            QueryPos qPos = QueryPos.getInstance(q);
212    
213                            qPos.add(groupId);
214                            qPos.add(counterName);
215                            qPos.add(startPeriod);
216                            qPos.add(endPeriod);
217                            qPos.add(periodLength);
218                            qPos.add(endPeriod);
219    
220                            List<SocialActivityCounter> activityCounters =
221                                    new ArrayList<SocialActivityCounter>();
222    
223                            Iterator<Object[]> itr = q.iterate();
224    
225                            while (itr.hasNext()) {
226                                    Object[] array = itr.next();
227    
228                                    SocialActivityCounter activityCounter =
229                                            new SocialActivityCounterImpl();
230    
231                                    activityCounter.setClassNameId(GetterUtil.getLong(array[0]));
232                                    activityCounter.setName(GetterUtil.getString(array[1]));
233                                    activityCounter.setCurrentValue(
234                                            GetterUtil.getInteger(array[2]));
235    
236                                    activityCounters.add(activityCounter);
237                            }
238    
239                            return activityCounters;
240                    }
241                    catch (Exception e) {
242                            throw new SystemException(e);
243                    }
244                    finally {
245                            closeSession(session);
246                    }
247            }
248    
249            public List<SocialActivityCounter> findAC_By_G_C_C_N_S_E(
250                            long groupId, List<Long> userIds, String[] names, int start,
251                            int end)
252                    throws SystemException {
253    
254                    if (names.length == 0) {
255                            return null;
256                    }
257    
258                    Session session = null;
259    
260                    try {
261                            session = openSession();
262    
263                            String sql = CustomSQLUtil.get(FIND_AC_BY_G_C_C_N_S_E);
264    
265                            sql = StringUtil.replace(
266                                    sql, new String[] {"[$CLASS_PK$]", "[$NAME$]"},
267                                    new String[] {StringUtil.merge(userIds), getNames(names)});
268    
269                            SQLQuery q = session.createSQLQuery(sql);
270    
271                            q.addEntity(
272                                    "SocialActivityCounter", SocialActivityCounterImpl.class);
273    
274                            QueryPos qPos = QueryPos.getInstance(q);
275    
276                            qPos.add(groupId);
277                            qPos.add(PortalUtil.getClassNameId(User.class.getName()));
278    
279                            setNames(qPos, names);
280    
281                            return (List<SocialActivityCounter>)QueryUtil.list(
282                                    q, getDialect(), start, end);
283                    }
284                    catch (Exception e) {
285                            throw new SystemException(e);
286                    }
287                    finally {
288                            closeSession(session);
289                    }
290            }
291    
292            public List<Long> findU_ByG_N(
293                            long groupId, String[] names, int start, int end)
294                    throws SystemException {
295    
296                    if (names.length == 0) {
297                            return null;
298                    }
299    
300                    Session session = null;
301    
302                    try {
303                            session = openSession();
304    
305                            String sql = CustomSQLUtil.get(FIND_U_BY_G_C_N_S_E);
306    
307                            sql = StringUtil.replace(sql, "[$NAME$]", getNames(names));
308    
309                            SQLQuery q = session.createSQLQuery(sql);
310    
311                            q.addScalar("classPK", Type.LONG);
312    
313                            QueryPos qPos = QueryPos.getInstance(q);
314    
315                            qPos.add(groupId);
316                            qPos.add(PortalUtil.getClassNameId(User.class.getName()));
317    
318                            setNames(qPos, names);
319    
320                            qPos.add(SocialCounterPeriodUtil.getStartPeriod());
321    
322                            return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
323                    }
324                    catch (Exception e) {
325                            throw new SystemException(e);
326                    }
327                    finally {
328                            closeSession(session);
329                    }
330            }
331    
332            protected String getNames(String[] names) {
333                    if (names.length == 0) {
334                            return StringPool.BLANK;
335                    }
336    
337                    StringBundler sb = new StringBundler(names.length * 2 - 1);
338    
339                    for (int i = 0; i < names.length; i++) {
340                            sb.append(StringPool.QUESTION);
341    
342                            if ((i + 1) < names.length) {
343                                    sb.append(StringPool.COMMA);
344                            }
345                    }
346    
347                    return sb.toString();
348            }
349    
350            protected void setNames(QueryPos qPos, String[] names) {
351                    if ((names != null) && (names.length > 0)) {
352                            for (String name : names) {
353                                    qPos.add(name);
354                            }
355                    }
356            }
357    
358            private static PortalCache<String, Serializable> _activityCounters =
359                    MultiVMPoolUtil.getCache(SocialActivityCounterFinder.class.getName());
360    
361    }