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 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 = (Long)folderIds.get(i);
91  
92                  qPos.add(folderId);
93              }
94  
95              Iterator itr = q.list().iterator();
96  
97              if (itr.hasNext()) {
98                  Long count = (Long)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 itr = q.list().iterator();
132 
133             if (itr.hasNext()) {
134                 Long count = (Long)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 itr = q.list().iterator();
169 
170             if (itr.hasNext()) {
171                 Long count = (Long)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 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 QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
207         }
208         catch (Exception e) {
209             throw new SystemException(e);
210         }
211         finally {
212             HibernateUtil.closeSession(session);
213         }
214     }
215 
216     public List findByNoAssets() throws SystemException {
217         Session session = null;
218 
219         try {
220             session = HibernateUtil.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             HibernateUtil.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 = HibernateUtil.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 list = q.list();
258 
259             if (list.size() == 0) {
260                 StringMaker sm = new StringMaker();
261 
262                 sm.append("No BookmarksEntry exists with the key {uuid=");
263                 sm.append(uuid);
264                 sm.append(", groupId=");
265                 sm.append(groupId);
266                 sm.append("}");
267 
268                 throw new NoSuchEntryException(sm.toString());
269             }
270             else {
271                 return (BookmarksEntry)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             HibernateUtil.closeSession(session);
282         }
283     }
284 
285     public List findByG_U(long groupId, long userId, int begin, int end)
286         throws SystemException {
287 
288         Session session = null;
289 
290         try {
291             session = HibernateUtil.openSession();
292 
293             String sql = CustomSQLUtil.get(FIND_BY_G_U);
294 
295             SQLQuery q = session.createSQLQuery(sql);
296 
297             q.addEntity("BookmarksEntry", BookmarksEntryImpl.class);
298 
299             QueryPos qPos = QueryPos.getInstance(q);
300 
301             qPos.add(groupId);
302             qPos.add(userId);
303 
304             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
305         }
306         catch (Exception e) {
307             throw new SystemException(e);
308         }
309         finally {
310             HibernateUtil.closeSession(session);
311         }
312     }
313 
314     protected String getFolderIds(List folderIds) {
315         StringMaker sm = new StringMaker();
316 
317         for (int i = 0; i < folderIds.size(); i++) {
318             sm.append("folderId = ? ");
319 
320             if ((i + 1) != folderIds.size()) {
321                 sm.append("OR ");
322             }
323         }
324 
325         return sm.toString();
326     }
327 
328 }