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