1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
44   * <a href="IGImageFinderImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
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<Long> 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 = folderIds.get(i);
91  
92                  qPos.add(folderId);
93              }
94  
95              Iterator<Long> itr = q.list().iterator();
96  
97              if (itr.hasNext()) {
98                  Long count = 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<Long> itr = q.list().iterator();
132 
133             if (itr.hasNext()) {
134                 Long count = 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<Long> itr = q.list().iterator();
171 
172             if (itr.hasNext()) {
173                 Long count = 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<IGImage> 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 (List<IGImage>)QueryUtil.list(
209                 q, HibernateUtil.getDialect(), begin, end);
210         }
211         catch (Exception e) {
212             throw new SystemException(e);
213         }
214         finally {
215             HibernateUtil.closeSession(session);
216         }
217     }
218 
219     public List<IGImage> findByNoAssets() throws SystemException {
220         Session session = null;
221 
222         try {
223             session = HibernateUtil.openSession();
224 
225             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
226 
227             SQLQuery q = session.createSQLQuery(sql);
228 
229             q.addEntity("IGImage", IGImageImpl.class);
230 
231             return q.list();
232         }
233         catch (Exception e) {
234             throw new SystemException(e);
235         }
236         finally {
237             HibernateUtil.closeSession(session);
238         }
239     }
240 
241     public IGImage findByUuid_G(String uuid, long groupId)
242         throws NoSuchImageException, SystemException {
243 
244         Session session = null;
245 
246         try {
247             session = HibernateUtil.openSession();
248 
249             String sql = CustomSQLUtil.get(FIND_BY_UUID_G);
250 
251             SQLQuery q = session.createSQLQuery(sql);
252 
253             q.addEntity("IGImage", IGImageImpl.class);
254 
255             QueryPos qPos = QueryPos.getInstance(q);
256 
257             qPos.add(uuid);
258             qPos.add(groupId);
259 
260             List<IGImage> list = q.list();
261 
262             if (list.size() == 0) {
263                 StringMaker sm = new StringMaker();
264 
265                 sm.append("No IGImage exists with the key {uuid=");
266                 sm.append(uuid);
267                 sm.append(", groupId=");
268                 sm.append(groupId);
269                 sm.append("}");
270 
271                 throw new NoSuchImageException(sm.toString());
272             }
273             else {
274                 return list.get(0);
275             }
276         }
277         catch (NoSuchImageException nsie) {
278             throw nsie;
279         }
280         catch (Exception e) {
281             throw new SystemException(e);
282         }
283         finally {
284             HibernateUtil.closeSession(session);
285         }
286     }
287 
288     public List<IGImage> findByG_U(
289             long groupId, long userId, int begin, int end)
290         throws SystemException {
291 
292         Session session = null;
293 
294         try {
295             session = HibernateUtil.openSession();
296 
297             String sql = CustomSQLUtil.get(FIND_BY_G_U);
298 
299             SQLQuery q = session.createSQLQuery(sql);
300 
301             q.addEntity("IGImage", IGImageImpl.class);
302 
303             QueryPos qPos = QueryPos.getInstance(q);
304 
305             qPos.add(groupId);
306             qPos.add(userId);
307 
308             return (List<IGImage>)QueryUtil.list(
309                 q, HibernateUtil.getDialect(), begin, end);
310         }
311         catch (Exception e) {
312             throw new SystemException(e);
313         }
314         finally {
315             HibernateUtil.closeSession(session);
316         }
317     }
318 
319     protected String getFolderIds(List<Long> folderIds) {
320         StringMaker sm = new StringMaker();
321 
322         for (int i = 0; i < folderIds.size(); i++) {
323             sm.append("folderId = ? ");
324 
325             if ((i + 1) != folderIds.size()) {
326                 sm.append("OR ");
327             }
328         }
329 
330         return sm.toString();
331     }
332 
333 }