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