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.blogs.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.portlet.blogs.model.BlogsStatsUser;
029    import com.liferay.portlet.blogs.model.impl.BlogsStatsUserImpl;
030    import com.liferay.util.dao.orm.CustomSQLUtil;
031    
032    import java.util.ArrayList;
033    import java.util.Date;
034    import java.util.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class BlogsStatsUserFinderImpl
041            extends BasePersistenceImpl<BlogsStatsUser>
042            implements BlogsStatsUserFinder {
043    
044            public static final String COUNT_BY_ORGANIZATION_IDS =
045                    BlogsStatsUserFinder.class.getName() + ".countByOrganizationIds";
046    
047            public static final String FIND_BY_GROUP_IDS =
048                    BlogsStatsUserFinder.class.getName() + ".findByGroupIds";
049    
050            public static final String FIND_BY_ORGANIZATION_IDS =
051                    BlogsStatsUserFinder.class.getName() + ".findByOrganizationIds";
052    
053            @Override
054            public int countByOrganizationId(long organizationId)
055                    throws SystemException {
056    
057                    List<Long> organizationIds = new ArrayList<Long>();
058    
059                    organizationIds.add(organizationId);
060    
061                    return countByOrganizationIds(organizationIds);
062            }
063    
064            @Override
065            public int countByOrganizationIds(List<Long> organizationIds)
066                    throws SystemException {
067    
068                    Session session = null;
069    
070                    try {
071                            session = openSession();
072    
073                            String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
074    
075                            sql = StringUtil.replace(
076                                    sql, "[$ORGANIZATION_ID$]",
077                                    getOrganizationIds(organizationIds));
078    
079                            SQLQuery q = session.createSQLQuery(sql);
080    
081                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
082    
083                            QueryPos qPos = QueryPos.getInstance(q);
084    
085                            for (int i = 0; i < organizationIds.size(); i++) {
086                                    Long organizationId = organizationIds.get(i);
087    
088                                    qPos.add(organizationId);
089                            }
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            @Override
112            public List<BlogsStatsUser> findByGroupIds(
113                            long companyId, long groupId, int start, int end)
114                    throws SystemException {
115    
116                    Session session = null;
117    
118                    try {
119                            session = openSession();
120    
121                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_IDS);
122    
123                            SQLQuery q = session.createSQLQuery(sql);
124    
125                            q.addScalar("userId", Type.LONG);
126                            q.addScalar("lastPostDate", Type.TIMESTAMP);
127    
128                            QueryPos qPos = QueryPos.getInstance(q);
129    
130                            qPos.add(companyId);
131                            qPos.add(groupId);
132                            qPos.add(groupId);
133                            qPos.add(groupId);
134    
135                            List<BlogsStatsUser> statsUsers = new ArrayList<BlogsStatsUser>();
136    
137                            Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
138                                    q, getDialect(), start, end);
139    
140                            while (itr.hasNext()) {
141                                    Object[] array = itr.next();
142    
143                                    long userId = (Long)array[0];
144                                    Date lastPostDate = (Date)array[1];
145    
146                                    List<BlogsStatsUser> curStatsUsers =
147                                            BlogsStatsUserUtil.findByU_L(userId, lastPostDate);
148    
149                                    if (!curStatsUsers.isEmpty()) {
150                                            BlogsStatsUser statsUser = curStatsUsers.get(0);
151    
152                                            statsUsers.add(statsUser);
153                                    }
154                            }
155    
156                            return statsUsers;
157                    }
158                    catch (Exception e) {
159                            throw new SystemException(e);
160                    }
161                    finally {
162                            closeSession(session);
163                    }
164            }
165    
166            @Override
167            public List<BlogsStatsUser> findByOrganizationId(
168                            long organizationId, int start, int end, OrderByComparator obc)
169                    throws SystemException {
170    
171                    List<Long> organizationIds = new ArrayList<Long>();
172    
173                    organizationIds.add(organizationId);
174    
175                    return findByOrganizationIds(organizationIds, start, end, obc);
176            }
177    
178            @Override
179            public List<BlogsStatsUser> findByOrganizationIds(
180                            List<Long> organizationIds, int start, int end,
181                            OrderByComparator obc)
182                    throws SystemException {
183    
184                    Session session = null;
185    
186                    try {
187                            session = openSession();
188    
189                            String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
190    
191                            sql = StringUtil.replace(
192                                    sql, "[$ORGANIZATION_ID$]",
193                                    getOrganizationIds(organizationIds));
194                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
195    
196                            SQLQuery q = session.createSQLQuery(sql);
197    
198                            q.addEntity("BlogsStatsUser", BlogsStatsUserImpl.class);
199    
200                            QueryPos qPos = QueryPos.getInstance(q);
201    
202                            for (int i = 0; i < organizationIds.size(); i++) {
203                                    Long organizationId = organizationIds.get(i);
204    
205                                    qPos.add(organizationId);
206                            }
207    
208                            return (List<BlogsStatsUser>)QueryUtil.list(
209                                    q, getDialect(), start, end);
210                    }
211                    catch (Exception e) {
212                            throw new SystemException(e);
213                    }
214                    finally {
215                            closeSession(session);
216                    }
217            }
218    
219            protected String getOrganizationIds(List<Long> organizationIds) {
220                    if (organizationIds.isEmpty()) {
221                            return StringPool.BLANK;
222                    }
223    
224                    StringBundler sb = new StringBundler(organizationIds.size() * 2 - 1);
225    
226                    for (int i = 0; i < organizationIds.size(); i++) {
227                            sb.append("Users_Orgs.organizationId = ? ");
228    
229                            if ((i + 1) != organizationIds.size()) {
230                                    sb.append("OR ");
231                            }
232                    }
233    
234                    return sb.toString();
235            }
236    
237    }