1
22
23 package com.liferay.portlet.blogs.service.persistence;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.util.OrderByComparator;
27 import com.liferay.portal.kernel.util.StringMaker;
28 import com.liferay.portal.kernel.util.StringUtil;
29 import com.liferay.portal.spring.hibernate.CustomSQLUtil;
30 import com.liferay.portal.spring.hibernate.HibernateUtil;
31 import com.liferay.portlet.blogs.model.BlogsStatsUser;
32 import com.liferay.portlet.blogs.model.impl.BlogsStatsUserImpl;
33 import com.liferay.util.dao.hibernate.QueryPos;
34 import com.liferay.util.dao.hibernate.QueryUtil;
35
36 import java.util.ArrayList;
37 import java.util.Iterator;
38 import java.util.List;
39
40 import org.hibernate.Hibernate;
41 import org.hibernate.SQLQuery;
42 import org.hibernate.Session;
43
44
50 public class BlogsStatsUserFinderImpl implements BlogsStatsUserFinder {
51
52 public static String COUNT_BY_ORGANIZATION_IDS =
53 BlogsStatsUserFinder.class.getName() + ".countByOrganizationIds";
54
55 public static String FIND_BY_ORGANIZATION_IDS =
56 BlogsStatsUserFinder.class.getName() + ".findByOrganizationIds";
57
58 public int countByOrganizationId(long organizationId)
59 throws SystemException {
60
61 List<Long> organizationIds = new ArrayList<Long>();
62
63 organizationIds.add(organizationId);
64
65 return countByOrganizationIds(organizationIds);
66 }
67
68 public int countByOrganizationIds(List<Long> organizationIds)
69 throws SystemException {
70
71 Session session = null;
72
73 try {
74 session = HibernateUtil.openSession();
75
76 String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
77
78 sql = StringUtil.replace(
79 sql, "[$ORGANIZATION_ID$]",
80 getOrganizationIds(organizationIds));
81
82 SQLQuery q = session.createSQLQuery(sql);
83
84 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
85
86 QueryPos qPos = QueryPos.getInstance(q);
87
88 for (int i = 0; i < organizationIds.size(); i++) {
89 Long organizationId = organizationIds.get(i);
90
91 qPos.add(organizationId);
92 }
93
94 Iterator<Long> itr = q.list().iterator();
95
96 if (itr.hasNext()) {
97 Long count = itr.next();
98
99 if (count != null) {
100 return count.intValue();
101 }
102 }
103
104 return 0;
105 }
106 catch (Exception e) {
107 throw new SystemException(e);
108 }
109 finally {
110 HibernateUtil.closeSession(session);
111 }
112 }
113
114 public List<BlogsStatsUser> findByOrganizationId(
115 long organizationId, int begin, int end, OrderByComparator obc)
116 throws SystemException {
117
118 List<Long> organizationIds = new ArrayList<Long>();
119
120 organizationIds.add(organizationId);
121
122 return findByOrganizationIds(organizationIds, begin, end, obc);
123 }
124
125 public List<BlogsStatsUser> findByOrganizationIds(
126 List<Long> organizationIds, int begin, int end,
127 OrderByComparator obc)
128 throws SystemException {
129
130 Session session = null;
131
132 try {
133 session = HibernateUtil.openSession();
134
135 String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
136
137 sql = StringUtil.replace(
138 sql, "[$ORGANIZATION_ID$]",
139 getOrganizationIds(organizationIds));
140 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
141
142 SQLQuery q = session.createSQLQuery(sql);
143
144 q.addEntity("BlogsStatsUser", BlogsStatsUserImpl.class);
145
146 QueryPos qPos = QueryPos.getInstance(q);
147
148 for (int i = 0; i < organizationIds.size(); i++) {
149 Long organizationId = organizationIds.get(i);
150
151 qPos.add(organizationId);
152 }
153
154 return (List<BlogsStatsUser>)QueryUtil.list(
155 q, HibernateUtil.getDialect(), begin, end);
156 }
157 catch (Exception e) {
158 throw new SystemException(e);
159 }
160 finally {
161 HibernateUtil.closeSession(session);
162 }
163 }
164
165 protected String getOrganizationIds(List<Long> organizationIds) {
166 StringMaker sm = new StringMaker();
167
168 for (int i = 0; i < organizationIds.size(); i++) {
169 sm.append("Users_Orgs.organizationId = ? ");
170
171 if ((i + 1) != organizationIds.size()) {
172 sm.append("OR ");
173 }
174 }
175
176 return sm.toString();
177 }
178
179 }