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.document.library.kernel.model.DLFileEntry;
018    import com.liferay.document.library.kernel.model.DLFileShortcutConstants;
019    import com.liferay.document.library.kernel.model.DLFolder;
020    import com.liferay.document.library.kernel.service.persistence.DLFileEntryUtil;
021    import com.liferay.document.library.kernel.service.persistence.DLFileShortcutUtil;
022    import com.liferay.document.library.kernel.service.persistence.DLFolderFinder;
023    import com.liferay.document.library.kernel.service.persistence.DLFolderUtil;
024    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
025    import com.liferay.portal.kernel.dao.orm.QueryPos;
026    import com.liferay.portal.kernel.dao.orm.QueryUtil;
027    import com.liferay.portal.kernel.dao.orm.SQLQuery;
028    import com.liferay.portal.kernel.dao.orm.Session;
029    import com.liferay.portal.kernel.dao.orm.Type;
030    import com.liferay.portal.kernel.exception.SystemException;
031    import com.liferay.portal.kernel.security.permission.InlineSQLHelperUtil;
032    import com.liferay.portal.kernel.util.ArrayUtil;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.workflow.WorkflowConstants;
037    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
038    import com.liferay.portlet.documentlibrary.model.impl.DLFileShortcutImpl;
039    import com.liferay.portlet.documentlibrary.model.impl.DLFileVersionImpl;
040    import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
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    
257                            if ((queryDefinition.getOwnerUserId() > 0) &&
258                                    queryDefinition.isIncludeOwner()) {
259    
260                                    qPos.add(queryDefinition.getOwnerUserId());
261                                    qPos.add(WorkflowConstants.STATUS_IN_TRASH);
262                            }
263    
264                            qPos.add(folderId);
265    
266                            if (mimeTypes != null) {
267                                    qPos.add(mimeTypes);
268                            }
269    
270                            qPos.add(groupId);
271                            qPos.add(true);
272                            qPos.add(queryDefinition.getStatus());
273                            qPos.add(folderId);
274    
275                            if (mimeTypes != null) {
276                                    qPos.add(mimeTypes);
277                            }
278    
279                            int count = 0;
280    
281                            Iterator<Long> itr = q.iterate();
282    
283                            while (itr.hasNext()) {
284                                    Long l = itr.next();
285    
286                                    if (l != null) {
287                                            count += l.intValue();
288                                    }
289                            }
290    
291                            return count;
292                    }
293                    catch (Exception e) {
294                            throw new SystemException(e);
295                    }
296                    finally {
297                            closeSession(session);
298                    }
299            }
300    
301            protected int doCountFE_ByG_F(
302                    long groupId, long folderId, QueryDefinition<?> queryDefinition,
303                    boolean inlineSQLHelper) {
304    
305                    Session session = null;
306    
307                    try {
308                            session = openSession();
309    
310                            String sql = getFileVersionsSQL(
311                                    COUNT_FE_BY_G_F, groupId, null, queryDefinition,
312                                    inlineSQLHelper);
313    
314                            sql = updateSQL(sql, folderId, false);
315    
316                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
317    
318                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
319    
320                            QueryPos qPos = QueryPos.getInstance(q);
321    
322                            qPos.add(groupId);
323                            qPos.add(queryDefinition.getStatus());
324                            qPos.add(folderId);
325    
326                            Iterator<Long> itr = q.iterate();
327    
328                            if (itr.hasNext()) {
329                                    Long count = itr.next();
330    
331                                    if (count != null) {
332                                            return count.intValue();
333                                    }
334                            }
335    
336                            return 0;
337                    }
338                    catch (Exception e) {
339                            throw new SystemException(e);
340                    }
341                    finally {
342                            closeSession(session);
343                    }
344            }
345    
346            protected int doCountFE_FS_ByG_F_M(
347                    long groupId, long folderId, String[] mimeTypes,
348                    QueryDefinition<?> queryDefinition, boolean inlineSQLHelper) {
349    
350                    Session session = null;
351    
352                    try {
353                            session = openSession();
354    
355                            StringBundler sb = new StringBundler(5);
356    
357                            sb.append(StringPool.OPEN_PARENTHESIS);
358    
359                            String sql = getFileVersionsSQL(
360                                    COUNT_FE_BY_G_F, groupId, mimeTypes, queryDefinition,
361                                    inlineSQLHelper);
362    
363                            sb.append(sql);
364                            sb.append(") UNION ALL (");
365                            sb.append(
366                                    getFileShortcutsSQL(
367                                            COUNT_FS_BY_G_F_A, groupId, mimeTypes, queryDefinition,
368                                            inlineSQLHelper));
369                            sb.append(StringPool.CLOSE_PARENTHESIS);
370    
371                            sql = sb.toString();
372    
373                            sql = updateSQL(sql, folderId, false);
374    
375                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
376    
377                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
378    
379                            QueryPos qPos = QueryPos.getInstance(q);
380    
381                            qPos.add(groupId);
382                            qPos.add(queryDefinition.getStatus());
383                            qPos.add(folderId);
384    
385                            if (mimeTypes != null) {
386                                    qPos.add(mimeTypes);
387                            }
388    
389                            qPos.add(groupId);
390                            qPos.add(true);
391                            qPos.add(queryDefinition.getStatus());
392                            qPos.add(folderId);
393    
394                            if (mimeTypes != null) {
395                                    qPos.add(mimeTypes);
396                            }
397    
398                            int count = 0;
399    
400                            Iterator<Long> itr = q.iterate();
401    
402                            while (itr.hasNext()) {
403                                    Long l = itr.next();
404    
405                                    if (l != null) {
406                                            count += l.intValue();
407                                    }
408                            }
409    
410                            return count;
411                    }
412                    catch (Exception e) {
413                            throw new SystemException(e);
414                    }
415                    finally {
416                            closeSession(session);
417                    }
418            }
419    
420            protected List<Object> doFindF_FE_FS_ByG_F_M_M(
421                    long groupId, long folderId, String[] mimeTypes,
422                    boolean includeMountFolders, QueryDefinition<?> queryDefinition,
423                    boolean inlineSQLHelper) {
424    
425                    Session session = null;
426    
427                    try {
428                            session = openSession();
429    
430                            StringBundler sb = new StringBundler(7);
431    
432                            sb.append("SELECT * FROM (");
433    
434                            String sql = CustomSQLUtil.get(
435                                    FIND_F_BY_G_M_F, queryDefinition, DLFolderImpl.TABLE_NAME);
436    
437                            if (inlineSQLHelper) {
438                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
439                                            sql, DLFolder.class.getName(), "DLFolder.folderId",
440                                            groupId);
441                            }
442    
443                            sb.append(sql);
444                            sb.append(" UNION ALL ");
445    
446                            sql = getFileEntriesSQL(
447                                    FIND_FE_BY_G_F, groupId, mimeTypes, queryDefinition,
448                                    inlineSQLHelper);
449    
450                            sb.append(sql);
451                            sb.append(" UNION ALL ");
452    
453                            sql = getFileShortcutsSQL(
454                                    FIND_FS_BY_G_F_A, groupId, mimeTypes, queryDefinition,
455                                    inlineSQLHelper);
456    
457                            sb.append(sql);
458                            sb.append(") TEMP_TABLE ORDER BY modelFolder DESC, title ASC");
459    
460                            sql = sb.toString();
461    
462                            sql = updateSQL(sql, folderId, includeMountFolders);
463                            sql = CustomSQLUtil.replaceOrderBy(
464                                    sql, queryDefinition.getOrderByComparator());
465    
466                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
467    
468                            q.addScalar("modelFolderId", Type.LONG);
469                            q.addScalar("name", Type.STRING);
470                            q.addScalar("title", Type.STRING);
471                            q.addScalar("fileShortcutId", Type.LONG);
472                            q.addScalar("modelFolder", Type.LONG);
473    
474                            QueryPos qPos = QueryPos.getInstance(q);
475    
476                            qPos.add(groupId);
477    
478                            if (!includeMountFolders) {
479                                    qPos.add(false);
480                            }
481    
482                            qPos.add(queryDefinition.getStatus());
483                            qPos.add(folderId);
484                            qPos.add(groupId);
485                            qPos.add(queryDefinition.getStatus());
486    
487                            if ((queryDefinition.getOwnerUserId() > 0) &&
488                                    queryDefinition.isIncludeOwner()) {
489    
490                                    qPos.add(queryDefinition.getOwnerUserId());
491                                    qPos.add(WorkflowConstants.STATUS_IN_TRASH);
492                            }
493    
494                            qPos.add(folderId);
495    
496                            if (mimeTypes != null) {
497                                    qPos.add(mimeTypes);
498                            }
499    
500                            qPos.add(groupId);
501                            qPos.add(true);
502                            qPos.add(queryDefinition.getStatus());
503                            qPos.add(folderId);
504    
505                            if (mimeTypes != null) {
506                                    qPos.add(mimeTypes);
507                            }
508    
509                            List<Object> models = new ArrayList<>();
510    
511                            Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
512                                    q, getDialect(), queryDefinition.getStart(),
513                                    queryDefinition.getEnd());
514    
515                            while (itr.hasNext()) {
516                                    Object[] array = itr.next();
517    
518                                    long curFolderId = (Long)array[0];
519                                    String name = (String)array[1];
520                                    //String title = (String)array[2];
521                                    long fileShortcutId = (Long)array[3];
522                                    long modelFolder = (Long)array[4];
523    
524                                    Object obj = null;
525    
526                                    if (modelFolder == 1) {
527                                            obj = DLFolderUtil.findByPrimaryKey(curFolderId);
528                                    }
529                                    else if (fileShortcutId > 0) {
530                                            obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
531                                    }
532                                    else {
533                                            obj = DLFileEntryUtil.findByG_F_N(
534                                                    groupId, curFolderId, name);
535                                    }
536    
537                                    models.add(obj);
538                            }
539    
540                            return models;
541                    }
542                    catch (Exception e) {
543                            throw new SystemException(e);
544                    }
545                    finally {
546                            closeSession(session);
547                    }
548            }
549    
550            protected List<Object> doFindFE_FS_ByG_F(
551                    long groupId, long folderId, QueryDefinition<?> queryDefinition,
552                    boolean inlineSQLHelper) {
553    
554                    Session session = null;
555    
556                    try {
557                            session = openSession();
558    
559                            StringBundler sb = new StringBundler(5);
560    
561                            sb.append("SELECT * FROM (");
562    
563                            String sql = getFileEntriesSQL(
564                                    FIND_FE_BY_G_F, groupId, null, queryDefinition,
565                                    inlineSQLHelper);
566    
567                            sb.append(sql);
568                            sb.append(" UNION ALL ");
569    
570                            sql = getFileShortcutsSQL(
571                                    FIND_FS_BY_G_F_A, groupId, null, queryDefinition,
572                                    inlineSQLHelper);
573    
574                            sb.append(sql);
575                            sb.append(") TEMP_TABLE ORDER BY modelFolder DESC, title ASC");
576    
577                            sql = sb.toString();
578    
579                            sql = updateSQL(sql, folderId, false);
580    
581                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
582    
583                            q.addScalar("modelFolderId", Type.LONG);
584                            q.addScalar("name", Type.STRING);
585                            q.addScalar("title", Type.STRING);
586                            q.addScalar("fileShortcutId", Type.LONG);
587    
588                            QueryPos qPos = QueryPos.getInstance(q);
589    
590                            qPos.add(groupId);
591                            qPos.add(queryDefinition.getStatus());
592                            qPos.add(folderId);
593                            qPos.add(groupId);
594                            qPos.add(true);
595                            qPos.add(queryDefinition.getStatus());
596                            qPos.add(folderId);
597    
598                            List<Object> models = new ArrayList<>();
599    
600                            Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
601                                    q, getDialect(), queryDefinition.getStart(),
602                                    queryDefinition.getEnd());
603    
604                            while (itr.hasNext()) {
605                                    Object[] array = itr.next();
606    
607                                    long folderId2 = (Long)array[0];
608                                    String name = (String)array[1];
609                                    //String title = (String)array[2];
610                                    long fileShortcutId = (Long)array[3];
611    
612                                    Object obj = null;
613    
614                                    if (fileShortcutId > 0) {
615                                            obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
616                                    }
617                                    else {
618                                            obj = DLFileEntryUtil.findByG_F_N(groupId, folderId2, name);
619                                    }
620    
621                                    models.add(obj);
622                            }
623    
624                            return models;
625                    }
626                    catch (Exception e) {
627                            throw new SystemException(e);
628                    }
629                    finally {
630                            closeSession(session);
631                    }
632            }
633    
634            protected String getFileEntriesSQL(
635                    String id, long groupId, String[] mimeTypes,
636                    QueryDefinition<?> queryDefinition, boolean inlineSQLHelper) {
637    
638                    String sql = CustomSQLUtil.get(
639                            id, queryDefinition, DLFileVersionImpl.TABLE_NAME);
640    
641                    if (inlineSQLHelper) {
642                            sql = InlineSQLHelperUtil.replacePermissionCheck(
643                                    sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
644                                    groupId);
645                    }
646    
647                    if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
648                            sql = StringUtil.replace(sql, "[$JOIN$]", StringPool.BLANK);
649                    }
650                    else {
651                            sql = StringUtil.replace(
652                                    sql, "[$JOIN$]",
653                                    CustomSQLUtil.get(
654                                            DLFolderFinderImpl.JOIN_FE_BY_DL_FILE_VERSION));
655                    }
656    
657                    if (ArrayUtil.isNotEmpty(mimeTypes)) {
658                            StringBundler sb = new StringBundler(5);
659    
660                            sb.append(sql);
661                            sb.append(WHERE_AND);
662                            sb.append(StringPool.OPEN_PARENTHESIS);
663                            sb.append(getMimeTypes(mimeTypes, DLFileEntryImpl.TABLE_NAME));
664                            sb.append(StringPool.CLOSE_PARENTHESIS);
665    
666                            sql = sb.toString();
667                    }
668    
669                    return sql;
670            }
671    
672            protected String getFileShortcutsSQL(
673                    String id, long groupId, String[] mimeTypes,
674                    QueryDefinition<?> queryDefinition, boolean inlineSQLHelper) {
675    
676                    String sql = CustomSQLUtil.get(
677                            id, queryDefinition, DLFileShortcutImpl.TABLE_NAME);
678    
679                    if (inlineSQLHelper) {
680                            sql = InlineSQLHelperUtil.replacePermissionCheck(
681                                    sql, DLFileShortcutConstants.getClassName(),
682                                    "DLFileShortcut.fileShortcutId", groupId);
683                    }
684    
685                    if (ArrayUtil.isNotEmpty(mimeTypes)) {
686                            StringBundler sb = new StringBundler(5);
687    
688                            sb.append(
689                                    StringUtil.replace(
690                                            sql, "[$JOIN$]",
691                                            CustomSQLUtil.get(JOIN_FS_BY_DL_FILE_ENTRY)));
692    
693                            sb.append(WHERE_AND);
694                            sb.append(StringPool.OPEN_PARENTHESIS);
695                            sb.append(getMimeTypes(mimeTypes, DLFileEntryImpl.TABLE_NAME));
696                            sb.append(StringPool.CLOSE_PARENTHESIS);
697    
698                            sql = sb.toString();
699                    }
700                    else {
701                            sql = StringUtil.replace(sql, "[$JOIN$]", StringPool.BLANK);
702                    }
703    
704                    return sql;
705            }
706    
707            protected String getFileVersionsSQL(
708                    String id, long groupId, String[] mimeTypes,
709                    QueryDefinition<?> queryDefinition, boolean inlineSQLHelper) {
710    
711                    String sql = CustomSQLUtil.get(
712                            id, queryDefinition, DLFileVersionImpl.TABLE_NAME);
713    
714                    if (inlineSQLHelper) {
715                            sql = InlineSQLHelperUtil.replacePermissionCheck(
716                                    sql, DLFileEntry.class.getName(), "DLFileVersion.fileEntryId",
717                                    groupId);
718                    }
719    
720                    if (ArrayUtil.isNotEmpty(mimeTypes)) {
721                            StringBundler sb = new StringBundler(5);
722    
723                            sb.append(sql);
724                            sb.append(WHERE_AND);
725                            sb.append(StringPool.OPEN_PARENTHESIS);
726                            sb.append(getMimeTypes(mimeTypes, DLFileVersionImpl.TABLE_NAME));
727                            sb.append(StringPool.CLOSE_PARENTHESIS);
728    
729                            sql = sb.toString();
730                    }
731    
732                    return sql;
733            }
734    
735            protected String getFolderId(long folderId, String tableName) {
736                    StringBundler sb = new StringBundler(4);
737    
738                    sb.append(tableName);
739                    sb.append(".");
740    
741                    if (tableName.equals(DLFolderImpl.TABLE_NAME)) {
742                            sb.append("parentFolderId");
743                    }
744                    else {
745                            sb.append("folderId");
746                    }
747    
748                    sb.append("= ? ");
749    
750                    return sb.toString();
751            }
752    
753            protected String getMimeTypes(String[] mimeTypes, String tableName) {
754                    if (mimeTypes.length == 0) {
755                            return StringPool.BLANK;
756                    }
757    
758                    StringBundler sb = new StringBundler(mimeTypes.length * 3 - 1);
759    
760                    for (int i = 0; i < mimeTypes.length; i++) {
761                            sb.append(tableName);
762                            sb.append(".mimeType = ?");
763    
764                            if ((i + 1) != mimeTypes.length) {
765                                    sb.append(WHERE_OR);
766                            }
767                    }
768    
769                    return sb.toString();
770            }
771    
772            protected String updateSQL(
773                    String sql, long folderId, boolean includeMountFolders) {
774    
775                    sql = StringUtil.replace(
776                            sql,
777                            new String[] {
778                                    "[$FILE_ENTRY_FOLDER_ID$]", "[$FILE_SHORTCUT_FOLDER_ID$]",
779                                    "[$FILE_VERSION_FOLDER_ID$]", "[$FOLDER_PARENT_FOLDER_ID$]"
780                            },
781                            new String[] {
782                                    getFolderId(folderId, DLFileEntryImpl.TABLE_NAME),
783                                    getFolderId(folderId, DLFileShortcutImpl.TABLE_NAME),
784                                    getFolderId(folderId, DLFileVersionImpl.TABLE_NAME),
785                                    getFolderId(folderId, DLFolderImpl.TABLE_NAME)
786                            });
787    
788                    if (includeMountFolders) {
789                            sql = StringUtil.replace(
790                                    sql, "(DLFolder.mountPoint = ?) AND", StringPool.BLANK);
791                    }
792    
793                    return sql;
794            }
795    
796    }