1
22
23 package com.liferay.portlet.imagegallery.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.imagegallery.NoSuchImageException;
31 import com.liferay.portlet.imagegallery.model.IGImage;
32 import com.liferay.portlet.imagegallery.model.impl.IGImageImpl;
33 import com.liferay.util.dao.hibernate.QueryPos;
34 import com.liferay.util.dao.hibernate.QueryUtil;
35
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 IGImageFinderImpl implements IGImageFinder {
50
51 public static String COUNT_BY_FOLDER_IDS =
52 IGImageFinder.class.getName() + ".countByFolderIds";
53
54 public static String COUNT_BY_GROUP_ID =
55 IGImageFinder.class.getName() + ".countByGroupId";
56
57 public static String COUNT_BY_G_U =
58 IGImageFinder.class.getName() + ".countByG_U";
59
60 public static String FIND_BY_GROUP_ID =
61 IGImageFinder.class.getName() + ".findByGroupId";
62
63 public static String FIND_BY_NO_ASSETS =
64 IGImageFinder.class.getName() + ".findByNoAssets";
65
66 public static String FIND_BY_UUID_G =
67 IGImageFinder.class.getName() + ".findByUuid_G";
68
69 public static String FIND_BY_G_U =
70 IGImageFinder.class.getName() + ".findByG_U";
71
72 public int countByFolderIds(List folderIds) throws SystemException {
73 Session session = null;
74
75 try {
76 session = HibernateUtil.openSession();
77
78 String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
79
80 sql = StringUtil.replace(
81 sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
82
83 SQLQuery q = session.createSQLQuery(sql);
84
85 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
86
87 QueryPos qPos = QueryPos.getInstance(q);
88
89 for (int i = 0; i < folderIds.size(); i++) {
90 Long folderId = (Long)folderIds.get(i);
91
92 qPos.add(folderId);
93 }
94
95 Iterator itr = q.list().iterator();
96
97 if (itr.hasNext()) {
98 Long count = (Long)itr.next();
99
100 if (count != null) {
101 return count.intValue();
102 }
103 }
104
105 return 0;
106 }
107 catch (Exception e) {
108 throw new SystemException(e);
109 }
110 finally {
111 HibernateUtil.closeSession(session);
112 }
113 }
114
115 public int countByGroupId(long groupId) throws SystemException {
116 Session session = null;
117
118 try {
119 session = HibernateUtil.openSession();
120
121 String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
122
123 SQLQuery q = session.createSQLQuery(sql);
124
125 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
126
127 QueryPos qPos = QueryPos.getInstance(q);
128
129 qPos.add(groupId);
130
131 Iterator itr = q.list().iterator();
132
133 if (itr.hasNext()) {
134 Long count = (Long)itr.next();
135
136 if (count != null) {
137 return count.intValue();
138 }
139 }
140
141 return 0;
142 }
143 catch (Exception e) {
144 throw new SystemException(e);
145 }
146 finally {
147 HibernateUtil.closeSession(session);
148 }
149 }
150
151 public int countByG_U(long groupId, long userId)
152 throws SystemException {
153
154 Session session = null;
155
156 try {
157 session = HibernateUtil.openSession();
158
159 String sql = CustomSQLUtil.get(COUNT_BY_G_U);
160
161 SQLQuery q = session.createSQLQuery(sql);
162
163 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
164
165 QueryPos qPos = QueryPos.getInstance(q);
166
167 qPos.add(groupId);
168 qPos.add(userId);
169
170 Iterator itr = q.list().iterator();
171
172 if (itr.hasNext()) {
173 Long count = (Long)itr.next();
174
175 if (count != null) {
176 return count.intValue();
177 }
178 }
179
180 return 0;
181 }
182 catch (Exception e) {
183 throw new SystemException(e);
184 }
185 finally {
186 HibernateUtil.closeSession(session);
187 }
188 }
189
190 public List findByGroupId(long groupId, int begin, int end)
191 throws SystemException {
192
193 Session session = null;
194
195 try {
196 session = HibernateUtil.openSession();
197
198 String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
199
200 SQLQuery q = session.createSQLQuery(sql);
201
202 q.addEntity("IGImage", IGImageImpl.class);
203
204 QueryPos qPos = QueryPos.getInstance(q);
205
206 qPos.add(groupId);
207
208 return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
209 }
210 catch (Exception e) {
211 throw new SystemException(e);
212 }
213 finally {
214 HibernateUtil.closeSession(session);
215 }
216 }
217
218 public List findByNoAssets() throws SystemException {
219 Session session = null;
220
221 try {
222 session = HibernateUtil.openSession();
223
224 String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
225
226 SQLQuery q = session.createSQLQuery(sql);
227
228 q.addEntity("IGImage", IGImageImpl.class);
229
230 return q.list();
231 }
232 catch (Exception e) {
233 throw new SystemException(e);
234 }
235 finally {
236 HibernateUtil.closeSession(session);
237 }
238 }
239
240 public IGImage findByUuid_G(String uuid, long groupId)
241 throws NoSuchImageException, SystemException {
242
243 Session session = null;
244
245 try {
246 session = HibernateUtil.openSession();
247
248 String sql = CustomSQLUtil.get(FIND_BY_UUID_G);
249
250 SQLQuery q = session.createSQLQuery(sql);
251
252 q.addEntity("IGImage", IGImageImpl.class);
253
254 QueryPos qPos = QueryPos.getInstance(q);
255
256 qPos.add(uuid);
257 qPos.add(groupId);
258
259 List list = q.list();
260
261 if (list.size() == 0) {
262 StringMaker sm = new StringMaker();
263
264 sm.append("No IGImage exists with the key {uuid=");
265 sm.append(uuid);
266 sm.append(", groupId=");
267 sm.append(groupId);
268 sm.append("}");
269
270 throw new NoSuchImageException(sm.toString());
271 }
272 else {
273 return (IGImage)list.get(0);
274 }
275 }
276 catch (NoSuchImageException nsie) {
277 throw nsie;
278 }
279 catch (Exception e) {
280 throw new SystemException(e);
281 }
282 finally {
283 HibernateUtil.closeSession(session);
284 }
285 }
286
287 public List findByG_U(long groupId, long userId, int begin, int end)
288 throws SystemException {
289
290 Session session = null;
291
292 try {
293 session = HibernateUtil.openSession();
294
295 String sql = CustomSQLUtil.get(FIND_BY_G_U);
296
297 SQLQuery q = session.createSQLQuery(sql);
298
299 q.addEntity("IGImage", IGImageImpl.class);
300
301 QueryPos qPos = QueryPos.getInstance(q);
302
303 qPos.add(groupId);
304 qPos.add(userId);
305
306 return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
307 }
308 catch (Exception e) {
309 throw new SystemException(e);
310 }
311 finally {
312 HibernateUtil.closeSession(session);
313 }
314 }
315
316 protected String getFolderIds(List folderIds) {
317 StringMaker sm = new StringMaker();
318
319 for (int i = 0; i < folderIds.size(); i++) {
320 sm.append("folderId = ? ");
321
322 if ((i + 1) != folderIds.size()) {
323 sm.append("OR ");
324 }
325 }
326
327 return sm.toString();
328 }
329
330 }