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