1   /**
2    * Copyright (c) 2000-2007 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.model.impl.IGImageImpl;
31  import com.liferay.util.dao.hibernate.QueryPos;
32  import com.liferay.util.dao.hibernate.QueryUtil;
33  
34  import java.util.Iterator;
35  import java.util.List;
36  
37  import org.hibernate.Hibernate;
38  import org.hibernate.SQLQuery;
39  import org.hibernate.Session;
40  
41  /**
42   * <a href="IGImageFinder.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class IGImageFinder {
48  
49      public static String COUNT_BY_FOLDER_IDS =
50          IGImageFinder.class.getName() + ".countByFolderIds";
51  
52      public static String COUNT_BY_GROUP_ID =
53          IGImageFinder.class.getName() + ".countByGroupId";
54  
55      public static String COUNT_BY_G_U =
56          IGImageFinder.class.getName() + ".countByG_U";
57  
58      public static String FIND_BY_GROUP_ID =
59          IGImageFinder.class.getName() + ".findByGroupId";
60  
61      public static String FIND_BY_NO_ASSETS =
62          IGImageFinder.class.getName() + ".findByNoAssets";
63  
64      public static String FIND_BY_G_U =
65          IGImageFinder.class.getName() + ".findByG_U";
66  
67      public static int countByFolderIds(List folderIds)
68          throws SystemException {
69  
70          Session session = null;
71  
72          try {
73              session = HibernateUtil.openSession();
74  
75              String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
76  
77              sql = StringUtil.replace(
78                  sql, "[$FOLDER_ID$]", _getFolderIds(folderIds));
79  
80              SQLQuery q = session.createSQLQuery(sql);
81  
82              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
83  
84              QueryPos qPos = QueryPos.getInstance(q);
85  
86              for (int i = 0; i < folderIds.size(); i++) {
87                  Long folderId = (Long)folderIds.get(i);
88  
89                  qPos.add(folderId);
90              }
91  
92              Iterator itr = q.list().iterator();
93  
94              if (itr.hasNext()) {
95                  Long count = (Long)itr.next();
96  
97                  if (count != null) {
98                      return count.intValue();
99                  }
100             }
101 
102             return 0;
103         }
104         catch (Exception e) {
105             throw new SystemException(e);
106         }
107         finally {
108             HibernateUtil.closeSession(session);
109         }
110     }
111 
112     public static int countByGroupId(long groupId) throws SystemException {
113         Session session = null;
114 
115         try {
116             session = HibernateUtil.openSession();
117 
118             String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
119 
120             SQLQuery q = session.createSQLQuery(sql);
121 
122             q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
123 
124             QueryPos qPos = QueryPos.getInstance(q);
125 
126             qPos.add(groupId);
127 
128             Iterator itr = q.list().iterator();
129 
130             if (itr.hasNext()) {
131                 Long count = (Long)itr.next();
132 
133                 if (count != null) {
134                     return count.intValue();
135                 }
136             }
137 
138             return 0;
139         }
140         catch (Exception e) {
141             throw new SystemException(e);
142         }
143         finally {
144             HibernateUtil.closeSession(session);
145         }
146     }
147 
148     public static int countByG_U(long groupId, long userId)
149         throws SystemException {
150 
151         Session session = null;
152 
153         try {
154             session = HibernateUtil.openSession();
155 
156             String sql = CustomSQLUtil.get(COUNT_BY_G_U);
157 
158             SQLQuery q = session.createSQLQuery(sql);
159 
160             q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
161 
162             QueryPos qPos = QueryPos.getInstance(q);
163 
164             qPos.add(groupId);
165             qPos.add(userId);
166 
167             Iterator itr = q.list().iterator();
168 
169             if (itr.hasNext()) {
170                 Long count = (Long)itr.next();
171 
172                 if (count != null) {
173                     return count.intValue();
174                 }
175             }
176 
177             return 0;
178         }
179         catch (Exception e) {
180             throw new SystemException(e);
181         }
182         finally {
183             HibernateUtil.closeSession(session);
184         }
185     }
186 
187     public static List findByGroupId(long groupId, int begin, int end)
188         throws SystemException {
189 
190         Session session = null;
191 
192         try {
193             session = HibernateUtil.openSession();
194 
195             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
196 
197             SQLQuery q = session.createSQLQuery(sql);
198 
199             q.addEntity("IGImage", IGImageImpl.class);
200 
201             QueryPos qPos = QueryPos.getInstance(q);
202 
203             qPos.add(groupId);
204 
205             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
206         }
207         catch (Exception e) {
208             throw new SystemException(e);
209         }
210         finally {
211             HibernateUtil.closeSession(session);
212         }
213     }
214 
215     public static List findByNoAssets() throws SystemException {
216         Session session = null;
217 
218         try {
219             session = HibernateUtil.openSession();
220 
221             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
222 
223             SQLQuery q = session.createSQLQuery(sql);
224 
225             q.addEntity("IGImage", IGImageImpl.class);
226 
227             return q.list();
228         }
229         catch (Exception e) {
230             throw new SystemException(e);
231         }
232         finally {
233             HibernateUtil.closeSession(session);
234         }
235     }
236 
237     public static List findByG_U(long groupId, long userId, int begin, int end)
238         throws SystemException {
239 
240         Session session = null;
241 
242         try {
243             session = HibernateUtil.openSession();
244 
245             String sql = CustomSQLUtil.get(FIND_BY_G_U);
246 
247             SQLQuery q = session.createSQLQuery(sql);
248 
249             q.addEntity("IGImage", IGImageImpl.class);
250 
251             QueryPos qPos = QueryPos.getInstance(q);
252 
253             qPos.add(groupId);
254             qPos.add(userId);
255 
256             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
257         }
258         catch (Exception e) {
259             throw new SystemException(e);
260         }
261         finally {
262             HibernateUtil.closeSession(session);
263         }
264     }
265 
266     private static String _getFolderIds(List folderIds) {
267         StringMaker sm = new StringMaker();
268 
269         for (int i = 0; i < folderIds.size(); i++) {
270             sm.append("folderId = ? ");
271 
272             if ((i + 1) != folderIds.size()) {
273                 sm.append("OR ");
274             }
275         }
276 
277         return sm.toString();
278     }
279 
280 }