1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.documentlibrary.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryPos;
18  import com.liferay.portal.kernel.dao.orm.QueryUtil;
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.exception.SystemException;
23  import com.liferay.portal.kernel.util.StringBundler;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.workflow.StatusConstants;
27  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
28  import com.liferay.portlet.documentlibrary.model.DLFolder;
29  import com.liferay.util.dao.orm.CustomSQLUtil;
30  
31  import java.util.ArrayList;
32  import java.util.Iterator;
33  import java.util.List;
34  
35  /**
36   * <a href="DLFolderFinderImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class DLFolderFinderImpl
41      extends BasePersistenceImpl<DLFolder> implements DLFolderFinder {
42  
43      public static String COUNT_FE_FS_BY_G_F_S =
44          DLFolderFinder.class.getName() + ".countFE_FS_ByG_F_S";
45  
46      public static String COUNT_F_FE_FS_BY_G_F_S =
47          DLFolderFinder.class.getName() + ".countF_FE_FS_ByG_F_S";
48  
49      public static String FIND_FE_FS_BY_G_F_S =
50          DLFolderFinder.class.getName() + ".findFE_FS_ByG_F_S";
51  
52      public static String FIND_F_FE_FS_BY_G_F_S =
53          DLFolderFinder.class.getName() + ".findF_FE_FS_ByG_F_S";
54  
55      public int countFE_FS_ByG_F_S(
56              long groupId, List<Long> folderIds, int status)
57          throws SystemException {
58  
59          Session session = null;
60  
61          try {
62              session = openSession();
63  
64              String sql = CustomSQLUtil.get(COUNT_FE_FS_BY_G_F_S);
65  
66              sql = StringUtil.replace(
67                  sql, "[$FILE_ENTRY_FOLDER_ID$]",
68                  getFolderIds(folderIds, "DLFileEntry"));
69              sql = StringUtil.replace(
70                  sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
71                  getFolderIds(folderIds, "DLFileShortcut"));
72  
73              if (status == StatusConstants.ANY) {
74                  sql = StringUtil.replace(
75                      sql, "(DLFileVersion.status = ?) AND", "");
76              }
77  
78              SQLQuery q = session.createSQLQuery(sql);
79  
80              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
81  
82              QueryPos qPos = QueryPos.getInstance(q);
83  
84              qPos.add(groupId);
85  
86              if (status != StatusConstants.ANY) {
87                  qPos.add(status);
88              }
89  
90              for (Long folderId : folderIds) {
91                  qPos.add(folderId);
92              }
93  
94              qPos.add(groupId);
95  
96              for (Long folderId : folderIds) {
97                  qPos.add(folderId);
98              }
99  
100             int count = 0;
101 
102             Iterator<Long> itr = q.list().iterator();
103 
104             while (itr.hasNext()) {
105                 Long l = itr.next();
106 
107                 if (l != null) {
108                     count += l.intValue();
109                 }
110             }
111 
112             return count;
113         }
114         catch (Exception e) {
115             throw new SystemException(e);
116         }
117         finally {
118             closeSession(session);
119         }
120     }
121 
122     public int countF_FE_FS_ByG_F_S(
123             long groupId, List<Long> folderIds, int status)
124         throws SystemException {
125 
126         Session session = null;
127 
128         try {
129             session = openSession();
130 
131             String sql = CustomSQLUtil.get(COUNT_F_FE_FS_BY_G_F_S);
132 
133             sql = StringUtil.replace(
134                 sql, "[$FOLDER_PARENT_FOLDER_ID$]",
135                 getFolderIds(folderIds, "DLFolder"));
136             sql = StringUtil.replace(
137                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
138                 getFolderIds(folderIds, "DLFileEntry"));
139             sql = StringUtil.replace(
140                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
141                 getFolderIds(folderIds, "DLFileShortcut"));
142 
143             if (status == StatusConstants.ANY) {
144                 sql = StringUtil.replace(
145                     sql, "(DLFileVersion.status = ?) AND", "");
146             }
147 
148             SQLQuery q = session.createSQLQuery(sql);
149 
150             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
151 
152             QueryPos qPos = QueryPos.getInstance(q);
153 
154             qPos.add(groupId);
155 
156             if (status != StatusConstants.ANY) {
157                 qPos.add(status);
158             }
159 
160             for (Long folderId : folderIds) {
161                 qPos.add(folderId);
162             }
163 
164             qPos.add(groupId);
165 
166             for (Long folderId : folderIds) {
167                 qPos.add(folderId);
168             }
169 
170             qPos.add(groupId);
171 
172             for (Long folderId : folderIds) {
173                 qPos.add(folderId);
174             }
175 
176             int count = 0;
177 
178             Iterator<Long> itr = q.list().iterator();
179 
180             while (itr.hasNext()) {
181                 Long l = itr.next();
182 
183                 if (l != null) {
184                     count += l.intValue();
185                 }
186             }
187 
188             return count;
189         }
190         catch (Exception e) {
191             throw new SystemException(e);
192         }
193         finally {
194             closeSession(session);
195         }
196     }
197 
198     public List<Object> findFE_FS_ByG_F_S(
199             long groupId, List<Long> folderIds, int status, int start, int end)
200         throws SystemException {
201 
202         Session session = null;
203 
204         try {
205             session = openSession();
206 
207             String sql = CustomSQLUtil.get(FIND_FE_FS_BY_G_F_S);
208 
209             sql = StringUtil.replace(
210                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
211                 getFolderIds(folderIds, "DLFileEntry"));
212             sql = StringUtil.replace(
213                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
214                 getFolderIds(folderIds, "DLFileShortcut"));
215 
216             if (status == StatusConstants.ANY) {
217                 sql = StringUtil.replace(
218                     sql, "(DLFileVersion.status = ?) AND", "");
219             }
220 
221             SQLQuery q = session.createSQLQuery(sql);
222 
223             q.addScalar("modelFolderId", Type.LONG);
224             q.addScalar("name", Type.STRING);
225             q.addScalar("title", Type.STRING);
226             q.addScalar("fileShortcutId", Type.LONG);
227 
228             QueryPos qPos = QueryPos.getInstance(q);
229 
230             qPos.add(groupId);
231 
232             if (status != StatusConstants.ANY) {
233                 qPos.add(status);
234             }
235 
236             for (Long folderId : folderIds) {
237                 qPos.add(folderId);
238             }
239 
240             qPos.add(groupId);
241 
242             for (Long folderId : folderIds) {
243                 qPos.add(folderId);
244             }
245 
246             List<Object> models = new ArrayList<Object>();
247 
248             Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
249                 q, getDialect(), start, end);
250 
251             while (itr.hasNext()) {
252                 Object[] array = itr.next();
253 
254                 long folderId = (Long)array[0];
255                 String name = (String)array[1];
256                 //String title = (String)array[2];
257                 long fileShortcutId = (Long)array[3];
258 
259                 Object obj = null;
260 
261                 if (fileShortcutId > 0) {
262                     obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
263                 }
264                 else {
265                     obj = DLFileEntryUtil.findByG_F_N(groupId, folderId, name);
266                 }
267 
268                 models.add(obj);
269             }
270 
271             return models;
272         }
273         catch (Exception e) {
274             throw new SystemException(e);
275         }
276         finally {
277             closeSession(session);
278         }
279     }
280 
281     public List<Object> findF_FE_FS_ByG_F_S(
282             long groupId, List<Long> folderIds, int status, int start, int end)
283         throws SystemException {
284 
285         Session session = null;
286 
287         try {
288             session = openSession();
289 
290             String sql = CustomSQLUtil.get(FIND_F_FE_FS_BY_G_F_S);
291 
292             sql = StringUtil.replace(
293                 sql, "[$FOLDER_PARENT_FOLDER_ID$]",
294                 getFolderIds(folderIds, "DLFolder"));
295             sql = StringUtil.replace(
296                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
297                 getFolderIds(folderIds, "DLFileEntry"));
298             sql = StringUtil.replace(
299                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
300                 getFolderIds(folderIds, "DLFileShortcut"));
301 
302             if (status == StatusConstants.ANY) {
303                 sql = StringUtil.replace(
304                     sql, "(DLFileVersion.status = ?) AND", "");
305             }
306 
307             SQLQuery q = session.createSQLQuery(sql);
308 
309             q.addScalar("modelFolderId", Type.LONG);
310             q.addScalar("name", Type.STRING);
311             q.addScalar("title", Type.STRING);
312             q.addScalar("fileShortcutId", Type.LONG);
313             q.addScalar("modelFolder", Type.LONG);
314 
315             QueryPos qPos = QueryPos.getInstance(q);
316 
317             qPos.add(groupId);
318 
319             if (status != StatusConstants.ANY) {
320                 qPos.add(status);
321             }
322 
323             for (Long folderId : folderIds) {
324                 qPos.add(folderId);
325             }
326 
327             qPos.add(groupId);
328 
329             for (Long folderId : folderIds) {
330                 qPos.add(folderId);
331             }
332 
333             qPos.add(groupId);
334 
335             for (Long folderId : folderIds) {
336                 qPos.add(folderId);
337             }
338 
339             List<Object> models = new ArrayList<Object>();
340 
341             Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
342                 q, getDialect(), start, end);
343 
344             while (itr.hasNext()) {
345                 Object[] array = itr.next();
346 
347                 long folderId = (Long)array[0];
348                 String name = (String)array[1];
349                 //String title = (String)array[2];
350                 long fileShortcutId = (Long)array[3];
351                 long modelFolder = (Long)array[4];
352 
353                 Object obj = null;
354 
355                 if (modelFolder == 1) {
356                     obj = DLFolderUtil.findByPrimaryKey(folderId);
357                 }
358                 else if (fileShortcutId > 0) {
359                     obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
360                 }
361                 else {
362                     obj = DLFileEntryUtil.findByG_F_N(groupId, folderId, name);
363                 }
364 
365                 models.add(obj);
366             }
367 
368             return models;
369         }
370         catch (Exception e) {
371             throw new SystemException(e);
372         }
373         finally {
374             closeSession(session);
375         }
376     }
377 
378     protected String getFolderIds(List<Long> folderIds, String table) {
379         if (folderIds.isEmpty()) {
380             return StringPool.BLANK;
381         }
382 
383         StringBundler sb = new StringBundler(folderIds.size() * 5 - 1);
384 
385         for (int i = 0; i < folderIds.size(); i++) {
386             sb.append(table);
387             sb.append(".");
388 
389             if (table.equals("DLFolder")) {
390                 sb.append("parentFolderId");
391             }
392             else {
393                 sb.append("folderId");
394             }
395 
396             sb.append("= ? ");
397 
398             if ((i + 1) != folderIds.size()) {
399                 sb.append("OR ");
400             }
401         }
402 
403         return sb.toString();
404     }
405 
406 }