1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.bookmarks.service.persistence;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.orm.QueryPos;
19  import com.liferay.portal.kernel.dao.orm.SQLQuery;
20  import com.liferay.portal.kernel.dao.orm.Session;
21  import com.liferay.portal.kernel.dao.orm.Type;
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_FOLDER_IDS =
43          BookmarksEntryFinder.class.getName() + ".countByFolderIds";
44  
45      public static String FIND_BY_NO_ASSETS =
46          BookmarksEntryFinder.class.getName() + ".findByNoAssets";
47  
48      public int countByFolderIds(List<Long> folderIds) throws SystemException {
49          Session session = null;
50  
51          try {
52              session = openSession();
53  
54              String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
55  
56              sql = StringUtil.replace(
57                  sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
58  
59              SQLQuery q = session.createSQLQuery(sql);
60  
61              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
62  
63              QueryPos qPos = QueryPos.getInstance(q);
64  
65              for (int i = 0; i < folderIds.size(); i++) {
66                  Long folderId = folderIds.get(i);
67  
68                  qPos.add(folderId);
69              }
70  
71              Iterator<Long> itr = q.list().iterator();
72  
73              if (itr.hasNext()) {
74                  Long count = itr.next();
75  
76                  if (count != null) {
77                      return count.intValue();
78                  }
79              }
80  
81              return 0;
82          }
83          catch (Exception e) {
84              throw new SystemException(e);
85          }
86          finally {
87              closeSession(session);
88          }
89      }
90  
91      public List<BookmarksEntry> findByNoAssets() throws SystemException {
92          Session session = null;
93  
94          try {
95              session = openSession();
96  
97              String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
98  
99              SQLQuery q = session.createSQLQuery(sql);
100 
101             q.addEntity("BookmarksEntry", BookmarksEntryImpl.class);
102 
103             return q.list();
104         }
105         catch (Exception e) {
106             throw new SystemException(e);
107         }
108         finally {
109             closeSession(session);
110         }
111     }
112 
113     protected String getFolderIds(List<Long> folderIds) {
114         if (folderIds.isEmpty()) {
115             return StringPool.BLANK;
116         }
117 
118         StringBundler sb = new StringBundler(folderIds.size() * 2 - 1);
119 
120         for (int i = 0; i < folderIds.size(); i++) {
121             sb.append("folderId = ? ");
122 
123             if ((i + 1) != folderIds.size()) {
124                 sb.append("OR ");
125             }
126         }
127 
128         return sb.toString();
129     }
130 
131 }