1   /**
2    * Copyright (c) 2000-2007 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.documentlibrary.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.util.OrderByComparator;
27  import com.liferay.portal.kernel.util.StringMaker;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
30  import com.liferay.portal.spring.hibernate.HibernateUtil;
31  import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
32  import com.liferay.util.dao.hibernate.QueryPos;
33  import com.liferay.util.dao.hibernate.QueryUtil;
34  
35  import java.util.Iterator;
36  import java.util.List;
37  
38  import org.hibernate.Hibernate;
39  import org.hibernate.SQLQuery;
40  import org.hibernate.Session;
41  
42  /**
43   * <a href="DLFileEntryFinder.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class DLFileEntryFinder {
49  
50      public static String COUNT_BY_FOLDER_IDS =
51          DLFileEntryFinder.class.getName() + ".countByFolderIds";
52  
53      public static String COUNT_BY_GROUP_ID =
54          DLFileEntryFinder.class.getName() + ".countByGroupId";
55  
56      public static String COUNT_BY_G_U =
57          DLFileEntryFinder.class.getName() + ".countByG_U";
58  
59      public static String FIND_BY_GROUP_ID =
60          DLFileEntryFinder.class.getName() + ".findByGroupId";
61  
62      public static String FIND_BY_NO_ASSETS =
63          DLFileEntryFinder.class.getName() + ".findByNoAssets";
64  
65      public static String FIND_BY_G_U =
66          DLFileEntryFinder.class.getName() + ".findByG_U";
67  
68      public static int countByFolderIds(List folderIds)
69          throws SystemException {
70  
71          Session session = null;
72  
73          try {
74              session = HibernateUtil.openSession();
75  
76              String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
77  
78              sql = StringUtil.replace(
79                  sql, "[$FOLDER_ID$]", _getFolderIds(folderIds));
80  
81              SQLQuery q = session.createSQLQuery(sql);
82  
83              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
84  
85              QueryPos qPos = QueryPos.getInstance(q);
86  
87              for (int i = 0; i < folderIds.size(); i++) {
88                  Long folderId = (Long)folderIds.get(i);
89  
90                  qPos.add(folderId);
91              }
92  
93              Iterator itr = q.list().iterator();
94  
95              if (itr.hasNext()) {
96                  Long count = (Long)itr.next();
97  
98                  if (count != null) {
99                      return count.intValue();
100                 }
101             }
102 
103             return 0;
104         }
105         catch (Exception e) {
106             throw new SystemException(e);
107         }
108         finally {
109             HibernateUtil.closeSession(session);
110         }
111     }
112 
113     public static int countByGroupId(long groupId) throws SystemException {
114         Session session = null;
115 
116         try {
117             session = HibernateUtil.openSession();
118 
119             String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
120 
121             SQLQuery q = session.createSQLQuery(sql);
122 
123             q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
124 
125             QueryPos qPos = QueryPos.getInstance(q);
126 
127             qPos.add(groupId);
128 
129             Iterator itr = q.list().iterator();
130 
131             if (itr.hasNext()) {
132                 Long count = (Long)itr.next();
133 
134                 if (count != null) {
135                     return count.intValue();
136                 }
137             }
138 
139             return 0;
140         }
141         catch (Exception e) {
142             throw new SystemException(e);
143         }
144         finally {
145             HibernateUtil.closeSession(session);
146         }
147     }
148 
149     public static int countByG_U(long groupId, long userId)
150         throws SystemException {
151 
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 static List findByGroupId(long groupId, int begin, int end)
189         throws SystemException {
190 
191         return findByGroupId(groupId, begin, end, null);
192     }
193 
194     public static List findByGroupId(
195             long groupId, int begin, int end, OrderByComparator obc)
196         throws SystemException {
197 
198         Session session = null;
199 
200         try {
201             session = HibernateUtil.openSession();
202 
203             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
204 
205             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
206 
207             SQLQuery q = session.createSQLQuery(sql);
208 
209             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
210 
211             QueryPos qPos = QueryPos.getInstance(q);
212 
213             qPos.add(groupId);
214 
215             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
216         }
217         catch (Exception e) {
218             throw new SystemException(e);
219         }
220         finally {
221             HibernateUtil.closeSession(session);
222         }
223     }
224 
225     public static List findByNoAssets() throws SystemException {
226         Session session = null;
227 
228         try {
229             session = HibernateUtil.openSession();
230 
231             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
232 
233             SQLQuery q = session.createSQLQuery(sql);
234 
235             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
236 
237             return q.list();
238         }
239         catch (Exception e) {
240             throw new SystemException(e);
241         }
242         finally {
243             HibernateUtil.closeSession(session);
244         }
245     }
246 
247     public static List findByG_U(long groupId, long userId, int begin, int end)
248         throws SystemException {
249 
250         return findByG_U(groupId, userId, begin, end, null);
251     }
252 
253     public static List findByG_U(
254             long groupId, long userId, int begin, int end,
255             OrderByComparator obc)
256         throws SystemException {
257 
258         Session session = null;
259 
260         try {
261             session = HibernateUtil.openSession();
262 
263             String sql = CustomSQLUtil.get(FIND_BY_G_U);
264 
265             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
266 
267             SQLQuery q = session.createSQLQuery(sql);
268 
269             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
270 
271             QueryPos qPos = QueryPos.getInstance(q);
272 
273             qPos.add(groupId);
274             qPos.add(userId);
275 
276             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
277         }
278         catch (Exception e) {
279             throw new SystemException(e);
280         }
281         finally {
282             HibernateUtil.closeSession(session);
283         }
284     }
285 
286     private static String _getFolderIds(List folderIds) {
287         StringMaker sm = new StringMaker();
288 
289         for (int i = 0; i < folderIds.size(); i++) {
290             sm.append("folderId = ? ");
291 
292             if ((i + 1) != folderIds.size()) {
293                 sm.append("OR ");
294             }
295         }
296 
297         return sm.toString();
298     }
299 
300 }