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_DDM_STRUCTURE_IDS =
071                    DLFileEntryFinder.class.getName() + ".findByDDMStructureIds";
072    
073            public static final String FIND_BY_MISVERSIONED =
074                    DLFileEntryFinder.class.getName() + ".findByMisversioned";
075    
076            public static final String FIND_BY_NO_ASSETS =
077                    DLFileEntryFinder.class.getName() + ".findByNoAssets";
078    
079            public static final String FIND_BY_ORPHANED_FILE_ENTRIES =
080                    DLFileEntryFinder.class.getName() + ".findByOrphanedFileEntries";
081    
082            public static final String FIND_BY_G_F =
083                    DLFileEntryFinder.class.getName() + ".findByG_F";
084    
085            public static final String FIND_BY_G_U_F =
086                    DLFileEntryFinder.class.getName() + ".findByG_U_F";
087    
088            @Override
089            public int countByExtraSettings() throws SystemException {
090                    Session session = null;
091    
092                    try {
093                            session = openSession();
094    
095                            String sql = CustomSQLUtil.get(COUNT_BY_EXTRA_SETTINGS);
096    
097                            SQLQuery q = session.createSQLQuery(sql);
098    
099                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
100    
101                            Iterator<Long> itr = q.iterate();
102    
103                            if (itr.hasNext()) {
104                                    Long count = itr.next();
105    
106                                    if (count != null) {
107                                            return count.intValue();
108                                    }
109                            }
110    
111                            return 0;
112                    }
113                    catch (Exception e) {
114                            throw new SystemException(e);
115                    }
116                    finally {
117                            closeSession(session);
118                    }
119            }
120    
121            @Override
122            public int countByG_F(
123                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
124                    throws SystemException {
125    
126                    return doCountByG_U_F_M(
127                            groupId, 0, folderIds, null, queryDefinition, false);
128            }
129    
130            @Override
131            public int countByG_M_R(
132                            long groupId, DateRange dateRange, long repositoryId,
133                            QueryDefinition queryDefinition)
134                    throws SystemException {
135    
136                    Session session = null;
137    
138                    try {
139                            session = openSession();
140    
141                            String sql = getFileEntriesSQL(
142                                    COUNT_BY_G_M_R, groupId, null, null, queryDefinition, false);
143    
144                            if ((dateRange == null) || (dateRange.getStartDate() == null)) {
145                                    sql = StringUtil.replace(
146                                            sql, "(DLFileEntry.modifiedDate >= ?) AND",
147                                            StringPool.BLANK);
148                            }
149    
150                            if ((dateRange == null) || (dateRange.getEndDate() == null)) {
151                                    sql = StringUtil.replace(
152                                            sql, "(DLFileEntry.modifiedDate <= ?) AND",
153                                            StringPool.BLANK);
154                            }
155    
156                            SQLQuery q = session.createSQLQuery(sql);
157    
158                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
159    
160                            QueryPos qPos = QueryPos.getInstance(q);
161    
162                            qPos.add(groupId);
163    
164                            if ((dateRange != null) && (dateRange.getStartDate() != null)) {
165                                    qPos.add(dateRange.getStartDate());
166                            }
167    
168                            if ((dateRange != null) && (dateRange.getEndDate() != null)) {
169                                    qPos.add(dateRange.getEndDate());
170                            }
171    
172                            qPos.add(repositoryId);
173                            qPos.add(queryDefinition.getStatus());
174    
175                            Iterator<Long> itr = q.iterate();
176    
177                            if (itr.hasNext()) {
178                                    Long count = itr.next();
179    
180                                    if (count != null) {
181                                            return count.intValue();
182                                    }
183                            }
184    
185                            return 0;
186                    }
187                    catch (Exception e) {
188                            throw new SystemException(e);
189                    }
190                    finally {
191                            closeSession(session);
192                    }
193            }
194    
195            @Override
196            public int countByG_U_F_M(
197                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
198                            QueryDefinition queryDefinition)
199                    throws SystemException {
200    
201                    return doCountByG_U_F_M(
202                            groupId, userId, folderIds, mimeTypes, queryDefinition, false);
203            }
204    
205            @Override
206            public int filterCountByG_U_F_M(
207                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
208                            QueryDefinition queryDefinition)
209                    throws SystemException {
210    
211                    return doCountByG_U_F_M(
212                            groupId, userId, folderIds, mimeTypes, queryDefinition, true);
213            }
214    
215            @Override
216            public DLFileEntry fetchByAnyImageId(long imageId) throws SystemException {
217                    Session session = null;
218    
219                    try {
220                            session = openSession();
221    
222                            String sql = CustomSQLUtil.get(FIND_BY_ANY_IMAGE_ID);
223    
224                            SQLQuery q = session.createSQLQuery(sql);
225    
226                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
227    
228                            QueryPos qPos = QueryPos.getInstance(q);
229    
230                            qPos.add(imageId);
231                            qPos.add(imageId);
232                            qPos.add(imageId);
233                            qPos.add(imageId);
234    
235                            List<DLFileEntry> dlFileEntries = q.list();
236    
237                            if (!dlFileEntries.isEmpty()) {
238                                    return dlFileEntries.get(0);
239                            }
240    
241                            return null;
242                    }
243                    catch (Exception e) {
244                            throw new SystemException(e);
245                    }
246                    finally {
247                            closeSession(session);
248                    }
249            }
250    
251            @Override
252            public int filterCountByG_F(
253                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
254                    throws SystemException {
255    
256                    return doCountByG_U_F_M(
257                            groupId, 0, folderIds, null, queryDefinition, true);
258            }
259    
260            @Override
261            public List<DLFileEntry> filterFindByG_F(
262                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
263                    throws SystemException {
264    
265                    return doFindByG_U_F_M(
266                            groupId, 0, folderIds, null, queryDefinition, true);
267            }
268    
269            @Override
270            public List<DLFileEntry> filterFindByG_U_F_M(
271                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
272                            QueryDefinition queryDefinition)
273                    throws SystemException {
274    
275                    return doFindByG_U_F_M(
276                            groupId, userId, folderIds, mimeTypes, queryDefinition, true);
277            }
278    
279            @Override
280            public DLFileEntry findByAnyImageId(long imageId)
281                    throws NoSuchFileEntryException, SystemException {
282    
283                    DLFileEntry dlFileEntry = fetchByAnyImageId(imageId);
284    
285                    if (dlFileEntry != null) {
286                            return dlFileEntry;
287                    }
288    
289                    throw new NoSuchFileEntryException(
290                            "No DLFileEntry exists with the imageId " + imageId);
291            }
292    
293            @Override
294            public List<DLFileEntry> findByCompanyId(
295                            long companyId, QueryDefinition queryDefinition)
296                    throws SystemException {
297    
298                    Session session = null;
299    
300                    try {
301                            session = openSession();
302    
303                            String sql = CustomSQLUtil.get(
304                                    FIND_BY_COMPANY_ID, queryDefinition,
305                                    DLFileVersionImpl.TABLE_NAME);
306    
307                            sql = CustomSQLUtil.replaceOrderBy(
308                                    sql, queryDefinition.getOrderByComparator());
309    
310                            SQLQuery q = session.createSQLQuery(sql);
311    
312                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
313    
314                            QueryPos qPos = QueryPos.getInstance(q);
315    
316                            qPos.add(companyId);
317                            qPos.add(queryDefinition.getStatus());
318    
319                            return (List<DLFileEntry>)QueryUtil.list(
320                                    q, getDialect(), queryDefinition.getStart(),
321                                    queryDefinition.getEnd());
322                    }
323                    catch (Exception e) {
324                            throw new SystemException(e);
325                    }
326                    finally {
327                            closeSession(session);
328                    }
329            }
330    
331            @Override
332            public List<DLFileEntry> findByDDMStructureIds(
333                            long[] ddmStructureIds, int start, int end)
334                    throws SystemException {
335    
336                    Session session = null;
337    
338                    try {
339                            session = openSession();
340    
341                            String sql = CustomSQLUtil.get(FIND_BY_DDM_STRUCTURE_IDS);
342    
343                            if ((ddmStructureIds == null) || (ddmStructureIds.length <= 0)) {
344                                    return Collections.emptyList();
345                            }
346    
347                            sql = StringUtil.replace(
348                                    sql, "[$DDM_STRUCTURE_ID$]",
349                                    getDDMStructureIds(ddmStructureIds));
350    
351                            SQLQuery q = session.createSQLQuery(sql);
352    
353                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
354    
355                            QueryPos qPos = QueryPos.getInstance(q);
356    
357                            qPos.add(ddmStructureIds);
358    
359                            return (List<DLFileEntry>)QueryUtil.list(
360                                    q, getDialect(), start, end);
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> findByExtraSettings(int start, int end)
372                    throws SystemException {
373    
374                    Session session = null;
375    
376                    try {
377                            session = openSession();
378    
379                            String sql = CustomSQLUtil.get(FIND_BY_EXTRA_SETTINGS);
380    
381                            SQLQuery q = session.createSQLQuery(sql);
382    
383                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
384    
385                            return (List<DLFileEntry>)QueryUtil.list(
386                                    q, getDialect(), start, end);
387                    }
388                    catch (Exception e) {
389                            throw new SystemException(e);
390                    }
391                    finally {
392                            closeSession(session);
393                    }
394            }
395    
396            @Override
397            public List<DLFileEntry> findByMisversioned() throws SystemException {
398                    Session session = null;
399    
400                    try {
401                            session = openSession();
402    
403                            String sql = CustomSQLUtil.get(FIND_BY_MISVERSIONED);
404    
405                            SQLQuery q = session.createSQLQuery(sql);
406    
407                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
408    
409                            return q.list(true);
410                    }
411                    catch (Exception e) {
412                            throw new SystemException(e);
413                    }
414                    finally {
415                            closeSession(session);
416                    }
417            }
418    
419            @Override
420            public List<DLFileEntry> findByNoAssets() throws SystemException {
421                    Session session = null;
422    
423                    try {
424                            session = openSession();
425    
426                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
427    
428                            SQLQuery q = session.createSQLQuery(sql);
429    
430                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
431    
432                            return q.list(true);
433                    }
434                    catch (Exception e) {
435                            throw new SystemException(e);
436                    }
437                    finally {
438                            closeSession(session);
439                    }
440            }
441    
442            @Override
443            public List<DLFileEntry> findByOrphanedFileEntries()
444                    throws SystemException {
445    
446                    Session session = null;
447    
448                    try {
449                            session = openSession();
450    
451                            String sql = CustomSQLUtil.get(FIND_BY_ORPHANED_FILE_ENTRIES);
452    
453                            SQLQuery q = session.createSQLQuery(sql);
454    
455                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
456    
457                            return q.list(true);
458                    }
459                    catch (Exception e) {
460                            throw new SystemException(e);
461                    }
462                    finally {
463                            closeSession(session);
464                    }
465            }
466    
467            @Override
468            public List<DLFileEntry> findByG_F(
469                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
470                    throws SystemException {
471    
472                    return doFindByG_U_F_M(
473                            groupId, 0, folderIds, null, queryDefinition, false);
474            }
475    
476            @Override
477            public List<DLFileEntry> findByG_U_F_M(
478                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
479                            QueryDefinition queryDefinition)
480                    throws SystemException {
481    
482                    return doFindByG_U_F_M(
483                            groupId, userId, folderIds, mimeTypes, queryDefinition, false);
484            }
485    
486            protected int doCountByG_U_F_M(
487                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
488                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
489                    throws SystemException {
490    
491                    Session session = null;
492    
493                    try {
494                            session = openSession();
495    
496                            String id = null;
497    
498                            if (userId <= 0) {
499                                    id = COUNT_BY_G_F;
500                            }
501                            else {
502                                    id = COUNT_BY_G_U_F;
503                            }
504    
505                            String sql = getFileEntriesSQL(
506                                    id, groupId, folderIds, mimeTypes, queryDefinition,
507                                    inlineSQLHelper);
508    
509                            SQLQuery q = session.createSQLQuery(sql);
510    
511                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
512    
513                            QueryPos qPos = QueryPos.getInstance(q);
514    
515                            qPos.add(groupId);
516    
517                            if (userId > 0) {
518                                    qPos.add(userId);
519                            }
520    
521                            qPos.add(queryDefinition.getStatus());
522    
523                            for (Long folderId : folderIds) {
524                                    qPos.add(folderId);
525                            }
526    
527                            if (mimeTypes != null) {
528                                    qPos.add(mimeTypes);
529                            }
530    
531                            Iterator<Long> itr = q.iterate();
532    
533                            if (itr.hasNext()) {
534                                    Long count = itr.next();
535    
536                                    if (count != null) {
537                                            return count.intValue();
538                                    }
539                            }
540    
541                            return 0;
542                    }
543                    catch (Exception e) {
544                            throw new SystemException(e);
545                    }
546                    finally {
547                            closeSession(session);
548                    }
549            }
550    
551            protected List<DLFileEntry> doFindByG_U_F_M(
552                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
553                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
554                    throws SystemException {
555    
556                    Session session = null;
557    
558                    try {
559                            session = openSession();
560    
561                            String id = null;
562    
563                            if (userId <= 0) {
564                                    id = FIND_BY_G_F;
565                            }
566                            else {
567                                    id = FIND_BY_G_U_F;
568                            }
569    
570                            String sql = getFileEntriesSQL(
571                                    id, groupId, folderIds, mimeTypes, queryDefinition,
572                                    inlineSQLHelper);
573    
574                            sql = CustomSQLUtil.replaceOrderBy(
575                                    sql, queryDefinition.getOrderByComparator());
576    
577                            SQLQuery q = session.createSQLQuery(sql);
578    
579                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
580    
581                            QueryPos qPos = QueryPos.getInstance(q);
582    
583                            qPos.add(groupId);
584    
585                            if (userId > 0) {
586                                    qPos.add(userId);
587                            }
588    
589                            qPos.add(queryDefinition.getStatus());
590    
591                            for (Long folderId : folderIds) {
592                                    qPos.add(folderId);
593                            }
594    
595                            if (mimeTypes != null) {
596                                    qPos.add(mimeTypes);
597                            }
598    
599                            return (List<DLFileEntry>)QueryUtil.list(
600                                    q, getDialect(), queryDefinition.getStart(),
601                                    queryDefinition.getEnd());
602                    }
603                    catch (Exception e) {
604                            throw new SystemException(e);
605                    }
606                    finally {
607                            closeSession(session);
608                    }
609            }
610    
611            protected String getDDMStructureIds(long[] ddmStructureIds) {
612                    StringBundler sb = new StringBundler(
613                            (ddmStructureIds.length * 2 - 1) + 2);
614    
615                    sb.append(StringPool.OPEN_PARENTHESIS);
616    
617                    for (int i = 0; i < ddmStructureIds.length; i++) {
618                            sb.append("DLFileEntryTypes_DDMStructures.structureId = ?");
619    
620                            if ((i + 1) != ddmStructureIds.length) {
621                                    sb.append(WHERE_OR);
622                            }
623                    }
624    
625                    sb.append(StringPool.CLOSE_PARENTHESIS);
626    
627                    return sb.toString();
628            }
629    
630            protected String getFileEntriesSQL(
631                    String id, long groupId, List<Long> folderIds, String[] mimeTypes,
632                    QueryDefinition queryDefinition, boolean inlineSQLHelper) {
633    
634                    String tableName = DLFileVersionImpl.TABLE_NAME;
635    
636                    String sql = CustomSQLUtil.get(id, queryDefinition, tableName);
637    
638                    if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
639                            sql = StringUtil.replace(sql, "[$JOIN$]", StringPool.BLANK);
640    
641                            tableName = DLFileEntryImpl.TABLE_NAME;
642                    }
643                    else {
644                            sql = StringUtil.replace(
645                                    sql, "[$JOIN$]",
646                                    CustomSQLUtil.get(
647                                            DLFolderFinderImpl.JOIN_FE_BY_DL_FILE_VERSION));
648                    }
649    
650                    if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled()) {
651                            sql = InlineSQLHelperUtil.replacePermissionCheck(
652                                    sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
653                                    groupId);
654                    }
655    
656                    StringBundler sb = new StringBundler(7);
657    
658                    if (((folderIds != null) && !folderIds.isEmpty()) ||
659                            ArrayUtil.isNotEmpty(mimeTypes)) {
660    
661                            if ((folderIds != null) && !folderIds.isEmpty()) {
662                                    sb.append(WHERE_AND);
663                                    sb.append(StringPool.OPEN_PARENTHESIS);
664                                    sb.append(getFolderIds(folderIds, tableName));
665                                    sb.append(StringPool.CLOSE_PARENTHESIS);
666                            }
667    
668                            if (ArrayUtil.isNotEmpty(mimeTypes)) {
669                                    sb.append(WHERE_AND);
670                                    sb.append(StringPool.OPEN_PARENTHESIS);
671                                    sb.append(getMimeTypes(mimeTypes, tableName));
672                                    sb.append(StringPool.CLOSE_PARENTHESIS);
673                            }
674    
675                            return StringUtil.replace(sql, "[$FOLDER_ID$]", sb.toString());
676                    }
677                    else {
678                            return StringUtil.replace(sql, "[$FOLDER_ID$]", StringPool.BLANK);
679                    }
680            }
681    
682            protected String getFolderIds(List<Long> folderIds, String tableName) {
683                    if (folderIds.isEmpty()) {
684                            return StringPool.BLANK;
685                    }
686    
687                    StringBundler sb = new StringBundler(folderIds.size() * 3 + 1);
688    
689                    sb.append(StringPool.OPEN_PARENTHESIS);
690    
691                    for (int i = 0; i < folderIds.size(); i++) {
692                            sb.append(tableName);
693                            sb.append(".folderId = ? ");
694    
695                            if ((i + 1) != folderIds.size()) {
696                                    sb.append(WHERE_OR);
697                            }
698                    }
699    
700                    sb.append(StringPool.CLOSE_PARENTHESIS);
701    
702                    return sb.toString();
703            }
704    
705            protected String getMimeTypes(String[] mimeTypes, String tableName) {
706                    if (mimeTypes.length == 0) {
707                            return StringPool.BLANK;
708                    }
709    
710                    StringBundler sb = new StringBundler(mimeTypes.length * 2 - 1);
711    
712                    for (int i = 0; i < mimeTypes.length; i++) {
713                            sb.append(tableName);
714                            sb.append(".mimeType = ?");
715    
716                            if ((i + 1) != mimeTypes.length) {
717                                    sb.append(WHERE_OR);
718                            }
719                    }
720    
721                    return sb.toString();
722            }
723    
724    }