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