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