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.bookmarks.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.bookmarks.NoSuchEntryException;
31  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
32  import com.liferay.portlet.bookmarks.model.impl.BookmarksEntryImpl;
33  import com.liferay.util.dao.orm.CustomSQLUtil;
34  
35  import java.util.Iterator;
36  import java.util.List;
37  
38  /**
39   * <a href="BookmarksEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class BookmarksEntryFinderImpl
45      extends BasePersistenceImpl implements BookmarksEntryFinder {
46  
47      public static String COUNT_BY_FOLDER_IDS =
48          BookmarksEntryFinder.class.getName() + ".countByFolderIds";
49  
50      public static String COUNT_BY_GROUP_ID =
51          BookmarksEntryFinder.class.getName() + ".countByGroupId";
52  
53      public static String COUNT_BY_G_U =
54          BookmarksEntryFinder.class.getName() + ".countByG_U";
55  
56      public static String FIND_BY_GROUP_ID =
57          BookmarksEntryFinder.class.getName() + ".findByGroupId";
58  
59      public static String FIND_BY_NO_ASSETS =
60          BookmarksEntryFinder.class.getName() + ".findByNoAssets";
61  
62      public static String FIND_BY_UUID_G =
63          BookmarksEntryFinder.class.getName() + ".findByUuid_G";
64  
65      public static String FIND_BY_G_U =
66          BookmarksEntryFinder.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) throws SystemException {
148         Session session = null;
149 
150         try {
151             session = openSession();
152 
153             String sql = CustomSQLUtil.get(COUNT_BY_G_U);
154 
155             SQLQuery q = session.createSQLQuery(sql);
156 
157             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
158 
159             QueryPos qPos = QueryPos.getInstance(q);
160 
161             qPos.add(groupId);
162             qPos.add(userId);
163 
164             Iterator<Long> itr = q.list().iterator();
165 
166             if (itr.hasNext()) {
167                 Long count = itr.next();
168 
169                 if (count != null) {
170                     return count.intValue();
171                 }
172             }
173 
174             return 0;
175         }
176         catch (Exception e) {
177             throw new SystemException(e);
178         }
179         finally {
180             closeSession(session);
181         }
182     }
183 
184     public List<BookmarksEntry> findByGroupId(long groupId, int start, int end)
185         throws SystemException {
186 
187         Session session = null;
188 
189         try {
190             session = openSession();
191 
192             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
193 
194             SQLQuery q = session.createSQLQuery(sql);
195 
196             q.addEntity("BookmarksEntry", BookmarksEntryImpl.class);
197 
198             QueryPos qPos = QueryPos.getInstance(q);
199 
200             qPos.add(groupId);
201 
202             return (List<BookmarksEntry>)QueryUtil.list(
203                 q, getDialect(), start, end);
204         }
205         catch (Exception e) {
206             throw new SystemException(e);
207         }
208         finally {
209             closeSession(session);
210         }
211     }
212 
213     public List<BookmarksEntry> findByNoAssets() throws SystemException {
214         Session session = null;
215 
216         try {
217             session = openSession();
218 
219             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
220 
221             SQLQuery q = session.createSQLQuery(sql);
222 
223             q.addEntity("BookmarksEntry", BookmarksEntryImpl.class);
224 
225             return q.list();
226         }
227         catch (Exception e) {
228             throw new SystemException(e);
229         }
230         finally {
231             closeSession(session);
232         }
233     }
234 
235     public BookmarksEntry findByUuid_G(String uuid, long groupId)
236         throws NoSuchEntryException, SystemException {
237 
238         Session session = null;
239 
240         try {
241             session = openSession();
242 
243             String sql = CustomSQLUtil.get(FIND_BY_UUID_G);
244 
245             SQLQuery q = session.createSQLQuery(sql);
246 
247             q.addEntity("BookmarksEntry", BookmarksEntryImpl.class);
248 
249             QueryPos qPos = QueryPos.getInstance(q);
250 
251             qPos.add(uuid);
252             qPos.add(groupId);
253 
254             List<BookmarksEntry> list = q.list();
255 
256             if (list.size() == 0) {
257                 StringBuilder sb = new StringBuilder();
258 
259                 sb.append("No BookmarksEntry exists with the key {uuid=");
260                 sb.append(uuid);
261                 sb.append(", groupId=");
262                 sb.append(groupId);
263                 sb.append("}");
264 
265                 throw new NoSuchEntryException(sb.toString());
266             }
267             else {
268                 return list.get(0);
269             }
270         }
271         catch (NoSuchEntryException nsee) {
272             throw nsee;
273         }
274         catch (Exception e) {
275             throw new SystemException(e);
276         }
277         finally {
278             closeSession(session);
279         }
280     }
281 
282     public List<BookmarksEntry> findByG_U(
283             long groupId, long userId, int start, int end)
284         throws SystemException {
285 
286         Session session = null;
287 
288         try {
289             session = openSession();
290 
291             String sql = CustomSQLUtil.get(FIND_BY_G_U);
292 
293             SQLQuery q = session.createSQLQuery(sql);
294 
295             q.addEntity("BookmarksEntry", BookmarksEntryImpl.class);
296 
297             QueryPos qPos = QueryPos.getInstance(q);
298 
299             qPos.add(groupId);
300             qPos.add(userId);
301 
302             return (List<BookmarksEntry>)QueryUtil.list(
303                 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 }