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