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