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