001    /**
002     * Copyright (c) 2000-2013 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.QueryDefinition;
018    import com.liferay.portal.kernel.dao.orm.QueryPos;
019    import com.liferay.portal.kernel.dao.orm.QueryUtil;
020    import com.liferay.portal.kernel.dao.orm.SQLQuery;
021    import com.liferay.portal.kernel.dao.orm.Session;
022    import com.liferay.portal.kernel.dao.orm.Type;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.util.ArrayUtil;
025    import com.liferay.portal.kernel.util.DateRange;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
031    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
032    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
033    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
034    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
035    import com.liferay.portlet.documentlibrary.model.impl.DLFileVersionImpl;
036    import com.liferay.util.dao.orm.CustomSQLUtil;
037    
038    import java.util.Collections;
039    import java.util.Iterator;
040    import java.util.List;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Shuyang Zhou
045     */
046    public class DLFileEntryFinderImpl
047            extends BasePersistenceImpl<DLFileEntry> implements DLFileEntryFinder {
048    
049            public static final String COUNT_BY_EXTRA_SETTINGS =
050                    DLFileEntryFinder.class.getName() + ".countByExtraSettings";
051    
052            public static final String COUNT_BY_G_F =
053                    DLFileEntryFinder.class.getName() + ".countByG_F";
054    
055            public static final String COUNT_BY_G_M_R =
056                    DLFileEntryFinder.class.getName() + ".countByG_M_R";
057    
058            public static final String COUNT_BY_G_U_F =
059                    DLFileEntryFinder.class.getName() + ".countByG_U_F";
060    
061            public static final String FIND_BY_ANY_IMAGE_ID =
062                    DLFileEntryFinder.class.getName() + ".findByAnyImageId";
063    
064            public static final String FIND_BY_COMPANY_ID =
065                    DLFileEntryFinder.class.getName() + ".findByCompanyId";
066    
067            public static final String FIND_BY_EXTRA_SETTINGS =
068                    DLFileEntryFinder.class.getName() + ".findByExtraSettings";
069    
070            public static final String FIND_BY_G_DDM_STRUCTURE_IDS =
071                    DLFileEntryFinder.class.getName() + ".findByG_DDMStructureIds";
072    
073            public static final String FIND_BY_DDM_STRUCTURE_IDS =
074                    DLFileEntryFinder.class.getName() + ".findByDDMStructureIds";
075    
076            public static final String FIND_BY_MISVERSIONED =
077                    DLFileEntryFinder.class.getName() + ".findByMisversioned";
078    
079            public static final String FIND_BY_NO_ASSETS =
080                    DLFileEntryFinder.class.getName() + ".findByNoAssets";
081    
082            public static final String FIND_BY_ORPHANED_FILE_ENTRIES =
083                    DLFileEntryFinder.class.getName() + ".findByOrphanedFileEntries";
084    
085            public static final String FIND_BY_G_F =
086                    DLFileEntryFinder.class.getName() + ".findByG_F";
087    
088            public static final String FIND_BY_G_U_F =
089                    DLFileEntryFinder.class.getName() + ".findByG_U_F";
090    
091            @Override
092            public int countByExtraSettings() throws SystemException {
093                    Session session = null;
094    
095                    try {
096                            session = openSession();
097    
098                            String sql = CustomSQLUtil.get(COUNT_BY_EXTRA_SETTINGS);
099    
100                            SQLQuery q = session.createSQLQuery(sql);
101    
102                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
103    
104                            Iterator<Long> itr = q.iterate();
105    
106                            if (itr.hasNext()) {
107                                    Long count = itr.next();
108    
109                                    if (count != null) {
110                                            return count.intValue();
111                                    }
112                            }
113    
114                            return 0;
115                    }
116                    catch (Exception e) {
117                            throw new SystemException(e);
118                    }
119                    finally {
120                            closeSession(session);
121                    }
122            }
123    
124            @Override
125            public int countByG_F(
126                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
127                    throws SystemException {
128    
129                    return doCountByG_U_F_M(
130                            groupId, 0, folderIds, null, queryDefinition, false);
131            }
132    
133            /**
134            * @deprecated As of 7.0.0, with no direct replacement
135            */
136            @Deprecated
137            @Override
138            public int countByG_M_R(
139                            long groupId, DateRange dateRange, long repositoryId,
140                            QueryDefinition queryDefinition)
141                    throws SystemException {
142    
143                    Session session = null;
144    
145                    try {
146                            session = openSession();
147    
148                            String sql = getFileEntriesSQL(
149                                    COUNT_BY_G_M_R, groupId, null, null, queryDefinition, false);
150    
151                            if ((dateRange == null) || (dateRange.getStartDate() == null)) {
152                                    sql = StringUtil.replace(
153                                            sql, "(DLFileEntry.modifiedDate >= ?) AND",
154                                            StringPool.BLANK);
155                            }
156    
157                            if ((dateRange == null) || (dateRange.getEndDate() == null)) {
158                                    sql = StringUtil.replace(
159                                            sql, "(DLFileEntry.modifiedDate <= ?) AND",
160                                            StringPool.BLANK);
161                            }
162    
163                            SQLQuery q = session.createSQLQuery(sql);
164    
165                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
166    
167                            QueryPos qPos = QueryPos.getInstance(q);
168    
169                            qPos.add(groupId);
170    
171                            if ((dateRange != null) && (dateRange.getStartDate() != null)) {
172                                    qPos.add(dateRange.getStartDate());
173                            }
174    
175                            if ((dateRange != null) && (dateRange.getEndDate() != null)) {
176                                    qPos.add(dateRange.getEndDate());
177                            }
178    
179                            qPos.add(repositoryId);
180                            qPos.add(queryDefinition.getStatus());
181    
182                            Iterator<Long> itr = q.iterate();
183    
184                            if (itr.hasNext()) {
185                                    Long count = itr.next();
186    
187                                    if (count != null) {
188                                            return count.intValue();
189                                    }
190                            }
191    
192                            return 0;
193                    }
194                    catch (Exception e) {
195                            throw new SystemException(e);
196                    }
197                    finally {
198                            closeSession(session);
199                    }
200            }
201    
202            @Override
203            public int countByG_U_F_M(
204                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
205                            QueryDefinition queryDefinition)
206                    throws SystemException {
207    
208                    return doCountByG_U_F_M(
209                            groupId, userId, folderIds, mimeTypes, queryDefinition, false);
210            }
211    
212            @Override
213            public int countByG_U_R_F_M(
214                            long groupId, long userId, List<Long> repositoryIds,
215                            List<Long> folderIds, String[] mimeTypes,
216                            QueryDefinition queryDefinition)
217                    throws SystemException {
218    
219                    return doCountByG_U_R_F_M(
220                            groupId, userId, repositoryIds, folderIds, mimeTypes,
221                            queryDefinition, false);
222            }
223    
224            @Override
225            public int filterCountByG_U_F_M(
226                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
227                            QueryDefinition queryDefinition)
228                    throws SystemException {
229    
230                    return doCountByG_U_F_M(
231                            groupId, userId, folderIds, mimeTypes, queryDefinition, true);
232            }
233    
234            @Override
235            public DLFileEntry fetchByAnyImageId(long imageId) throws SystemException {
236                    Session session = null;
237    
238                    try {
239                            session = openSession();
240    
241                            String sql = CustomSQLUtil.get(FIND_BY_ANY_IMAGE_ID);
242    
243                            SQLQuery q = session.createSQLQuery(sql);
244    
245                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
246    
247                            QueryPos qPos = QueryPos.getInstance(q);
248    
249                            qPos.add(imageId);
250                            qPos.add(imageId);
251                            qPos.add(imageId);
252                            qPos.add(imageId);
253    
254                            List<DLFileEntry> dlFileEntries = q.list();
255    
256                            if (!dlFileEntries.isEmpty()) {
257                                    return dlFileEntries.get(0);
258                            }
259    
260                            return null;
261                    }
262                    catch (Exception e) {
263                            throw new SystemException(e);
264                    }
265                    finally {
266                            closeSession(session);
267                    }
268            }
269    
270            @Override
271            public int filterCountByG_F(
272                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
273                    throws SystemException {
274    
275                    return doCountByG_U_F_M(
276                            groupId, 0, folderIds, null, queryDefinition, true);
277            }
278    
279            @Override
280            public int filterCountByG_U_R_F_M(
281                            long groupId, long userId, List<Long> repositoryIds,
282                            List<Long> folderIds, String[] mimeTypes,
283                            QueryDefinition queryDefinition)
284                    throws SystemException {
285    
286                    return doCountByG_U_R_F_M(
287                            groupId, userId, repositoryIds, folderIds, mimeTypes,
288                            queryDefinition, true);
289            }
290    
291            @Override
292            public List<DLFileEntry> filterFindByG_F(
293                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
294                    throws SystemException {
295    
296                    return doFindByG_U_F_M(
297                            groupId, 0, folderIds, null, queryDefinition, true);
298            }
299    
300            @Override
301            public List<DLFileEntry> filterFindByG_U_F_M(
302                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
303                            QueryDefinition queryDefinition)
304                    throws SystemException {
305    
306                    return doFindByG_U_F_M(
307                            groupId, userId, folderIds, mimeTypes, queryDefinition, true);
308            }
309    
310            @Override
311            public List<DLFileEntry> filterFindByG_U_R_F_M(
312                            long groupId, long userId, List<Long> repositoryIds,
313                            List<Long> folderIds, String[] mimeTypes,
314                            QueryDefinition queryDefinition)
315                    throws SystemException {
316    
317                    return doFindByG_U_R_F_M(
318                            groupId, userId, repositoryIds, folderIds, mimeTypes,
319                            queryDefinition, true);
320            }
321    
322            @Override
323            public DLFileEntry findByAnyImageId(long imageId)
324                    throws NoSuchFileEntryException, SystemException {
325    
326                    DLFileEntry dlFileEntry = fetchByAnyImageId(imageId);
327    
328                    if (dlFileEntry != null) {
329                            return dlFileEntry;
330                    }
331    
332                    throw new NoSuchFileEntryException(
333                            "No DLFileEntry exists with the imageId " + imageId);
334            }
335    
336            @Override
337            public List<DLFileEntry> findByCompanyId(
338                            long companyId, QueryDefinition queryDefinition)
339                    throws SystemException {
340    
341                    Session session = null;
342    
343                    try {
344                            session = openSession();
345    
346                            String sql = CustomSQLUtil.get(
347                                    FIND_BY_COMPANY_ID, queryDefinition,
348                                    DLFileVersionImpl.TABLE_NAME);
349    
350                            sql = CustomSQLUtil.replaceOrderBy(
351                                    sql, queryDefinition.getOrderByComparator());
352    
353                            SQLQuery q = session.createSQLQuery(sql);
354    
355                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
356    
357                            QueryPos qPos = QueryPos.getInstance(q);
358    
359                            qPos.add(companyId);
360                            qPos.add(queryDefinition.getStatus());
361    
362                            return (List<DLFileEntry>)QueryUtil.list(
363                                    q, getDialect(), queryDefinition.getStart(),
364                                    queryDefinition.getEnd());
365                    }
366                    catch (Exception e) {
367                            throw new SystemException(e);
368                    }
369                    finally {
370                            closeSession(session);
371                    }
372            }
373    
374            @Override
375            public List<DLFileEntry> findByDDMStructureIds(
376                            long[] ddmStructureIds, int start, int end)
377                    throws SystemException {
378    
379                    return findByDDMStructureIds(0, ddmStructureIds, start, end);
380            }
381    
382            @Override
383            public List<DLFileEntry> findByDDMStructureIds(
384                            long groupId, long[] ddmStructureIds, int start, int end)
385                    throws SystemException {
386    
387                    Session session = null;
388    
389                    try {
390                            session = openSession();
391    
392                            String sql = CustomSQLUtil.get(FIND_BY_DDM_STRUCTURE_IDS);
393    
394                            if ((ddmStructureIds == null) || (ddmStructureIds.length <= 0)) {
395                                    return Collections.emptyList();
396                            }
397    
398                            if (groupId <= 0) {
399                                    sql = StringUtil.replace(
400                                            sql, "(DLFileEntry.groupId = ?) AND", StringPool.BLANK);
401                            }
402    
403                            sql = StringUtil.replace(
404                                    sql, "[$DDM_STRUCTURE_ID$]",
405                                    getDDMStructureIds(ddmStructureIds));
406    
407                            SQLQuery q = session.createSQLQuery(sql);
408    
409                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
410    
411                            QueryPos qPos = QueryPos.getInstance(q);
412    
413                            if (groupId > 0) {
414                                    qPos.add(groupId);
415                            }
416    
417                            qPos.add(ddmStructureIds);
418    
419                            return (List<DLFileEntry>)QueryUtil.list(
420                                    q, getDialect(), start, end);
421                    }
422                    catch (Exception e) {
423                            throw new SystemException(e);
424                    }
425                    finally {
426                            closeSession(session);
427                    }
428            }
429    
430            @Override
431            public List<DLFileEntry> findByExtraSettings(int start, int end)
432                    throws SystemException {
433    
434                    Session session = null;
435    
436                    try {
437                            session = openSession();
438    
439                            String sql = CustomSQLUtil.get(FIND_BY_EXTRA_SETTINGS);
440    
441                            SQLQuery q = session.createSQLQuery(sql);
442    
443                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
444    
445                            return (List<DLFileEntry>)QueryUtil.list(
446                                    q, getDialect(), start, end);
447                    }
448                    catch (Exception e) {
449                            throw new SystemException(e);
450                    }
451                    finally {
452                            closeSession(session);
453                    }
454            }
455    
456            @Override
457            public List<DLFileEntry> findByMisversioned() throws SystemException {
458                    Session session = null;
459    
460                    try {
461                            session = openSession();
462    
463                            String sql = CustomSQLUtil.get(FIND_BY_MISVERSIONED);
464    
465                            SQLQuery q = session.createSQLQuery(sql);
466    
467                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
468    
469                            return q.list(true);
470                    }
471                    catch (Exception e) {
472                            throw new SystemException(e);
473                    }
474                    finally {
475                            closeSession(session);
476                    }
477            }
478    
479            @Override
480            public List<DLFileEntry> findByNoAssets() throws SystemException {
481                    Session session = null;
482    
483                    try {
484                            session = openSession();
485    
486                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
487    
488                            SQLQuery q = session.createSQLQuery(sql);
489    
490                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
491    
492                            return q.list(true);
493                    }
494                    catch (Exception e) {
495                            throw new SystemException(e);
496                    }
497                    finally {
498                            closeSession(session);
499                    }
500            }
501    
502            @Override
503            public List<DLFileEntry> findByOrphanedFileEntries()
504                    throws SystemException {
505    
506                    Session session = null;
507    
508                    try {
509                            session = openSession();
510    
511                            String sql = CustomSQLUtil.get(FIND_BY_ORPHANED_FILE_ENTRIES);
512    
513                            SQLQuery q = session.createSQLQuery(sql);
514    
515                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
516    
517                            return q.list(true);
518                    }
519                    catch (Exception e) {
520                            throw new SystemException(e);
521                    }
522                    finally {
523                            closeSession(session);
524                    }
525            }
526    
527            @Override
528            public List<DLFileEntry> findByG_F(
529                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
530                    throws SystemException {
531    
532                    return doFindByG_U_F_M(
533                            groupId, 0, folderIds, null, queryDefinition, false);
534            }
535    
536            @Override
537            public List<DLFileEntry> findByG_R_F(
538                            long groupId, List<Long> repositoryIds, List<Long> folderIds,
539                            QueryDefinition queryDefinition)
540                    throws SystemException {
541    
542                    return doFindByG_U_R_F_M(
543                            groupId, 0, repositoryIds, folderIds, null, queryDefinition, false);
544            }
545    
546            @Override
547            public List<DLFileEntry> findByG_U_F(
548                            long groupId, long userId, List<Long> folderIds,
549                            QueryDefinition queryDefinition)
550                    throws SystemException {
551    
552                    List<Long> repositoryIds = Collections.emptyList();
553    
554                    return doFindByG_U_R_F_M(
555                            groupId, userId, repositoryIds, folderIds, null, queryDefinition,
556                            false);
557            }
558    
559            @Override
560            public List<DLFileEntry> findByG_U_R_F(
561                            long groupId, long userId, List<Long> repositoryIds,
562                            List<Long> folderIds, QueryDefinition queryDefinition)
563                    throws SystemException {
564    
565                    return doFindByG_U_R_F_M(
566                            groupId, userId, repositoryIds, folderIds, null, queryDefinition,
567                            false);
568            }
569    
570            @Override
571            public List<DLFileEntry> findByG_U_R_F_M(
572                            long groupId, long userId, List<Long> repositoryIds,
573                            List<Long> folderIds, String[] mimeTypes,
574                            QueryDefinition queryDefinition)
575                    throws SystemException {
576    
577                    return doFindByG_U_R_F_M(
578                            groupId, userId, repositoryIds, folderIds, mimeTypes,
579                            queryDefinition, false);
580            }
581    
582            @Override
583            public List<DLFileEntry> findByG_U_F_M(
584                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
585                            QueryDefinition queryDefinition)
586                    throws SystemException {
587    
588                    return doFindByG_U_F_M(
589                            groupId, userId, folderIds, mimeTypes, queryDefinition, false);
590            }
591    
592            protected int doCountByG_U_F_M(
593                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
594                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
595                    throws SystemException {
596    
597                    List<Long> repositoryIds = Collections.emptyList();
598    
599                    return doCountByG_U_R_F_M(
600                            groupId, userId, repositoryIds, folderIds, mimeTypes,
601                            queryDefinition, inlineSQLHelper);
602            }
603    
604            protected int doCountByG_U_R_F_M(
605                            long groupId, long userId, List<Long> repositoryIds,
606                            List<Long> folderIds, String[] mimeTypes,
607                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
608                    throws SystemException {
609    
610                    Session session = null;
611    
612                    try {
613                            session = openSession();
614    
615                            String id = null;
616    
617                            if (userId <= 0) {
618                                    id = COUNT_BY_G_F;
619                            }
620                            else {
621                                    id = COUNT_BY_G_U_F;
622                            }
623    
624                            String sql = getFileEntriesSQL(
625                                    id, groupId, repositoryIds, folderIds, mimeTypes,
626                                    queryDefinition, inlineSQLHelper);
627    
628                            SQLQuery q = session.createSQLQuery(sql);
629    
630                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
631    
632                            QueryPos qPos = QueryPos.getInstance(q);
633    
634                            qPos.add(groupId);
635    
636                            if (userId > 0) {
637                                    qPos.add(userId);
638                            }
639    
640                            qPos.add(queryDefinition.getStatus());
641    
642                            for (Long repositoryId : repositoryIds) {
643                                    qPos.add(repositoryId);
644                            }
645    
646                            for (Long folderId : folderIds) {
647                                    qPos.add(folderId);
648                            }
649    
650                            if (mimeTypes != null) {
651                                    qPos.add(mimeTypes);
652                            }
653    
654                            Iterator<Long> itr = q.iterate();
655    
656                            if (itr.hasNext()) {
657                                    Long count = itr.next();
658    
659                                    if (count != null) {
660                                            return count.intValue();
661                                    }
662                            }
663    
664                            return 0;
665                    }
666                    catch (Exception e) {
667                            throw new SystemException(e);
668                    }
669                    finally {
670                            closeSession(session);
671                    }
672            }
673    
674            protected List<DLFileEntry> doFindByG_U_F_M(
675                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
676                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
677                    throws SystemException {
678    
679                    Session session = null;
680    
681                    try {
682                            session = openSession();
683    
684                            String id = null;
685    
686                            if (userId <= 0) {
687                                    id = FIND_BY_G_F;
688                            }
689                            else {
690                                    id = FIND_BY_G_U_F;
691                            }
692    
693                            String sql = getFileEntriesSQL(
694                                    id, groupId, folderIds, mimeTypes, queryDefinition,
695                                    inlineSQLHelper);
696    
697                            sql = CustomSQLUtil.replaceOrderBy(
698                                    sql, queryDefinition.getOrderByComparator());
699    
700                            SQLQuery q = session.createSQLQuery(sql);
701    
702                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
703    
704                            QueryPos qPos = QueryPos.getInstance(q);
705    
706                            qPos.add(groupId);
707    
708                            if (userId > 0) {
709                                    qPos.add(userId);
710                            }
711    
712                            qPos.add(queryDefinition.getStatus());
713    
714                            for (Long folderId : folderIds) {
715                                    qPos.add(folderId);
716                            }
717    
718                            if (mimeTypes != null) {
719                                    qPos.add(mimeTypes);
720                            }
721    
722                            return (List<DLFileEntry>)QueryUtil.list(
723                                    q, getDialect(), queryDefinition.getStart(),
724                                    queryDefinition.getEnd());
725                    }
726                    catch (Exception e) {
727                            throw new SystemException(e);
728                    }
729                    finally {
730                            closeSession(session);
731                    }
732            }
733    
734            protected List<DLFileEntry> doFindByG_U_R_F_M(
735                            long groupId, long userId, List<Long> repositoryIds,
736                            List<Long> folderIds, String[] mimeTypes,
737                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
738                    throws SystemException {
739    
740                    Session session = null;
741    
742                    try {
743                            session = openSession();
744    
745                            String id = null;
746    
747                            if (userId <= 0) {
748                                    id = FIND_BY_G_F;
749                            }
750                            else {
751                                    id = FIND_BY_G_U_F;
752                            }
753    
754                            String sql = getFileEntriesSQL(
755                                    id, groupId, repositoryIds, folderIds, mimeTypes,
756                                    queryDefinition, inlineSQLHelper);
757    
758                            sql = CustomSQLUtil.replaceOrderBy(
759                                    sql, queryDefinition.getOrderByComparator());
760    
761                            SQLQuery q = session.createSQLQuery(sql);
762    
763                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
764    
765                            QueryPos qPos = QueryPos.getInstance(q);
766    
767                            qPos.add(groupId);
768    
769                            if (userId > 0) {
770                                    qPos.add(userId);
771                            }
772    
773                            qPos.add(queryDefinition.getStatus());
774    
775                            for (Long repositoryId : repositoryIds) {
776                                    qPos.add(repositoryId);
777                            }
778    
779                            for (Long folderId : folderIds) {
780                                    qPos.add(folderId);
781                            }
782    
783                            if (mimeTypes != null) {
784                                    qPos.add(mimeTypes);
785                            }
786    
787                            return (List<DLFileEntry>)QueryUtil.list(
788                                    q, getDialect(), queryDefinition.getStart(),
789                                    queryDefinition.getEnd());
790                    }
791                    catch (Exception e) {
792                            throw new SystemException(e);
793                    }
794                    finally {
795                            closeSession(session);
796                    }
797            }
798    
799            protected String getDDMStructureIds(long[] ddmStructureIds) {
800                    StringBundler sb = new StringBundler(
801                            (ddmStructureIds.length * 2 - 1) + 2);
802    
803                    sb.append(StringPool.OPEN_PARENTHESIS);
804    
805                    for (int i = 0; i < ddmStructureIds.length; i++) {
806                            sb.append("DLFileEntryTypes_DDMStructures.structureId = ?");
807    
808                            if ((i + 1) != ddmStructureIds.length) {
809                                    sb.append(WHERE_OR);
810                            }
811                    }
812    
813                    sb.append(StringPool.CLOSE_PARENTHESIS);
814    
815                    return sb.toString();
816            }
817    
818            protected String getFileEntriesSQL(
819                    String id, long groupId, List<Long> folderIds, String[] mimeTypes,
820                    QueryDefinition queryDefinition, boolean inlineSQLHelper) {
821    
822                    return getFileEntriesSQL(
823                            id, groupId, null, folderIds, mimeTypes, queryDefinition,
824                            inlineSQLHelper);
825            }
826    
827            protected String getFileEntriesSQL(
828                    String id, long groupId, List<Long> repositoryIds, List<Long> folderIds,
829                    String[] mimeTypes, QueryDefinition queryDefinition,
830                    boolean inlineSQLHelper) {
831    
832                    String tableName = DLFileVersionImpl.TABLE_NAME;
833    
834                    String sql = CustomSQLUtil.get(id, queryDefinition, tableName);
835    
836                    if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
837                            sql = StringUtil.replace(sql, "[$JOIN$]", StringPool.BLANK);
838    
839                            tableName = DLFileEntryImpl.TABLE_NAME;
840                    }
841                    else {
842                            sql = StringUtil.replace(
843                                    sql, "[$JOIN$]",
844                                    CustomSQLUtil.get(
845                                            DLFolderFinderImpl.JOIN_FE_BY_DL_FILE_VERSION));
846                    }
847    
848                    if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled()) {
849                            sql = InlineSQLHelperUtil.replacePermissionCheck(
850                                    sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
851                                    groupId);
852                    }
853    
854                    StringBundler sb = new StringBundler(7);
855    
856                    if (((repositoryIds != null) && !repositoryIds.isEmpty()) ||
857                            ((folderIds != null) && !folderIds.isEmpty()) ||
858                            ArrayUtil.isNotEmpty(mimeTypes)) {
859    
860                            if ((repositoryIds != null) && !repositoryIds.isEmpty()) {
861                                    sb.append(WHERE_AND);
862                                    sb.append(StringPool.OPEN_PARENTHESIS);
863                                    sb.append(getRepositoryIds(repositoryIds, tableName));
864                                    sb.append(StringPool.CLOSE_PARENTHESIS);
865                            }
866    
867                            if ((folderIds != null) && !folderIds.isEmpty()) {
868                                    sb.append(WHERE_AND);
869                                    sb.append(StringPool.OPEN_PARENTHESIS);
870                                    sb.append(getFolderIds(folderIds, tableName));
871                                    sb.append(StringPool.CLOSE_PARENTHESIS);
872                            }
873    
874                            if (ArrayUtil.isNotEmpty(mimeTypes)) {
875                                    sb.append(WHERE_AND);
876                                    sb.append(StringPool.OPEN_PARENTHESIS);
877                                    sb.append(getMimeTypes(mimeTypes, tableName));
878                                    sb.append(StringPool.CLOSE_PARENTHESIS);
879                            }
880    
881                            return StringUtil.replace(sql, "[$FOLDER_ID$]", sb.toString());
882                    }
883                    else {
884                            return StringUtil.replace(sql, "[$FOLDER_ID$]", StringPool.BLANK);
885                    }
886            }
887    
888            protected String getFolderIds(List<Long> folderIds, String tableName) {
889                    if (folderIds.isEmpty()) {
890                            return StringPool.BLANK;
891                    }
892    
893                    StringBundler sb = new StringBundler(folderIds.size() * 3 + 1);
894    
895                    sb.append(StringPool.OPEN_PARENTHESIS);
896    
897                    for (int i = 0; i < folderIds.size(); i++) {
898                            sb.append(tableName);
899                            sb.append(".folderId = ? ");
900    
901                            if ((i + 1) != folderIds.size()) {
902                                    sb.append(WHERE_OR);
903                            }
904                    }
905    
906                    sb.append(StringPool.CLOSE_PARENTHESIS);
907    
908                    return sb.toString();
909            }
910    
911            protected String getMimeTypes(String[] mimeTypes, String tableName) {
912                    if (mimeTypes.length == 0) {
913                            return StringPool.BLANK;
914                    }
915    
916                    StringBundler sb = new StringBundler(mimeTypes.length * 2 - 1);
917    
918                    for (int i = 0; i < mimeTypes.length; i++) {
919                            sb.append(tableName);
920                            sb.append(".mimeType = ?");
921    
922                            if ((i + 1) != mimeTypes.length) {
923                                    sb.append(WHERE_OR);
924                            }
925                    }
926    
927                    return sb.toString();
928            }
929    
930            protected String getRepositoryIds(
931                            List<Long> repositoryIds, String tableName) {
932    
933                            if (repositoryIds.isEmpty()) {
934                                    return StringPool.BLANK;
935                            }
936    
937                            StringBundler sb = new StringBundler(repositoryIds.size() * 3 + 1);
938    
939                            sb.append(StringPool.OPEN_PARENTHESIS);
940    
941                            for (int i = 0; i < repositoryIds.size(); i++) {
942                                    sb.append(tableName);
943                                    sb.append(".repositoryId = ? ");
944    
945                                    if ((i + 1) != repositoryIds.size()) {
946                                            sb.append(WHERE_OR);
947                                    }
948                            }
949    
950                            sb.append(StringPool.CLOSE_PARENTHESIS);
951    
952                            return sb.toString();
953                    }
954    
955    }