1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.blogs.service.persistence;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.orm.QueryPos;
19  import com.liferay.portal.kernel.dao.orm.QueryUtil;
20  import com.liferay.portal.kernel.dao.orm.SQLQuery;
21  import com.liferay.portal.kernel.dao.orm.Session;
22  import com.liferay.portal.kernel.dao.orm.Type;
23  import com.liferay.portal.kernel.util.OrderByComparator;
24  import com.liferay.portal.kernel.util.StringBundler;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
28  import com.liferay.portlet.blogs.model.BlogsStatsUser;
29  import com.liferay.portlet.blogs.model.impl.BlogsStatsUserImpl;
30  import com.liferay.util.dao.orm.CustomSQLUtil;
31  
32  import java.util.ArrayList;
33  import java.util.Date;
34  import java.util.Iterator;
35  import java.util.List;
36  
37  /**
38   * <a href="BlogsStatsUserFinderImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class BlogsStatsUserFinderImpl
43      extends BasePersistenceImpl<BlogsStatsUser>
44      implements BlogsStatsUserFinder {
45  
46      public static String COUNT_BY_ORGANIZATION_IDS =
47          BlogsStatsUserFinder.class.getName() + ".countByOrganizationIds";
48  
49      public static String FIND_BY_GROUP_IDS =
50          BlogsStatsUserFinder.class.getName() + ".findByGroupIds";
51  
52      public static String FIND_BY_ORGANIZATION_IDS =
53          BlogsStatsUserFinder.class.getName() + ".findByOrganizationIds";
54  
55      public int countByOrganizationId(long organizationId)
56          throws SystemException {
57  
58          List<Long> organizationIds = new ArrayList<Long>();
59  
60          organizationIds.add(organizationId);
61  
62          return countByOrganizationIds(organizationIds);
63      }
64  
65      public int countByOrganizationIds(List<Long> organizationIds)
66          throws SystemException {
67  
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
74  
75              sql = StringUtil.replace(
76                  sql, "[$ORGANIZATION_ID$]",
77                  getOrganizationIds(organizationIds));
78  
79              SQLQuery q = session.createSQLQuery(sql);
80  
81              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
82  
83              QueryPos qPos = QueryPos.getInstance(q);
84  
85              for (int i = 0; i < organizationIds.size(); i++) {
86                  Long organizationId = organizationIds.get(i);
87  
88                  qPos.add(organizationId);
89              }
90  
91              Iterator<Long> itr = q.list().iterator();
92  
93              if (itr.hasNext()) {
94                  Long count = itr.next();
95  
96                  if (count != null) {
97                      return count.intValue();
98                  }
99              }
100 
101             return 0;
102         }
103         catch (Exception e) {
104             throw new SystemException(e);
105         }
106         finally {
107             closeSession(session);
108         }
109     }
110 
111     public List<BlogsStatsUser> findByGroupIds(
112             long companyId, long groupId, int start, int end)
113         throws SystemException {
114 
115         Session session = null;
116 
117         try {
118             session = openSession();
119 
120             String sql = CustomSQLUtil.get(FIND_BY_GROUP_IDS);
121 
122             SQLQuery q = session.createSQLQuery(sql);
123 
124             q.addScalar("userId", Type.LONG);
125             q.addScalar("lastPostDate", Type.TIMESTAMP);
126 
127             QueryPos qPos = QueryPos.getInstance(q);
128 
129             qPos.add(companyId);
130             qPos.add(groupId);
131             qPos.add(groupId);
132             qPos.add(groupId);
133 
134             List<BlogsStatsUser> statsUsers = new ArrayList<BlogsStatsUser>();
135 
136             Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
137                 q, getDialect(), start, end);
138 
139             while (itr.hasNext()) {
140                 Object[] array = itr.next();
141 
142                 long userId = (Long)array[0];
143                 Date lastPostDate = (Date)array[1];
144 
145                 List<BlogsStatsUser> curStatsUsers =
146                     BlogsStatsUserUtil.findByU_L(userId, lastPostDate);
147 
148                 if (!curStatsUsers.isEmpty()) {
149                     BlogsStatsUser statsUser = curStatsUsers.get(0);
150 
151                     statsUsers.add(statsUser);
152                 }
153             }
154 
155             return statsUsers;
156         }
157         catch (Exception e) {
158             throw new SystemException(e);
159         }
160         finally {
161             closeSession(session);
162         }
163     }
164 
165     public List<BlogsStatsUser> findByOrganizationId(
166             long organizationId, int start, int end, OrderByComparator obc)
167         throws SystemException {
168 
169         List<Long> organizationIds = new ArrayList<Long>();
170 
171         organizationIds.add(organizationId);
172 
173         return findByOrganizationIds(organizationIds, start, end, obc);
174     }
175 
176     public List<BlogsStatsUser> findByOrganizationIds(
177             List<Long> organizationIds, int start, int end,
178             OrderByComparator obc)
179         throws SystemException {
180 
181         Session session = null;
182 
183         try {
184             session = openSession();
185 
186             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
187 
188             sql = StringUtil.replace(
189                 sql, "[$ORGANIZATION_ID$]",
190                 getOrganizationIds(organizationIds));
191             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
192 
193             SQLQuery q = session.createSQLQuery(sql);
194 
195             q.addEntity("BlogsStatsUser", BlogsStatsUserImpl.class);
196 
197             QueryPos qPos = QueryPos.getInstance(q);
198 
199             for (int i = 0; i < organizationIds.size(); i++) {
200                 Long organizationId = organizationIds.get(i);
201 
202                 qPos.add(organizationId);
203             }
204 
205             return (List<BlogsStatsUser>)QueryUtil.list(
206                 q, getDialect(), start, end);
207         }
208         catch (Exception e) {
209             throw new SystemException(e);
210         }
211         finally {
212             closeSession(session);
213         }
214     }
215 
216     protected String getOrganizationIds(List<Long> organizationIds) {
217         if (organizationIds.isEmpty()) {
218             return StringPool.BLANK;
219         }
220 
221         StringBundler sb = new StringBundler(organizationIds.size() * 2 - 1);
222 
223         for (int i = 0; i < organizationIds.size(); i++) {
224             sb.append("Users_Orgs.organizationId = ? ");
225 
226             if ((i + 1) != organizationIds.size()) {
227                 sb.append("OR ");
228             }
229         }
230 
231         return sb.toString();
232     }
233 
234 }