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