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