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.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.NoSuchFileEntryException;
32  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
33  import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
34  import com.liferay.util.dao.hibernate.QueryPos;
35  import com.liferay.util.dao.hibernate.QueryUtil;
36  
37  import java.util.Iterator;
38  import java.util.List;
39  
40  import org.hibernate.Hibernate;
41  import org.hibernate.SQLQuery;
42  import org.hibernate.Session;
43  
44  /**
45   * <a href="DLFileEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class DLFileEntryFinderImpl implements DLFileEntryFinder {
51  
52      public static String COUNT_BY_FOLDER_IDS =
53          DLFileEntryFinder.class.getName() + ".countByFolderIds";
54  
55      public static String COUNT_BY_GROUP_ID =
56          DLFileEntryFinder.class.getName() + ".countByGroupId";
57  
58      public static String COUNT_BY_G_U =
59          DLFileEntryFinder.class.getName() + ".countByG_U";
60  
61      public static String FIND_BY_GROUP_ID =
62          DLFileEntryFinder.class.getName() + ".findByGroupId";
63  
64      public static String FIND_BY_NO_ASSETS =
65          DLFileEntryFinder.class.getName() + ".findByNoAssets";
66  
67      public static String FIND_BY_UUID_G =
68          DLFileEntryFinder.class.getName() + ".findByUuid_G";
69  
70      public static String FIND_BY_G_U =
71          DLFileEntryFinder.class.getName() + ".findByG_U";
72  
73      public int countByFolderIds(List folderIds) throws SystemException {
74          Session session = null;
75  
76          try {
77              session = HibernateUtil.openSession();
78  
79              String sql = CustomSQLUtil.get(COUNT_BY_FOLDER_IDS);
80  
81              sql = StringUtil.replace(
82                  sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
83  
84              SQLQuery q = session.createSQLQuery(sql);
85  
86              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
87  
88              QueryPos qPos = QueryPos.getInstance(q);
89  
90              for (int i = 0; i < folderIds.size(); i++) {
91                  Long folderId = (Long)folderIds.get(i);
92  
93                  qPos.add(folderId);
94              }
95  
96              Iterator itr = q.list().iterator();
97  
98              if (itr.hasNext()) {
99                  Long count = (Long)itr.next();
100 
101                 if (count != null) {
102                     return count.intValue();
103                 }
104             }
105 
106             return 0;
107         }
108         catch (Exception e) {
109             throw new SystemException(e);
110         }
111         finally {
112             HibernateUtil.closeSession(session);
113         }
114     }
115 
116     public int countByGroupId(long groupId) throws SystemException {
117         Session session = null;
118 
119         try {
120             session = HibernateUtil.openSession();
121 
122             String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
123 
124             SQLQuery q = session.createSQLQuery(sql);
125 
126             q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
127 
128             QueryPos qPos = QueryPos.getInstance(q);
129 
130             qPos.add(groupId);
131 
132             Iterator itr = q.list().iterator();
133 
134             if (itr.hasNext()) {
135                 Long count = (Long)itr.next();
136 
137                 if (count != null) {
138                     return count.intValue();
139                 }
140             }
141 
142             return 0;
143         }
144         catch (Exception e) {
145             throw new SystemException(e);
146         }
147         finally {
148             HibernateUtil.closeSession(session);
149         }
150     }
151 
152     public int countByG_U(long groupId, long userId) throws SystemException {
153         Session session = null;
154 
155         try {
156             session = HibernateUtil.openSession();
157 
158             String sql = CustomSQLUtil.get(COUNT_BY_G_U);
159 
160             SQLQuery q = session.createSQLQuery(sql);
161 
162             q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
163 
164             QueryPos qPos = QueryPos.getInstance(q);
165 
166             qPos.add(groupId);
167             qPos.add(userId);
168 
169             Iterator itr = q.list().iterator();
170 
171             if (itr.hasNext()) {
172                 Long count = (Long)itr.next();
173 
174                 if (count != null) {
175                     return count.intValue();
176                 }
177             }
178 
179             return 0;
180         }
181         catch (Exception e) {
182             throw new SystemException(e);
183         }
184         finally {
185             HibernateUtil.closeSession(session);
186         }
187     }
188 
189     public List findByGroupId(long groupId, int begin, int end)
190         throws SystemException {
191 
192         return findByGroupId(groupId, begin, end, null);
193     }
194 
195     public List findByGroupId(
196             long groupId, int begin, int end, OrderByComparator obc)
197         throws SystemException {
198 
199         Session session = null;
200 
201         try {
202             session = HibernateUtil.openSession();
203 
204             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
205 
206             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
207 
208             SQLQuery q = session.createSQLQuery(sql);
209 
210             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
211 
212             QueryPos qPos = QueryPos.getInstance(q);
213 
214             qPos.add(groupId);
215 
216             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
217         }
218         catch (Exception e) {
219             throw new SystemException(e);
220         }
221         finally {
222             HibernateUtil.closeSession(session);
223         }
224     }
225 
226     public List findByNoAssets() throws SystemException {
227         Session session = null;
228 
229         try {
230             session = HibernateUtil.openSession();
231 
232             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
233 
234             SQLQuery q = session.createSQLQuery(sql);
235 
236             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
237 
238             return q.list();
239         }
240         catch (Exception e) {
241             throw new SystemException(e);
242         }
243         finally {
244             HibernateUtil.closeSession(session);
245         }
246     }
247 
248     public List findByG_U(long groupId, long userId, int begin, int end)
249         throws SystemException {
250 
251         return findByG_U(groupId, userId, begin, end, null);
252     }
253 
254     public List findByG_U(
255             long groupId, long userId, int begin, int end,
256             OrderByComparator obc)
257         throws SystemException {
258 
259         Session session = null;
260 
261         try {
262             session = HibernateUtil.openSession();
263 
264             String sql = CustomSQLUtil.get(FIND_BY_G_U);
265 
266             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
267 
268             SQLQuery q = session.createSQLQuery(sql);
269 
270             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
271 
272             QueryPos qPos = QueryPos.getInstance(q);
273 
274             qPos.add(groupId);
275             qPos.add(userId);
276 
277             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
278         }
279         catch (Exception e) {
280             throw new SystemException(e);
281         }
282         finally {
283             HibernateUtil.closeSession(session);
284         }
285     }
286 
287     public DLFileEntry findByUuid_G(String uuid, long groupId)
288         throws NoSuchFileEntryException, SystemException {
289 
290         Session session = null;
291 
292         try {
293             session = HibernateUtil.openSession();
294 
295             String sql = CustomSQLUtil.get(FIND_BY_UUID_G);
296 
297             SQLQuery q = session.createSQLQuery(sql);
298 
299             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
300 
301             QueryPos qPos = QueryPos.getInstance(q);
302 
303             qPos.add(uuid);
304             qPos.add(groupId);
305 
306             List list = q.list();
307 
308             if (list.size() == 0) {
309                 StringMaker sm = new StringMaker();
310 
311                 sm.append("No DLFileEntry exists with the key {uuid=");
312                 sm.append(uuid);
313                 sm.append(", groupId=");
314                 sm.append(groupId);
315                 sm.append("}");
316 
317                 throw new NoSuchFileEntryException(sm.toString());
318             }
319             else {
320                 return (DLFileEntry)list.get(0);
321             }
322         }
323         catch (NoSuchFileEntryException nsfee) {
324             throw nsfee;
325         }
326         catch (Exception e) {
327             throw new SystemException(e);
328         }
329         finally {
330             HibernateUtil.closeSession(session);
331         }
332     }
333 
334     protected String getFolderIds(List folderIds) {
335         StringMaker sm = new StringMaker();
336 
337         for (int i = 0; i < folderIds.size(); i++) {
338             sm.append("folderId = ? ");
339 
340             if ((i + 1) != folderIds.size()) {
341                 sm.append("OR ");
342             }
343         }
344 
345         return sm.toString();
346     }
347 
348 }