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