001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.service.persistence.impl;
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.ListUtil;
027    import com.liferay.portal.kernel.util.StringBundler;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.kernel.workflow.WorkflowConstants;
031    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
032    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
033    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
034    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
035    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
036    import com.liferay.portlet.documentlibrary.model.impl.DLFileVersionImpl;
037    import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryFinder;
038    import com.liferay.util.dao.orm.CustomSQLUtil;
039    
040    import java.util.Collections;
041    import java.util.Iterator;
042    import java.util.List;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     * @author Shuyang Zhou
047     */
048    public class DLFileEntryFinderImpl
049            extends BasePersistenceImpl<DLFileEntry> implements DLFileEntryFinder {
050    
051            public static final String COUNT_BY_EXTRA_SETTINGS =
052                    DLFileEntryFinder.class.getName() + ".countByExtraSettings";
053    
054            public static final String COUNT_BY_G_F =
055                    DLFileEntryFinder.class.getName() + ".countByG_F";
056    
057            public static final String COUNT_BY_G_M_R =
058                    DLFileEntryFinder.class.getName() + ".countByG_M_R";
059    
060            public static final String COUNT_BY_G_U_F =
061                    DLFileEntryFinder.class.getName() + ".countByG_U_F";
062    
063            public static final String FIND_BY_ANY_IMAGE_ID =
064                    DLFileEntryFinder.class.getName() + ".findByAnyImageId";
065    
066            public static final String FIND_BY_COMPANY_ID =
067                    DLFileEntryFinder.class.getName() + ".findByCompanyId";
068    
069            public static final String FIND_BY_EXTRA_SETTINGS =
070                    DLFileEntryFinder.class.getName() + ".findByExtraSettings";
071    
072            public static final String FIND_BY_DDM_STRUCTURE_IDS =
073                    DLFileEntryFinder.class.getName() + ".findByDDMStructureIds";
074    
075            public static final String FIND_BY_MISVERSIONED =
076                    DLFileEntryFinder.class.getName() + ".findByMisversioned";
077    
078            public static final String FIND_BY_NO_ASSETS =
079                    DLFileEntryFinder.class.getName() + ".findByNoAssets";
080    
081            public static final String FIND_BY_ORPHANED_FILE_ENTRIES =
082                    DLFileEntryFinder.class.getName() + ".findByOrphanedFileEntries";
083    
084            public static final String FIND_BY_G_F =
085                    DLFileEntryFinder.class.getName() + ".findByG_F";
086    
087            public static final String FIND_BY_G_U_F =
088                    DLFileEntryFinder.class.getName() + ".findByG_U_F";
089    
090            @Override
091            public int countByExtraSettings() {
092                    Session session = null;
093    
094                    try {
095                            session = openSession();
096    
097                            String sql = CustomSQLUtil.get(COUNT_BY_EXTRA_SETTINGS);
098    
099                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
100    
101                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
102    
103                            Iterator<Long> itr = q.iterate();
104    
105                            if (itr.hasNext()) {
106                                    Long count = itr.next();
107    
108                                    if (count != null) {
109                                            return count.intValue();
110                                    }
111                            }
112    
113                            return 0;
114                    }
115                    catch (Exception e) {
116                            throw new SystemException(e);
117                    }
118                    finally {
119                            closeSession(session);
120                    }
121            }
122    
123            @Override
124            public int countByG_F(
125                    long groupId, List<Long> folderIds,
126                    QueryDefinition<DLFileEntry> queryDefinition) {
127    
128                    List<Long> repositoryIds = Collections.emptyList();
129    
130                    return doCountByG_U_R_F_M(
131                            groupId, 0, repositoryIds, folderIds, null, queryDefinition, false);
132            }
133    
134            /**
135             * @deprecated As of 7.0.0, with no direct replacement
136             */
137            @Deprecated
138            @Override
139            public int countByG_M_R(
140                    long groupId, DateRange dateRange, long repositoryId,
141                    QueryDefinition<DLFileEntry> queryDefinition) {
142    
143                    return 0;
144            }
145    
146            @Override
147            public int countByG_R_F(
148                    long groupId, List<Long> repositoryIds, List<Long> folderIds,
149                    QueryDefinition<DLFileEntry> queryDefinition) {
150    
151                    return doCountByG_U_R_F_M(
152                            groupId, 0, repositoryIds, folderIds, null, queryDefinition, false);
153            }
154    
155            @Override
156            public int countByG_U_F_M(
157                    long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
158                    QueryDefinition<DLFileEntry> queryDefinition) {
159    
160                    List<Long> repositoryIds = Collections.emptyList();
161    
162                    return doCountByG_U_R_F_M(
163                            groupId, userId, repositoryIds, folderIds, mimeTypes,
164                            queryDefinition, false);
165            }
166    
167            @Override
168            public int countByG_U_R_F_M(
169                    long groupId, long userId, List<Long> repositoryIds,
170                    List<Long> folderIds, String[] mimeTypes,
171                    QueryDefinition<DLFileEntry> queryDefinition) {
172    
173                    return doCountByG_U_R_F_M(
174                            groupId, userId, repositoryIds, folderIds, mimeTypes,
175                            queryDefinition, false);
176            }
177    
178            @Override
179            public int filterCountByG_U_F_M(
180                    long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
181                    QueryDefinition<DLFileEntry> queryDefinition) {
182    
183                    List<Long> repositoryIds = Collections.emptyList();
184    
185                    return doCountByG_U_R_F_M(
186                            groupId, userId, repositoryIds, folderIds, mimeTypes,
187                            queryDefinition, true);
188            }
189    
190            @Override
191            public int filterCountByG_U_R_F_M(
192                    long groupId, long userId, List<Long> repositoryIds,
193                    List<Long> folderIds, String[] mimeTypes,
194                    QueryDefinition<DLFileEntry> queryDefinition) {
195    
196                    return doCountByG_U_R_F_M(
197                            groupId, userId, repositoryIds, folderIds, mimeTypes,
198                            queryDefinition, true);
199            }
200    
201            @Override
202            public DLFileEntry fetchByAnyImageId(long imageId) {
203                    Session session = null;
204    
205                    try {
206                            session = openSession();
207    
208                            String sql = CustomSQLUtil.get(FIND_BY_ANY_IMAGE_ID);
209    
210                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
211    
212                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
213    
214                            q.setMaxResults(1);
215    
216                            QueryPos qPos = QueryPos.getInstance(q);
217    
218                            qPos.add(imageId);
219                            qPos.add(imageId);
220                            qPos.add(imageId);
221                            qPos.add(imageId);
222    
223                            List<DLFileEntry> dlFileEntries = q.list();
224    
225                            if (!dlFileEntries.isEmpty()) {
226                                    return dlFileEntries.get(0);
227                            }
228    
229                            return null;
230                    }
231                    catch (Exception e) {
232                            throw new SystemException(e);
233                    }
234                    finally {
235                            closeSession(session);
236                    }
237            }
238    
239            @Override
240            public int filterCountByG_F(
241                    long groupId, List<Long> folderIds,
242                    QueryDefinition<DLFileEntry> queryDefinition) {
243    
244                    List<Long> repositoryIds = Collections.emptyList();
245    
246                    return doCountByG_U_R_F_M(
247                            groupId, 0, repositoryIds, folderIds, null, queryDefinition, true);
248            }
249    
250            @Override
251            public int filterCountByG_R_F(
252                    long groupId, List<Long> repositoryIds, List<Long> folderIds,
253                    QueryDefinition<DLFileEntry> queryDefinition) {
254    
255                    return doCountByG_U_R_F_M(
256                            groupId, 0, repositoryIds, folderIds, null, queryDefinition, true);
257            }
258    
259            @Override
260            public List<DLFileEntry> filterFindByG_F(
261                    long groupId, List<Long> folderIds,
262                    QueryDefinition<DLFileEntry> queryDefinition) {
263    
264                    List<Long> repositoryIds = Collections.emptyList();
265    
266                    return doFindByG_U_R_F_M(
267                            groupId, 0, repositoryIds, folderIds, null, queryDefinition, true);
268            }
269    
270            @Override
271            public List<DLFileEntry> filterFindByG_R_F(
272                    long groupId, List<Long> repositoryIds, List<Long> folderIds,
273                    QueryDefinition<DLFileEntry> queryDefinition) {
274    
275                    return doFindByG_U_R_F_M(
276                            groupId, 0, repositoryIds, folderIds, null, queryDefinition, true);
277            }
278    
279            @Override
280            public List<DLFileEntry> filterFindByG_U_F_M(
281                    long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
282                    QueryDefinition<DLFileEntry> queryDefinition) {
283    
284                    List<Long> repositoryIds = Collections.emptyList();
285    
286                    return doFindByG_U_R_F_M(
287                            groupId, userId, repositoryIds, folderIds, mimeTypes,
288                            queryDefinition, true);
289            }
290    
291            @Override
292            public List<DLFileEntry> filterFindByG_U_R_F_M(
293                    long groupId, long userId, List<Long> repositoryIds,
294                    List<Long> folderIds, String[] mimeTypes,
295                    QueryDefinition<DLFileEntry> queryDefinition) {
296    
297                    return doFindByG_U_R_F_M(
298                            groupId, userId, repositoryIds, folderIds, mimeTypes,
299                            queryDefinition, true);
300            }
301    
302            @Override
303            public DLFileEntry findByAnyImageId(long imageId)
304                    throws NoSuchFileEntryException {
305    
306                    DLFileEntry dlFileEntry = fetchByAnyImageId(imageId);
307    
308                    if (dlFileEntry != null) {
309                            return dlFileEntry;
310                    }
311    
312                    throw new NoSuchFileEntryException(
313                            "No DLFileEntry exists with the imageId " + imageId);
314            }
315    
316            @Override
317            public List<DLFileEntry> findByCompanyId(
318                    long companyId, QueryDefinition<DLFileEntry> queryDefinition) {
319    
320                    Session session = null;
321    
322                    try {
323                            session = openSession();
324    
325                            String sql = CustomSQLUtil.get(
326                                    FIND_BY_COMPANY_ID, queryDefinition,
327                                    DLFileVersionImpl.TABLE_NAME);
328    
329                            sql = CustomSQLUtil.replaceOrderBy(
330                                    sql, queryDefinition.getOrderByComparator());
331    
332                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
333    
334                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
335    
336                            QueryPos qPos = QueryPos.getInstance(q);
337    
338                            qPos.add(companyId);
339                            qPos.add(queryDefinition.getStatus());
340    
341                            return (List<DLFileEntry>)QueryUtil.list(
342                                    q, getDialect(), queryDefinition.getStart(),
343                                    queryDefinition.getEnd());
344                    }
345                    catch (Exception e) {
346                            throw new SystemException(e);
347                    }
348                    finally {
349                            closeSession(session);
350                    }
351            }
352    
353            @Override
354            public List<DLFileEntry> findByDDMStructureIds(
355                    long[] ddmStructureIds, int start, int end) {
356    
357                    return findByDDMStructureIds(0, ddmStructureIds, start, end);
358            }
359    
360            @Override
361            public List<DLFileEntry> findByDDMStructureIds(
362                    long groupId, long[] ddmStructureIds, int start, int end) {
363    
364                    Session session = null;
365    
366                    try {
367                            session = openSession();
368    
369                            String sql = CustomSQLUtil.get(FIND_BY_DDM_STRUCTURE_IDS);
370    
371                            if ((ddmStructureIds == null) || (ddmStructureIds.length <= 0)) {
372                                    return Collections.emptyList();
373                            }
374    
375                            if (groupId <= 0) {
376                                    sql = StringUtil.replace(
377                                            sql, "(DLFileEntry.groupId = ?) AND", StringPool.BLANK);
378                            }
379    
380                            sql = StringUtil.replace(
381                                    sql, "[$DDM_STRUCTURE_ID$]",
382                                    getDDMStructureIds(ddmStructureIds));
383    
384                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
385    
386                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
387    
388                            QueryPos qPos = QueryPos.getInstance(q);
389    
390                            if (groupId > 0) {
391                                    qPos.add(groupId);
392                            }
393    
394                            qPos.add(ddmStructureIds);
395    
396                            return (List<DLFileEntry>)QueryUtil.list(
397                                    q, getDialect(), start, end);
398                    }
399                    catch (Exception e) {
400                            throw new SystemException(e);
401                    }
402                    finally {
403                            closeSession(session);
404                    }
405            }
406    
407            @Override
408            public List<DLFileEntry> findByExtraSettings(int start, int end) {
409                    Session session = null;
410    
411                    try {
412                            session = openSession();
413    
414                            String sql = CustomSQLUtil.get(FIND_BY_EXTRA_SETTINGS);
415    
416                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
417    
418                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
419    
420                            return (List<DLFileEntry>)QueryUtil.list(
421                                    q, getDialect(), start, end);
422                    }
423                    catch (Exception e) {
424                            throw new SystemException(e);
425                    }
426                    finally {
427                            closeSession(session);
428                    }
429            }
430    
431            @Override
432            public List<DLFileEntry> findByMisversioned() {
433                    Session session = null;
434    
435                    try {
436                            session = openSession();
437    
438                            String sql = CustomSQLUtil.get(FIND_BY_MISVERSIONED);
439    
440                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
441    
442                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
443    
444                            return q.list(true);
445                    }
446                    catch (Exception e) {
447                            throw new SystemException(e);
448                    }
449                    finally {
450                            closeSession(session);
451                    }
452            }
453    
454            @Override
455            public List<DLFileEntry> findByNoAssets() {
456                    Session session = null;
457    
458                    try {
459                            session = openSession();
460    
461                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
462    
463                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
464    
465                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
466    
467                            return q.list(true);
468                    }
469                    catch (Exception e) {
470                            throw new SystemException(e);
471                    }
472                    finally {
473                            closeSession(session);
474                    }
475            }
476    
477            @Override
478            public List<DLFileEntry> findByOrphanedFileEntries() {
479                    Session session = null;
480    
481                    try {
482                            session = openSession();
483    
484                            String sql = CustomSQLUtil.get(FIND_BY_ORPHANED_FILE_ENTRIES);
485    
486                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
487    
488                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
489    
490                            return q.list(true);
491                    }
492                    catch (Exception e) {
493                            throw new SystemException(e);
494                    }
495                    finally {
496                            closeSession(session);
497                    }
498            }
499    
500            @Override
501            public List<DLFileEntry> findByG_F(
502                    long groupId, List<Long> folderIds,
503                    QueryDefinition<DLFileEntry> queryDefinition) {
504    
505                    List<Long> repositoryIds = Collections.emptyList();
506    
507                    return doFindByG_U_R_F_M(
508                            groupId, 0, repositoryIds, folderIds, null, queryDefinition, false);
509            }
510    
511            @Override
512            public List<DLFileEntry> findByG_R_F(
513                    long groupId, List<Long> repositoryIds, List<Long> folderIds,
514                    QueryDefinition<DLFileEntry> queryDefinition) {
515    
516                    return doFindByG_U_R_F_M(
517                            groupId, 0, repositoryIds, folderIds, null, queryDefinition, false);
518            }
519    
520            @Override
521            public List<DLFileEntry> findByG_U_F(
522                    long groupId, long userId, List<Long> folderIds,
523                    QueryDefinition<DLFileEntry> queryDefinition) {
524    
525                    List<Long> repositoryIds = Collections.emptyList();
526    
527                    return doFindByG_U_R_F_M(
528                            groupId, userId, repositoryIds, folderIds, null, queryDefinition,
529                            false);
530            }
531    
532            @Override
533            public List<DLFileEntry> findByG_U_R_F(
534                    long groupId, long userId, List<Long> repositoryIds,
535                    List<Long> folderIds, QueryDefinition<DLFileEntry> queryDefinition) {
536    
537                    return doFindByG_U_R_F_M(
538                            groupId, userId, repositoryIds, folderIds, null, queryDefinition,
539                            false);
540            }
541    
542            @Override
543            public List<DLFileEntry> findByG_U_F_M(
544                    long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
545                    QueryDefinition<DLFileEntry> queryDefinition) {
546    
547                    List<Long> repositoryIds = Collections.emptyList();
548    
549                    return doFindByG_U_R_F_M(
550                            groupId, userId, repositoryIds, folderIds, mimeTypes,
551                            queryDefinition, false);
552            }
553    
554            @Override
555            public List<DLFileEntry> findByG_U_R_F_M(
556                    long groupId, long userId, List<Long> repositoryIds,
557                    List<Long> folderIds, String[] mimeTypes,
558                    QueryDefinition<DLFileEntry> queryDefinition) {
559    
560                    return doFindByG_U_R_F_M(
561                            groupId, userId, repositoryIds, folderIds, mimeTypes,
562                            queryDefinition, false);
563            }
564    
565            protected int doCountByG_U_R_F_M(
566                    long groupId, long userId, List<Long> repositoryIds,
567                    List<Long> folderIds, String[] mimeTypes,
568                    QueryDefinition<DLFileEntry> queryDefinition, boolean inlineSQLHelper) {
569    
570                    Session session = null;
571    
572                    try {
573                            session = openSession();
574    
575                            String id = null;
576    
577                            if (userId <= 0) {
578                                    id = COUNT_BY_G_F;
579                            }
580                            else {
581                                    id = COUNT_BY_G_U_F;
582                            }
583    
584                            String sql = getFileEntriesSQL(
585                                    id, groupId, repositoryIds, folderIds, mimeTypes,
586                                    queryDefinition, inlineSQLHelper);
587    
588                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
589    
590                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
591    
592                            QueryPos qPos = QueryPos.getInstance(q);
593    
594                            qPos.add(groupId);
595    
596                            if (userId > 0) {
597                                    qPos.add(userId);
598                            }
599    
600                            qPos.add(queryDefinition.getStatus());
601    
602                            for (Long repositoryId : repositoryIds) {
603                                    qPos.add(repositoryId);
604                            }
605    
606                            for (Long folderId : folderIds) {
607                                    qPos.add(folderId);
608                            }
609    
610                            if (mimeTypes != null) {
611                                    qPos.add(mimeTypes);
612                            }
613    
614                            Iterator<Long> itr = q.iterate();
615    
616                            if (itr.hasNext()) {
617                                    Long count = itr.next();
618    
619                                    if (count != null) {
620                                            return count.intValue();
621                                    }
622                            }
623    
624                            return 0;
625                    }
626                    catch (Exception e) {
627                            throw new SystemException(e);
628                    }
629                    finally {
630                            closeSession(session);
631                    }
632            }
633    
634            protected List<DLFileEntry> doFindByG_U_R_F_M(
635                    long groupId, long userId, List<Long> repositoryIds,
636                    List<Long> folderIds, String[] mimeTypes,
637                    QueryDefinition<DLFileEntry> queryDefinition, boolean inlineSQLHelper) {
638    
639                    Session session = null;
640    
641                    try {
642                            session = openSession();
643    
644                            String id = null;
645    
646                            if (userId <= 0) {
647                                    id = FIND_BY_G_F;
648                            }
649                            else {
650                                    id = FIND_BY_G_U_F;
651                            }
652    
653                            String sql = getFileEntriesSQL(
654                                    id, groupId, repositoryIds, folderIds, mimeTypes,
655                                    queryDefinition, inlineSQLHelper);
656    
657                            sql = CustomSQLUtil.replaceOrderBy(
658                                    sql, queryDefinition.getOrderByComparator());
659    
660                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
661    
662                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
663    
664                            QueryPos qPos = QueryPos.getInstance(q);
665    
666                            qPos.add(groupId);
667    
668                            if (userId > 0) {
669                                    qPos.add(userId);
670                            }
671    
672                            qPos.add(queryDefinition.getStatus());
673    
674                            for (Long repositoryId : repositoryIds) {
675                                    qPos.add(repositoryId);
676                            }
677    
678                            for (Long folderId : folderIds) {
679                                    qPos.add(folderId);
680                            }
681    
682                            if (mimeTypes != null) {
683                                    qPos.add(mimeTypes);
684                            }
685    
686                            return (List<DLFileEntry>)QueryUtil.list(
687                                    q, getDialect(), queryDefinition.getStart(),
688                                    queryDefinition.getEnd());
689                    }
690                    catch (Exception e) {
691                            throw new SystemException(e);
692                    }
693                    finally {
694                            closeSession(session);
695                    }
696            }
697    
698            protected String getDDMStructureIds(long[] ddmStructureIds) {
699                    StringBundler sb = new StringBundler(
700                            (ddmStructureIds.length * 2 - 1) + 2);
701    
702                    sb.append(StringPool.OPEN_PARENTHESIS);
703    
704                    for (int i = 0; i < ddmStructureIds.length; i++) {
705                            sb.append("DLFileEntryTypes_DDMStructures.structureId = ?");
706    
707                            if ((i + 1) != ddmStructureIds.length) {
708                                    sb.append(WHERE_OR);
709                            }
710                    }
711    
712                    sb.append(StringPool.CLOSE_PARENTHESIS);
713    
714                    return sb.toString();
715            }
716    
717            protected String getFileEntriesSQL(
718                    String id, long groupId, List<Long> repositoryIds, List<Long> folderIds,
719                    String[] mimeTypes, QueryDefinition<DLFileEntry> queryDefinition,
720                    boolean inlineSQLHelper) {
721    
722                    String tableName = DLFileVersionImpl.TABLE_NAME;
723    
724                    String sql = CustomSQLUtil.get(id, queryDefinition, tableName);
725    
726                    if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
727                            sql = StringUtil.replace(sql, "[$JOIN$]", StringPool.BLANK);
728    
729                            tableName = DLFileEntryImpl.TABLE_NAME;
730                    }
731                    else {
732                            sql = StringUtil.replace(
733                                    sql, "[$JOIN$]",
734                                    CustomSQLUtil.get(
735                                            DLFolderFinderImpl.JOIN_FE_BY_DL_FILE_VERSION));
736                    }
737    
738                    if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled()) {
739                            sql = InlineSQLHelperUtil.replacePermissionCheck(
740                                    sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
741                                    groupId);
742                    }
743    
744                    StringBundler sb = new StringBundler(7);
745    
746                    if (ListUtil.isNotEmpty(repositoryIds) ||
747                            ListUtil.isNotEmpty(folderIds) || ArrayUtil.isNotEmpty(mimeTypes)) {
748    
749                            if (ListUtil.isNotEmpty(repositoryIds)) {
750                                    sb.append(WHERE_AND);
751                                    sb.append(StringPool.OPEN_PARENTHESIS);
752                                    sb.append(getRepositoryIds(repositoryIds, tableName));
753                                    sb.append(StringPool.CLOSE_PARENTHESIS);
754                            }
755    
756                            if (ListUtil.isNotEmpty(folderIds)) {
757                                    sb.append(WHERE_AND);
758                                    sb.append(StringPool.OPEN_PARENTHESIS);
759                                    sb.append(getFolderIds(folderIds, tableName));
760                                    sb.append(StringPool.CLOSE_PARENTHESIS);
761                            }
762    
763                            if (ArrayUtil.isNotEmpty(mimeTypes)) {
764                                    sb.append(WHERE_AND);
765                                    sb.append(StringPool.OPEN_PARENTHESIS);
766                                    sb.append(getMimeTypes(mimeTypes, tableName));
767                                    sb.append(StringPool.CLOSE_PARENTHESIS);
768                            }
769    
770                            return StringUtil.replace(sql, "[$FOLDER_ID$]", sb.toString());
771                    }
772                    else {
773                            return StringUtil.replace(sql, "[$FOLDER_ID$]", StringPool.BLANK);
774                    }
775            }
776    
777            protected String getFolderIds(List<Long> folderIds, String tableName) {
778                    if (folderIds.isEmpty()) {
779                            return StringPool.BLANK;
780                    }
781    
782                    StringBundler sb = new StringBundler(folderIds.size() * 3 + 1);
783    
784                    sb.append(StringPool.OPEN_PARENTHESIS);
785    
786                    for (int i = 0; i < folderIds.size(); i++) {
787                            sb.append(tableName);
788                            sb.append(".folderId = ? ");
789    
790                            if ((i + 1) != folderIds.size()) {
791                                    sb.append(WHERE_OR);
792                            }
793                    }
794    
795                    sb.append(StringPool.CLOSE_PARENTHESIS);
796    
797                    return sb.toString();
798            }
799    
800            protected String getMimeTypes(String[] mimeTypes, String tableName) {
801                    if (mimeTypes.length == 0) {
802                            return StringPool.BLANK;
803                    }
804    
805                    StringBundler sb = new StringBundler(mimeTypes.length * 2 - 1);
806    
807                    for (int i = 0; i < mimeTypes.length; i++) {
808                            sb.append(tableName);
809                            sb.append(".mimeType = ?");
810    
811                            if ((i + 1) != mimeTypes.length) {
812                                    sb.append(WHERE_OR);
813                            }
814                    }
815    
816                    return sb.toString();
817            }
818    
819            protected String getRepositoryIds(
820                    List<Long> repositoryIds, String tableName) {
821    
822                    if (repositoryIds.isEmpty()) {
823                            return StringPool.BLANK;
824                    }
825    
826                    StringBundler sb = new StringBundler(repositoryIds.size() * 3 + 1);
827    
828                    sb.append(StringPool.OPEN_PARENTHESIS);
829    
830                    for (int i = 0; i < repositoryIds.size(); i++) {
831                            sb.append(tableName);
832                            sb.append(".repositoryId = ? ");
833    
834                            if ((i + 1) != repositoryIds.size()) {
835                                    sb.append(WHERE_OR);
836                            }
837                    }
838    
839                    sb.append(StringPool.CLOSE_PARENTHESIS);
840    
841                    return sb.toString();
842            }
843    
844    }