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