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