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