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.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.OrderByComparator;
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_M =
083                    DLFileEntryFinder.class.getName() + ".findByG_F_M";
084    
085            public static final String FIND_BY_G_F_S =
086                    DLFileEntryFinder.class.getName() + ".findByG_F_S";
087    
088            public static final String FIND_BY_G_U_F_M =
089                    DLFileEntryFinder.class.getName() + ".findByG_U_F_M";
090    
091            public static final String FIND_BY_G_U_F_S =
092                    DLFileEntryFinder.class.getName() + ".findByG_U_F_S";
093    
094            public static final String FIND_BY_G_F_S_M =
095                    DLFileEntryFinder.class.getName() + ".findByG_F_S_M";
096    
097            public static final String FIND_BY_G_U_F_S_M =
098                    DLFileEntryFinder.class.getName() + ".findByG_U_F_S_M";
099    
100            @Override
101            public int countByExtraSettings() throws SystemException {
102                    Session session = null;
103    
104                    try {
105                            session = openSession();
106    
107                            String sql = CustomSQLUtil.get(COUNT_BY_EXTRA_SETTINGS);
108    
109                            SQLQuery q = session.createSQLQuery(sql);
110    
111                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
112    
113                            Iterator<Long> itr = q.iterate();
114    
115                            if (itr.hasNext()) {
116                                    Long count = itr.next();
117    
118                                    if (count != null) {
119                                            return count.intValue();
120                                    }
121                            }
122    
123                            return 0;
124                    }
125                    catch (Exception e) {
126                            throw new SystemException(e);
127                    }
128                    finally {
129                            closeSession(session);
130                    }
131            }
132    
133            @Override
134            public int countByG_F_S(long groupId, List<Long> folderIds, int status)
135                    throws SystemException {
136    
137                    return doCountByG_F_S(groupId, folderIds, status, false);
138            }
139    
140            @Override
141            public int countByG_U_F_M_S(
142                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
143                            int status)
144                    throws SystemException {
145    
146                    Session session = null;
147    
148                    String table = DLFileVersionImpl.TABLE_NAME;
149    
150                    try {
151                            session = openSession();
152    
153                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_F_S);
154    
155                            if (userId <= 0) {
156                                    if (status == WorkflowConstants.STATUS_ANY) {
157                                            table = DLFileEntryImpl.TABLE_NAME;
158    
159                                            sql = CustomSQLUtil.get(COUNT_BY_G_F);
160                                    }
161                                    else {
162                                            sql = CustomSQLUtil.get(COUNT_BY_G_F_S);
163    
164                                            sql = StringUtil.replace(sql, "[$JOIN$]", "");
165                                    }
166                            }
167                            else {
168                                    if (status == WorkflowConstants.STATUS_ANY) {
169                                            table = DLFileEntryImpl.TABLE_NAME;
170    
171                                            sql = CustomSQLUtil.get(COUNT_BY_G_U_F);
172                                    }
173                            }
174    
175                            StringBundler sb = new StringBundler();
176    
177                            if (folderIds.size() > 0) {
178                                    sb.append(StringPool.OPEN_PARENTHESIS);
179                                    sb.append(getFolderIds(folderIds, table));
180                                    sb.append(StringPool.CLOSE_PARENTHESIS);
181                            }
182    
183                            if ((mimeTypes != null) && (mimeTypes.length > 0)) {
184                                    sb.append(WHERE_AND);
185                                    sb.append(StringPool.OPEN_PARENTHESIS);
186                                    sb.append(getMimeTypes(mimeTypes, table));
187                                    sb.append(StringPool.CLOSE_PARENTHESIS);
188                            }
189    
190                            sql = StringUtil.replace(sql, "[$FOLDER_ID$]", sb.toString());
191    
192                            SQLQuery q = session.createSQLQuery(sql);
193    
194                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
195    
196                            QueryPos qPos = QueryPos.getInstance(q);
197    
198                            qPos.add(groupId);
199    
200                            if (userId > 0) {
201                                    qPos.add(userId);
202                            }
203    
204                            if (status != WorkflowConstants.STATUS_ANY) {
205                                    qPos.add(status);
206                            }
207    
208                            for (Long folderId : folderIds) {
209                                    qPos.add(folderId);
210                            }
211    
212                            if (mimeTypes != null) {
213                                    qPos.add(mimeTypes);
214                            }
215    
216                            Iterator<Long> itr = q.iterate();
217    
218                            if (itr.hasNext()) {
219                                    Long count = itr.next();
220    
221                                    if (count != null) {
222                                            return count.intValue();
223                                    }
224                            }
225    
226                            return 0;
227                    }
228                    catch (Exception e) {
229                            throw new SystemException(e);
230                    }
231                    finally {
232                            closeSession(session);
233                    }
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("DLFileEntry", 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_S(
274                            long groupId, List<Long> folderIds, int status)
275                    throws SystemException {
276    
277                    return doCountByG_F_S(groupId, folderIds, status, true);
278            }
279    
280            @Override
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            @Override
295            public List<DLFileEntry> findByExtraSettings(int start, int end)
296                    throws SystemException {
297    
298                    Session session = null;
299    
300                    try {
301                            session = openSession();
302    
303                            String sql = CustomSQLUtil.get(FIND_BY_EXTRA_SETTINGS);
304    
305                            SQLQuery q = session.createSQLQuery(sql);
306    
307                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
308    
309                            return (List<DLFileEntry>)QueryUtil.list(
310                                    q, getDialect(), start, end);
311                    }
312                    catch (Exception e) {
313                            throw new SystemException(e);
314                    }
315                    finally {
316                            closeSession(session);
317                    }
318            }
319    
320            @Override
321            public List<DLFileEntry> findByMisversioned() throws SystemException {
322                    Session session = null;
323    
324                    try {
325                            session = openSession();
326    
327                            String sql = CustomSQLUtil.get(FIND_BY_MISVERSIONED);
328    
329                            SQLQuery q = session.createSQLQuery(sql);
330    
331                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
332    
333                            return q.list(true);
334                    }
335                    catch (Exception e) {
336                            throw new SystemException(e);
337                    }
338                    finally {
339                            closeSession(session);
340                    }
341            }
342    
343            @Override
344            public List<DLFileEntry> findByNoAssets() throws SystemException {
345                    Session session = null;
346    
347                    try {
348                            session = openSession();
349    
350                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
351    
352                            SQLQuery q = session.createSQLQuery(sql);
353    
354                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
355    
356                            return q.list(true);
357                    }
358                    catch (Exception e) {
359                            throw new SystemException(e);
360                    }
361                    finally {
362                            closeSession(session);
363                    }
364            }
365    
366            @Override
367            public List<DLFileEntry> findByOrphanedFileEntries()
368                    throws SystemException {
369    
370                    Session session = null;
371    
372                    try {
373                            session = openSession();
374    
375                            String sql = CustomSQLUtil.get(FIND_BY_ORPHANED_FILE_ENTRIES);
376    
377                            SQLQuery q = session.createSQLQuery(sql);
378    
379                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
380    
381                            return q.list(true);
382                    }
383                    catch (Exception e) {
384                            throw new SystemException(e);
385                    }
386                    finally {
387                            closeSession(session);
388                    }
389            }
390    
391            @Override
392            public List<DLFileEntry> findByG_U_F_M_S(
393                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
394                            int status, int start, int end, OrderByComparator obc)
395                    throws SystemException {
396    
397                    Session session = null;
398    
399                    String table = DLFileVersionImpl.TABLE_NAME;
400    
401                    try {
402                            session = openSession();
403    
404                            String sql = StringPool.BLANK;
405    
406                            if ((mimeTypes == null) || (mimeTypes.length == 0)) {
407                                    sql = CustomSQLUtil.get(FIND_BY_G_U_F_S);
408                            }
409                            else {
410                                    sql = CustomSQLUtil.get(FIND_BY_G_U_F_S_M);
411                            }
412    
413                            if (userId <= 0) {
414                                    if (status == WorkflowConstants.STATUS_ANY) {
415                                            table = DLFileEntryImpl.TABLE_NAME;
416    
417                                            if ((mimeTypes == null) || (mimeTypes.length == 0)) {
418                                                    sql = CustomSQLUtil.get(FIND_BY_G_F);
419                                            }
420                                            else {
421                                                    sql = CustomSQLUtil.get(FIND_BY_G_F_M);
422                                            }
423                                    }
424                                    else if ((mimeTypes == null) || (mimeTypes.length == 0)) {
425                                            sql = CustomSQLUtil.get(FIND_BY_G_F_S);
426                                    }
427                                    else {
428                                            sql = CustomSQLUtil.get(FIND_BY_G_F_S_M);
429                                    }
430                            }
431                            else if (status == WorkflowConstants.STATUS_ANY) {
432                                    table = DLFileEntryImpl.TABLE_NAME;
433    
434                                    if ((mimeTypes == null) || (mimeTypes.length == 0)) {
435                                            sql = CustomSQLUtil.get(FIND_BY_G_U_F);
436                                    }
437                                    else {
438                                            sql = CustomSQLUtil.get(FIND_BY_G_U_F_M);
439                                    }
440                            }
441    
442                            StringBundler sb = new StringBundler(3);
443    
444                            if (folderIds.size() > 0) {
445                                    sb.append(StringPool.OPEN_PARENTHESIS);
446                                    sb.append(getFolderIds(folderIds, table));
447                                    sb.append(StringPool.CLOSE_PARENTHESIS);
448                            }
449    
450                            sql = StringUtil.replace(sql, "[$FOLDER_ID$]", sb.toString());
451    
452                            sb.setIndex(0);
453    
454                            if ((mimeTypes != null) && (mimeTypes.length > 0)) {
455                                    sb.append(StringPool.OPEN_PARENTHESIS);
456                                    sb.append(getMimeTypes(mimeTypes, DLFileEntryImpl.TABLE_NAME));
457                                    sb.append(StringPool.CLOSE_PARENTHESIS);
458                            }
459    
460                            sql = StringUtil.replace(sql, "[$MIME_TYPE$]", sb.toString());
461                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
462    
463                            SQLQuery q = session.createSQLQuery(sql);
464    
465                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
466    
467                            QueryPos qPos = QueryPos.getInstance(q);
468    
469                            qPos.add(groupId);
470    
471                            if (userId > 0) {
472                                    qPos.add(userId);
473                            }
474    
475                            if (status != WorkflowConstants.STATUS_ANY) {
476                                    qPos.add(status);
477                            }
478    
479                            for (Long folderId : folderIds) {
480                                    qPos.add(folderId);
481                            }
482    
483                            if (mimeTypes != null) {
484                                    qPos.add(mimeTypes);
485                            }
486    
487                            return (List<DLFileEntry>)QueryUtil.list(
488                                    q, getDialect(), start, end);
489                    }
490                    catch (Exception e) {
491                            throw new SystemException(e);
492                    }
493                    finally {
494                            closeSession(session);
495                    }
496            }
497    
498            protected int doCountByG_F_S(
499                            long groupId, List<Long> folderIds, int status,
500                            boolean inlineSQLHelper)
501                    throws SystemException {
502    
503                    Session session = null;
504    
505                    try {
506                            session = openSession();
507    
508                            String sql = null;
509    
510                            String table = "DLFileEntry";
511    
512                            if (status == WorkflowConstants.STATUS_ANY) {
513                                    sql = CustomSQLUtil.get(COUNT_BY_G_F);
514                            }
515                            else {
516                                    sql = CustomSQLUtil.get(COUNT_BY_G_F_S);
517    
518                                    if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled()) {
519                                            sql = StringUtil.replace(
520                                                    sql, "[$JOIN$]",
521                                                    CustomSQLUtil.get(
522                                                            DLFolderFinderImpl.JOIN_FV_BY_DL_FILE_ENTRY));
523                                    }
524                                    else {
525                                            table = "DLFileVersion";
526    
527                                            sql = StringUtil.replace(sql, "[$JOIN$]", "");
528                                    }
529                            }
530    
531                            if (inlineSQLHelper) {
532                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
533                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
534                                            groupId);
535                            }
536    
537                            sql = StringUtil.replace(
538                                    sql, "[$FOLDER_ID$]", getFolderIds(folderIds, table));
539    
540                            SQLQuery q = session.createSQLQuery(sql);
541    
542                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
543    
544                            QueryPos qPos = QueryPos.getInstance(q);
545    
546                            qPos.add(groupId);
547    
548                            if (status != WorkflowConstants.STATUS_ANY) {
549                                    qPos.add(status);
550                            }
551    
552                            for (int i = 0; i < folderIds.size(); i++) {
553                                    Long folderId = folderIds.get(i);
554    
555                                    qPos.add(folderId);
556                            }
557    
558                            Iterator<Long> itr = q.iterate();
559    
560                            if (itr.hasNext()) {
561                                    Long count = itr.next();
562    
563                                    if (count != null) {
564                                            return count.intValue();
565                                    }
566                            }
567    
568                            return 0;
569                    }
570                    catch (Exception e) {
571                            throw new SystemException(e);
572                    }
573                    finally {
574                            closeSession(session);
575                    }
576            }
577    
578            protected String getFolderIds(List<Long> folderIds, String table) {
579                    if (folderIds.isEmpty()) {
580                            return StringPool.BLANK;
581                    }
582    
583                    StringBundler sb = new StringBundler(folderIds.size() * 2 - 1);
584    
585                    for (int i = 0; i < folderIds.size(); i++) {
586                            sb.append(table);
587                            sb.append(".folderId = ? ");
588    
589                            if ((i + 1) != folderIds.size()) {
590                                    sb.append(WHERE_OR);
591                            }
592                    }
593    
594                    return sb.toString();
595            }
596    
597            protected String getMimeTypes(String[] mimeTypes, String table) {
598                    if (mimeTypes.length == 0) {
599                            return StringPool.BLANK;
600                    }
601    
602                    StringBundler sb = new StringBundler(mimeTypes.length * 2 - 1);
603    
604                    for (int i = 0; i < mimeTypes.length; i++) {
605                            sb.append(table);
606                            sb.append(".mimeType = ?");
607    
608                            if ((i + 1) != mimeTypes.length) {
609                                    sb.append(WHERE_OR);
610                            }
611                    }
612    
613                    return sb.toString();
614            }
615    
616    }