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