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