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.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.Collections;
037    import java.util.Iterator;
038    import java.util.List;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Shuyang Zhou
043     */
044    public class DLFileEntryFinderImpl
045            extends BasePersistenceImpl<DLFileEntry> implements DLFileEntryFinder {
046    
047            public static final String COUNT_BY_EXTRA_SETTINGS =
048                    DLFileEntryFinder.class.getName() + ".countByExtraSettings";
049    
050            public static final String COUNT_BY_G_F =
051                    DLFileEntryFinder.class.getName() + ".countByG_F";
052    
053            public static final String COUNT_BY_G_U_F =
054                    DLFileEntryFinder.class.getName() + ".countByG_U_F";
055    
056            public static final String FIND_BY_ANY_IMAGE_ID =
057                    DLFileEntryFinder.class.getName() + ".findByAnyImageId";
058    
059            public static final String FIND_BY_EXTRA_SETTINGS =
060                    DLFileEntryFinder.class.getName() + ".findByExtraSettings";
061    
062            public static final String FIND_BY_DDM_STRUCTURE_IDS =
063                    DLFileEntryFinder.class.getName() + ".findByDDMStructureIds";
064    
065            public static final String FIND_BY_MISVERSIONED =
066                    DLFileEntryFinder.class.getName() + ".findByMisversioned";
067    
068            public static final String FIND_BY_NO_ASSETS =
069                    DLFileEntryFinder.class.getName() + ".findByNoAssets";
070    
071            public static final String FIND_BY_ORPHANED_FILE_ENTRIES =
072                    DLFileEntryFinder.class.getName() + ".findByOrphanedFileEntries";
073    
074            public static final String FIND_BY_G_F =
075                    DLFileEntryFinder.class.getName() + ".findByG_F";
076    
077            public static final String FIND_BY_G_U_F =
078                    DLFileEntryFinder.class.getName() + ".findByG_U_F";
079    
080            public int countByExtraSettings() throws SystemException {
081                    Session session = null;
082    
083                    try {
084                            session = openSession();
085    
086                            String sql = CustomSQLUtil.get(COUNT_BY_EXTRA_SETTINGS);
087    
088                            SQLQuery q = session.createSQLQuery(sql);
089    
090                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
091    
092                            Iterator<Long> itr = q.iterate();
093    
094                            if (itr.hasNext()) {
095                                    Long count = itr.next();
096    
097                                    if (count != null) {
098                                            return count.intValue();
099                                    }
100                            }
101    
102                            return 0;
103                    }
104                    catch (Exception e) {
105                            throw new SystemException(e);
106                    }
107                    finally {
108                            closeSession(session);
109                    }
110            }
111    
112            public int countByG_F(
113                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
114                    throws SystemException {
115    
116                    return doCountByG_U_F_M(
117                            groupId, 0, folderIds, null, queryDefinition, false);
118            }
119    
120            public int countByG_U_F_M(
121                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
122                            QueryDefinition queryDefinition)
123                    throws SystemException {
124    
125                    return doCountByG_U_F_M(
126                            groupId, userId, folderIds, mimeTypes, queryDefinition, false);
127            }
128    
129            public DLFileEntry fetchByAnyImageId(long imageId) throws SystemException {
130                    Session session = null;
131    
132                    try {
133                            session = openSession();
134    
135                            String sql = CustomSQLUtil.get(FIND_BY_ANY_IMAGE_ID);
136    
137                            SQLQuery q = session.createSQLQuery(sql);
138    
139                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
140    
141                            QueryPos qPos = QueryPos.getInstance(q);
142    
143                            qPos.add(imageId);
144                            qPos.add(imageId);
145                            qPos.add(imageId);
146                            qPos.add(imageId);
147    
148                            List<DLFileEntry> dlFileEntries = q.list();
149    
150                            if (!dlFileEntries.isEmpty()) {
151                                    return dlFileEntries.get(0);
152                            }
153    
154                            return null;
155                    }
156                    catch (Exception e) {
157                            throw new SystemException(e);
158                    }
159                    finally {
160                            closeSession(session);
161                    }
162            }
163    
164            public int filterCountByG_F(
165                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
166                    throws SystemException {
167    
168                    return doCountByG_U_F_M(
169                            groupId, 0, folderIds, null, queryDefinition, true);
170            }
171    
172            public List<DLFileEntry> filterFindByG_F(
173                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
174                    throws SystemException {
175    
176                    return doFindByG_U_F_M(
177                            groupId, 0, folderIds, null, queryDefinition, true);
178            }
179    
180            public DLFileEntry findByAnyImageId(long imageId)
181                    throws NoSuchFileEntryException, SystemException {
182    
183                    DLFileEntry dlFileEntry = fetchByAnyImageId(imageId);
184    
185                    if (dlFileEntry != null) {
186                            return dlFileEntry;
187                    }
188    
189                    throw new NoSuchFileEntryException(
190                            "No DLFileEntry exists with the imageId " + imageId);
191            }
192    
193            public List<DLFileEntry> findByDDMStructureIds(
194                            long[] ddmStructureIds, int start, int end)
195                    throws SystemException {
196    
197                    Session session = null;
198    
199                    try {
200                            session = openSession();
201    
202                            String sql = CustomSQLUtil.get(FIND_BY_DDM_STRUCTURE_IDS);
203    
204                            if ((ddmStructureIds == null) || (ddmStructureIds.length <= 0)) {
205                                    return Collections.emptyList();
206                            }
207    
208                            sql = StringUtil.replace(
209                                    sql, "[$DDM_STRUCTURE_ID$]",
210                                    getDDMStructureIds(ddmStructureIds));
211    
212                            SQLQuery q = session.createSQLQuery(sql);
213    
214                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
215    
216                            QueryPos qPos = QueryPos.getInstance(q);
217    
218                            qPos.add(ddmStructureIds);
219    
220                            return (List<DLFileEntry>)QueryUtil.list(
221                                    q, getDialect(), start, end);
222                    }
223                    catch (Exception e) {
224                            throw new SystemException(e);
225                    }
226                    finally {
227                            closeSession(session);
228                    }
229            }
230    
231            public List<DLFileEntry> findByExtraSettings(int start, int end)
232                    throws SystemException {
233    
234                    Session session = null;
235    
236                    try {
237                            session = openSession();
238    
239                            String sql = CustomSQLUtil.get(FIND_BY_EXTRA_SETTINGS);
240    
241                            SQLQuery q = session.createSQLQuery(sql);
242    
243                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
244    
245                            return (List<DLFileEntry>)QueryUtil.list(
246                                    q, getDialect(), start, end);
247                    }
248                    catch (Exception e) {
249                            throw new SystemException(e);
250                    }
251                    finally {
252                            closeSession(session);
253                    }
254            }
255    
256            public List<DLFileEntry> findByMisversioned() throws SystemException {
257                    Session session = null;
258    
259                    try {
260                            session = openSession();
261    
262                            String sql = CustomSQLUtil.get(FIND_BY_MISVERSIONED);
263    
264                            SQLQuery q = session.createSQLQuery(sql);
265    
266                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
267    
268                            return q.list(true);
269                    }
270                    catch (Exception e) {
271                            throw new SystemException(e);
272                    }
273                    finally {
274                            closeSession(session);
275                    }
276            }
277    
278            public List<DLFileEntry> findByNoAssets() throws SystemException {
279                    Session session = null;
280    
281                    try {
282                            session = openSession();
283    
284                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
285    
286                            SQLQuery q = session.createSQLQuery(sql);
287    
288                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
289    
290                            return q.list(true);
291                    }
292                    catch (Exception e) {
293                            throw new SystemException(e);
294                    }
295                    finally {
296                            closeSession(session);
297                    }
298            }
299    
300            public List<DLFileEntry> findByOrphanedFileEntries()
301                    throws SystemException {
302    
303                    Session session = null;
304    
305                    try {
306                            session = openSession();
307    
308                            String sql = CustomSQLUtil.get(FIND_BY_ORPHANED_FILE_ENTRIES);
309    
310                            SQLQuery q = session.createSQLQuery(sql);
311    
312                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
313    
314                            return q.list(true);
315                    }
316                    catch (Exception e) {
317                            throw new SystemException(e);
318                    }
319                    finally {
320                            closeSession(session);
321                    }
322            }
323    
324            public List<DLFileEntry> findByG_F(
325                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
326                    throws SystemException {
327    
328                    return doFindByG_U_F_M(
329                            groupId, 0, folderIds, null, queryDefinition, false);
330            }
331    
332            public List<DLFileEntry> findByG_U_F_M(
333                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
334                            QueryDefinition queryDefinition)
335                    throws SystemException {
336    
337                    return doFindByG_U_F_M(
338                            groupId, userId, folderIds, mimeTypes, queryDefinition, false);
339            }
340    
341            protected int doCountByG_U_F_M(
342                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
343                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
344                    throws SystemException {
345    
346                    Session session = null;
347    
348                    try {
349                            session = openSession();
350    
351                            String id = null;
352    
353                            if (userId <= 0) {
354                                    id = COUNT_BY_G_F;
355                            }
356                            else {
357                                    id = COUNT_BY_G_U_F;
358                            }
359    
360                            String sql = getFileEntriesSQL(
361                                    id, groupId, folderIds, mimeTypes, queryDefinition,
362                                    inlineSQLHelper);
363    
364                            SQLQuery q = session.createSQLQuery(sql);
365    
366                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
367    
368                            QueryPos qPos = QueryPos.getInstance(q);
369    
370                            qPos.add(groupId);
371    
372                            if (userId > 0) {
373                                    qPos.add(userId);
374                            }
375    
376                            qPos.add(queryDefinition.getStatus());
377    
378                            for (Long folderId : folderIds) {
379                                    qPos.add(folderId);
380                            }
381    
382                            if (mimeTypes != null) {
383                                    qPos.add(mimeTypes);
384                            }
385    
386                            Iterator<Long> itr = q.iterate();
387    
388                            if (itr.hasNext()) {
389                                    Long count = itr.next();
390    
391                                    if (count != null) {
392                                            return count.intValue();
393                                    }
394                            }
395    
396                            return 0;
397                    }
398                    catch (Exception e) {
399                            throw new SystemException(e);
400                    }
401                    finally {
402                            closeSession(session);
403                    }
404            }
405    
406            protected List<DLFileEntry> doFindByG_U_F_M(
407                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
408                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
409                    throws SystemException {
410    
411                    Session session = null;
412    
413                    try {
414                            session = openSession();
415    
416                            String id = null;
417    
418                            if (userId <= 0) {
419                                    id = FIND_BY_G_F;
420                            }
421                            else {
422                                    id = FIND_BY_G_U_F;
423                            }
424    
425                            String sql = getFileEntriesSQL(
426                                    id, groupId, folderIds, mimeTypes, queryDefinition,
427                                    inlineSQLHelper);
428    
429                            sql = CustomSQLUtil.replaceOrderBy(
430                                    sql, queryDefinition.getOrderByComparator());
431    
432                            SQLQuery q = session.createSQLQuery(sql);
433    
434                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
435    
436                            QueryPos qPos = QueryPos.getInstance(q);
437    
438                            qPos.add(groupId);
439    
440                            if (userId > 0) {
441                                    qPos.add(userId);
442                            }
443    
444                            qPos.add(queryDefinition.getStatus());
445    
446                            for (Long folderId : folderIds) {
447                                    qPos.add(folderId);
448                            }
449    
450                            if (mimeTypes != null) {
451                                    qPos.add(mimeTypes);
452                            }
453    
454                            return (List<DLFileEntry>)QueryUtil.list(
455                                    q, getDialect(), queryDefinition.getStart(),
456                                    queryDefinition.getEnd());
457                    }
458                    catch (Exception e) {
459                            throw new SystemException(e);
460                    }
461                    finally {
462                            closeSession(session);
463                    }
464            }
465    
466            protected String getDDMStructureIds(long[] ddmStructureIds) {
467                    StringBundler sb = new StringBundler(
468                            (ddmStructureIds.length * 2 - 1) + 2);
469    
470                    sb.append(StringPool.OPEN_PARENTHESIS);
471    
472                    for (int i = 0; i < ddmStructureIds.length; i++) {
473                            sb.append("DLFileEntryTypes_DDMStructures.structureId = ?");
474    
475                            if ((i + 1) != ddmStructureIds.length) {
476                                    sb.append(WHERE_OR);
477                            }
478                    }
479    
480                    sb.append(StringPool.CLOSE_PARENTHESIS);
481    
482                    return sb.toString();
483            }
484    
485            protected String getFileEntriesSQL(
486                    String id, long groupId, List<Long> folderIds, String[] mimeTypes,
487                    QueryDefinition queryDefinition, boolean inlineSQLHelper) {
488    
489                    String tableName = DLFileVersionImpl.TABLE_NAME;
490    
491                    String sql = CustomSQLUtil.get(id, queryDefinition, tableName);
492    
493                    if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
494                            sql = StringUtil.replace(sql, "[$JOIN$]", StringPool.BLANK);
495    
496                            tableName = DLFileEntryImpl.TABLE_NAME;
497                    }
498                    else {
499                            sql = StringUtil.replace(
500                                    sql, "[$JOIN$]",
501                                    CustomSQLUtil.get(
502                                            DLFolderFinderImpl.JOIN_FE_BY_DL_FILE_VERSION));
503                    }
504    
505                    if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled()) {
506                            sql = InlineSQLHelperUtil.replacePermissionCheck(
507                                    sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
508                                    groupId);
509                    }
510    
511                    StringBundler sb = new StringBundler(7);
512    
513                    if (!folderIds.isEmpty()) {
514                            sb.append(StringPool.OPEN_PARENTHESIS);
515                            sb.append(getFolderIds(folderIds, tableName));
516                            sb.append(StringPool.CLOSE_PARENTHESIS);
517                    }
518    
519                    if ((mimeTypes != null) && (mimeTypes.length > 0)) {
520                            sb.append(WHERE_AND);
521                            sb.append(StringPool.OPEN_PARENTHESIS);
522                            sb.append(getMimeTypes(mimeTypes, tableName));
523                            sb.append(StringPool.CLOSE_PARENTHESIS);
524                    }
525    
526                    return StringUtil.replace(sql, "[$FOLDER_ID$]", sb.toString());
527            }
528    
529            protected String getFolderIds(List<Long> folderIds, String tableName) {
530                    if (folderIds.isEmpty()) {
531                            return StringPool.BLANK;
532                    }
533    
534                    StringBundler sb = new StringBundler(folderIds.size() * 3 + 1);
535    
536                    sb.append(StringPool.OPEN_PARENTHESIS);
537    
538                    for (int i = 0; i < folderIds.size(); i++) {
539                            sb.append(tableName);
540                            sb.append(".folderId = ? ");
541    
542                            if ((i + 1) != folderIds.size()) {
543                                    sb.append(WHERE_OR);
544                            }
545                    }
546    
547                    sb.append(StringPool.CLOSE_PARENTHESIS);
548    
549                    return sb.toString();
550            }
551    
552            protected String getMimeTypes(String[] mimeTypes, String tableName) {
553                    if (mimeTypes.length == 0) {
554                            return StringPool.BLANK;
555                    }
556    
557                    StringBundler sb = new StringBundler(mimeTypes.length * 2 - 1);
558    
559                    for (int i = 0; i < mimeTypes.length; i++) {
560                            sb.append(tableName);
561                            sb.append(".mimeType = ?");
562    
563                            if ((i + 1) != mimeTypes.length) {
564                                    sb.append(WHERE_OR);
565                            }
566                    }
567    
568                    return sb.toString();
569            }
570    
571    }