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