001
014
015 package com.liferay.portlet.documentlibrary.service.persistence;
016
017 import com.liferay.portal.kernel.dao.orm.QueryPos;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.dao.orm.SQLQuery;
020 import com.liferay.portal.kernel.dao.orm.Session;
021 import com.liferay.portal.kernel.dao.orm.Type;
022 import com.liferay.portal.kernel.exception.SystemException;
023 import com.liferay.portal.kernel.util.OrderByComparator;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.workflow.WorkflowConstants;
028 import com.liferay.portal.security.permission.InlineSQLHelperUtil;
029 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
030 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
031 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
032 import com.liferay.portlet.documentlibrary.model.DLFolder;
033 import com.liferay.util.dao.orm.CustomSQLUtil;
034
035 import java.util.ArrayList;
036 import java.util.Iterator;
037 import java.util.List;
038
039
043 public class DLFolderFinderImpl
044 extends BasePersistenceImpl<DLFolder> implements DLFolderFinder {
045
046 public static final String COUNT_F_BY_G_M_F =
047 DLFolderFinder.class.getName() + ".countF_ByG_M_F";
048
049 public static final String COUNT_FE_BY_G_F =
050 DLFolderFinder.class.getName() + ".countFE_ByG_F";
051
052 public static final String COUNT_FE_BY_G_F_S =
053 DLFolderFinder.class.getName() + ".countFE_ByG_F_S";
054
055 public static final String COUNT_FS_BY_G_F_S =
056 DLFolderFinder.class.getName() + ".countFS_ByG_F_S";
057
058 public static final String FIND_F_BY_G_M_F =
059 DLFolderFinder.class.getName() + ".findF_ByG_M_F";
060
061 public static final String FIND_FE_BY_G_F =
062 DLFolderFinder.class.getName() + ".findFE_ByG_F";
063
064 public static final String FIND_FE_BY_G_F_S =
065 DLFolderFinder.class.getName() + ".findFE_ByG_F_S";
066
067 public static final String FIND_FS_BY_G_F_S =
068 DLFolderFinder.class.getName() + ".findFS_ByG_F_S";
069
070 public static final String JOIN_FS_BY_DL_FILE_ENTRY =
071 DLFolderFinder.class.getName() + ".joinFS_ByDLFileEntry";
072
073 public static final String JOIN_FV_BY_DL_FILE_ENTRY =
074 DLFolderFinder.class.getName() + ".joinFV_ByDLFileEntry";
075
076 public int countF_FE_FS_ByG_F_S_M_M(
077 long groupId, long folderId, int status, String[] mimeTypes,
078 boolean includeMountFolders)
079 throws SystemException {
080
081 return doCountF_FE_FS_ByG_F_S_M_M(
082 groupId, folderId, status, mimeTypes, includeMountFolders, false);
083 }
084
085 public int countFE_ByG_F_S(long groupId, long folderId, int status)
086 throws SystemException {
087
088 return doCountFE_ByG_F_S(groupId, folderId, status, false);
089 }
090
091 public int filterCountF_FE_FS_ByG_F_S_M_M(
092 long groupId, long folderId, int status, String[] mimeTypes,
093 boolean includeMountFolders)
094 throws SystemException {
095
096 return doCountF_FE_FS_ByG_F_S_M_M(
097 groupId, folderId, status, mimeTypes, includeMountFolders, true);
098 }
099
100 public int filterCountFE_ByG_F_S(long groupId, long folderId, int status)
101 throws SystemException {
102
103 return doCountFE_ByG_F_S(groupId, folderId, status, true);
104 }
105
106 public int filterCountFE_FS_ByG_F_S_M(
107 long groupId, long folderId, int status, String[] mimeTypes)
108 throws SystemException {
109
110 return doCountFE_FS_ByG_F_S_M(
111 groupId, folderId, status, mimeTypes, true);
112 }
113
114 public List<Object> filterFindF_FE_FS_ByG_F_S_M_M(
115 long groupId, long folderId, int status, String[] mimeTypes,
116 boolean includeMountFolders, int start, int end,
117 OrderByComparator obc)
118 throws SystemException {
119
120 return doFindF_FE_FS_ByG_F_S_M_M(
121 groupId, folderId, status, mimeTypes, includeMountFolders, start,
122 end, obc, true);
123 }
124
125 public List<Object> filterFindFE_FS_ByG_F_S(
126 long groupId, long folderId, int status, int start, int end)
127 throws SystemException {
128
129 return doFindFE_FS_ByG_F_S(groupId, folderId, status, start, end, true);
130 }
131
132 public List<Object> findF_FE_FS_ByG_F_S_M_M(
133 long groupId, long folderId, int status, String[] mimeTypes,
134 boolean includeMountFolders, int start, int end,
135 OrderByComparator obc)
136 throws SystemException {
137
138 return doFindF_FE_FS_ByG_F_S_M_M(
139 groupId, folderId, status, mimeTypes, includeMountFolders, start,
140 end, obc, false);
141 }
142
143 public List<Object> findFE_FS_ByG_F_S(
144 long groupId, long folderId, int status, int start, int end)
145 throws SystemException {
146
147 return doFindFE_FS_ByG_F_S(
148 groupId, folderId, status, start, end, false);
149 }
150
151 protected int doCountF_FE_FS_ByG_F_S_M_M(
152 long groupId, long folderId, int status, String[] mimeTypes,
153 boolean includeMountFolders, boolean inlineSQLHelper)
154 throws SystemException {
155
156 Session session = null;
157
158 try {
159 session = openSession();
160
161 StringBundler sb = new StringBundler(7);
162
163 sb.append(StringPool.OPEN_PARENTHESIS);
164
165 String sql = CustomSQLUtil.get(COUNT_F_BY_G_M_F);
166
167 if (inlineSQLHelper) {
168 sql = InlineSQLHelperUtil.replacePermissionCheck(
169 sql, DLFolder.class.getName(), "DLFolder.folderId",
170 groupId);
171 }
172
173 sb.append(sql);
174 sb.append(") UNION ALL (");
175 sb.append(
176 getFileEntriesSQL(groupId, status, mimeTypes, inlineSQLHelper));
177 sb.append(") UNION ALL (");
178 sb.append(
179 getFileShortcutsSQL(
180 groupId, status, mimeTypes, inlineSQLHelper));
181 sb.append(StringPool.CLOSE_PARENTHESIS);
182
183 sql = sb.toString();
184
185 sql = updateSQL(sql, folderId, status, includeMountFolders);
186
187 SQLQuery q = session.createSQLQuery(sql);
188
189 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
190
191 QueryPos qPos = QueryPos.getInstance(q);
192
193 qPos.add(groupId);
194
195 if (!includeMountFolders) {
196 qPos.add(false);
197 }
198
199 qPos.add(folderId);
200 qPos.add(groupId);
201
202 if (status != WorkflowConstants.STATUS_ANY) {
203 qPos.add(status);
204 }
205
206 qPos.add(folderId);
207 qPos.add(groupId);
208 qPos.add(folderId);
209
210 int count = 0;
211
212 Iterator<Long> itr = q.iterate();
213
214 while (itr.hasNext()) {
215 Long l = itr.next();
216
217 if (l != null) {
218 count += l.intValue();
219 }
220 }
221
222 return count;
223 }
224 catch (Exception e) {
225 throw new SystemException(e);
226 }
227 finally {
228 closeSession(session);
229 }
230 }
231
232 protected int doCountFE_ByG_F_S(
233 long groupId, long folderId, int status, boolean inlineSQLHelper)
234 throws SystemException {
235
236 Session session = null;
237
238 try {
239 session = openSession();
240
241 String sql = CustomSQLUtil.get(COUNT_FE_BY_G_F_S);
242
243 if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled(groupId)) {
244 sql = StringUtil.replace(
245 sql, "[$JOIN$]",
246 CustomSQLUtil.get(JOIN_FV_BY_DL_FILE_ENTRY));
247
248 sql = InlineSQLHelperUtil.replacePermissionCheck(
249 sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
250 groupId);
251 }
252 else {
253 sql = StringUtil.replace(sql, "[$JOIN$]", "");
254 }
255
256 sql = StringUtil.replace(
257 sql, "[$FILE_ENTRY_FOLDER_ID$]",
258 getFolderId(folderId, "DLFileVersion"));
259
260 SQLQuery q = session.createSQLQuery(sql);
261
262 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
263
264 QueryPos qPos = QueryPos.getInstance(q);
265
266 qPos.add(groupId);
267 qPos.add(status);
268 qPos.add(folderId);
269
270 Iterator<Long> itr = q.iterate();
271
272 if (itr.hasNext()) {
273 Long count = itr.next();
274
275 if (count != null) {
276 return count.intValue();
277 }
278 }
279
280 return 0;
281 }
282 catch (Exception e) {
283 throw new SystemException(e);
284 }
285 finally {
286 closeSession(session);
287 }
288 }
289
290 protected int doCountFE_FS_ByG_F_S_M(
291 long groupId, long folderId, int status, String[] mimeTypes,
292 boolean inlineSQLHelper)
293 throws SystemException {
294
295 Session session = null;
296
297 try {
298 session = openSession();
299
300 StringBundler sb = new StringBundler(7);
301
302 sb.append(StringPool.OPEN_PARENTHESIS);
303
304 String sql = getFileEntriesSQL(
305 groupId, status, mimeTypes, inlineSQLHelper);
306
307 sb.append(sql);
308 sb.append(") UNION ALL (");
309 sb.append(
310 getFileShortcutsSQL(
311 groupId, status, mimeTypes, inlineSQLHelper));
312 sb.append(StringPool.CLOSE_PARENTHESIS);
313
314 sql = sb.toString();
315
316 sql = updateSQL(sql, folderId, status, false);
317
318 SQLQuery q = session.createSQLQuery(sql);
319
320 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
321
322 QueryPos qPos = QueryPos.getInstance(q);
323
324 qPos.add(groupId);
325
326 if (status != WorkflowConstants.STATUS_ANY) {
327 qPos.add(status);
328 }
329
330 qPos.add(folderId);
331 qPos.add(groupId);
332 qPos.add(folderId);
333
334 int count = 0;
335
336 Iterator<Long> itr = q.iterate();
337
338 while (itr.hasNext()) {
339 Long l = itr.next();
340
341 if (l != null) {
342 count += l.intValue();
343 }
344 }
345
346 return count;
347 }
348 catch (Exception e) {
349 throw new SystemException(e);
350 }
351 finally {
352 closeSession(session);
353 }
354 }
355
356 protected List<Object> doFindF_FE_FS_ByG_F_S_M_M(
357 long groupId, long folderId, int status, String[] mimeTypes,
358 boolean includeMountFolders, int start, int end,
359 OrderByComparator obc, boolean inlineSQLHelper)
360 throws SystemException {
361
362 Session session = null;
363
364 try {
365 session = openSession();
366
367 StringBundler sb = new StringBundler(7);
368
369 sb.append("SELECT * FROM (");
370
371 String sql = CustomSQLUtil.get(FIND_F_BY_G_M_F);
372
373 if (inlineSQLHelper) {
374 sql = InlineSQLHelperUtil.replacePermissionCheck(
375 sql, DLFolder.class.getName(), "DLFolder.folderId",
376 groupId);
377 }
378
379 sb.append(sql);
380 sb.append(" UNION ALL ");
381
382 if (status == WorkflowConstants.STATUS_ANY) {
383 sql = CustomSQLUtil.get(FIND_FE_BY_G_F);
384 }
385 else {
386 sql = CustomSQLUtil.get(FIND_FE_BY_G_F_S);
387 }
388
389 if (inlineSQLHelper) {
390 sql = InlineSQLHelperUtil.replacePermissionCheck(
391 sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
392 groupId);
393 }
394
395 sb.append(sql);
396
397 if ((mimeTypes != null) && (mimeTypes.length > 0)) {
398 for (int i = 0; i < mimeTypes.length; i++) {
399 if (i == 0) {
400 sb.append(" AND (");
401 }
402 else {
403 sb.append(" OR");
404 }
405
406 sb.append(" DLFileEntry.mimeType = '");
407 sb.append(mimeTypes[i]);
408 sb.append("'");
409 }
410
411 sb.append(StringPool.CLOSE_PARENTHESIS);
412 }
413
414 sb.append(" UNION ALL ");
415
416 sql = CustomSQLUtil.get(FIND_FS_BY_G_F_S);
417
418 if (inlineSQLHelper) {
419 sql = InlineSQLHelperUtil.replacePermissionCheck(
420 sql, DLFileShortcut.class.getName(),
421 "DLFileShortcut.fileShortcutId", groupId);
422 }
423
424 sb.append(sql);
425
426 if ((mimeTypes != null) && (mimeTypes.length > 0)) {
427 for (int i = 0; i < mimeTypes.length; i++) {
428 if (i == 0) {
429 sb.append(" AND (");
430 }
431 else {
432 sb.append(" OR");
433 }
434
435 sb.append(" mimeType = '");
436 sb.append(mimeTypes[i]);
437 sb.append("'");
438 }
439
440 sb.append(StringPool.CLOSE_PARENTHESIS);
441 }
442
443 sb.append(") TEMP_TABLE ORDER BY modelFolder DESC, title ASC");
444
445 sql = sb.toString();
446
447 if (includeMountFolders) {
448 sql = StringUtil.replace(
449 sql, "(DLFolder.mountPoint = ?) AND", "");
450 }
451
452 sql = StringUtil.replace(
453 sql, "[$FOLDER_PARENT_FOLDER_ID$]",
454 getFolderId(folderId, "DLFolder"));
455 sql = StringUtil.replace(
456 sql, "[$FILE_ENTRY_FOLDER_ID$]",
457 getFolderId(folderId, "DLFileEntry"));
458 sql = StringUtil.replace(
459 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
460 getFolderId(folderId, "DLFileShortcut"));
461 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
462
463 SQLQuery q = session.createSQLQuery(sql);
464
465 q.addScalar("modelFolderId", Type.LONG);
466 q.addScalar("name", Type.STRING);
467 q.addScalar("title", Type.STRING);
468 q.addScalar("fileShortcutId", Type.LONG);
469 q.addScalar("modelFolder", Type.LONG);
470
471 QueryPos qPos = QueryPos.getInstance(q);
472
473 qPos.add(groupId);
474
475 if (!includeMountFolders) {
476 qPos.add(false);
477 }
478
479 qPos.add(folderId);
480 qPos.add(groupId);
481
482 if (status != WorkflowConstants.STATUS_ANY) {
483 qPos.add(status);
484 }
485
486 qPos.add(folderId);
487 qPos.add(groupId);
488 qPos.add(folderId);
489
490 List<Object> models = new ArrayList<Object>();
491
492 Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
493 q, getDialect(), start, end);
494
495 while (itr.hasNext()) {
496 Object[] array = itr.next();
497
498 long curFolderId = (Long)array[0];
499 String name = (String)array[1];
500
501 long fileShortcutId = (Long)array[3];
502 long modelFolder = (Long)array[4];
503
504 Object obj = null;
505
506 if (modelFolder == 1) {
507 obj = DLFolderUtil.findByPrimaryKey(curFolderId);
508 }
509 else if (fileShortcutId > 0) {
510 obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
511 }
512 else {
513 obj = DLFileEntryUtil.findByG_F_N(
514 groupId, curFolderId, name);
515 }
516
517 models.add(obj);
518 }
519
520 return models;
521 }
522 catch (Exception e) {
523 throw new SystemException(e);
524 }
525 finally {
526 closeSession(session);
527 }
528 }
529
530 protected List<Object> doFindFE_FS_ByG_F_S(
531 long groupId, long folderId, int status, int start, int end,
532 boolean inlineSQLHelper)
533 throws SystemException {
534
535 Session session = null;
536
537 try {
538 session = openSession();
539
540 StringBundler sb = new StringBundler(5);
541
542 sb.append("SELECT * FROM (");
543
544 String sql = null;
545
546 if (status == WorkflowConstants.STATUS_ANY) {
547 sql = CustomSQLUtil.get(FIND_FE_BY_G_F);
548 }
549 else {
550 sql = CustomSQLUtil.get(FIND_FE_BY_G_F_S);
551 }
552
553 if (inlineSQLHelper) {
554 sql = InlineSQLHelperUtil.replacePermissionCheck(
555 sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
556 groupId);
557 }
558
559 sb.append(sql);
560 sb.append(" UNION ALL ");
561
562 sql = CustomSQLUtil.get(FIND_FS_BY_G_F_S);
563
564 if (inlineSQLHelper) {
565 sql = InlineSQLHelperUtil.replacePermissionCheck(
566 sql, DLFileShortcut.class.getName(),
567 "DLFileShortcut.fileShortcutId", groupId);
568 }
569
570 sb.append(sql);
571 sb.append(") TEMP_TABLE ORDER BY modelFolder DESC, title ASC");
572
573 sql = sb.toString();
574
575 sql = StringUtil.replace(
576 sql, "[$FILE_ENTRY_FOLDER_ID$]",
577 getFolderId(folderId, "DLFileEntry"));
578 sql = StringUtil.replace(
579 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
580 getFolderId(folderId, "DLFileShortcut"));
581
582 SQLQuery q = session.createSQLQuery(sql);
583
584 q.addScalar("modelFolderId", Type.LONG);
585 q.addScalar("name", Type.STRING);
586 q.addScalar("title", Type.STRING);
587 q.addScalar("fileShortcutId", Type.LONG);
588
589 QueryPos qPos = QueryPos.getInstance(q);
590
591 qPos.add(groupId);
592
593 if (status != WorkflowConstants.STATUS_ANY) {
594 qPos.add(status);
595 }
596
597 qPos.add(folderId);
598 qPos.add(groupId);
599 qPos.add(folderId);
600
601 List<Object> models = new ArrayList<Object>();
602
603 Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
604 q, getDialect(), start, end);
605
606 while (itr.hasNext()) {
607 Object[] array = itr.next();
608
609 long folderId2 = (Long)array[0];
610 String name = (String)array[1];
611
612 long fileShortcutId = (Long)array[3];
613
614 Object obj = null;
615
616 if (fileShortcutId > 0) {
617 obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
618 }
619 else {
620 obj = DLFileEntryUtil.findByG_F_N(groupId, folderId2, name);
621 }
622
623 models.add(obj);
624 }
625
626 return models;
627 }
628 catch (Exception e) {
629 throw new SystemException(e);
630 }
631 finally {
632 closeSession(session);
633 }
634 }
635
636 protected String getFileEntriesSQL(
637 long groupId, int status, String[] mimeTypes, boolean inlineSQLHelper) {
638
639 StringBundler sb = new StringBundler();
640
641 String sql = null;
642
643 if (status == WorkflowConstants.STATUS_ANY) {
644 sql = CustomSQLUtil.get(COUNT_FE_BY_G_F);
645 }
646 else {
647 sql = CustomSQLUtil.get(COUNT_FE_BY_G_F_S);
648
649 if ((inlineSQLHelper &&
650 InlineSQLHelperUtil.isEnabled(groupId)) ||
651 ((mimeTypes != null) && (mimeTypes.length > 0))) {
652
653 sql = StringUtil.replace(
654 sql, "[$JOIN$]",
655 CustomSQLUtil.get(JOIN_FV_BY_DL_FILE_ENTRY));
656 }
657 else {
658 sql = StringUtil.replace(sql, "[$JOIN$]", "");
659 }
660 }
661
662 if (inlineSQLHelper) {
663 sql = InlineSQLHelperUtil.replacePermissionCheck(
664 sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
665 groupId);
666 }
667
668 sb.append(sql);
669
670 if ((mimeTypes != null) && (mimeTypes.length > 0)) {
671 sb.append(WHERE_AND);
672 sb.append(StringPool.OPEN_PARENTHESIS);
673 sb.append(getMimeTypes(mimeTypes, "DLFileEntry"));
674 sb.append(StringPool.CLOSE_PARENTHESIS);
675 }
676
677 return sb.toString();
678 }
679
680 protected String getFileShortcutsSQL(
681 long groupId, int status, String[] mimeTypes, boolean inlineSQLHelper) {
682
683 String sql = CustomSQLUtil.get(COUNT_FS_BY_G_F_S);
684
685 if ((inlineSQLHelper && InlineSQLHelperUtil.isEnabled(groupId)) ||
686 ((mimeTypes != null) && (mimeTypes.length > 0))) {
687
688 sql = StringUtil.replace(
689 sql, "[$JOIN$]", CustomSQLUtil.get(JOIN_FS_BY_DL_FILE_ENTRY));
690 }
691 else {
692 sql = StringUtil.replace(sql, "[$JOIN$]", "");
693 }
694
695 if (inlineSQLHelper) {
696 sql = InlineSQLHelperUtil.replacePermissionCheck(
697 sql, DLFileShortcut.class.getName(),
698 "DLFileShortcut.fileShortcutId", groupId);
699 }
700
701 StringBundler sb = new StringBundler(sql);
702
703 if ((mimeTypes != null) && (mimeTypes.length > 0)) {
704 sb.append(WHERE_AND);
705 sb.append(StringPool.OPEN_PARENTHESIS);
706 sb.append(getMimeTypes(mimeTypes, "DLFileEntry"));
707 sb.append(StringPool.CLOSE_PARENTHESIS);
708 }
709
710 return sb.toString();
711 }
712
713 protected String getFolderId(long folderId, String table) {
714 StringBundler sb = new StringBundler(4);
715
716 sb.append(table);
717 sb.append(".");
718
719 if (table.equals("DLFolder")) {
720 sb.append("parentFolderId");
721 }
722 else {
723 sb.append("folderId");
724 }
725
726 sb.append("= ? ");
727
728 return sb.toString();
729 }
730
731 protected String getMimeTypes(String[] mimeTypes, String table) {
732 if (mimeTypes.length == 0) {
733 return StringPool.BLANK;
734 }
735
736 StringBundler sb = new StringBundler(mimeTypes.length * 2 - 1);
737
738 for (int i = 0; i < mimeTypes.length; i++) {
739 sb.append(table);
740
741 sb.append(".mimeType = '");
742 sb.append(mimeTypes[i]);
743 sb.append("'");
744
745 if ((i + 1) != mimeTypes.length) {
746 sb.append(WHERE_OR);
747 }
748 }
749
750 return sb.toString();
751 }
752
753 protected String updateSQL(
754 String sql, long folderId, int status, boolean includeMountFolders) {
755
756 if (includeMountFolders) {
757 sql = StringUtil.replace(sql, "(DLFolder.mountPoint = ?) AND", "");
758 }
759
760 sql = StringUtil.replace(
761 sql, "[$FOLDER_PARENT_FOLDER_ID$]",
762 getFolderId(folderId, "DLFolder"));
763
764 if (status == WorkflowConstants.STATUS_ANY) {
765 sql = StringUtil.replace(
766 sql, "[$FILE_ENTRY_FOLDER_ID$]",
767 getFolderId(folderId, "DLFileEntry"));
768 }
769 else {
770 sql = StringUtil.replace(
771 sql, "[$FILE_ENTRY_FOLDER_ID$]",
772 getFolderId(folderId, "DLFileVersion"));
773 }
774
775 return StringUtil.replace(
776 sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
777 getFolderId(folderId, "DLFileShortcut"));
778 }
779
780 }