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.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.workflow.WorkflowConstants;
029    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
030    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
031    import com.liferay.portlet.documentlibrary.model.DLFileShortcutConstants;
032    import com.liferay.portlet.documentlibrary.model.DLFolder;
033    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
034    import com.liferay.portlet.documentlibrary.model.impl.DLFileShortcutImpl;
035    import com.liferay.portlet.documentlibrary.model.impl.DLFileVersionImpl;
036    import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
037    import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryUtil;
038    import com.liferay.portlet.documentlibrary.service.persistence.DLFileShortcutUtil;
039    import com.liferay.portlet.documentlibrary.service.persistence.DLFolderFinder;
040    import com.liferay.portlet.documentlibrary.service.persistence.DLFolderUtil;
041    import com.liferay.util.dao.orm.CustomSQLUtil;
042    
043    import java.util.ArrayList;
044    import java.util.Iterator;
045    import java.util.List;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     * @author Shuyang Zhou
050     */
051    public class DLFolderFinderImpl
052            extends DLFolderFinderBaseImpl implements DLFolderFinder {
053    
054            public static final String COUNT_F_BY_G_M_F =
055                    DLFolderFinder.class.getName() + ".countF_ByG_M_F";
056    
057            public static final String COUNT_FE_BY_G_F =
058                    DLFolderFinder.class.getName() + ".countFE_ByG_F";
059    
060            public static final String COUNT_FS_BY_G_F_A =
061                    DLFolderFinder.class.getName() + ".countFS_ByG_F_A";
062    
063            public static final String FIND_F_BY_NO_ASSETS =
064                    DLFolderFinder.class.getName() + ".findF_ByNoAssets";
065    
066            public static final String FIND_F_BY_G_M_F =
067                    DLFolderFinder.class.getName() + ".findF_ByG_M_F";
068    
069            public static final String FIND_FE_BY_G_F =
070                    DLFolderFinder.class.getName() + ".findFE_ByG_F";
071    
072            public static final String FIND_FS_BY_G_F_A =
073                    DLFolderFinder.class.getName() + ".findFS_ByG_F_A";
074    
075            public static final String JOIN_FE_BY_DL_FILE_VERSION =
076                    DLFolderFinder.class.getName() + ".joinFE_ByDLFileVersion";
077    
078            public static final String JOIN_FS_BY_DL_FILE_ENTRY =
079                    DLFolderFinder.class.getName() + ".joinFS_ByDLFileEntry";
080    
081            public static final String JOIN_AE_BY_DL_FOLDER =
082                    DLFolderFinder.class.getName() + ".joinAE_ByDLFolder";
083    
084            @Override
085            public int countF_FE_FS_ByG_F_M_M(
086                    long groupId, long folderId, String[] mimeTypes,
087                    boolean includeMountFolders, QueryDefinition<?> queryDefinition) {
088    
089                    return doCountF_FE_FS_ByG_F_M_M(
090                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition,
091                            false);
092            }
093    
094            @Override
095            public int countFE_ByG_F(
096                    long groupId, long folderId, QueryDefinition<?> queryDefinition) {
097    
098                    return doCountFE_ByG_F(groupId, folderId, queryDefinition, false);
099            }
100    
101            @Override
102            public int countFE_FS_ByG_F(
103                    long groupId, long folderId, QueryDefinition<?> queryDefinition) {
104    
105                    return doCountFE_FS_ByG_F_M(
106                            groupId, folderId, null, queryDefinition, false);
107            }
108    
109            @Override
110            public int filterCountF_FE_FS_ByG_F_M_M(
111                    long groupId, long folderId, String[] mimeTypes,
112                    boolean includeMountFolders, QueryDefinition<?> queryDefinition) {
113    
114                    return doCountF_FE_FS_ByG_F_M_M(
115                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition,
116                            true);
117            }
118    
119            @Override
120            public int filterCountFE_ByG_F(
121                    long groupId, long folderId, QueryDefinition<?> queryDefinition) {
122    
123                    return doCountFE_ByG_F(groupId, folderId, queryDefinition, true);
124            }
125    
126            @Override
127            public int filterCountFE_FS_ByG_F(
128                    long groupId, long folderId, QueryDefinition<?> queryDefinition) {
129    
130                    return doCountFE_FS_ByG_F_M(
131                            groupId, folderId, null, queryDefinition, true);
132            }
133    
134            @Override
135            public int filterCountFE_FS_ByG_F_M(
136                    long groupId, long folderId, String[] mimeTypes,
137                    QueryDefinition<?> queryDefinition) {
138    
139                    return doCountFE_FS_ByG_F_M(
140                            groupId, folderId, mimeTypes, queryDefinition, true);
141            }
142    
143            @Override
144            public List<Object> filterFindF_FE_FS_ByG_F_M_M(
145                    long groupId, long folderId, String[] mimeTypes,
146                    boolean includeMountFolders, QueryDefinition<?> queryDefinition) {
147    
148                    return doFindF_FE_FS_ByG_F_M_M(
149                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition,
150                            true);
151            }
152    
153            @Override
154            public List<Object> filterFindFE_FS_ByG_F(
155                    long groupId, long folderId, QueryDefinition<?> queryDefinition) {
156    
157                    return doFindFE_FS_ByG_F(groupId, folderId, queryDefinition, true);
158            }
159    
160            @Override
161            public List<DLFolder> findF_ByNoAssets() {
162                    Session session = null;
163    
164                    try {
165                            session = openSession();
166    
167                            String sql = CustomSQLUtil.get(FIND_F_BY_NO_ASSETS);
168    
169                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
170    
171                            q.addEntity("DLFolder", DLFolderImpl.class);
172    
173                            return q.list(true);
174                    }
175                    catch (Exception e) {
176                            throw new SystemException(e);
177                    }
178                    finally {
179                            closeSession(session);
180                    }
181            }
182    
183            @Override
184            public List<Object> findF_FE_FS_ByG_F_M_M(
185                    long groupId, long folderId, String[] mimeTypes,
186                    boolean includeMountFolders, QueryDefinition<?> queryDefinition) {
187    
188                    return doFindF_FE_FS_ByG_F_M_M(
189                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition,
190                            false);
191            }
192    
193            @Override
194            public List<Object> findFE_FS_ByG_F(
195                    long groupId, long folderId, QueryDefinition<?> queryDefinition) {
196    
197                    return doFindFE_FS_ByG_F(groupId, folderId, queryDefinition, false);
198            }
199    
200            protected int doCountF_FE_FS_ByG_F_M_M(
201                    long groupId, long folderId, String[] mimeTypes,
202                    boolean includeMountFolders, QueryDefinition<?> queryDefinition,
203                    boolean inlineSQLHelper) {
204    
205                    Session session = null;
206    
207                    try {
208                            session = openSession();
209    
210                            StringBundler sb = new StringBundler(7);
211    
212                            sb.append(StringPool.OPEN_PARENTHESIS);
213    
214                            String sql = CustomSQLUtil.get(
215                                    COUNT_F_BY_G_M_F, queryDefinition, DLFolderImpl.TABLE_NAME);
216    
217                            if (inlineSQLHelper) {
218                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
219                                            sql, DLFolder.class.getName(), "DLFolder.folderId",
220                                            groupId);
221                            }
222    
223                            sb.append(sql);
224                            sb.append(") UNION ALL (");
225                            sb.append(
226                                    getFileVersionsSQL(
227                                            COUNT_FE_BY_G_F, groupId, mimeTypes, queryDefinition,
228                                            inlineSQLHelper));
229                            sb.append(") UNION ALL (");
230                            sb.append(
231                                    getFileShortcutsSQL(
232                                            COUNT_FS_BY_G_F_A, groupId, mimeTypes, queryDefinition,
233                                            inlineSQLHelper));
234                            sb.append(StringPool.CLOSE_PARENTHESIS);
235    
236                            sql = sb.toString();
237    
238                            sql = updateSQL(sql, folderId, includeMountFolders);
239    
240                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
241    
242                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
243    
244                            QueryPos qPos = QueryPos.getInstance(q);
245    
246                            qPos.add(groupId);
247    
248                            if (!includeMountFolders) {
249                                    qPos.add(false);
250                            }
251    
252                            qPos.add(queryDefinition.getStatus());
253                            qPos.add(folderId);
254                            qPos.add(groupId);
255                            qPos.add(queryDefinition.getStatus());
256                            qPos.add(folderId);
257    
258                            if (mimeTypes != null) {
259                                    qPos.add(mimeTypes);
260                            }
261    
262                            qPos.add(groupId);
263                            qPos.add(true);
264                            qPos.add(queryDefinition.getStatus());
265                            qPos.add(folderId);
266    
267                            if (mimeTypes != null) {
268                                    qPos.add(mimeTypes);
269                            }
270    
271                            int count = 0;
272    
273                            Iterator<Long> itr = q.iterate();
274    
275                            while (itr.hasNext()) {
276                                    Long l = itr.next();
277    
278                                    if (l != null) {
279                                            count += l.intValue();
280                                    }
281                            }
282    
283                            return count;
284                    }
285                    catch (Exception e) {
286                            throw new SystemException(e);
287                    }
288                    finally {
289                            closeSession(session);
290                    }
291            }
292    
293            protected int doCountFE_ByG_F(
294                    long groupId, long folderId, QueryDefinition<?> queryDefinition,
295                    boolean inlineSQLHelper) {
296    
297                    Session session = null;
298    
299                    try {
300                            session = openSession();
301    
302                            String sql = getFileVersionsSQL(
303                                    COUNT_FE_BY_G_F, groupId, null, queryDefinition,
304                                    inlineSQLHelper);
305    
306                            sql = updateSQL(sql, folderId, false);
307    
308                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
309    
310                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
311    
312                            QueryPos qPos = QueryPos.getInstance(q);
313    
314                            qPos.add(groupId);
315                            qPos.add(queryDefinition.getStatus());
316                            qPos.add(folderId);
317    
318                            Iterator<Long> itr = q.iterate();
319    
320                            if (itr.hasNext()) {
321                                    Long count = itr.next();
322    
323                                    if (count != null) {
324                                            return count.intValue();
325                                    }
326                            }
327    
328                            return 0;
329                    }
330                    catch (Exception e) {
331                            throw new SystemException(e);
332                    }
333                    finally {
334                            closeSession(session);
335                    }
336            }
337    
338            protected int doCountFE_FS_ByG_F_M(
339                    long groupId, long folderId, String[] mimeTypes,
340                    QueryDefinition<?> queryDefinition, boolean inlineSQLHelper) {
341    
342                    Session session = null;
343    
344                    try {
345                            session = openSession();
346    
347                            StringBundler sb = new StringBundler(5);
348    
349                            sb.append(StringPool.OPEN_PARENTHESIS);
350    
351                            String sql = getFileVersionsSQL(
352                                    COUNT_FE_BY_G_F, groupId, mimeTypes, queryDefinition,
353                                    inlineSQLHelper);
354    
355                            sb.append(sql);
356                            sb.append(") UNION ALL (");
357                            sb.append(
358                                    getFileShortcutsSQL(
359                                            COUNT_FS_BY_G_F_A, groupId, mimeTypes, queryDefinition,
360                                            inlineSQLHelper));
361                            sb.append(StringPool.CLOSE_PARENTHESIS);
362    
363                            sql = sb.toString();
364    
365                            sql = updateSQL(sql, folderId, false);
366    
367                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
368    
369                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
370    
371                            QueryPos qPos = QueryPos.getInstance(q);
372    
373                            qPos.add(groupId);
374                            qPos.add(queryDefinition.getStatus());
375                            qPos.add(folderId);
376    
377                            if (mimeTypes != null) {
378                                    qPos.add(mimeTypes);
379                            }
380    
381                            qPos.add(groupId);
382                            qPos.add(true);
383                            qPos.add(queryDefinition.getStatus());
384                            qPos.add(folderId);
385    
386                            if (mimeTypes != null) {
387                                    qPos.add(mimeTypes);
388                            }
389    
390                            int count = 0;
391    
392                            Iterator<Long> itr = q.iterate();
393    
394                            while (itr.hasNext()) {
395                                    Long l = itr.next();
396    
397                                    if (l != null) {
398                                            count += l.intValue();
399                                    }
400                            }
401    
402                            return count;
403                    }
404                    catch (Exception e) {
405                            throw new SystemException(e);
406                    }
407                    finally {
408                            closeSession(session);
409                    }
410            }
411    
412            protected List<Object> doFindF_FE_FS_ByG_F_M_M(
413                    long groupId, long folderId, String[] mimeTypes,
414                    boolean includeMountFolders, QueryDefinition<?> queryDefinition,
415                    boolean inlineSQLHelper) {
416    
417                    Session session = null;
418    
419                    try {
420                            session = openSession();
421    
422                            StringBundler sb = new StringBundler(7);
423    
424                            sb.append("SELECT * FROM (");
425    
426                            String sql = CustomSQLUtil.get(
427                                    FIND_F_BY_G_M_F, queryDefinition, DLFolderImpl.TABLE_NAME);
428    
429                            if (inlineSQLHelper) {
430                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
431                                            sql, DLFolder.class.getName(), "DLFolder.folderId",
432                                            groupId);
433                            }
434    
435                            sb.append(sql);
436                            sb.append(" UNION ALL ");
437    
438                            sql = getFileEntriesSQL(
439                                    FIND_FE_BY_G_F, groupId, mimeTypes, queryDefinition,
440                                    inlineSQLHelper);
441    
442                            sb.append(sql);
443                            sb.append(" UNION ALL ");
444    
445                            sql = getFileShortcutsSQL(
446                                    FIND_FS_BY_G_F_A, groupId, mimeTypes, queryDefinition,
447                                    inlineSQLHelper);
448    
449                            sb.append(sql);
450                            sb.append(") TEMP_TABLE ORDER BY modelFolder DESC, title ASC");
451    
452                            sql = sb.toString();
453    
454                            sql = updateSQL(sql, folderId, includeMountFolders);
455                            sql = CustomSQLUtil.replaceOrderBy(
456                                    sql, queryDefinition.getOrderByComparator());
457    
458                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
459    
460                            q.addScalar("modelFolderId", Type.LONG);
461                            q.addScalar("name", Type.STRING);
462                            q.addScalar("title", Type.STRING);
463                            q.addScalar("fileShortcutId", Type.LONG);
464                            q.addScalar("modelFolder", Type.LONG);
465    
466                            QueryPos qPos = QueryPos.getInstance(q);
467    
468                            qPos.add(groupId);
469    
470                            if (!includeMountFolders) {
471                                    qPos.add(false);
472                            }
473    
474                            qPos.add(queryDefinition.getStatus());
475                            qPos.add(folderId);
476                            qPos.add(groupId);
477                            qPos.add(queryDefinition.getStatus());
478                            qPos.add(folderId);
479    
480                            if (mimeTypes != null) {
481                                    qPos.add(mimeTypes);
482                            }
483    
484                            qPos.add(groupId);
485                            qPos.add(true);
486                            qPos.add(queryDefinition.getStatus());
487                            qPos.add(folderId);
488    
489                            if (mimeTypes != null) {
490                                    qPos.add(mimeTypes);
491                            }
492    
493                            List<Object> models = new ArrayList<>();
494    
495                            Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
496                                    q, getDialect(), queryDefinition.getStart(),
497                                    queryDefinition.getEnd());
498    
499                            while (itr.hasNext()) {
500                                    Object[] array = itr.next();
501    
502                                    long curFolderId = (Long)array[0];
503                                    String name = (String)array[1];
504                                    //String title = (String)array[2];
505                                    long fileShortcutId = (Long)array[3];
506                                    long modelFolder = (Long)array[4];
507    
508                                    Object obj = null;
509    
510                                    if (modelFolder == 1) {
511                                            obj = DLFolderUtil.findByPrimaryKey(curFolderId);
512                                    }
513                                    else if (fileShortcutId > 0) {
514                                            obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
515                                    }
516                                    else {
517                                            obj = DLFileEntryUtil.findByG_F_N(
518                                                    groupId, curFolderId, name);
519                                    }
520    
521                                    models.add(obj);
522                            }
523    
524                            return models;
525                    }
526                    catch (Exception e) {
527                            throw new SystemException(e);
528                    }
529                    finally {
530                            closeSession(session);
531                    }
532            }
533    
534            protected List<Object> doFindFE_FS_ByG_F(
535                    long groupId, long folderId, QueryDefinition<?> queryDefinition,
536                    boolean inlineSQLHelper) {
537    
538                    Session session = null;
539    
540                    try {
541                            session = openSession();
542    
543                            StringBundler sb = new StringBundler(5);
544    
545                            sb.append("SELECT * FROM (");
546    
547                            String sql = getFileEntriesSQL(
548                                    FIND_FE_BY_G_F, groupId, null, queryDefinition,
549                                    inlineSQLHelper);
550    
551                            sb.append(sql);
552                            sb.append(" UNION ALL ");
553    
554                            sql = getFileShortcutsSQL(
555                                    FIND_FS_BY_G_F_A, groupId, null, queryDefinition,
556                                    inlineSQLHelper);
557    
558                            sb.append(sql);
559                            sb.append(") TEMP_TABLE ORDER BY modelFolder DESC, title ASC");
560    
561                            sql = sb.toString();
562    
563                            sql = updateSQL(sql, folderId, false);
564    
565                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
566    
567                            q.addScalar("modelFolderId", Type.LONG);
568                            q.addScalar("name", Type.STRING);
569                            q.addScalar("title", Type.STRING);
570                            q.addScalar("fileShortcutId", Type.LONG);
571    
572                            QueryPos qPos = QueryPos.getInstance(q);
573    
574                            qPos.add(groupId);
575                            qPos.add(queryDefinition.getStatus());
576                            qPos.add(folderId);
577                            qPos.add(groupId);
578                            qPos.add(true);
579                            qPos.add(queryDefinition.getStatus());
580                            qPos.add(folderId);
581    
582                            List<Object> models = new ArrayList<>();
583    
584                            Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
585                                    q, getDialect(), queryDefinition.getStart(),
586                                    queryDefinition.getEnd());
587    
588                            while (itr.hasNext()) {
589                                    Object[] array = itr.next();
590    
591                                    long folderId2 = (Long)array[0];
592                                    String name = (String)array[1];
593                                    //String title = (String)array[2];
594                                    long fileShortcutId = (Long)array[3];
595    
596                                    Object obj = null;
597    
598                                    if (fileShortcutId > 0) {
599                                            obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
600                                    }
601                                    else {
602                                            obj = DLFileEntryUtil.findByG_F_N(groupId, folderId2, name);
603                                    }
604    
605                                    models.add(obj);
606                            }
607    
608                            return models;
609                    }
610                    catch (Exception e) {
611                            throw new SystemException(e);
612                    }
613                    finally {
614                            closeSession(session);
615                    }
616            }
617    
618            protected String getFileEntriesSQL(
619                    String id, long groupId, String[] mimeTypes,
620                    QueryDefinition<?> queryDefinition, boolean inlineSQLHelper) {
621    
622                    String sql = CustomSQLUtil.get(
623                            id, queryDefinition, DLFileVersionImpl.TABLE_NAME);
624    
625                    if (inlineSQLHelper) {
626                            sql = InlineSQLHelperUtil.replacePermissionCheck(
627                                    sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
628                                    groupId);
629                    }
630    
631                    if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
632                            sql = StringUtil.replace(sql, "[$JOIN$]", StringPool.BLANK);
633                    }
634                    else {
635                            sql = StringUtil.replace(
636                                    sql, "[$JOIN$]",
637                                    CustomSQLUtil.get(
638                                            DLFolderFinderImpl.JOIN_FE_BY_DL_FILE_VERSION));
639                    }
640    
641                    if (ArrayUtil.isNotEmpty(mimeTypes)) {
642                            StringBundler sb = new StringBundler(5);
643    
644                            sb.append(sql);
645                            sb.append(WHERE_AND);
646                            sb.append(StringPool.OPEN_PARENTHESIS);
647                            sb.append(getMimeTypes(mimeTypes, DLFileEntryImpl.TABLE_NAME));
648                            sb.append(StringPool.CLOSE_PARENTHESIS);
649    
650                            sql = sb.toString();
651                    }
652    
653                    return sql;
654            }
655    
656            protected String getFileShortcutsSQL(
657                    String id, long groupId, String[] mimeTypes,
658                    QueryDefinition<?> queryDefinition, boolean inlineSQLHelper) {
659    
660                    String sql = CustomSQLUtil.get(
661                            id, queryDefinition, DLFileShortcutImpl.TABLE_NAME);
662    
663                    if (inlineSQLHelper) {
664                            sql = InlineSQLHelperUtil.replacePermissionCheck(
665                                    sql, DLFileShortcutConstants.getClassName(),
666                                    "DLFileShortcut.fileShortcutId", groupId);
667                    }
668    
669                    if (ArrayUtil.isNotEmpty(mimeTypes)) {
670                            StringBundler sb = new StringBundler(5);
671    
672                            sb.append(
673                                    StringUtil.replace(
674                                            sql, "[$JOIN$]",
675                                            CustomSQLUtil.get(JOIN_FS_BY_DL_FILE_ENTRY)));
676    
677                            sb.append(WHERE_AND);
678                            sb.append(StringPool.OPEN_PARENTHESIS);
679                            sb.append(getMimeTypes(mimeTypes, DLFileEntryImpl.TABLE_NAME));
680                            sb.append(StringPool.CLOSE_PARENTHESIS);
681    
682                            sql = sb.toString();
683                    }
684                    else {
685                            sql = StringUtil.replace(sql, "[$JOIN$]", StringPool.BLANK);
686                    }
687    
688                    return sql;
689            }
690    
691            protected String getFileVersionsSQL(
692                    String id, long groupId, String[] mimeTypes,
693                    QueryDefinition<?> queryDefinition, boolean inlineSQLHelper) {
694    
695                    String sql = CustomSQLUtil.get(
696                            id, queryDefinition, DLFileVersionImpl.TABLE_NAME);
697    
698                    if (inlineSQLHelper) {
699                            sql = InlineSQLHelperUtil.replacePermissionCheck(
700                                    sql, DLFileEntry.class.getName(), "DLFileVersion.fileEntryId",
701                                    groupId);
702                    }
703    
704                    if (ArrayUtil.isNotEmpty(mimeTypes)) {
705                            StringBundler sb = new StringBundler(5);
706    
707                            sb.append(sql);
708                            sb.append(WHERE_AND);
709                            sb.append(StringPool.OPEN_PARENTHESIS);
710                            sb.append(getMimeTypes(mimeTypes, DLFileVersionImpl.TABLE_NAME));
711                            sb.append(StringPool.CLOSE_PARENTHESIS);
712    
713                            sql = sb.toString();
714                    }
715    
716                    return sql;
717            }
718    
719            protected String getFolderId(long folderId, String tableName) {
720                    StringBundler sb = new StringBundler(4);
721    
722                    sb.append(tableName);
723                    sb.append(".");
724    
725                    if (tableName.equals(DLFolderImpl.TABLE_NAME)) {
726                            sb.append("parentFolderId");
727                    }
728                    else {
729                            sb.append("folderId");
730                    }
731    
732                    sb.append("= ? ");
733    
734                    return sb.toString();
735            }
736    
737            protected String getMimeTypes(String[] mimeTypes, String tableName) {
738                    if (mimeTypes.length == 0) {
739                            return StringPool.BLANK;
740                    }
741    
742                    StringBundler sb = new StringBundler(mimeTypes.length * 3 - 1);
743    
744                    for (int i = 0; i < mimeTypes.length; i++) {
745                            sb.append(tableName);
746                            sb.append(".mimeType = ?");
747    
748                            if ((i + 1) != mimeTypes.length) {
749                                    sb.append(WHERE_OR);
750                            }
751                    }
752    
753                    return sb.toString();
754            }
755    
756            protected String updateSQL(
757                    String sql, long folderId, boolean includeMountFolders) {
758    
759                    sql = StringUtil.replace(
760                            sql,
761                            new String[] {
762                                    "[$FILE_ENTRY_FOLDER_ID$]", "[$FILE_SHORTCUT_FOLDER_ID$]",
763                                    "[$FILE_VERSION_FOLDER_ID$]", "[$FOLDER_PARENT_FOLDER_ID$]"
764                            },
765                            new String[] {
766                                    getFolderId(folderId, DLFileEntryImpl.TABLE_NAME),
767                                    getFolderId(folderId, DLFileShortcutImpl.TABLE_NAME),
768                                    getFolderId(folderId, DLFileVersionImpl.TABLE_NAME),
769                                    getFolderId(folderId, DLFolderImpl.TABLE_NAME)
770                            });
771    
772                    if (includeMountFolders) {
773                            sql = StringUtil.replace(
774                                    sql, "(DLFolder.mountPoint = ?) AND", StringPool.BLANK);
775                    }
776    
777                    return sql;
778            }
779    
780    }