1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.imagegallery.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryPos;
18  import com.liferay.portal.kernel.dao.orm.SQLQuery;
19  import com.liferay.portal.kernel.dao.orm.Session;
20  import com.liferay.portal.kernel.dao.orm.Type;
21  import com.liferay.portal.kernel.exception.SystemException;
22  import com.liferay.portal.kernel.util.StringBundler;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
26  import com.liferay.portlet.imagegallery.NoSuchImageException;
27  import com.liferay.portlet.imagegallery.model.IGImage;
28  import com.liferay.portlet.imagegallery.model.impl.IGImageImpl;
29  import com.liferay.util.dao.orm.CustomSQLUtil;
30  
31  import java.util.Iterator;
32  import java.util.List;
33  
34  /**
35   * <a href="IGImageFinderImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   * @author Julio Camarero
39   */
40  public class IGImageFinderImpl
41      extends BasePersistenceImpl<IGImage> implements IGImageFinder {
42  
43      public static String COUNT_BY_G_F =
44          IGImageFinder.class.getName() + ".countByG_F";
45  
46      public static String FIND_BY_ANY_IMAGE_ID =
47          IGImageFinder.class.getName() + ".findByAnyImageId";
48  
49      public static String FIND_BY_NO_ASSETS =
50          IGImageFinder.class.getName() + ".findByNoAssets";
51  
52      public int countByG_F(long groupId, List<Long> folderIds)
53          throws SystemException {
54  
55          Session session = null;
56  
57          try {
58              session = openSession();
59  
60              String sql = CustomSQLUtil.get(COUNT_BY_G_F);
61  
62              sql = StringUtil.replace(
63                  sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
64  
65              SQLQuery q = session.createSQLQuery(sql);
66  
67              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
68  
69              QueryPos qPos = QueryPos.getInstance(q);
70  
71              qPos.add(groupId);
72  
73              for (int i = 0; i < folderIds.size(); i++) {
74                  Long folderId = folderIds.get(i);
75  
76                  qPos.add(folderId);
77              }
78  
79              Iterator<Long> itr = q.list().iterator();
80  
81              if (itr.hasNext()) {
82                  Long count = itr.next();
83  
84                  if (count != null) {
85                      return count.intValue();
86                  }
87              }
88  
89              return 0;
90          }
91          catch (Exception e) {
92              throw new SystemException(e);
93          }
94          finally {
95              closeSession(session);
96          }
97      }
98  
99      public IGImage fetchByAnyImageId(long imageId) throws SystemException {
100         Session session = null;
101 
102         try {
103             session = openSession();
104 
105             String sql = CustomSQLUtil.get(FIND_BY_ANY_IMAGE_ID);
106 
107             SQLQuery q = session.createSQLQuery(sql);
108 
109             q.addEntity("IGImage", IGImageImpl.class);
110 
111             QueryPos qPos = QueryPos.getInstance(q);
112 
113             qPos.add(imageId);
114             qPos.add(imageId);
115             qPos.add(imageId);
116             qPos.add(imageId);
117 
118             List<IGImage> list = q.list();
119 
120             if (list.isEmpty()) {
121                 return null;
122             }
123             else {
124                 return list.get(0);
125             }
126         }
127         catch (Exception e) {
128             throw new SystemException(e);
129         }
130         finally {
131             closeSession(session);
132         }
133     }
134 
135     public IGImage findByAnyImageId(long imageId)
136         throws NoSuchImageException, SystemException {
137 
138         IGImage image = fetchByAnyImageId(imageId);
139 
140         if (image == null) {
141             throw new NoSuchImageException(
142                 "No IGImage exists with the imageId " + imageId);
143         }
144         else {
145             return image;
146         }
147     }
148 
149     public List<IGImage> findByNoAssets() throws SystemException {
150         Session session = null;
151 
152         try {
153             session = openSession();
154 
155             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
156 
157             SQLQuery q = session.createSQLQuery(sql);
158 
159             q.addEntity("IGImage", IGImageImpl.class);
160 
161             return q.list();
162         }
163         catch (Exception e) {
164             throw new SystemException(e);
165         }
166         finally {
167             closeSession(session);
168         }
169     }
170 
171     protected String getFolderIds(List<Long> folderIds) {
172         if (folderIds.isEmpty()) {
173             return StringPool.BLANK;
174         }
175 
176         StringBundler sb = new StringBundler(folderIds.size() * 2 - 1);
177 
178         for (int i = 0; i < folderIds.size(); i++) {
179             sb.append("folderId = ? ");
180 
181             if ((i + 1) != folderIds.size()) {
182                 sb.append("OR ");
183             }
184         }
185 
186         return sb.toString();
187     }
188 
189 }