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