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