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.WorkflowConstants;
27  import com.liferay.portal.security.permission.InlineSQLHelperUtil;
28  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
29  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
30  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
31  import com.liferay.portlet.documentlibrary.model.DLFolder;
32  import com.liferay.util.dao.orm.CustomSQLUtil;
33  
34  import java.util.ArrayList;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  /**
39   * <a href="DLFolderFinderImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class DLFolderFinderImpl
44      extends BasePersistenceImpl<DLFolder> implements DLFolderFinder {
45  
46      public static String COUNT_F_BY_G_F_S =
47          DLFolderFinder.class.getName() + ".countF_ByG_F_S";
48  
49      public static String COUNT_FE_BY_G_F_S =
50          DLFolderFinder.class.getName() + ".countFE_ByG_F_S";
51  
52      public static String COUNT_FS_BY_G_F_S =
53          DLFolderFinder.class.getName() + ".countFS_ByG_F_S";
54  
55      public static String FIND_F_BY_G_F_S =
56          DLFolderFinder.class.getName() + ".findF_ByG_F_S";
57  
58      public static String FIND_FE_BY_G_F_S =
59          DLFolderFinder.class.getName() + ".findFE_ByG_F_S";
60  
61      public static String FIND_FS_BY_G_F_S =
62          DLFolderFinder.class.getName() + ".findFS_ByG_F_S";
63  
64      public int countF_FE_FS_ByG_F_S(
65              long groupId, List<Long> folderIds, int status)
66          throws SystemException {
67  
68          return doCountF_FE_FS_ByG_F_S(groupId, folderIds, status, false);
69      }
70  
71      public int countFE_FS_ByG_F_S(
72              long groupId, List<Long> folderIds, int status)
73          throws SystemException {
74  
75          return doCountFE_FS_ByG_F_S(groupId, folderIds, status, false);
76      }
77  
78      public int filterCountF_FE_FS_ByG_F_S(
79              long groupId, List<Long> folderIds, int status)
80          throws SystemException {
81  
82          return doCountF_FE_FS_ByG_F_S(groupId, folderIds, status, true);
83      }
84  
85      public int filterCountFE_FS_ByG_F_S(
86              long groupId, List<Long> folderIds, int status)
87          throws SystemException {
88  
89          return doCountFE_FS_ByG_F_S(groupId, folderIds, status, true);
90      }
91  
92      public List<Object> filterFindF_FE_FS_ByG_F_S(
93              long groupId, List<Long> folderIds, int status, int start, int end)
94          throws SystemException {
95  
96          return doFindF_FE_FS_ByG_F_S(
97              groupId, folderIds, status, start, end, true);
98      }
99  
100     public List<Object> filterFindFE_FS_ByG_F_S(
101             long groupId, List<Long> folderIds, int status, int start, int end)
102         throws SystemException {
103 
104         return doFindFE_FS_ByG_F_S(
105             groupId, folderIds, status, start, end, true);
106     }
107 
108     public List<Object> findF_FE_FS_ByG_F_S(
109             long groupId, List<Long> folderIds, int status, int start, int end)
110         throws SystemException {
111 
112         return doFindF_FE_FS_ByG_F_S(
113             groupId, folderIds, status, start, end, false);
114     }
115 
116     public List<Object> findFE_FS_ByG_F_S(
117             long groupId, List<Long> folderIds, int status, int start, int end)
118         throws SystemException {
119 
120         return doFindFE_FS_ByG_F_S(
121             groupId, folderIds, status, start, end, false);
122     }
123 
124     protected int doCountF_FE_FS_ByG_F_S(
125             long groupId, List<Long> folderIds, int status,
126             boolean inlineSQLHelper)
127         throws SystemException {
128 
129         Session session = null;
130 
131         try {
132             session = openSession();
133 
134             StringBundler sb = new StringBundler(7);
135 
136             sb.append(StringPool.OPEN_PARENTHESIS);
137 
138             String sql = CustomSQLUtil.get(COUNT_F_BY_G_F_S);
139 
140             if (inlineSQLHelper) {
141                 sql = InlineSQLHelperUtil.replacePermissionCheck(
142                     sql, DLFolder.class.getName(), "DLFolder.folderId",
143                     "DLFolder.userId", groupId);
144             }
145 
146             sb.append(sql);
147             sb.append(") UNION ALL (");
148 
149             sql = CustomSQLUtil.get(COUNT_FE_BY_G_F_S);
150 
151             if (inlineSQLHelper) {
152                 sql = InlineSQLHelperUtil.replacePermissionCheck(
153                     sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
154                     "DLFileEntry.userId", groupId);
155             }
156 
157             sb.append(sql);
158             sb.append(") UNION ALL (");
159 
160             sql = CustomSQLUtil.get(COUNT_FS_BY_G_F_S);
161 
162             if (inlineSQLHelper) {
163                 sql = InlineSQLHelperUtil.replacePermissionCheck(
164                     sql, DLFileShortcut.class.getName(),
165                     "DLFileShortcut.fileShortcutId", "DLFileShortcut.userId",
166                     groupId);
167             }
168 
169             sb.append(sql);
170             sb.append(StringPool.CLOSE_PARENTHESIS);
171 
172             sql = sb.toString();
173 
174             sql = StringUtil.replace(
175                 sql, "[$FOLDER_PARENT_FOLDER_ID$]",
176                 getFolderIds(folderIds, "DLFolder"));
177             sql = StringUtil.replace(
178                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
179                 getFolderIds(folderIds, "DLFileEntry"));
180             sql = StringUtil.replace(
181                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
182                 getFolderIds(folderIds, "DLFileShortcut"));
183 
184             if (status == WorkflowConstants.STATUS_ANY) {
185                 sql = StringUtil.replace(
186                     sql, "(DLFileVersion.status = ?) AND", "");
187             }
188 
189             SQLQuery q = session.createSQLQuery(sql);
190 
191             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
192 
193             QueryPos qPos = QueryPos.getInstance(q);
194 
195             qPos.add(groupId);
196 
197             for (Long folderId : folderIds) {
198                 qPos.add(folderId);
199             }
200 
201             qPos.add(groupId);
202 
203             if (status != WorkflowConstants.STATUS_ANY) {
204                 qPos.add(status);
205             }
206 
207             for (Long folderId : folderIds) {
208                 qPos.add(folderId);
209             }
210 
211             qPos.add(groupId);
212 
213             for (Long folderId : folderIds) {
214                 qPos.add(folderId);
215             }
216 
217             int count = 0;
218 
219             Iterator<Long> itr = q.list().iterator();
220 
221             while (itr.hasNext()) {
222                 Long l = itr.next();
223 
224                 if (l != null) {
225                     count += l.intValue();
226                 }
227             }
228 
229             return count;
230         }
231         catch (Exception e) {
232             throw new SystemException(e);
233         }
234         finally {
235             closeSession(session);
236         }
237     }
238 
239     protected int doCountFE_FS_ByG_F_S(
240             long groupId, List<Long> folderIds, int status,
241             boolean inlineSQLHelper)
242         throws SystemException {
243 
244         Session session = null;
245 
246         try {
247             session = openSession();
248 
249             StringBundler sb = new StringBundler(5);
250 
251             sb.append(StringPool.OPEN_PARENTHESIS);
252 
253             String sql = CustomSQLUtil.get(COUNT_FE_BY_G_F_S);
254 
255             if (inlineSQLHelper) {
256                 sql = InlineSQLHelperUtil.replacePermissionCheck(
257                     sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
258                     "DLFileEntry.userId", groupId);
259             }
260 
261             sb.append(sql);
262             sb.append(") UNION ALL (");
263 
264             sql = CustomSQLUtil.get(COUNT_FS_BY_G_F_S);
265 
266             if (inlineSQLHelper) {
267                 sql = InlineSQLHelperUtil.replacePermissionCheck(
268                     sql, DLFileShortcut.class.getName(),
269                     "DLFileShortcut.fileShortcutId", "DLFileShortcut.userId",
270                     groupId);
271             }
272 
273             sb.append(sql);
274             sb.append(StringPool.CLOSE_PARENTHESIS);
275 
276             sql = sb.toString();
277 
278             sql = StringUtil.replace(
279                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
280                 getFolderIds(folderIds, "DLFileEntry"));
281             sql = StringUtil.replace(
282                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
283                 getFolderIds(folderIds, "DLFileShortcut"));
284 
285             if (status == WorkflowConstants.STATUS_ANY) {
286                 sql = StringUtil.replace(
287                     sql, "(DLFileVersion.status = ?) AND", "");
288             }
289 
290             SQLQuery q = session.createSQLQuery(sql);
291 
292             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
293 
294             QueryPos qPos = QueryPos.getInstance(q);
295 
296             qPos.add(groupId);
297 
298             if (status != WorkflowConstants.STATUS_ANY) {
299                 qPos.add(status);
300             }
301 
302             for (Long folderId : folderIds) {
303                 qPos.add(folderId);
304             }
305 
306             qPos.add(groupId);
307 
308             for (Long folderId : folderIds) {
309                 qPos.add(folderId);
310             }
311 
312             int count = 0;
313 
314             Iterator<Long> itr = q.list().iterator();
315 
316             while (itr.hasNext()) {
317                 Long l = itr.next();
318 
319                 if (l != null) {
320                     count += l.intValue();
321                 }
322             }
323 
324             return count;
325         }
326         catch (Exception e) {
327             throw new SystemException(e);
328         }
329         finally {
330             closeSession(session);
331         }
332     }
333 
334     protected List<Object> doFindF_FE_FS_ByG_F_S(
335             long groupId, List<Long> folderIds, int status, int start, int end,
336             boolean inlineSQLHelper)
337         throws SystemException {
338 
339         Session session = null;
340 
341         try {
342             session = openSession();
343 
344             StringBundler sb = new StringBundler(7);
345 
346             sb.append("SELECT * FROM ((");
347 
348             String sql = CustomSQLUtil.get(FIND_F_BY_G_F_S);
349 
350             if (inlineSQLHelper) {
351                 sql = InlineSQLHelperUtil.replacePermissionCheck(
352                     sql, DLFolder.class.getName(), "DLFolder.folderId",
353                     "DLFolder.userId", groupId);
354             }
355 
356             sb.append(sql);
357             sb.append(") UNION ALL (");
358 
359             sql = CustomSQLUtil.get(FIND_FE_BY_G_F_S);
360 
361             if (inlineSQLHelper) {
362                 sql = InlineSQLHelperUtil.replacePermissionCheck(
363                     sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
364                     "DLFileEntry.userId", groupId);
365             }
366 
367             sb.append(sql);
368             sb.append(") UNION ALL (");
369 
370             sql = CustomSQLUtil.get(FIND_FS_BY_G_F_S);
371 
372             if (inlineSQLHelper) {
373                 sql = InlineSQLHelperUtil.replacePermissionCheck(
374                     sql, DLFileShortcut.class.getName(),
375                     "DLFileShortcut.fileShortcutId", "DLFileShortcut.userId",
376                     groupId);
377             }
378 
379             sb.append(sql);
380             sb.append(")) TEMP_TABLE ORDER BY modelFolder DESC, title ASC");
381 
382             sql = sb.toString();
383 
384             sql = StringUtil.replace(
385                 sql, "[$FOLDER_PARENT_FOLDER_ID$]",
386                 getFolderIds(folderIds, "DLFolder"));
387             sql = StringUtil.replace(
388                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
389                 getFolderIds(folderIds, "DLFileEntry"));
390             sql = StringUtil.replace(
391                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
392                 getFolderIds(folderIds, "DLFileShortcut"));
393 
394             if (status == WorkflowConstants.STATUS_ANY) {
395                 sql = StringUtil.replace(
396                     sql, "(DLFileVersion.status = ?) AND", "");
397             }
398 
399             SQLQuery q = session.createSQLQuery(sql);
400 
401             q.addScalar("modelFolderId", Type.LONG);
402             q.addScalar("name", Type.STRING);
403             q.addScalar("title", Type.STRING);
404             q.addScalar("fileShortcutId", Type.LONG);
405             q.addScalar("modelFolder", Type.LONG);
406 
407             QueryPos qPos = QueryPos.getInstance(q);
408 
409             qPos.add(groupId);
410 
411             for (Long folderId : folderIds) {
412                 qPos.add(folderId);
413             }
414 
415             qPos.add(groupId);
416 
417             if (status != WorkflowConstants.STATUS_ANY) {
418                 qPos.add(status);
419             }
420 
421             for (Long folderId : folderIds) {
422                 qPos.add(folderId);
423             }
424 
425             qPos.add(groupId);
426 
427             for (Long folderId : folderIds) {
428                 qPos.add(folderId);
429             }
430 
431             List<Object> models = new ArrayList<Object>();
432 
433             Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
434                 q, getDialect(), start, end);
435 
436             while (itr.hasNext()) {
437                 Object[] array = itr.next();
438 
439                 long folderId = (Long)array[0];
440                 String name = (String)array[1];
441                 //String title = (String)array[2];
442                 long fileShortcutId = (Long)array[3];
443                 long modelFolder = (Long)array[4];
444 
445                 Object obj = null;
446 
447                 if (modelFolder == 1) {
448                     obj = DLFolderUtil.findByPrimaryKey(folderId);
449                 }
450                 else if (fileShortcutId > 0) {
451                     obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
452                 }
453                 else {
454                     obj = DLFileEntryUtil.findByG_F_N(groupId, folderId, name);
455                 }
456 
457                 models.add(obj);
458             }
459 
460             return models;
461         }
462         catch (Exception e) {
463             throw new SystemException(e);
464         }
465         finally {
466             closeSession(session);
467         }
468     }
469 
470     protected List<Object> doFindFE_FS_ByG_F_S(
471             long groupId, List<Long> folderIds, int status, int start, int end,
472             boolean inlineSQLHelper)
473         throws SystemException {
474 
475         Session session = null;
476 
477         try {
478             session = openSession();
479 
480             StringBundler sb = new StringBundler(5);
481 
482             sb.append("SELECT * FROM ((");
483 
484             String sql = CustomSQLUtil.get(FIND_FE_BY_G_F_S);
485 
486             if (inlineSQLHelper) {
487                 sql = InlineSQLHelperUtil.replacePermissionCheck(
488                     sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
489                     "DLFileEntry.userId", groupId);
490             }
491 
492             sb.append(sql);
493             sb.append(") UNION ALL (");
494 
495             sql = CustomSQLUtil.get(FIND_FS_BY_G_F_S);
496 
497             if (inlineSQLHelper) {
498                 sql = InlineSQLHelperUtil.replacePermissionCheck(
499                     sql, DLFileShortcut.class.getName(),
500                     "DLFileShortcut.fileShortcutId", "DLFileShortcut.userId",
501                     groupId);
502             }
503 
504             sb.append(sql);
505             sb.append(")) TEMP_TABLE ORDER BY modelFolder DESC, title ASC");
506 
507             sql = sb.toString();
508 
509             sql = StringUtil.replace(
510                 sql, "[$FILE_ENTRY_FOLDER_ID$]",
511                 getFolderIds(folderIds, "DLFileEntry"));
512             sql = StringUtil.replace(
513                 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
514                 getFolderIds(folderIds, "DLFileShortcut"));
515 
516             if (status == WorkflowConstants.STATUS_ANY) {
517                 sql = StringUtil.replace(
518                     sql, "(DLFileVersion.status = ?) AND", "");
519             }
520 
521             SQLQuery q = session.createSQLQuery(sql);
522 
523             q.addScalar("modelFolderId", Type.LONG);
524             q.addScalar("name", Type.STRING);
525             q.addScalar("title", Type.STRING);
526             q.addScalar("fileShortcutId", Type.LONG);
527 
528             QueryPos qPos = QueryPos.getInstance(q);
529 
530             qPos.add(groupId);
531 
532             if (status != WorkflowConstants.STATUS_ANY) {
533                 qPos.add(status);
534             }
535 
536             for (Long folderId : folderIds) {
537                 qPos.add(folderId);
538             }
539 
540             qPos.add(groupId);
541 
542             for (Long folderId : folderIds) {
543                 qPos.add(folderId);
544             }
545 
546             List<Object> models = new ArrayList<Object>();
547 
548             Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
549                 q, getDialect(), start, end);
550 
551             while (itr.hasNext()) {
552                 Object[] array = itr.next();
553 
554                 long folderId = (Long)array[0];
555                 String name = (String)array[1];
556                 //String title = (String)array[2];
557                 long fileShortcutId = (Long)array[3];
558 
559                 Object obj = null;
560 
561                 if (fileShortcutId > 0) {
562                     obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
563                 }
564                 else {
565                     obj = DLFileEntryUtil.findByG_F_N(groupId, folderId, name);
566                 }
567 
568                 models.add(obj);
569             }
570 
571             return models;
572         }
573         catch (Exception e) {
574             throw new SystemException(e);
575         }
576         finally {
577             closeSession(session);
578         }
579     }
580 
581     protected String getFolderIds(List<Long> folderIds, String table) {
582         if (folderIds.isEmpty()) {
583             return StringPool.BLANK;
584         }
585 
586         StringBundler sb = new StringBundler(folderIds.size() * 5 - 1);
587 
588         for (int i = 0; i < folderIds.size(); i++) {
589             sb.append(table);
590             sb.append(".");
591 
592             if (table.equals("DLFolder")) {
593                 sb.append("parentFolderId");
594             }
595             else {
596                 sb.append("folderId");
597             }
598 
599             sb.append("= ? ");
600 
601             if ((i + 1) != folderIds.size()) {
602                 sb.append("OR ");
603             }
604         }
605 
606         return sb.toString();
607     }
608 
609 }