001    /**
002     * Copyright (c) 2000-2010 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.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
028    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
029    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
030    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
031    import com.liferay.portlet.documentlibrary.model.DLFolder;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.ArrayList;
035    import java.util.Iterator;
036    import java.util.List;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class DLFolderFinderImpl
042            extends BasePersistenceImpl<DLFolder> implements DLFolderFinder {
043    
044            public static String COUNT_F_BY_G_F_S =
045                    DLFolderFinder.class.getName() + ".countF_ByG_F_S";
046    
047            public static String COUNT_FE_BY_G_F_S =
048                    DLFolderFinder.class.getName() + ".countFE_ByG_F_S";
049    
050            public static String COUNT_FS_BY_G_F_S =
051                    DLFolderFinder.class.getName() + ".countFS_ByG_F_S";
052    
053            public static String FIND_F_BY_G_F_S =
054                    DLFolderFinder.class.getName() + ".findF_ByG_F_S";
055    
056            public static String FIND_FE_BY_G_F_S =
057                    DLFolderFinder.class.getName() + ".findFE_ByG_F_S";
058    
059            public static String FIND_FS_BY_G_F_S =
060                    DLFolderFinder.class.getName() + ".findFS_ByG_F_S";
061    
062            public int countF_FE_FS_ByG_F_S(
063                            long groupId, List<Long> folderIds, int status)
064                    throws SystemException {
065    
066                    return doCountF_FE_FS_ByG_F_S(groupId, folderIds, status, false);
067            }
068    
069            public int countFE_FS_ByG_F_S(
070                            long groupId, List<Long> folderIds, int status)
071                    throws SystemException {
072    
073                    return doCountFE_FS_ByG_F_S(groupId, folderIds, status, false);
074            }
075    
076            public int filterCountF_FE_FS_ByG_F_S(
077                            long groupId, List<Long> folderIds, int status)
078                    throws SystemException {
079    
080                    return doCountF_FE_FS_ByG_F_S(groupId, folderIds, status, true);
081            }
082    
083            public int filterCountFE_FS_ByG_F_S(
084                            long groupId, List<Long> folderIds, int status)
085                    throws SystemException {
086    
087                    return doCountFE_FS_ByG_F_S(groupId, folderIds, status, true);
088            }
089    
090            public List<Object> filterFindF_FE_FS_ByG_F_S(
091                            long groupId, List<Long> folderIds, int status, int start, int end)
092                    throws SystemException {
093    
094                    return doFindF_FE_FS_ByG_F_S(
095                            groupId, folderIds, status, start, end, true);
096            }
097    
098            public List<Object> filterFindFE_FS_ByG_F_S(
099                            long groupId, List<Long> folderIds, int status, int start, int end)
100                    throws SystemException {
101    
102                    return doFindFE_FS_ByG_F_S(
103                            groupId, folderIds, status, start, end, true);
104            }
105    
106            public List<Object> findF_FE_FS_ByG_F_S(
107                            long groupId, List<Long> folderIds, int status, int start, int end)
108                    throws SystemException {
109    
110                    return doFindF_FE_FS_ByG_F_S(
111                            groupId, folderIds, status, start, end, false);
112            }
113    
114            public List<Object> findFE_FS_ByG_F_S(
115                            long groupId, List<Long> folderIds, int status, int start, int end)
116                    throws SystemException {
117    
118                    return doFindFE_FS_ByG_F_S(
119                            groupId, folderIds, status, start, end, false);
120            }
121    
122            protected int doCountF_FE_FS_ByG_F_S(
123                            long groupId, List<Long> folderIds, int status,
124                            boolean inlineSQLHelper)
125                    throws SystemException {
126    
127                    Session session = null;
128    
129                    try {
130                            session = openSession();
131    
132                            StringBundler sb = new StringBundler(7);
133    
134                            sb.append(StringPool.OPEN_PARENTHESIS);
135    
136                            String sql = CustomSQLUtil.get(COUNT_F_BY_G_F_S);
137    
138                            if (inlineSQLHelper) {
139                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
140                                            sql, DLFolder.class.getName(), "DLFolder.folderId",
141                                            "DLFolder.userId", groupId);
142                            }
143    
144                            sb.append(sql);
145                            sb.append(") UNION ALL (");
146    
147                            sql = CustomSQLUtil.get(COUNT_FE_BY_G_F_S);
148    
149                            if (inlineSQLHelper) {
150                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
151                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
152                                            "DLFileEntry.userId", groupId);
153                            }
154    
155                            sb.append(sql);
156                            sb.append(") UNION ALL (");
157    
158                            sql = CustomSQLUtil.get(COUNT_FS_BY_G_F_S);
159    
160                            if (inlineSQLHelper) {
161                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
162                                            sql, DLFileShortcut.class.getName(),
163                                            "DLFileShortcut.fileShortcutId", "DLFileShortcut.userId",
164                                            groupId);
165                            }
166    
167                            sb.append(sql);
168                            sb.append(StringPool.CLOSE_PARENTHESIS);
169    
170                            sql = sb.toString();
171    
172                            sql = StringUtil.replace(
173                                    sql, "[$FOLDER_PARENT_FOLDER_ID$]",
174                                    getFolderIds(folderIds, "DLFolder"));
175                            sql = StringUtil.replace(
176                                    sql, "[$FILE_ENTRY_FOLDER_ID$]",
177                                    getFolderIds(folderIds, "DLFileEntry"));
178                            sql = StringUtil.replace(
179                                    sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
180                                    getFolderIds(folderIds, "DLFileShortcut"));
181    
182                            if (status == WorkflowConstants.STATUS_ANY) {
183                                    sql = StringUtil.replace(
184                                            sql, "(DLFileVersion.status = ?) AND", "");
185                            }
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                            for (Long folderId : folderIds) {
196                                    qPos.add(folderId);
197                            }
198    
199                            qPos.add(groupId);
200    
201                            if (status != WorkflowConstants.STATUS_ANY) {
202                                    qPos.add(status);
203                            }
204    
205                            for (Long folderId : folderIds) {
206                                    qPos.add(folderId);
207                            }
208    
209                            qPos.add(groupId);
210    
211                            for (Long folderId : folderIds) {
212                                    qPos.add(folderId);
213                            }
214    
215                            int count = 0;
216    
217                            Iterator<Long> itr = q.list().iterator();
218    
219                            while (itr.hasNext()) {
220                                    Long l = itr.next();
221    
222                                    if (l != null) {
223                                            count += l.intValue();
224                                    }
225                            }
226    
227                            return count;
228                    }
229                    catch (Exception e) {
230                            throw new SystemException(e);
231                    }
232                    finally {
233                            closeSession(session);
234                    }
235            }
236    
237            protected int doCountFE_FS_ByG_F_S(
238                            long groupId, List<Long> folderIds, int status,
239                            boolean inlineSQLHelper)
240                    throws SystemException {
241    
242                    Session session = null;
243    
244                    try {
245                            session = openSession();
246    
247                            StringBundler sb = new StringBundler(5);
248    
249                            sb.append(StringPool.OPEN_PARENTHESIS);
250    
251                            String sql = CustomSQLUtil.get(COUNT_FE_BY_G_F_S);
252    
253                            if (inlineSQLHelper) {
254                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
255                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
256                                            "DLFileEntry.userId", groupId);
257                            }
258    
259                            sb.append(sql);
260                            sb.append(") UNION ALL (");
261    
262                            sql = CustomSQLUtil.get(COUNT_FS_BY_G_F_S);
263    
264                            if (inlineSQLHelper) {
265                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
266                                            sql, DLFileShortcut.class.getName(),
267                                            "DLFileShortcut.fileShortcutId", "DLFileShortcut.userId",
268                                            groupId);
269                            }
270    
271                            sb.append(sql);
272                            sb.append(StringPool.CLOSE_PARENTHESIS);
273    
274                            sql = sb.toString();
275    
276                            sql = StringUtil.replace(
277                                    sql, "[$FILE_ENTRY_FOLDER_ID$]",
278                                    getFolderIds(folderIds, "DLFileEntry"));
279                            sql = StringUtil.replace(
280                                    sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
281                                    getFolderIds(folderIds, "DLFileShortcut"));
282    
283                            if (status == WorkflowConstants.STATUS_ANY) {
284                                    sql = StringUtil.replace(
285                                            sql, "(DLFileVersion.status = ?) AND", "");
286                            }
287    
288                            SQLQuery q = session.createSQLQuery(sql);
289    
290                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
291    
292                            QueryPos qPos = QueryPos.getInstance(q);
293    
294                            qPos.add(groupId);
295    
296                            if (status != WorkflowConstants.STATUS_ANY) {
297                                    qPos.add(status);
298                            }
299    
300                            for (Long folderId : folderIds) {
301                                    qPos.add(folderId);
302                            }
303    
304                            qPos.add(groupId);
305    
306                            for (Long folderId : folderIds) {
307                                    qPos.add(folderId);
308                            }
309    
310                            int count = 0;
311    
312                            Iterator<Long> itr = q.list().iterator();
313    
314                            while (itr.hasNext()) {
315                                    Long l = itr.next();
316    
317                                    if (l != null) {
318                                            count += l.intValue();
319                                    }
320                            }
321    
322                            return count;
323                    }
324                    catch (Exception e) {
325                            throw new SystemException(e);
326                    }
327                    finally {
328                            closeSession(session);
329                    }
330            }
331    
332            protected List<Object> doFindF_FE_FS_ByG_F_S(
333                            long groupId, List<Long> folderIds, int status, int start, int end,
334                            boolean inlineSQLHelper)
335                    throws SystemException {
336    
337                    Session session = null;
338    
339                    try {
340                            session = openSession();
341    
342                            StringBundler sb = new StringBundler(7);
343    
344                            sb.append("SELECT * FROM ((");
345    
346                            String sql = CustomSQLUtil.get(FIND_F_BY_G_F_S);
347    
348                            if (inlineSQLHelper) {
349                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
350                                            sql, DLFolder.class.getName(), "DLFolder.folderId",
351                                            "DLFolder.userId", groupId);
352                            }
353    
354                            sb.append(sql);
355                            sb.append(") UNION ALL (");
356    
357                            sql = CustomSQLUtil.get(FIND_FE_BY_G_F_S);
358    
359                            if (inlineSQLHelper) {
360                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
361                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
362                                            "DLFileEntry.userId", groupId);
363                            }
364    
365                            sb.append(sql);
366                            sb.append(") UNION ALL (");
367    
368                            sql = CustomSQLUtil.get(FIND_FS_BY_G_F_S);
369    
370                            if (inlineSQLHelper) {
371                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
372                                            sql, DLFileShortcut.class.getName(),
373                                            "DLFileShortcut.fileShortcutId", "DLFileShortcut.userId",
374                                            groupId);
375                            }
376    
377                            sb.append(sql);
378                            sb.append(")) TEMP_TABLE ORDER BY modelFolder DESC, title ASC");
379    
380                            sql = sb.toString();
381    
382                            sql = StringUtil.replace(
383                                    sql, "[$FOLDER_PARENT_FOLDER_ID$]",
384                                    getFolderIds(folderIds, "DLFolder"));
385                            sql = StringUtil.replace(
386                                    sql, "[$FILE_ENTRY_FOLDER_ID$]",
387                                    getFolderIds(folderIds, "DLFileEntry"));
388                            sql = StringUtil.replace(
389                                    sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
390                                    getFolderIds(folderIds, "DLFileShortcut"));
391    
392                            if (status == WorkflowConstants.STATUS_ANY) {
393                                    sql = StringUtil.replace(
394                                            sql, "(DLFileVersion.status = ?) AND", "");
395                            }
396    
397                            SQLQuery q = session.createSQLQuery(sql);
398    
399                            q.addScalar("modelFolderId", Type.LONG);
400                            q.addScalar("name", Type.STRING);
401                            q.addScalar("title", Type.STRING);
402                            q.addScalar("fileShortcutId", Type.LONG);
403                            q.addScalar("modelFolder", Type.LONG);
404    
405                            QueryPos qPos = QueryPos.getInstance(q);
406    
407                            qPos.add(groupId);
408    
409                            for (Long folderId : folderIds) {
410                                    qPos.add(folderId);
411                            }
412    
413                            qPos.add(groupId);
414    
415                            if (status != WorkflowConstants.STATUS_ANY) {
416                                    qPos.add(status);
417                            }
418    
419                            for (Long folderId : folderIds) {
420                                    qPos.add(folderId);
421                            }
422    
423                            qPos.add(groupId);
424    
425                            for (Long folderId : folderIds) {
426                                    qPos.add(folderId);
427                            }
428    
429                            List<Object> models = new ArrayList<Object>();
430    
431                            Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
432                                    q, getDialect(), start, end);
433    
434                            while (itr.hasNext()) {
435                                    Object[] array = itr.next();
436    
437                                    long folderId = (Long)array[0];
438                                    String name = (String)array[1];
439                                    //String title = (String)array[2];
440                                    long fileShortcutId = (Long)array[3];
441                                    long modelFolder = (Long)array[4];
442    
443                                    Object obj = null;
444    
445                                    if (modelFolder == 1) {
446                                            obj = DLFolderUtil.findByPrimaryKey(folderId);
447                                    }
448                                    else if (fileShortcutId > 0) {
449                                            obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
450                                    }
451                                    else {
452                                            obj = DLFileEntryUtil.findByG_F_N(groupId, folderId, name);
453                                    }
454    
455                                    models.add(obj);
456                            }
457    
458                            return models;
459                    }
460                    catch (Exception e) {
461                            throw new SystemException(e);
462                    }
463                    finally {
464                            closeSession(session);
465                    }
466            }
467    
468            protected List<Object> doFindFE_FS_ByG_F_S(
469                            long groupId, List<Long> folderIds, int status, int start, int end,
470                            boolean inlineSQLHelper)
471                    throws SystemException {
472    
473                    Session session = null;
474    
475                    try {
476                            session = openSession();
477    
478                            StringBundler sb = new StringBundler(5);
479    
480                            sb.append("SELECT * FROM ((");
481    
482                            String sql = CustomSQLUtil.get(FIND_FE_BY_G_F_S);
483    
484                            if (inlineSQLHelper) {
485                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
486                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
487                                            "DLFileEntry.userId", groupId);
488                            }
489    
490                            sb.append(sql);
491                            sb.append(") UNION ALL (");
492    
493                            sql = CustomSQLUtil.get(FIND_FS_BY_G_F_S);
494    
495                            if (inlineSQLHelper) {
496                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
497                                            sql, DLFileShortcut.class.getName(),
498                                            "DLFileShortcut.fileShortcutId", "DLFileShortcut.userId",
499                                            groupId);
500                            }
501    
502                            sb.append(sql);
503                            sb.append(")) TEMP_TABLE ORDER BY modelFolder DESC, title ASC");
504    
505                            sql = sb.toString();
506    
507                            sql = StringUtil.replace(
508                                    sql, "[$FILE_ENTRY_FOLDER_ID$]",
509                                    getFolderIds(folderIds, "DLFileEntry"));
510                            sql = StringUtil.replace(
511                                    sql, "[$FILE_SHORTCUT_FOLDER_ID$]",
512                                    getFolderIds(folderIds, "DLFileShortcut"));
513    
514                            if (status == WorkflowConstants.STATUS_ANY) {
515                                    sql = StringUtil.replace(
516                                            sql, "(DLFileVersion.status = ?) AND", "");
517                            }
518    
519                            SQLQuery q = session.createSQLQuery(sql);
520    
521                            q.addScalar("modelFolderId", Type.LONG);
522                            q.addScalar("name", Type.STRING);
523                            q.addScalar("title", Type.STRING);
524                            q.addScalar("fileShortcutId", Type.LONG);
525    
526                            QueryPos qPos = QueryPos.getInstance(q);
527    
528                            qPos.add(groupId);
529    
530                            if (status != WorkflowConstants.STATUS_ANY) {
531                                    qPos.add(status);
532                            }
533    
534                            for (Long folderId : folderIds) {
535                                    qPos.add(folderId);
536                            }
537    
538                            qPos.add(groupId);
539    
540                            for (Long folderId : folderIds) {
541                                    qPos.add(folderId);
542                            }
543    
544                            List<Object> models = new ArrayList<Object>();
545    
546                            Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
547                                    q, getDialect(), start, end);
548    
549                            while (itr.hasNext()) {
550                                    Object[] array = itr.next();
551    
552                                    long folderId = (Long)array[0];
553                                    String name = (String)array[1];
554                                    //String title = (String)array[2];
555                                    long fileShortcutId = (Long)array[3];
556    
557                                    Object obj = null;
558    
559                                    if (fileShortcutId > 0) {
560                                            obj = DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
561                                    }
562                                    else {
563                                            obj = DLFileEntryUtil.findByG_F_N(groupId, folderId, name);
564                                    }
565    
566                                    models.add(obj);
567                            }
568    
569                            return models;
570                    }
571                    catch (Exception e) {
572                            throw new SystemException(e);
573                    }
574                    finally {
575                            closeSession(session);
576                    }
577            }
578    
579            protected String getFolderIds(List<Long> folderIds, String table) {
580                    if (folderIds.isEmpty()) {
581                            return StringPool.BLANK;
582                    }
583    
584                    StringBundler sb = new StringBundler(folderIds.size() * 5 - 1);
585    
586                    for (int i = 0; i < folderIds.size(); i++) {
587                            sb.append(table);
588                            sb.append(".");
589    
590                            if (table.equals("DLFolder")) {
591                                    sb.append("parentFolderId");
592                            }
593                            else {
594                                    sb.append("folderId");
595                            }
596    
597                            sb.append("= ? ");
598    
599                            if ((i + 1) != folderIds.size()) {
600                                    sb.append("OR ");
601                            }
602                    }
603    
604                    return sb.toString();
605            }
606    
607    }