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