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