001    /**
002     * Copyright (c) 2000-2011 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 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(
101                            long groupId, long folderId, int status)
102                    throws SystemException {
103    
104                    return doCountFE_ByG_F_S(groupId, folderId, status, true);
105            }
106    
107            public List<Object> filterFindF_FE_FS_ByG_F_S(
108                            long groupId, long folderId, int status, String[] mimeTypes,
109                            boolean includeMountFolders, int start, int end,
110                            OrderByComparator obc)
111                    throws SystemException {
112    
113                    return doFindF_FE_FS_ByG_F_S(
114                            groupId, folderId, status, mimeTypes, includeMountFolders, start,
115                            end, obc, true);
116            }
117    
118            public List<Object> filterFindFE_FS_ByG_F_S(
119                            long groupId, long folderId, int status, int start, int end)
120                    throws SystemException {
121    
122                    return doFindFE_FS_ByG_F_S(
123                            groupId, folderId, status, start, end, true);
124            }
125    
126            public List<Object> findF_FE_FS_ByG_F_S(
127                            long groupId, long folderId, int status, String[] mimeTypes,
128                            boolean includeMountFolders, int start, int end,
129                            OrderByComparator obc)
130                    throws SystemException {
131    
132                    return doFindF_FE_FS_ByG_F_S(
133                            groupId, folderId, status, mimeTypes, includeMountFolders, start,
134                            end, obc, false);
135            }
136    
137            public List<Object> findFE_FS_ByG_F_S(
138                            long groupId, long folderId, int status, int start, int end)
139                    throws SystemException {
140    
141                    return doFindFE_FS_ByG_F_S(
142                            groupId, folderId, status, start, end, false);
143            }
144    
145            protected int doCountF_FE_FS_ByG_F_S(
146                            long groupId, long folderId, int status, String[] mimeTypes,
147                            boolean includeMountFolders, boolean inlineSQLHelper)
148                    throws SystemException {
149    
150                    Session session = null;
151    
152                    try {
153                            session = openSession();
154    
155                            StringBundler sb = new StringBundler(7);
156    
157                            sb.append(StringPool.OPEN_PARENTHESIS);
158    
159                            String sql = CustomSQLUtil.get(COUNT_F_BY_G_M_F);
160    
161                            if (inlineSQLHelper) {
162                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
163                                            sql, DLFolder.class.getName(), "DLFolder.folderId",
164                                            groupId);
165                            }
166    
167                            sb.append(sql);
168                            sb.append(") UNION ALL (");
169    
170                            if (status == WorkflowConstants.STATUS_ANY) {
171                                    sql = CustomSQLUtil.get(COUNT_FE_BY_G_F);
172                            }
173                            else {
174                                    sql = CustomSQLUtil.get(COUNT_FE_BY_G_F_S);
175    
176                                    if ((inlineSQLHelper &&
177                                            InlineSQLHelperUtil.isEnabled(groupId)) ||
178                                            ((mimeTypes != null) && (mimeTypes.length > 0))) {
179    
180                                            sql = StringUtil.replace(
181                                                    sql, "[$JOIN$]",
182                                                    CustomSQLUtil.get(JOIN_FV_BY_DL_FILE_ENTRY));
183                                    }
184                                    else {
185                                            sql = StringUtil.replace(sql, "[$JOIN$]", "");
186                                    }
187                            }
188    
189                            if (inlineSQLHelper) {
190                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
191                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
192                                            groupId);
193                            }
194    
195                            sb.append(sql);
196    
197                            if ((mimeTypes != null) && (mimeTypes.length > 0)) {
198                                    for (int i = 0; i < mimeTypes.length; i++) {
199                                            if (i == 0) {
200                                                    sb.append(" AND (");
201                                            }
202                                            else {
203                                                    sb.append(" OR");
204                                            }
205    
206                                            sb.append(" DLFileEntry.mimeType = '");
207                                            sb.append(mimeTypes[i]);
208                                            sb.append("'");
209                                    }
210    
211                                    sb.append(StringPool.CLOSE_PARENTHESIS);
212                            }
213    
214                            sb.append(") UNION ALL (");
215    
216                            sql = CustomSQLUtil.get(COUNT_FS_BY_G_F_S);
217    
218                            if ((inlineSQLHelper && InlineSQLHelperUtil.isEnabled(groupId)) ||
219                                    ((mimeTypes != null) && (mimeTypes.length > 0))) {
220    
221                                    sql = StringUtil.replace(
222                                            sql, "[$JOIN$]",
223                                            CustomSQLUtil.get(JOIN_FS_BY_DL_FILE_ENTRY));
224                            }
225                            else {
226                                    sql = StringUtil.replace(sql, "[$JOIN$]", "");
227                            }
228    
229                            if (inlineSQLHelper) {
230                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
231                                            sql, DLFileShortcut.class.getName(),
232                                            "DLFileShortcut.fileShortcutId", groupId);
233                            }
234    
235                            sb.append(sql);
236    
237                            if ((mimeTypes != null) && (mimeTypes.length > 0)) {
238                                    for (int i = 0; i < mimeTypes.length; i++) {
239                                            if (i == 0) {
240                                                    sb.append(" AND (");
241                                            }
242                                            else {
243                                                    sb.append(" OR");
244                                            }
245    
246                                            sb.append(" DLFileEntry.mimeType = '");
247                                            sb.append(mimeTypes[i]);
248                                            sb.append("'");
249                                    }
250    
251                                    sb.append(StringPool.CLOSE_PARENTHESIS);
252                            }
253    
254                            sb.append(StringPool.CLOSE_PARENTHESIS);
255    
256                            sql = sb.toString();
257    
258                            if (includeMountFolders) {
259                                    sql = StringUtil.replace(
260                                            sql, "(DLFolder.mountPoint = ?) AND", "");
261                            }
262    
263                            sql = StringUtil.replace(
264                                    sql, "[$FOLDER_PARENT_FOLDER_ID$]",
265                                    getFolderId(folderId, "DLFolder"));
266    
267                            if (status == WorkflowConstants.STATUS_ANY) {
268                                    sql = StringUtil.replace(
269                                            sql, "[$FILE_ENTRY_FOLDER_ID$]",
270                                            getFolderId(folderId, "DLFileEntry"));
271                            }
272                            else {
273                                    sql = StringUtil.replace(
274                                            sql, "[$FILE_ENTRY_FOLDER_ID$]",
275                                            getFolderId(folderId, "DLFileVersion"));
276                            }
277    
278                            sql = StringUtil.replace(
279                                    sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
280                                    getFolderId(folderId, "DLFileShortcut"));
281    
282                            SQLQuery q = session.createSQLQuery(sql);
283    
284                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
285    
286                            QueryPos qPos = QueryPos.getInstance(q);
287    
288                            qPos.add(groupId);
289    
290                            if (!includeMountFolders) {
291                                    qPos.add(false);
292                            }
293    
294                            qPos.add(folderId);
295                            qPos.add(groupId);
296    
297                            if (status != WorkflowConstants.STATUS_ANY) {
298                                    qPos.add(status);
299                            }
300    
301                            qPos.add(folderId);
302                            qPos.add(groupId);
303                            qPos.add(folderId);
304    
305                            int count = 0;
306    
307                            Iterator<Long> itr = q.iterate();
308    
309                            while (itr.hasNext()) {
310                                    Long l = itr.next();
311    
312                                    if (l != null) {
313                                            count += l.intValue();
314                                    }
315                            }
316    
317                            return count;
318                    }
319                    catch (Exception e) {
320                            throw new SystemException(e);
321                    }
322                    finally {
323                            closeSession(session);
324                    }
325            }
326    
327            protected int doCountFE_ByG_F_S(
328                            long groupId, long folderId, int status, boolean inlineSQLHelper)
329                    throws SystemException {
330    
331                    Session session = null;
332    
333                    try {
334                            session = openSession();
335    
336                            String sql = CustomSQLUtil.get(COUNT_FE_BY_G_F_S);
337    
338                            if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled(groupId)) {
339                                    sql = StringUtil.replace(
340                                            sql, "[$JOIN$]",
341                                            CustomSQLUtil.get(JOIN_FV_BY_DL_FILE_ENTRY));
342    
343                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
344                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
345                                            groupId);
346                            }
347                            else {
348                                    sql = StringUtil.replace(sql, "[$JOIN$]", "");
349                            }
350    
351                            sql = StringUtil.replace(
352                                    sql, "[$FILE_ENTRY_FOLDER_ID$]",
353                                    getFolderId(folderId, "DLFileVersion"));
354    
355                            SQLQuery q = session.createSQLQuery(sql);
356    
357                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
358    
359                            QueryPos qPos = QueryPos.getInstance(q);
360    
361                            qPos.add(groupId);
362                            qPos.add(status);
363                            qPos.add(folderId);
364    
365                            Iterator<Long> itr = q.iterate();
366    
367                            if (itr.hasNext()) {
368                                    Long count = itr.next();
369    
370                                    if (count != null) {
371                                            return count.intValue();
372                                    }
373                            }
374    
375                            return 0;
376                    }
377                    catch (Exception e) {
378                            throw new SystemException(e);
379                    }
380                    finally {
381                            closeSession(session);
382                    }
383            }
384    
385            protected List<Object> doFindF_FE_FS_ByG_F_S(
386                            long groupId, long folderId, int status, String[] mimeTypes,
387                            boolean includeMountFolders, int start, int end,
388                            OrderByComparator obc, boolean inlineSQLHelper)
389                    throws SystemException {
390    
391                    Session session = null;
392    
393                    try {
394                            session = openSession();
395    
396                            StringBundler sb = new StringBundler(7);
397    
398                            sb.append("SELECT * FROM (");
399    
400                            String sql = CustomSQLUtil.get(FIND_F_BY_G_M_F);
401    
402                            if (inlineSQLHelper) {
403                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
404                                            sql, DLFolder.class.getName(), "DLFolder.folderId",
405                                            groupId);
406                            }
407    
408                            sb.append(sql);
409                            sb.append(" UNION ALL ");
410    
411                            if (status == WorkflowConstants.STATUS_ANY) {
412                                    sql = CustomSQLUtil.get(FIND_FE_BY_G_F);
413                            }
414                            else {
415                                    sql = CustomSQLUtil.get(FIND_FE_BY_G_F_S);
416                            }
417    
418                            if (inlineSQLHelper) {
419                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
420                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
421                                            groupId);
422                            }
423    
424                            sb.append(sql);
425    
426                            if ((mimeTypes != null) && (mimeTypes.length > 0)) {
427                                    for (int i = 0; i < mimeTypes.length; i++) {
428                                            if (i == 0) {
429                                                    sb.append(" AND (");
430                                            }
431                                            else {
432                                                    sb.append(" OR");
433                                            }
434    
435                                            sb.append(" DLFileEntry.mimeType = '");
436                                            sb.append(mimeTypes[i]);
437                                            sb.append("'");
438                                    }
439    
440                                    sb.append(StringPool.CLOSE_PARENTHESIS);
441                            }
442    
443                            sb.append(" UNION ALL ");
444    
445                            sql = CustomSQLUtil.get(FIND_FS_BY_G_F_S);
446    
447                            if (inlineSQLHelper) {
448                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
449                                            sql, DLFileShortcut.class.getName(),
450                                            "DLFileShortcut.fileShortcutId", groupId);
451                            }
452    
453                            sb.append(sql);
454    
455                            if ((mimeTypes != null) && (mimeTypes.length > 0)) {
456                                    for (int i = 0; i < mimeTypes.length; i++) {
457                                            if (i == 0) {
458                                                    sb.append(" AND (");
459                                            }
460                                            else {
461                                                    sb.append(" OR");
462                                            }
463    
464                                            sb.append(" mimeType = '");
465                                            sb.append(mimeTypes[i]);
466                                            sb.append("'");
467                                    }
468    
469                                    sb.append(StringPool.CLOSE_PARENTHESIS);
470                            }
471    
472                            sb.append(") TEMP_TABLE ORDER BY modelFolder DESC, title ASC");
473    
474                            sql = sb.toString();
475    
476                            if (includeMountFolders) {
477                                    sql = StringUtil.replace(
478                                            sql, "(DLFolder.mountPoint = ?) AND", "");
479                            }
480    
481                            sql = StringUtil.replace(
482                                    sql, "[$FOLDER_PARENT_FOLDER_ID$]",
483                                    getFolderId(folderId, "DLFolder"));
484                            sql = StringUtil.replace(
485                                    sql, "[$FILE_ENTRY_FOLDER_ID$]",
486                                    getFolderId(folderId, "DLFileEntry"));
487                            sql = StringUtil.replace(
488                                    sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
489                                    getFolderId(folderId, "DLFileShortcut"));
490                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
491    
492                            SQLQuery q = session.createSQLQuery(sql);
493    
494                            q.addScalar("modelFolderId", Type.LONG);
495                            q.addScalar("name", Type.STRING);
496                            q.addScalar("title", Type.STRING);
497                            q.addScalar("fileShortcutId", Type.LONG);
498                            q.addScalar("modelFolder", Type.LONG);
499    
500                            QueryPos qPos = QueryPos.getInstance(q);
501    
502                            qPos.add(groupId);
503    
504                            if (!includeMountFolders) {
505                                    qPos.add(false);
506                            }
507    
508                            qPos.add(folderId);
509                            qPos.add(groupId);
510    
511                            if (status != WorkflowConstants.STATUS_ANY) {
512                                    qPos.add(status);
513                            }
514    
515                            qPos.add(folderId);
516                            qPos.add(groupId);
517                            qPos.add(folderId);
518    
519                            List<Object> models = new ArrayList<Object>();
520    
521                            Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
522                                    q, getDialect(), start, end);
523    
524                            while (itr.hasNext()) {
525                                    Object[] array = itr.next();
526    
527                                    long curFolderId = (Long)array[0];
528                                    String name = (String)array[1];
529                                    //String title = (String)array[2];
530                                    long fileShortcutId = (Long)array[3];
531                                    long modelFolder = (Long)array[4];
532    
533                                    Object obj = null;
534    
535                                    if (modelFolder == 1) {
536                                            obj = DLFolderUtil.findByPrimaryKey(curFolderId);
537                                    }
538                                    else if (fileShortcutId > 0) {
539                                            obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
540                                    }
541                                    else {
542                                            obj = DLFileEntryUtil.findByG_F_N(
543                                                    groupId, curFolderId, name);
544                                    }
545    
546                                    models.add(obj);
547                            }
548    
549                            return models;
550                    }
551                    catch (Exception e) {
552                            throw new SystemException(e);
553                    }
554                    finally {
555                            closeSession(session);
556                    }
557            }
558    
559            protected List<Object> doFindFE_FS_ByG_F_S(
560                            long groupId, long folderId, int status, int start, int end,
561                            boolean inlineSQLHelper)
562                    throws SystemException {
563    
564                    Session session = null;
565    
566                    try {
567                            session = openSession();
568    
569                            StringBundler sb = new StringBundler(5);
570    
571                            sb.append("SELECT * FROM (");
572    
573                            String sql = null;
574    
575                            if (status == WorkflowConstants.STATUS_ANY) {
576                                    sql = CustomSQLUtil.get(FIND_FE_BY_G_F);
577                            }
578                            else {
579                                    sql = CustomSQLUtil.get(FIND_FE_BY_G_F_S);
580                            }
581    
582                            if (inlineSQLHelper) {
583                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
584                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
585                                            groupId);
586                            }
587    
588                            sb.append(sql);
589                            sb.append(" UNION ALL ");
590    
591                            sql = CustomSQLUtil.get(FIND_FS_BY_G_F_S);
592    
593                            if (inlineSQLHelper) {
594                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
595                                            sql, DLFileShortcut.class.getName(),
596                                            "DLFileShortcut.fileShortcutId", groupId);
597                            }
598    
599                            sb.append(sql);
600                            sb.append(") TEMP_TABLE ORDER BY modelFolder DESC, title ASC");
601    
602                            sql = sb.toString();
603    
604                            sql = StringUtil.replace(
605                                    sql, "[$FILE_ENTRY_FOLDER_ID$]",
606                                    getFolderId(folderId, "DLFileEntry"));
607                            sql = StringUtil.replace(
608                                    sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
609                                    getFolderId(folderId, "DLFileShortcut"));
610    
611                            SQLQuery q = session.createSQLQuery(sql);
612    
613                            q.addScalar("modelFolderId", Type.LONG);
614                            q.addScalar("name", Type.STRING);
615                            q.addScalar("title", Type.STRING);
616                            q.addScalar("fileShortcutId", Type.LONG);
617    
618                            QueryPos qPos = QueryPos.getInstance(q);
619    
620                            qPos.add(groupId);
621    
622                            if (status != WorkflowConstants.STATUS_ANY) {
623                                    qPos.add(status);
624                            }
625    
626                            qPos.add(folderId);
627                            qPos.add(groupId);
628                            qPos.add(folderId);
629    
630                            List<Object> models = new ArrayList<Object>();
631    
632                            Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
633                                    q, getDialect(), start, end);
634    
635                            while (itr.hasNext()) {
636                                    Object[] array = itr.next();
637    
638                                    long folderId2 = (Long)array[0];
639                                    String name = (String)array[1];
640                                    //String title = (String)array[2];
641                                    long fileShortcutId = (Long)array[3];
642    
643                                    Object obj = null;
644    
645                                    if (fileShortcutId > 0) {
646                                            obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
647                                    }
648                                    else {
649                                            obj = DLFileEntryUtil.findByG_F_N(groupId, folderId2, name);
650                                    }
651    
652                                    models.add(obj);
653                            }
654    
655                            return models;
656                    }
657                    catch (Exception e) {
658                            throw new SystemException(e);
659                    }
660                    finally {
661                            closeSession(session);
662                    }
663            }
664    
665            protected String getFolderId(long folderId, String table) {
666                    StringBundler sb = new StringBundler(4);
667    
668                    sb.append(table);
669                    sb.append(".");
670    
671                    if (table.equals("DLFolder")) {
672                            sb.append("parentFolderId");
673                    }
674                    else {
675                            sb.append("folderId");
676                    }
677    
678                    sb.append("= ? ");
679    
680                    return sb.toString();
681            }
682    
683    }