001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
028    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
029    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
030    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
031    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
032    import com.liferay.portlet.documentlibrary.model.DLFolder;
033    import com.liferay.util.dao.orm.CustomSQLUtil;
034    
035    import java.util.ArrayList;
036    import java.util.Iterator;
037    import java.util.List;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Shuyang Zhou
042     */
043    public class DLFolderFinderImpl
044            extends BasePersistenceImpl<DLFolder> implements DLFolderFinder {
045    
046            public static String COUNT_F_BY_G_M_F =
047                    DLFolderFinder.class.getName() + ".countF_ByG_M_F";
048    
049            public static String COUNT_FE_BY_G_F =
050                    DLFolderFinder.class.getName() + ".countFE_ByG_F";
051    
052            public static String COUNT_FE_BY_G_F_S =
053                    DLFolderFinder.class.getName() + ".countFE_ByG_F_S";
054    
055            public static String COUNT_FS_BY_G_F_S =
056                    DLFolderFinder.class.getName() + ".countFS_ByG_F_S";
057    
058            public static String FIND_F_BY_G_M_F =
059                    DLFolderFinder.class.getName() + ".findF_ByG_M_F";
060    
061            public static String FIND_FE_BY_G_F =
062                    DLFolderFinder.class.getName() + ".findFE_ByG_F";
063    
064            public static String FIND_FE_BY_G_F_S =
065                    DLFolderFinder.class.getName() + ".findFE_ByG_F_S";
066    
067            public static String FIND_FS_BY_G_F_S =
068                    DLFolderFinder.class.getName() + ".findFS_ByG_F_S";
069    
070            public static String JOIN_FS_BY_DL_FILE_ENTRY =
071                    DLFolderFinder.class.getName() + ".joinFS_ByDLFileEntry";
072    
073            public static String JOIN_FV_BY_DL_FILE_ENTRY =
074                    DLFolderFinder.class.getName() + ".joinFV_ByDLFileEntry";
075    
076            public int countF_FE_FS_ByG_F_S(
077                            long groupId, long folderId, int status, String[] mimeTypes,
078                            boolean includeMountFolders)
079                    throws SystemException {
080    
081                    return doCountF_FE_FS_ByG_F_S(
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(
092                            long groupId, long folderId, int status, String[] mimeTypes,
093                            boolean includeMountFolders)
094                    throws SystemException {
095    
096                    return doCountF_FE_FS_ByG_F_S(
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 List<Object> filterFindF_FE_FS_ByG_F_S(
107                            long groupId, long folderId, int status, String[] mimeTypes,
108                            boolean includeMountFolders, int start, int end,
109                            OrderByComparator obc)
110                    throws SystemException {
111    
112                    return doFindF_FE_FS_ByG_F_S(
113                            groupId, folderId, status, mimeTypes, includeMountFolders, start,
114                            end, obc, true);
115            }
116    
117            public List<Object> filterFindFE_FS_ByG_F_S(
118                            long groupId, long folderId, int status, int start, int end)
119                    throws SystemException {
120    
121                    return doFindFE_FS_ByG_F_S(groupId, folderId, status, start, end, true);
122            }
123    
124            public List<Object> findF_FE_FS_ByG_F_S(
125                            long groupId, long folderId, int status, String[] mimeTypes,
126                            boolean includeMountFolders, int start, int end,
127                            OrderByComparator obc)
128                    throws SystemException {
129    
130                    return doFindF_FE_FS_ByG_F_S(
131                            groupId, folderId, status, mimeTypes, includeMountFolders, start,
132                            end, obc, false);
133            }
134    
135            public List<Object> findFE_FS_ByG_F_S(
136                            long groupId, long folderId, int status, int start, int end)
137                    throws SystemException {
138    
139                    return doFindFE_FS_ByG_F_S(
140                            groupId, folderId, status, start, end, false);
141            }
142    
143            protected int doCountF_FE_FS_ByG_F_S(
144                            long groupId, long folderId, int status, String[] mimeTypes,
145                            boolean includeMountFolders, boolean inlineSQLHelper)
146                    throws SystemException {
147    
148                    Session session = null;
149    
150                    try {
151                            session = openSession();
152    
153                            StringBundler sb = new StringBundler(7);
154    
155                            sb.append(StringPool.OPEN_PARENTHESIS);
156    
157                            String sql = CustomSQLUtil.get(COUNT_F_BY_G_M_F);
158    
159                            if (inlineSQLHelper) {
160                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
161                                            sql, DLFolder.class.getName(), "DLFolder.folderId",
162                                            groupId);
163                            }
164    
165                            sb.append(sql);
166                            sb.append(") UNION ALL (");
167    
168                            if (status == WorkflowConstants.STATUS_ANY) {
169                                    sql = CustomSQLUtil.get(COUNT_FE_BY_G_F);
170                            }
171                            else {
172                                    sql = CustomSQLUtil.get(COUNT_FE_BY_G_F_S);
173    
174                                    if ((inlineSQLHelper &&
175                                            InlineSQLHelperUtil.isEnabled(groupId)) ||
176                                            ((mimeTypes != null) && (mimeTypes.length > 0))) {
177    
178                                            sql = StringUtil.replace(
179                                                    sql, "[$JOIN$]",
180                                                    CustomSQLUtil.get(JOIN_FV_BY_DL_FILE_ENTRY));
181                                    }
182                                    else {
183                                            sql = StringUtil.replace(sql, "[$JOIN$]", "");
184                                    }
185                            }
186    
187                            if (inlineSQLHelper) {
188                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
189                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
190                                            groupId);
191                            }
192    
193                            sb.append(sql);
194    
195                            if ((mimeTypes != null) && (mimeTypes.length > 0)) {
196                                    for (int i = 0; i < mimeTypes.length; i++) {
197                                            if (i == 0) {
198                                                    sb.append(" AND (");
199                                            }
200                                            else {
201                                                    sb.append(" OR");
202                                            }
203    
204                                            sb.append(" DLFileEntry.mimeType = '");
205                                            sb.append(mimeTypes[i]);
206                                            sb.append("'");
207                                    }
208    
209                                    sb.append(StringPool.CLOSE_PARENTHESIS);
210                            }
211    
212                            sb.append(") UNION ALL (");
213    
214                            sql = CustomSQLUtil.get(COUNT_FS_BY_G_F_S);
215    
216                            if ((inlineSQLHelper && InlineSQLHelperUtil.isEnabled(groupId)) ||
217                                    ((mimeTypes != null) && (mimeTypes.length > 0))) {
218    
219                                    sql = StringUtil.replace(
220                                            sql, "[$JOIN$]",
221                                            CustomSQLUtil.get(JOIN_FS_BY_DL_FILE_ENTRY));
222                            }
223                            else {
224                                    sql = StringUtil.replace(sql, "[$JOIN$]", "");
225                            }
226    
227                            if (inlineSQLHelper) {
228                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
229                                            sql, DLFileShortcut.class.getName(),
230                                            "DLFileShortcut.fileShortcutId", groupId);
231                            }
232    
233                            sb.append(sql);
234    
235                            if ((mimeTypes != null) && (mimeTypes.length > 0)) {
236                                    for (int i = 0; i < mimeTypes.length; i++) {
237                                            if (i == 0) {
238                                                    sb.append(" AND (");
239                                            }
240                                            else {
241                                                    sb.append(" OR");
242                                            }
243    
244                                            sb.append(" DLFileEntry.mimeType = '");
245                                            sb.append(mimeTypes[i]);
246                                            sb.append("'");
247                                    }
248    
249                                    sb.append(StringPool.CLOSE_PARENTHESIS);
250                            }
251    
252                            sb.append(StringPool.CLOSE_PARENTHESIS);
253    
254                            sql = sb.toString();
255    
256                            if (includeMountFolders) {
257                                    sql = StringUtil.replace(
258                                            sql, "(DLFolder.mountPoint = ?) AND", "");
259                            }
260    
261                            sql = StringUtil.replace(
262                                    sql, "[$FOLDER_PARENT_FOLDER_ID$]",
263                                    getFolderId(folderId, "DLFolder"));
264    
265                            if (status == WorkflowConstants.STATUS_ANY) {
266                                    sql = StringUtil.replace(
267                                            sql, "[$FILE_ENTRY_FOLDER_ID$]",
268                                            getFolderId(folderId, "DLFileEntry"));
269                            }
270                            else {
271                                    sql = StringUtil.replace(
272                                            sql, "[$FILE_ENTRY_FOLDER_ID$]",
273                                            getFolderId(folderId, "DLFileVersion"));
274                            }
275    
276                            sql = StringUtil.replace(
277                                    sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
278                                    getFolderId(folderId, "DLFileShortcut"));
279    
280                            SQLQuery q = session.createSQLQuery(sql);
281    
282                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
283    
284                            QueryPos qPos = QueryPos.getInstance(q);
285    
286                            qPos.add(groupId);
287    
288                            if (!includeMountFolders) {
289                                    qPos.add(false);
290                            }
291    
292                            qPos.add(folderId);
293                            qPos.add(groupId);
294    
295                            if (status != WorkflowConstants.STATUS_ANY) {
296                                    qPos.add(status);
297                            }
298    
299                            qPos.add(folderId);
300                            qPos.add(groupId);
301                            qPos.add(folderId);
302    
303                            int count = 0;
304    
305                            Iterator<Long> itr = q.iterate();
306    
307                            while (itr.hasNext()) {
308                                    Long l = itr.next();
309    
310                                    if (l != null) {
311                                            count += l.intValue();
312                                    }
313                            }
314    
315                            return count;
316                    }
317                    catch (Exception e) {
318                            throw new SystemException(e);
319                    }
320                    finally {
321                            closeSession(session);
322                    }
323            }
324    
325            protected int doCountFE_ByG_F_S(
326                            long groupId, long folderId, int status, boolean inlineSQLHelper)
327                    throws SystemException {
328    
329                    Session session = null;
330    
331                    try {
332                            session = openSession();
333    
334                            String sql = CustomSQLUtil.get(COUNT_FE_BY_G_F_S);
335    
336                            if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled(groupId)) {
337                                    sql = StringUtil.replace(
338                                            sql, "[$JOIN$]",
339                                            CustomSQLUtil.get(JOIN_FV_BY_DL_FILE_ENTRY));
340    
341                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
342                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
343                                            groupId);
344                            }
345                            else {
346                                    sql = StringUtil.replace(sql, "[$JOIN$]", "");
347                            }
348    
349                            sql = StringUtil.replace(
350                                    sql, "[$FILE_ENTRY_FOLDER_ID$]",
351                                    getFolderId(folderId, "DLFileVersion"));
352    
353                            SQLQuery q = session.createSQLQuery(sql);
354    
355                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
356    
357                            QueryPos qPos = QueryPos.getInstance(q);
358    
359                            qPos.add(groupId);
360                            qPos.add(status);
361                            qPos.add(folderId);
362    
363                            Iterator<Long> itr = q.iterate();
364    
365                            if (itr.hasNext()) {
366                                    Long count = itr.next();
367    
368                                    if (count != null) {
369                                            return count.intValue();
370                                    }
371                            }
372    
373                            return 0;
374                    }
375                    catch (Exception e) {
376                            throw new SystemException(e);
377                    }
378                    finally {
379                            closeSession(session);
380                    }
381            }
382    
383            protected List<Object> doFindF_FE_FS_ByG_F_S(
384                            long groupId, long folderId, int status, String[] mimeTypes,
385                            boolean includeMountFolders, int start, int end,
386                            OrderByComparator obc, boolean inlineSQLHelper)
387                    throws SystemException {
388    
389                    Session session = null;
390    
391                    try {
392                            session = openSession();
393    
394                            StringBundler sb = new StringBundler(7);
395    
396                            sb.append("SELECT * FROM (");
397    
398                            String sql = CustomSQLUtil.get(FIND_F_BY_G_M_F);
399    
400                            if (inlineSQLHelper) {
401                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
402                                            sql, DLFolder.class.getName(), "DLFolder.folderId",
403                                            groupId);
404                            }
405    
406                            sb.append(sql);
407                            sb.append(" UNION ALL ");
408    
409                            if (status == WorkflowConstants.STATUS_ANY) {
410                                    sql = CustomSQLUtil.get(FIND_FE_BY_G_F);
411                            }
412                            else {
413                                    sql = CustomSQLUtil.get(FIND_FE_BY_G_F_S);
414                            }
415    
416                            if (inlineSQLHelper) {
417                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
418                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
419                                            groupId);
420                            }
421    
422                            sb.append(sql);
423    
424                            if ((mimeTypes != null) && (mimeTypes.length > 0)) {
425                                    for (int i = 0; i < mimeTypes.length; i++) {
426                                            if (i == 0) {
427                                                    sb.append(" AND (");
428                                            }
429                                            else {
430                                                    sb.append(" OR");
431                                            }
432    
433                                            sb.append(" DLFileEntry.mimeType = '");
434                                            sb.append(mimeTypes[i]);
435                                            sb.append("'");
436                                    }
437    
438                                    sb.append(StringPool.CLOSE_PARENTHESIS);
439                            }
440    
441                            sb.append(" UNION ALL ");
442    
443                            sql = CustomSQLUtil.get(FIND_FS_BY_G_F_S);
444    
445                            if (inlineSQLHelper) {
446                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
447                                            sql, DLFileShortcut.class.getName(),
448                                            "DLFileShortcut.fileShortcutId", groupId);
449                            }
450    
451                            sb.append(sql);
452    
453                            if ((mimeTypes != null) && (mimeTypes.length > 0)) {
454                                    for (int i = 0; i < mimeTypes.length; i++) {
455                                            if (i == 0) {
456                                                    sb.append(" AND (");
457                                            }
458                                            else {
459                                                    sb.append(" OR");
460                                            }
461    
462                                            sb.append(" mimeType = '");
463                                            sb.append(mimeTypes[i]);
464                                            sb.append("'");
465                                    }
466    
467                                    sb.append(StringPool.CLOSE_PARENTHESIS);
468                            }
469    
470                            sb.append(") TEMP_TABLE ORDER BY modelFolder DESC, title ASC");
471    
472                            sql = sb.toString();
473    
474                            if (includeMountFolders) {
475                                    sql = StringUtil.replace(
476                                            sql, "(DLFolder.mountPoint = ?) AND", "");
477                            }
478    
479                            sql = StringUtil.replace(
480                                    sql, "[$FOLDER_PARENT_FOLDER_ID$]",
481                                    getFolderId(folderId, "DLFolder"));
482                            sql = StringUtil.replace(
483                                    sql, "[$FILE_ENTRY_FOLDER_ID$]",
484                                    getFolderId(folderId, "DLFileEntry"));
485                            sql = StringUtil.replace(
486                                    sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
487                                    getFolderId(folderId, "DLFileShortcut"));
488                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
489    
490                            SQLQuery q = session.createSQLQuery(sql);
491    
492                            q.addScalar("modelFolderId", Type.LONG);
493                            q.addScalar("name", Type.STRING);
494                            q.addScalar("title", Type.STRING);
495                            q.addScalar("fileShortcutId", Type.LONG);
496                            q.addScalar("modelFolder", Type.LONG);
497    
498                            QueryPos qPos = QueryPos.getInstance(q);
499    
500                            qPos.add(groupId);
501    
502                            if (!includeMountFolders) {
503                                    qPos.add(false);
504                            }
505    
506                            qPos.add(folderId);
507                            qPos.add(groupId);
508    
509                            if (status != WorkflowConstants.STATUS_ANY) {
510                                    qPos.add(status);
511                            }
512    
513                            qPos.add(folderId);
514                            qPos.add(groupId);
515                            qPos.add(folderId);
516    
517                            List<Object> models = new ArrayList<Object>();
518    
519                            Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
520                                    q, getDialect(), start, end);
521    
522                            while (itr.hasNext()) {
523                                    Object[] array = itr.next();
524    
525                                    long curFolderId = (Long)array[0];
526                                    String name = (String)array[1];
527                                    //String title = (String)array[2];
528                                    long fileShortcutId = (Long)array[3];
529                                    long modelFolder = (Long)array[4];
530    
531                                    Object obj = null;
532    
533                                    if (modelFolder == 1) {
534                                            obj = DLFolderUtil.findByPrimaryKey(curFolderId);
535                                    }
536                                    else if (fileShortcutId > 0) {
537                                            obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
538                                    }
539                                    else {
540                                            obj = DLFileEntryUtil.findByG_F_N(
541                                                    groupId, curFolderId, name);
542                                    }
543    
544                                    models.add(obj);
545                            }
546    
547                            return models;
548                    }
549                    catch (Exception e) {
550                            throw new SystemException(e);
551                    }
552                    finally {
553                            closeSession(session);
554                    }
555            }
556    
557            protected List<Object> doFindFE_FS_ByG_F_S(
558                            long groupId, long folderId, int status, int start, int end,
559                            boolean inlineSQLHelper)
560                    throws SystemException {
561    
562                    Session session = null;
563    
564                    try {
565                            session = openSession();
566    
567                            StringBundler sb = new StringBundler(5);
568    
569                            sb.append("SELECT * FROM (");
570    
571                            String sql = null;
572    
573                            if (status == WorkflowConstants.STATUS_ANY) {
574                                    sql = CustomSQLUtil.get(FIND_FE_BY_G_F);
575                            }
576                            else {
577                                    sql = CustomSQLUtil.get(FIND_FE_BY_G_F_S);
578                            }
579    
580                            if (inlineSQLHelper) {
581                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
582                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
583                                            groupId);
584                            }
585    
586                            sb.append(sql);
587                            sb.append(" UNION ALL ");
588    
589                            sql = CustomSQLUtil.get(FIND_FS_BY_G_F_S);
590    
591                            if (inlineSQLHelper) {
592                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
593                                            sql, DLFileShortcut.class.getName(),
594                                            "DLFileShortcut.fileShortcutId", groupId);
595                            }
596    
597                            sb.append(sql);
598                            sb.append(") TEMP_TABLE ORDER BY modelFolder DESC, title ASC");
599    
600                            sql = sb.toString();
601    
602                            sql = StringUtil.replace(
603                                    sql, "[$FILE_ENTRY_FOLDER_ID$]",
604                                    getFolderId(folderId, "DLFileEntry"));
605                            sql = StringUtil.replace(
606                                    sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
607                                    getFolderId(folderId, "DLFileShortcut"));
608    
609                            SQLQuery q = session.createSQLQuery(sql);
610    
611                            q.addScalar("modelFolderId", Type.LONG);
612                            q.addScalar("name", Type.STRING);
613                            q.addScalar("title", Type.STRING);
614                            q.addScalar("fileShortcutId", Type.LONG);
615    
616                            QueryPos qPos = QueryPos.getInstance(q);
617    
618                            qPos.add(groupId);
619    
620                            if (status != WorkflowConstants.STATUS_ANY) {
621                                    qPos.add(status);
622                            }
623    
624                            qPos.add(folderId);
625                            qPos.add(groupId);
626                            qPos.add(folderId);
627    
628                            List<Object> models = new ArrayList<Object>();
629    
630                            Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
631                                    q, getDialect(), start, end);
632    
633                            while (itr.hasNext()) {
634                                    Object[] array = itr.next();
635    
636                                    long folderId2 = (Long)array[0];
637                                    String name = (String)array[1];
638                                    //String title = (String)array[2];
639                                    long fileShortcutId = (Long)array[3];
640    
641                                    Object obj = null;
642    
643                                    if (fileShortcutId > 0) {
644                                            obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
645                                    }
646                                    else {
647                                            obj = DLFileEntryUtil.findByG_F_N(groupId, folderId2, name);
648                                    }
649    
650                                    models.add(obj);
651                            }
652    
653                            return models;
654                    }
655                    catch (Exception e) {
656                            throw new SystemException(e);
657                    }
658                    finally {
659                            closeSession(session);
660                    }
661            }
662    
663            protected String getFolderId(long folderId, String table) {
664                    StringBundler sb = new StringBundler(4);
665    
666                    sb.append(table);
667                    sb.append(".");
668    
669                    if (table.equals("DLFolder")) {
670                            sb.append("parentFolderId");
671                    }
672                    else {
673                            sb.append("folderId");
674                    }
675    
676                    sb.append("= ? ");
677    
678                    return sb.toString();
679            }
680    
681    }