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<Long> 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 = folderIds.get(i);
92  
93                  qPos.add(folderId);
94              }
95  
96              Iterator<Long> itr = q.list().iterator();
97  
98              if (itr.hasNext()) {
99                  Long count = 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<Long> itr = q.list().iterator();
133 
134             if (itr.hasNext()) {
135                 Long count = 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<Long> itr = q.list().iterator();
170 
171             if (itr.hasNext()) {
172                 Long count = 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<DLFileEntry> findByGroupId(long groupId, int begin, int end)
190         throws SystemException {
191 
192         return findByGroupId(groupId, begin, end, null);
193     }
194 
195     public List<DLFileEntry> 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 (List<DLFileEntry>)QueryUtil.list(
217                 q, HibernateUtil.getDialect(), begin, end);
218         }
219         catch (Exception e) {
220             throw new SystemException(e);
221         }
222         finally {
223             HibernateUtil.closeSession(session);
224         }
225     }
226 
227     public List<DLFileEntry> findByNoAssets() throws SystemException {
228         Session session = null;
229 
230         try {
231             session = HibernateUtil.openSession();
232 
233             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
234 
235             SQLQuery q = session.createSQLQuery(sql);
236 
237             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
238 
239             return q.list();
240         }
241         catch (Exception e) {
242             throw new SystemException(e);
243         }
244         finally {
245             HibernateUtil.closeSession(session);
246         }
247     }
248 
249     public List<DLFileEntry> findByG_U(
250             long groupId, long userId, int begin, int end)
251         throws SystemException {
252 
253         return findByG_U(groupId, userId, begin, end, null);
254     }
255 
256     public List<DLFileEntry> findByG_U(
257             long groupId, long userId, int begin, int end,
258             OrderByComparator obc)
259         throws SystemException {
260 
261         Session session = null;
262 
263         try {
264             session = HibernateUtil.openSession();
265 
266             String sql = CustomSQLUtil.get(FIND_BY_G_U);
267 
268             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
269 
270             SQLQuery q = session.createSQLQuery(sql);
271 
272             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
273 
274             QueryPos qPos = QueryPos.getInstance(q);
275 
276             qPos.add(groupId);
277             qPos.add(userId);
278 
279             return (List<DLFileEntry>)QueryUtil.list(
280                 q, HibernateUtil.getDialect(), begin, end);
281         }
282         catch (Exception e) {
283             throw new SystemException(e);
284         }
285         finally {
286             HibernateUtil.closeSession(session);
287         }
288     }
289 
290     public DLFileEntry findByUuid_G(String uuid, long groupId)
291         throws NoSuchFileEntryException, SystemException {
292 
293         Session session = null;
294 
295         try {
296             session = HibernateUtil.openSession();
297 
298             String sql = CustomSQLUtil.get(FIND_BY_UUID_G);
299 
300             SQLQuery q = session.createSQLQuery(sql);
301 
302             q.addEntity("DLFileEntry", DLFileEntryImpl.class);
303 
304             QueryPos qPos = QueryPos.getInstance(q);
305 
306             qPos.add(uuid);
307             qPos.add(groupId);
308 
309             List<DLFileEntry> list = q.list();
310 
311             if (list.size() == 0) {
312                 StringMaker sm = new StringMaker();
313 
314                 sm.append("No DLFileEntry exists with the key {uuid=");
315                 sm.append(uuid);
316                 sm.append(", groupId=");
317                 sm.append(groupId);
318                 sm.append("}");
319 
320                 throw new NoSuchFileEntryException(sm.toString());
321             }
322             else {
323                 return list.get(0);
324             }
325         }
326         catch (NoSuchFileEntryException nsfee) {
327             throw nsfee;
328         }
329         catch (Exception e) {
330             throw new SystemException(e);
331         }
332         finally {
333             HibernateUtil.closeSession(session);
334         }
335     }
336 
337     protected String getFolderIds(List<Long> folderIds) {
338         StringMaker sm = new StringMaker();
339 
340         for (int i = 0; i < folderIds.size(); i++) {
341             sm.append("folderId = ? ");
342 
343             if ((i + 1) != folderIds.size()) {
344                 sm.append("OR ");
345             }
346         }
347 
348         return sm.toString();
349     }
350 
351 }