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