1
22
23 package com.liferay.portlet.blogs.service.persistence;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.util.StringMaker;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.spring.hibernate.CustomSQLUtil;
29 import com.liferay.portal.spring.hibernate.HibernateUtil;
30 import com.liferay.portlet.blogs.model.BlogsEntry;
31 import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
32 import com.liferay.util.dao.hibernate.QueryPos;
33 import com.liferay.util.dao.hibernate.QueryUtil;
34
35 import java.util.ArrayList;
36 import java.util.Iterator;
37 import java.util.List;
38
39 import org.hibernate.Hibernate;
40 import org.hibernate.SQLQuery;
41 import org.hibernate.Session;
42
43
49 public class BlogsEntryFinderImpl implements BlogsEntryFinder {
50
51 public static String COUNT_BY_ORGANIZATION_IDS =
52 BlogsEntryFinder.class.getName() + ".countByOrganizationIds";
53
54 public static String FIND_BY_ORGANIZATION_IDS =
55 BlogsEntryFinder.class.getName() + ".findByOrganizationIds";
56
57 public static String FIND_BY_NO_ASSETS =
58 BlogsEntryFinder.class.getName() + ".findByNoAssets";
59
60 public int countByOrganizationId(long organizationId)
61 throws SystemException {
62
63 List<Long> organizationIds = new ArrayList<Long>();
64
65 organizationIds.add(organizationId);
66
67 return countByOrganizationIds(organizationIds);
68 }
69
70 public int countByOrganizationIds(List<Long> organizationIds)
71 throws SystemException {
72
73 Session session = null;
74
75 try {
76 session = HibernateUtil.openSession();
77
78 String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
79
80 sql = StringUtil.replace(
81 sql, "[$ORGANIZATION_ID$]",
82 getOrganizationIds(organizationIds));
83
84 SQLQuery q = session.createSQLQuery(sql);
85
86 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
87
88 QueryPos qPos = QueryPos.getInstance(q);
89
90 for (int i = 0; i < organizationIds.size(); i++) {
91 Long organizationId = organizationIds.get(i);
92
93 qPos.add(organizationId);
94 }
95
96 Iterator<Long> itr = q.list().iterator();
97
98 if (itr.hasNext()) {
99 Long count = itr.next();
100
101 if (count != null) {
102 return count.intValue();
103 }
104 }
105
106 return 0;
107 }
108 catch (Exception e) {
109 throw new SystemException(e);
110 }
111 finally {
112 HibernateUtil.closeSession(session);
113 }
114 }
115
116 public List<BlogsEntry> findByOrganizationId(
117 long organizationId, int begin, int end)
118 throws SystemException {
119
120 List<Long> organizationIds = new ArrayList<Long>();
121
122 organizationIds.add(organizationId);
123
124 return findByOrganizationIds(organizationIds, begin, end);
125 }
126
127 public List<BlogsEntry> findByOrganizationIds(
128 List<Long> organizationIds, int begin, int end)
129 throws SystemException {
130
131 Session session = null;
132
133 try {
134 session = HibernateUtil.openSession();
135
136 String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
137
138 sql = StringUtil.replace(
139 sql, "[$ORGANIZATION_ID$]",
140 getOrganizationIds(organizationIds));
141
142 SQLQuery q = session.createSQLQuery(sql);
143
144 q.addEntity("BlogsEntry", BlogsEntryImpl.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<BlogsEntry>)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 public List<BlogsEntry> findByNoAssets() throws SystemException {
166 Session session = null;
167
168 try {
169 session = HibernateUtil.openSession();
170
171 String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
172
173 SQLQuery q = session.createSQLQuery(sql);
174
175 q.addEntity("BlogsEntry", BlogsEntryImpl.class);
176
177 return q.list();
178 }
179 catch (Exception e) {
180 throw new SystemException(e);
181 }
182 finally {
183 HibernateUtil.closeSession(session);
184 }
185 }
186
187 protected String getOrganizationIds(List<Long> organizationIds) {
188 StringMaker sm = new StringMaker();
189
190 for (int i = 0; i < organizationIds.size(); i++) {
191 sm.append("Users_Orgs.organizationId = ? ");
192
193 if ((i + 1) != organizationIds.size()) {
194 sm.append("OR ");
195 }
196 }
197
198 return sm.toString();
199 }
200
201 }