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