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.ArrayUtil;
025 import com.liferay.portal.kernel.util.DateRange;
026 import com.liferay.portal.kernel.util.StringBundler;
027 import com.liferay.portal.kernel.util.StringPool;
028 import com.liferay.portal.kernel.util.StringUtil;
029 import com.liferay.portal.kernel.workflow.WorkflowConstants;
030 import com.liferay.portal.security.permission.InlineSQLHelperUtil;
031 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
032 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
033 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
034 import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
035 import com.liferay.portlet.documentlibrary.model.impl.DLFileVersionImpl;
036 import com.liferay.util.dao.orm.CustomSQLUtil;
037
038 import java.util.Collections;
039 import java.util.Iterator;
040 import java.util.List;
041
042
046 public class DLFileEntryFinderImpl
047 extends BasePersistenceImpl<DLFileEntry> implements DLFileEntryFinder {
048
049 public static final String COUNT_BY_EXTRA_SETTINGS =
050 DLFileEntryFinder.class.getName() + ".countByExtraSettings";
051
052 public static final String COUNT_BY_G_F =
053 DLFileEntryFinder.class.getName() + ".countByG_F";
054
055 public static final String COUNT_BY_G_M_R =
056 DLFileEntryFinder.class.getName() + ".countByG_M_R";
057
058 public static final String COUNT_BY_G_U_F =
059 DLFileEntryFinder.class.getName() + ".countByG_U_F";
060
061 public static final String FIND_BY_ANY_IMAGE_ID =
062 DLFileEntryFinder.class.getName() + ".findByAnyImageId";
063
064 public static final String FIND_BY_COMPANY_ID =
065 DLFileEntryFinder.class.getName() + ".findByCompanyId";
066
067 public static final String FIND_BY_EXTRA_SETTINGS =
068 DLFileEntryFinder.class.getName() + ".findByExtraSettings";
069
070 public static final String FIND_BY_G_DDM_STRUCTURE_IDS =
071 DLFileEntryFinder.class.getName() + ".findByG_DDMStructureIds";
072
073 public static final String FIND_BY_DDM_STRUCTURE_IDS =
074 DLFileEntryFinder.class.getName() + ".findByDDMStructureIds";
075
076 public static final String FIND_BY_MISVERSIONED =
077 DLFileEntryFinder.class.getName() + ".findByMisversioned";
078
079 public static final String FIND_BY_NO_ASSETS =
080 DLFileEntryFinder.class.getName() + ".findByNoAssets";
081
082 public static final String FIND_BY_ORPHANED_FILE_ENTRIES =
083 DLFileEntryFinder.class.getName() + ".findByOrphanedFileEntries";
084
085 public static final String FIND_BY_G_F =
086 DLFileEntryFinder.class.getName() + ".findByG_F";
087
088 public static final String FIND_BY_G_U_F =
089 DLFileEntryFinder.class.getName() + ".findByG_U_F";
090
091 @Override
092 public int countByExtraSettings() throws SystemException {
093 Session session = null;
094
095 try {
096 session = openSession();
097
098 String sql = CustomSQLUtil.get(COUNT_BY_EXTRA_SETTINGS);
099
100 SQLQuery q = session.createSQLQuery(sql);
101
102 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
103
104 Iterator<Long> itr = q.iterate();
105
106 if (itr.hasNext()) {
107 Long count = itr.next();
108
109 if (count != null) {
110 return count.intValue();
111 }
112 }
113
114 return 0;
115 }
116 catch (Exception e) {
117 throw new SystemException(e);
118 }
119 finally {
120 closeSession(session);
121 }
122 }
123
124 @Override
125 public int countByG_F(
126 long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
127 throws SystemException {
128
129 return doCountByG_U_F_M(
130 groupId, 0, folderIds, null, queryDefinition, false);
131 }
132
133 @Override
134 public int countByG_M_R(
135 long groupId, DateRange dateRange, long repositoryId,
136 QueryDefinition queryDefinition)
137 throws SystemException {
138
139 Session session = null;
140
141 try {
142 session = openSession();
143
144 String sql = getFileEntriesSQL(
145 COUNT_BY_G_M_R, groupId, null, null, queryDefinition, false);
146
147 if ((dateRange == null) || (dateRange.getStartDate() == null)) {
148 sql = StringUtil.replace(
149 sql, "(DLFileEntry.modifiedDate >= ?) AND",
150 StringPool.BLANK);
151 }
152
153 if ((dateRange == null) || (dateRange.getEndDate() == null)) {
154 sql = StringUtil.replace(
155 sql, "(DLFileEntry.modifiedDate <= ?) AND",
156 StringPool.BLANK);
157 }
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
167 if ((dateRange != null) && (dateRange.getStartDate() != null)) {
168 qPos.add(dateRange.getStartDate());
169 }
170
171 if ((dateRange != null) && (dateRange.getEndDate() != null)) {
172 qPos.add(dateRange.getEndDate());
173 }
174
175 qPos.add(repositoryId);
176 qPos.add(queryDefinition.getStatus());
177
178 Iterator<Long> itr = q.iterate();
179
180 if (itr.hasNext()) {
181 Long count = itr.next();
182
183 if (count != null) {
184 return count.intValue();
185 }
186 }
187
188 return 0;
189 }
190 catch (Exception e) {
191 throw new SystemException(e);
192 }
193 finally {
194 closeSession(session);
195 }
196 }
197
198 @Override
199 public int countByG_U_F_M(
200 long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
201 QueryDefinition queryDefinition)
202 throws SystemException {
203
204 return doCountByG_U_F_M(
205 groupId, userId, folderIds, mimeTypes, queryDefinition, false);
206 }
207
208 @Override
209 public int filterCountByG_U_F_M(
210 long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
211 QueryDefinition queryDefinition)
212 throws SystemException {
213
214 return doCountByG_U_F_M(
215 groupId, userId, folderIds, mimeTypes, queryDefinition, true);
216 }
217
218 @Override
219 public DLFileEntry fetchByAnyImageId(long imageId) throws SystemException {
220 Session session = null;
221
222 try {
223 session = openSession();
224
225 String sql = CustomSQLUtil.get(FIND_BY_ANY_IMAGE_ID);
226
227 SQLQuery q = session.createSQLQuery(sql);
228
229 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
230
231 QueryPos qPos = QueryPos.getInstance(q);
232
233 qPos.add(imageId);
234 qPos.add(imageId);
235 qPos.add(imageId);
236 qPos.add(imageId);
237
238 List<DLFileEntry> dlFileEntries = q.list();
239
240 if (!dlFileEntries.isEmpty()) {
241 return dlFileEntries.get(0);
242 }
243
244 return null;
245 }
246 catch (Exception e) {
247 throw new SystemException(e);
248 }
249 finally {
250 closeSession(session);
251 }
252 }
253
254 @Override
255 public int filterCountByG_F(
256 long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
257 throws SystemException {
258
259 return doCountByG_U_F_M(
260 groupId, 0, folderIds, null, queryDefinition, true);
261 }
262
263 @Override
264 public List<DLFileEntry> filterFindByG_F(
265 long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
266 throws SystemException {
267
268 return doFindByG_U_F_M(
269 groupId, 0, folderIds, null, queryDefinition, true);
270 }
271
272 @Override
273 public List<DLFileEntry> filterFindByG_U_F_M(
274 long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
275 QueryDefinition queryDefinition)
276 throws SystemException {
277
278 return doFindByG_U_F_M(
279 groupId, userId, folderIds, mimeTypes, queryDefinition, true);
280 }
281
282 @Override
283 public DLFileEntry findByAnyImageId(long imageId)
284 throws NoSuchFileEntryException, SystemException {
285
286 DLFileEntry dlFileEntry = fetchByAnyImageId(imageId);
287
288 if (dlFileEntry != null) {
289 return dlFileEntry;
290 }
291
292 throw new NoSuchFileEntryException(
293 "No DLFileEntry exists with the imageId " + imageId);
294 }
295
296 @Override
297 public List<DLFileEntry> findByCompanyId(
298 long companyId, QueryDefinition queryDefinition)
299 throws SystemException {
300
301 Session session = null;
302
303 try {
304 session = openSession();
305
306 String sql = CustomSQLUtil.get(
307 FIND_BY_COMPANY_ID, queryDefinition,
308 DLFileVersionImpl.TABLE_NAME);
309
310 sql = CustomSQLUtil.replaceOrderBy(
311 sql, queryDefinition.getOrderByComparator());
312
313 SQLQuery q = session.createSQLQuery(sql);
314
315 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
316
317 QueryPos qPos = QueryPos.getInstance(q);
318
319 qPos.add(companyId);
320 qPos.add(queryDefinition.getStatus());
321
322 return (List<DLFileEntry>)QueryUtil.list(
323 q, getDialect(), queryDefinition.getStart(),
324 queryDefinition.getEnd());
325 }
326 catch (Exception e) {
327 throw new SystemException(e);
328 }
329 finally {
330 closeSession(session);
331 }
332 }
333
334 @Override
335 public List<DLFileEntry> findByDDMStructureIds(
336 long[] ddmStructureIds, int start, int end)
337 throws SystemException {
338
339 return findByDDMStructureIds(0, ddmStructureIds, start, end);
340 }
341
342 @Override
343 public List<DLFileEntry> findByDDMStructureIds(
344 long groupId, long[] ddmStructureIds, int start, int end)
345 throws SystemException {
346
347 Session session = null;
348
349 try {
350 session = openSession();
351
352 String sql = CustomSQLUtil.get(FIND_BY_DDM_STRUCTURE_IDS);
353
354 if ((ddmStructureIds == null) || (ddmStructureIds.length <= 0)) {
355 return Collections.emptyList();
356 }
357
358 if (groupId <= 0) {
359 sql = StringUtil.replace(
360 sql, "(DLFileEntry.groupId = ?) AND", StringPool.BLANK);
361 }
362
363 sql = StringUtil.replace(
364 sql, "[$DDM_STRUCTURE_ID$]",
365 getDDMStructureIds(ddmStructureIds));
366
367 SQLQuery q = session.createSQLQuery(sql);
368
369 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
370
371 QueryPos qPos = QueryPos.getInstance(q);
372
373 if (groupId > 0) {
374 qPos.add(groupId);
375 }
376
377 qPos.add(ddmStructureIds);
378
379 return (List<DLFileEntry>)QueryUtil.list(
380 q, getDialect(), start, end);
381 }
382 catch (Exception e) {
383 throw new SystemException(e);
384 }
385 finally {
386 closeSession(session);
387 }
388 }
389
390 @Override
391 public List<DLFileEntry> findByExtraSettings(int start, int end)
392 throws SystemException {
393
394 Session session = null;
395
396 try {
397 session = openSession();
398
399 String sql = CustomSQLUtil.get(FIND_BY_EXTRA_SETTINGS);
400
401 SQLQuery q = session.createSQLQuery(sql);
402
403 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
404
405 return (List<DLFileEntry>)QueryUtil.list(
406 q, getDialect(), start, end);
407 }
408 catch (Exception e) {
409 throw new SystemException(e);
410 }
411 finally {
412 closeSession(session);
413 }
414 }
415
416 @Override
417 public List<DLFileEntry> findByMisversioned() throws SystemException {
418 Session session = null;
419
420 try {
421 session = openSession();
422
423 String sql = CustomSQLUtil.get(FIND_BY_MISVERSIONED);
424
425 SQLQuery q = session.createSQLQuery(sql);
426
427 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
428
429 return q.list(true);
430 }
431 catch (Exception e) {
432 throw new SystemException(e);
433 }
434 finally {
435 closeSession(session);
436 }
437 }
438
439 @Override
440 public List<DLFileEntry> findByNoAssets() throws SystemException {
441 Session session = null;
442
443 try {
444 session = openSession();
445
446 String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
447
448 SQLQuery q = session.createSQLQuery(sql);
449
450 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
451
452 return q.list(true);
453 }
454 catch (Exception e) {
455 throw new SystemException(e);
456 }
457 finally {
458 closeSession(session);
459 }
460 }
461
462 @Override
463 public List<DLFileEntry> findByOrphanedFileEntries()
464 throws SystemException {
465
466 Session session = null;
467
468 try {
469 session = openSession();
470
471 String sql = CustomSQLUtil.get(FIND_BY_ORPHANED_FILE_ENTRIES);
472
473 SQLQuery q = session.createSQLQuery(sql);
474
475 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
476
477 return q.list(true);
478 }
479 catch (Exception e) {
480 throw new SystemException(e);
481 }
482 finally {
483 closeSession(session);
484 }
485 }
486
487 @Override
488 public List<DLFileEntry> findByG_F(
489 long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
490 throws SystemException {
491
492 return doFindByG_U_F_M(
493 groupId, 0, folderIds, null, queryDefinition, false);
494 }
495
496 @Override
497 public List<DLFileEntry> findByG_U_F_M(
498 long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
499 QueryDefinition queryDefinition)
500 throws SystemException {
501
502 return doFindByG_U_F_M(
503 groupId, userId, folderIds, mimeTypes, queryDefinition, false);
504 }
505
506 protected int doCountByG_U_F_M(
507 long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
508 QueryDefinition queryDefinition, boolean inlineSQLHelper)
509 throws SystemException {
510
511 Session session = null;
512
513 try {
514 session = openSession();
515
516 String id = null;
517
518 if (userId <= 0) {
519 id = COUNT_BY_G_F;
520 }
521 else {
522 id = COUNT_BY_G_U_F;
523 }
524
525 String sql = getFileEntriesSQL(
526 id, groupId, folderIds, mimeTypes, queryDefinition,
527 inlineSQLHelper);
528
529 SQLQuery q = session.createSQLQuery(sql);
530
531 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
532
533 QueryPos qPos = QueryPos.getInstance(q);
534
535 qPos.add(groupId);
536
537 if (userId > 0) {
538 qPos.add(userId);
539 }
540
541 qPos.add(queryDefinition.getStatus());
542
543 for (Long folderId : folderIds) {
544 qPos.add(folderId);
545 }
546
547 if (mimeTypes != null) {
548 qPos.add(mimeTypes);
549 }
550
551 Iterator<Long> itr = q.iterate();
552
553 if (itr.hasNext()) {
554 Long count = itr.next();
555
556 if (count != null) {
557 return count.intValue();
558 }
559 }
560
561 return 0;
562 }
563 catch (Exception e) {
564 throw new SystemException(e);
565 }
566 finally {
567 closeSession(session);
568 }
569 }
570
571 protected List<DLFileEntry> doFindByG_U_F_M(
572 long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
573 QueryDefinition queryDefinition, boolean inlineSQLHelper)
574 throws SystemException {
575
576 Session session = null;
577
578 try {
579 session = openSession();
580
581 String id = null;
582
583 if (userId <= 0) {
584 id = FIND_BY_G_F;
585 }
586 else {
587 id = FIND_BY_G_U_F;
588 }
589
590 String sql = getFileEntriesSQL(
591 id, groupId, folderIds, mimeTypes, queryDefinition,
592 inlineSQLHelper);
593
594 sql = CustomSQLUtil.replaceOrderBy(
595 sql, queryDefinition.getOrderByComparator());
596
597 SQLQuery q = session.createSQLQuery(sql);
598
599 q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
600
601 QueryPos qPos = QueryPos.getInstance(q);
602
603 qPos.add(groupId);
604
605 if (userId > 0) {
606 qPos.add(userId);
607 }
608
609 qPos.add(queryDefinition.getStatus());
610
611 for (Long folderId : folderIds) {
612 qPos.add(folderId);
613 }
614
615 if (mimeTypes != null) {
616 qPos.add(mimeTypes);
617 }
618
619 return (List<DLFileEntry>)QueryUtil.list(
620 q, getDialect(), queryDefinition.getStart(),
621 queryDefinition.getEnd());
622 }
623 catch (Exception e) {
624 throw new SystemException(e);
625 }
626 finally {
627 closeSession(session);
628 }
629 }
630
631 protected String getDDMStructureIds(long[] ddmStructureIds) {
632 StringBundler sb = new StringBundler(
633 (ddmStructureIds.length * 2 - 1) + 2);
634
635 sb.append(StringPool.OPEN_PARENTHESIS);
636
637 for (int i = 0; i < ddmStructureIds.length; i++) {
638 sb.append("DLFileEntryTypes_DDMStructures.structureId = ?");
639
640 if ((i + 1) != ddmStructureIds.length) {
641 sb.append(WHERE_OR);
642 }
643 }
644
645 sb.append(StringPool.CLOSE_PARENTHESIS);
646
647 return sb.toString();
648 }
649
650 protected String getFileEntriesSQL(
651 String id, long groupId, List<Long> folderIds, String[] mimeTypes,
652 QueryDefinition queryDefinition, boolean inlineSQLHelper) {
653
654 String tableName = DLFileVersionImpl.TABLE_NAME;
655
656 String sql = CustomSQLUtil.get(id, queryDefinition, tableName);
657
658 if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
659 sql = StringUtil.replace(sql, "[$JOIN$]", StringPool.BLANK);
660
661 tableName = DLFileEntryImpl.TABLE_NAME;
662 }
663 else {
664 sql = StringUtil.replace(
665 sql, "[$JOIN$]",
666 CustomSQLUtil.get(
667 DLFolderFinderImpl.JOIN_FE_BY_DL_FILE_VERSION));
668 }
669
670 if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled()) {
671 sql = InlineSQLHelperUtil.replacePermissionCheck(
672 sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
673 groupId);
674 }
675
676 StringBundler sb = new StringBundler(7);
677
678 if (((folderIds != null) && !folderIds.isEmpty()) ||
679 ArrayUtil.isNotEmpty(mimeTypes)) {
680
681 if ((folderIds != null) && !folderIds.isEmpty()) {
682 sb.append(WHERE_AND);
683 sb.append(StringPool.OPEN_PARENTHESIS);
684 sb.append(getFolderIds(folderIds, tableName));
685 sb.append(StringPool.CLOSE_PARENTHESIS);
686 }
687
688 if (ArrayUtil.isNotEmpty(mimeTypes)) {
689 sb.append(WHERE_AND);
690 sb.append(StringPool.OPEN_PARENTHESIS);
691 sb.append(getMimeTypes(mimeTypes, tableName));
692 sb.append(StringPool.CLOSE_PARENTHESIS);
693 }
694
695 return StringUtil.replace(sql, "[$FOLDER_ID$]", sb.toString());
696 }
697 else {
698 return StringUtil.replace(sql, "[$FOLDER_ID$]", StringPool.BLANK);
699 }
700 }
701
702 protected String getFolderIds(List<Long> folderIds, String tableName) {
703 if (folderIds.isEmpty()) {
704 return StringPool.BLANK;
705 }
706
707 StringBundler sb = new StringBundler(folderIds.size() * 3 + 1);
708
709 sb.append(StringPool.OPEN_PARENTHESIS);
710
711 for (int i = 0; i < folderIds.size(); i++) {
712 sb.append(tableName);
713 sb.append(".folderId = ? ");
714
715 if ((i + 1) != folderIds.size()) {
716 sb.append(WHERE_OR);
717 }
718 }
719
720 sb.append(StringPool.CLOSE_PARENTHESIS);
721
722 return sb.toString();
723 }
724
725 protected String getMimeTypes(String[] mimeTypes, String tableName) {
726 if (mimeTypes.length == 0) {
727 return StringPool.BLANK;
728 }
729
730 StringBundler sb = new StringBundler(mimeTypes.length * 2 - 1);
731
732 for (int i = 0; i < mimeTypes.length; i++) {
733 sb.append(tableName);
734 sb.append(".mimeType = ?");
735
736 if ((i + 1) != mimeTypes.length) {
737 sb.append(WHERE_OR);
738 }
739 }
740
741 return sb.toString();
742 }
743
744 }