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