1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.imagegallery.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.QueryPos;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.kernel.dao.orm.SQLQuery;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.dao.orm.Type;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
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.orm.CustomSQLUtil;
34  
35  import java.util.Iterator;
36  import java.util.List;
37  
38  /**
39   * <a href="IGImageFinderImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class IGImageFinderImpl
45      extends BasePersistenceImpl implements IGImageFinder {
46  
47      public static String COUNT_BY_FOLDER_IDS =
48          IGImageFinder.class.getName() + ".countByFolderIds";
49  
50      public static String COUNT_BY_GROUP_ID =
51          IGImageFinder.class.getName() + ".countByGroupId";
52  
53      public static String COUNT_BY_G_U =
54          IGImageFinder.class.getName() + ".countByG_U";
55  
56      public static String FIND_BY_GROUP_ID =
57          IGImageFinder.class.getName() + ".findByGroupId";
58  
59      public static String FIND_BY_NO_ASSETS =
60          IGImageFinder.class.getName() + ".findByNoAssets";
61  
62      public static String FIND_BY_UUID_G =
63          IGImageFinder.class.getName() + ".findByUuid_G";
64  
65      public static String FIND_BY_G_U =
66          IGImageFinder.class.getName() + ".findByG_U";
67  
68      public int countByFolderIds(List<Long> folderIds) throws SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
75  
76              sql = StringUtil.replace(
77                  sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
78  
79              SQLQuery q = session.createSQLQuery(sql);
80  
81              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
82  
83              QueryPos qPos = QueryPos.getInstance(q);
84  
85              for (int i = 0; i < folderIds.size(); i++) {
86                  Long folderId = folderIds.get(i);
87  
88                  qPos.add(folderId);
89              }
90  
91              Iterator<Long> itr = q.list().iterator();
92  
93              if (itr.hasNext()) {
94                  Long count = itr.next();
95  
96                  if (count != null) {
97                      return count.intValue();
98                  }
99              }
100 
101             return 0;
102         }
103         catch (Exception e) {
104             throw new SystemException(e);
105         }
106         finally {
107             closeSession(session);
108         }
109     }
110 
111     public int countByGroupId(long groupId) throws SystemException {
112         Session session = null;
113 
114         try {
115             session = openSession();
116 
117             String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
118 
119             SQLQuery q = session.createSQLQuery(sql);
120 
121             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
122 
123             QueryPos qPos = QueryPos.getInstance(q);
124 
125             qPos.add(groupId);
126 
127             Iterator<Long> itr = q.list().iterator();
128 
129             if (itr.hasNext()) {
130                 Long count = itr.next();
131 
132                 if (count != null) {
133                     return count.intValue();
134                 }
135             }
136 
137             return 0;
138         }
139         catch (Exception e) {
140             throw new SystemException(e);
141         }
142         finally {
143             closeSession(session);
144         }
145     }
146 
147     public int countByG_U(long groupId, long userId)
148         throws SystemException {
149 
150         Session session = null;
151 
152         try {
153             session = openSession();
154 
155             String sql = CustomSQLUtil.get(COUNT_BY_G_U);
156 
157             SQLQuery q = session.createSQLQuery(sql);
158 
159             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
160 
161             QueryPos qPos = QueryPos.getInstance(q);
162 
163             qPos.add(groupId);
164             qPos.add(userId);
165 
166             Iterator<Long> itr = q.list().iterator();
167 
168             if (itr.hasNext()) {
169                 Long count = itr.next();
170 
171                 if (count != null) {
172                     return count.intValue();
173                 }
174             }
175 
176             return 0;
177         }
178         catch (Exception e) {
179             throw new SystemException(e);
180         }
181         finally {
182             closeSession(session);
183         }
184     }
185 
186     public List<IGImage> findByGroupId(long groupId, int start, int end)
187         throws SystemException {
188 
189         Session session = null;
190 
191         try {
192             session = openSession();
193 
194             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
195 
196             SQLQuery q = session.createSQLQuery(sql);
197 
198             q.addEntity("IGImage", IGImageImpl.class);
199 
200             QueryPos qPos = QueryPos.getInstance(q);
201 
202             qPos.add(groupId);
203 
204             return (List<IGImage>)QueryUtil.list(q, getDialect(), start, end);
205         }
206         catch (Exception e) {
207             throw new SystemException(e);
208         }
209         finally {
210             closeSession(session);
211         }
212     }
213 
214     public List<IGImage> findByNoAssets() throws SystemException {
215         Session session = null;
216 
217         try {
218             session = openSession();
219 
220             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
221 
222             SQLQuery q = session.createSQLQuery(sql);
223 
224             q.addEntity("IGImage", IGImageImpl.class);
225 
226             return q.list();
227         }
228         catch (Exception e) {
229             throw new SystemException(e);
230         }
231         finally {
232             closeSession(session);
233         }
234     }
235 
236     public IGImage findByUuid_G(String uuid, long groupId)
237         throws NoSuchImageException, SystemException {
238 
239         Session session = null;
240 
241         try {
242             session = openSession();
243 
244             String sql = CustomSQLUtil.get(FIND_BY_UUID_G);
245 
246             SQLQuery q = session.createSQLQuery(sql);
247 
248             q.addEntity("IGImage", IGImageImpl.class);
249 
250             QueryPos qPos = QueryPos.getInstance(q);
251 
252             qPos.add(uuid);
253             qPos.add(groupId);
254 
255             List<IGImage> list = q.list();
256 
257             if (list.size() == 0) {
258                 StringBuilder sb = new StringBuilder();
259 
260                 sb.append("No IGImage exists with the key {uuid=");
261                 sb.append(uuid);
262                 sb.append(", groupId=");
263                 sb.append(groupId);
264                 sb.append("}");
265 
266                 throw new NoSuchImageException(sb.toString());
267             }
268             else {
269                 return list.get(0);
270             }
271         }
272         catch (NoSuchImageException nsie) {
273             throw nsie;
274         }
275         catch (Exception e) {
276             throw new SystemException(e);
277         }
278         finally {
279             closeSession(session);
280         }
281     }
282 
283     public List<IGImage> findByG_U(
284             long groupId, long userId, int start, int end)
285         throws SystemException {
286 
287         Session session = null;
288 
289         try {
290             session = openSession();
291 
292             String sql = CustomSQLUtil.get(FIND_BY_G_U);
293 
294             SQLQuery q = session.createSQLQuery(sql);
295 
296             q.addEntity("IGImage", IGImageImpl.class);
297 
298             QueryPos qPos = QueryPos.getInstance(q);
299 
300             qPos.add(groupId);
301             qPos.add(userId);
302 
303             return (List<IGImage>)QueryUtil.list(q, getDialect(), start, end);
304         }
305         catch (Exception e) {
306             throw new SystemException(e);
307         }
308         finally {
309             closeSession(session);
310         }
311     }
312 
313     protected String getFolderIds(List<Long> folderIds) {
314         StringBuilder sb = new StringBuilder();
315 
316         for (int i = 0; i < folderIds.size(); i++) {
317             sb.append("folderId = ? ");
318 
319             if ((i + 1) != folderIds.size()) {
320                 sb.append("OR ");
321             }
322         }
323 
324         return sb.toString();
325     }
326 
327 }