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.bookmarks.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.bookmarks.model.BookmarksEntry;
27  import com.liferay.portlet.bookmarks.model.impl.BookmarksEntryImpl;
28  import com.liferay.util.dao.orm.CustomSQLUtil;
29  
30  import java.util.Iterator;
31  import java.util.List;
32  
33  /**
34   * <a href="BookmarksEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class BookmarksEntryFinderImpl
39      extends BasePersistenceImpl<BookmarksEntry>
40      implements BookmarksEntryFinder {
41  
42      public static String COUNT_BY_G_F =
43          BookmarksEntryFinder.class.getName() + ".countByG_F";
44  
45      public static String FIND_BY_NO_ASSETS =
46          BookmarksEntryFinder.class.getName() + ".findByNoAssets";
47  
48      public int countByG_F(long groupId, List<Long> folderIds)
49          throws SystemException {
50  
51          Session session = null;
52  
53          try {
54              session = openSession();
55  
56              String sql = CustomSQLUtil.get(COUNT_BY_G_F);
57  
58              sql = StringUtil.replace(
59                  sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
60  
61              SQLQuery q = session.createSQLQuery(sql);
62  
63              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
64  
65              QueryPos qPos = QueryPos.getInstance(q);
66  
67              qPos.add(groupId);
68  
69              for (int i = 0; i < folderIds.size(); i++) {
70                  Long folderId = folderIds.get(i);
71  
72                  qPos.add(folderId);
73              }
74  
75              Iterator<Long> itr = q.list().iterator();
76  
77              if (itr.hasNext()) {
78                  Long count = itr.next();
79  
80                  if (count != null) {
81                      return count.intValue();
82                  }
83              }
84  
85              return 0;
86          }
87          catch (Exception e) {
88              throw new SystemException(e);
89          }
90          finally {
91              closeSession(session);
92          }
93      }
94  
95      public List<BookmarksEntry> findByNoAssets() throws SystemException {
96          Session session = null;
97  
98          try {
99              session = openSession();
100 
101             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
102 
103             SQLQuery q = session.createSQLQuery(sql);
104 
105             q.addEntity("BookmarksEntry", BookmarksEntryImpl.class);
106 
107             return q.list();
108         }
109         catch (Exception e) {
110             throw new SystemException(e);
111         }
112         finally {
113             closeSession(session);
114         }
115     }
116 
117     protected String getFolderIds(List<Long> folderIds) {
118         if (folderIds.isEmpty()) {
119             return StringPool.BLANK;
120         }
121 
122         StringBundler sb = new StringBundler(folderIds.size() * 2 - 1);
123 
124         for (int i = 0; i < folderIds.size(); i++) {
125             sb.append("folderId = ? ");
126 
127             if ((i + 1) != folderIds.size()) {
128                 sb.append("OR ");
129             }
130         }
131 
132         return sb.toString();
133     }
134 
135 }