001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
018    import com.liferay.portal.kernel.dao.orm.QueryPos;
019    import com.liferay.portal.kernel.dao.orm.QueryUtil;
020    import com.liferay.portal.kernel.dao.orm.SQLQuery;
021    import com.liferay.portal.kernel.dao.orm.Session;
022    import com.liferay.portal.kernel.dao.orm.Type;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.util.ArrayUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.workflow.WorkflowConstants;
029    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
030    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
031    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
032    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
033    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
034    import com.liferay.portlet.documentlibrary.model.impl.DLFileVersionImpl;
035    import com.liferay.util.dao.orm.CustomSQLUtil;
036    
037    import java.util.Collections;
038    import java.util.Iterator;
039    import java.util.List;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Shuyang Zhou
044     */
045    public class DLFileEntryFinderImpl
046            extends BasePersistenceImpl<DLFileEntry> implements DLFileEntryFinder {
047    
048            public static final String COUNT_BY_EXTRA_SETTINGS =
049                    DLFileEntryFinder.class.getName() + ".countByExtraSettings";
050    
051            public static final String COUNT_BY_G_F =
052                    DLFileEntryFinder.class.getName() + ".countByG_F";
053    
054            public static final String COUNT_BY_G_U_F =
055                    DLFileEntryFinder.class.getName() + ".countByG_U_F";
056    
057            public static final String FIND_BY_ANY_IMAGE_ID =
058                    DLFileEntryFinder.class.getName() + ".findByAnyImageId";
059    
060            public static final String FIND_BY_COMPANY_ID =
061                    DLFileEntryFinder.class.getName() + ".findByCompanyId";
062    
063            public static final String FIND_BY_EXTRA_SETTINGS =
064                    DLFileEntryFinder.class.getName() + ".findByExtraSettings";
065    
066            public static final String FIND_BY_DDM_STRUCTURE_IDS =
067                    DLFileEntryFinder.class.getName() + ".findByDDMStructureIds";
068    
069            public static final String FIND_BY_MISVERSIONED =
070                    DLFileEntryFinder.class.getName() + ".findByMisversioned";
071    
072            public static final String FIND_BY_NO_ASSETS =
073                    DLFileEntryFinder.class.getName() + ".findByNoAssets";
074    
075            public static final String FIND_BY_ORPHANED_FILE_ENTRIES =
076                    DLFileEntryFinder.class.getName() + ".findByOrphanedFileEntries";
077    
078            public static final String FIND_BY_G_F =
079                    DLFileEntryFinder.class.getName() + ".findByG_F";
080    
081            public static final String FIND_BY_G_U_F =
082                    DLFileEntryFinder.class.getName() + ".findByG_U_F";
083    
084            @Override
085            public int countByExtraSettings() throws SystemException {
086                    Session session = null;
087    
088                    try {
089                            session = openSession();
090    
091                            String sql = CustomSQLUtil.get(COUNT_BY_EXTRA_SETTINGS);
092    
093                            SQLQuery q = session.createSQLQuery(sql);
094    
095                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
096    
097                            Iterator<Long> itr = q.iterate();
098    
099                            if (itr.hasNext()) {
100                                    Long count = itr.next();
101    
102                                    if (count != null) {
103                                            return count.intValue();
104                                    }
105                            }
106    
107                            return 0;
108                    }
109                    catch (Exception e) {
110                            throw new SystemException(e);
111                    }
112                    finally {
113                            closeSession(session);
114                    }
115            }
116    
117            @Override
118            public int countByG_F(
119                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
120                    throws SystemException {
121    
122                    return doCountByG_U_F_M(
123                            groupId, 0, folderIds, null, queryDefinition, false);
124            }
125    
126            @Override
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                    return doCountByG_U_F_M(
133                            groupId, userId, folderIds, mimeTypes, queryDefinition, false);
134            }
135    
136            @Override
137            public DLFileEntry fetchByAnyImageId(long imageId) throws SystemException {
138                    Session session = null;
139    
140                    try {
141                            session = openSession();
142    
143                            String sql = CustomSQLUtil.get(FIND_BY_ANY_IMAGE_ID);
144    
145                            SQLQuery q = session.createSQLQuery(sql);
146    
147                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
148    
149                            QueryPos qPos = QueryPos.getInstance(q);
150    
151                            qPos.add(imageId);
152                            qPos.add(imageId);
153                            qPos.add(imageId);
154                            qPos.add(imageId);
155    
156                            List<DLFileEntry> dlFileEntries = q.list();
157    
158                            if (!dlFileEntries.isEmpty()) {
159                                    return dlFileEntries.get(0);
160                            }
161    
162                            return null;
163                    }
164                    catch (Exception e) {
165                            throw new SystemException(e);
166                    }
167                    finally {
168                            closeSession(session);
169                    }
170            }
171    
172            @Override
173            public int filterCountByG_F(
174                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
175                    throws SystemException {
176    
177                    return doCountByG_U_F_M(
178                            groupId, 0, folderIds, null, queryDefinition, true);
179            }
180    
181            @Override
182            public List<DLFileEntry> filterFindByG_F(
183                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
184                    throws SystemException {
185    
186                    return doFindByG_U_F_M(
187                            groupId, 0, folderIds, null, queryDefinition, true);
188            }
189    
190            @Override
191            public DLFileEntry findByAnyImageId(long imageId)
192                    throws NoSuchFileEntryException, SystemException {
193    
194                    DLFileEntry dlFileEntry = fetchByAnyImageId(imageId);
195    
196                    if (dlFileEntry != null) {
197                            return dlFileEntry;
198                    }
199    
200                    throw new NoSuchFileEntryException(
201                            "No DLFileEntry exists with the imageId " + imageId);
202            }
203    
204            @Override
205            public List<DLFileEntry> findByCompanyId(
206                            long companyId, QueryDefinition queryDefinition)
207                    throws SystemException {
208    
209                    Session session = null;
210    
211                    try {
212                            session = openSession();
213    
214                            String sql = CustomSQLUtil.get(
215                                    FIND_BY_COMPANY_ID, queryDefinition,
216                                    DLFileVersionImpl.TABLE_NAME);
217    
218                            sql = CustomSQLUtil.replaceOrderBy(
219                                    sql, queryDefinition.getOrderByComparator());
220    
221                            SQLQuery q = session.createSQLQuery(sql);
222    
223                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
224    
225                            QueryPos qPos = QueryPos.getInstance(q);
226    
227                            qPos.add(companyId);
228                            qPos.add(queryDefinition.getStatus());
229    
230                            return (List<DLFileEntry>)QueryUtil.list(
231                                    q, getDialect(), queryDefinition.getStart(),
232                                    queryDefinition.getEnd());
233                    }
234                    catch (Exception e) {
235                            throw new SystemException(e);
236                    }
237                    finally {
238                            closeSession(session);
239                    }
240            }
241    
242            @Override
243            public List<DLFileEntry> findByDDMStructureIds(
244                            long[] ddmStructureIds, int start, int end)
245                    throws SystemException {
246    
247                    Session session = null;
248    
249                    try {
250                            session = openSession();
251    
252                            String sql = CustomSQLUtil.get(FIND_BY_DDM_STRUCTURE_IDS);
253    
254                            if ((ddmStructureIds == null) || (ddmStructureIds.length <= 0)) {
255                                    return Collections.emptyList();
256                            }
257    
258                            sql = StringUtil.replace(
259                                    sql, "[$DDM_STRUCTURE_ID$]",
260                                    getDDMStructureIds(ddmStructureIds));
261    
262                            SQLQuery q = session.createSQLQuery(sql);
263    
264                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
265    
266                            QueryPos qPos = QueryPos.getInstance(q);
267    
268                            qPos.add(ddmStructureIds);
269    
270                            return (List<DLFileEntry>)QueryUtil.list(
271                                    q, getDialect(), start, end);
272                    }
273                    catch (Exception e) {
274                            throw new SystemException(e);
275                    }
276                    finally {
277                            closeSession(session);
278                    }
279            }
280    
281            @Override
282            public List<DLFileEntry> findByExtraSettings(int start, int end)
283                    throws SystemException {
284    
285                    Session session = null;
286    
287                    try {
288                            session = openSession();
289    
290                            String sql = CustomSQLUtil.get(FIND_BY_EXTRA_SETTINGS);
291    
292                            SQLQuery q = session.createSQLQuery(sql);
293    
294                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
295    
296                            return (List<DLFileEntry>)QueryUtil.list(
297                                    q, getDialect(), start, end);
298                    }
299                    catch (Exception e) {
300                            throw new SystemException(e);
301                    }
302                    finally {
303                            closeSession(session);
304                    }
305            }
306    
307            @Override
308            public List<DLFileEntry> findByMisversioned() throws SystemException {
309                    Session session = null;
310    
311                    try {
312                            session = openSession();
313    
314                            String sql = CustomSQLUtil.get(FIND_BY_MISVERSIONED);
315    
316                            SQLQuery q = session.createSQLQuery(sql);
317    
318                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
319    
320                            return q.list(true);
321                    }
322                    catch (Exception e) {
323                            throw new SystemException(e);
324                    }
325                    finally {
326                            closeSession(session);
327                    }
328            }
329    
330            @Override
331            public List<DLFileEntry> findByNoAssets() throws SystemException {
332                    Session session = null;
333    
334                    try {
335                            session = openSession();
336    
337                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
338    
339                            SQLQuery q = session.createSQLQuery(sql);
340    
341                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
342    
343                            return q.list(true);
344                    }
345                    catch (Exception e) {
346                            throw new SystemException(e);
347                    }
348                    finally {
349                            closeSession(session);
350                    }
351            }
352    
353            @Override
354            public List<DLFileEntry> findByOrphanedFileEntries()
355                    throws SystemException {
356    
357                    Session session = null;
358    
359                    try {
360                            session = openSession();
361    
362                            String sql = CustomSQLUtil.get(FIND_BY_ORPHANED_FILE_ENTRIES);
363    
364                            SQLQuery q = session.createSQLQuery(sql);
365    
366                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
367    
368                            return q.list(true);
369                    }
370                    catch (Exception e) {
371                            throw new SystemException(e);
372                    }
373                    finally {
374                            closeSession(session);
375                    }
376            }
377    
378            @Override
379            public List<DLFileEntry> findByG_F(
380                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
381                    throws SystemException {
382    
383                    return doFindByG_U_F_M(
384                            groupId, 0, folderIds, null, queryDefinition, false);
385            }
386    
387            @Override
388            public List<DLFileEntry> findByG_U_F_M(
389                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
390                            QueryDefinition queryDefinition)
391                    throws SystemException {
392    
393                    return doFindByG_U_F_M(
394                            groupId, userId, folderIds, mimeTypes, queryDefinition, false);
395            }
396    
397            protected int doCountByG_U_F_M(
398                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
399                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
400                    throws SystemException {
401    
402                    Session session = null;
403    
404                    try {
405                            session = openSession();
406    
407                            String id = null;
408    
409                            if (userId <= 0) {
410                                    id = COUNT_BY_G_F;
411                            }
412                            else {
413                                    id = COUNT_BY_G_U_F;
414                            }
415    
416                            String sql = getFileEntriesSQL(
417                                    id, groupId, folderIds, mimeTypes, queryDefinition,
418                                    inlineSQLHelper);
419    
420                            SQLQuery q = session.createSQLQuery(sql);
421    
422                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
423    
424                            QueryPos qPos = QueryPos.getInstance(q);
425    
426                            qPos.add(groupId);
427    
428                            if (userId > 0) {
429                                    qPos.add(userId);
430                            }
431    
432                            qPos.add(queryDefinition.getStatus());
433    
434                            for (Long folderId : folderIds) {
435                                    qPos.add(folderId);
436                            }
437    
438                            if (mimeTypes != null) {
439                                    qPos.add(mimeTypes);
440                            }
441    
442                            Iterator<Long> itr = q.iterate();
443    
444                            if (itr.hasNext()) {
445                                    Long count = itr.next();
446    
447                                    if (count != null) {
448                                            return count.intValue();
449                                    }
450                            }
451    
452                            return 0;
453                    }
454                    catch (Exception e) {
455                            throw new SystemException(e);
456                    }
457                    finally {
458                            closeSession(session);
459                    }
460            }
461    
462            protected List<DLFileEntry> doFindByG_U_F_M(
463                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
464                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
465                    throws SystemException {
466    
467                    Session session = null;
468    
469                    try {
470                            session = openSession();
471    
472                            String id = null;
473    
474                            if (userId <= 0) {
475                                    id = FIND_BY_G_F;
476                            }
477                            else {
478                                    id = FIND_BY_G_U_F;
479                            }
480    
481                            String sql = getFileEntriesSQL(
482                                    id, groupId, folderIds, mimeTypes, queryDefinition,
483                                    inlineSQLHelper);
484    
485                            sql = CustomSQLUtil.replaceOrderBy(
486                                    sql, queryDefinition.getOrderByComparator());
487    
488                            SQLQuery q = session.createSQLQuery(sql);
489    
490                            q.addEntity(DLFileEntryImpl.TABLE_NAME, DLFileEntryImpl.class);
491    
492                            QueryPos qPos = QueryPos.getInstance(q);
493    
494                            qPos.add(groupId);
495    
496                            if (userId > 0) {
497                                    qPos.add(userId);
498                            }
499    
500                            qPos.add(queryDefinition.getStatus());
501    
502                            for (Long folderId : folderIds) {
503                                    qPos.add(folderId);
504                            }
505    
506                            if (mimeTypes != null) {
507                                    qPos.add(mimeTypes);
508                            }
509    
510                            return (List<DLFileEntry>)QueryUtil.list(
511                                    q, getDialect(), queryDefinition.getStart(),
512                                    queryDefinition.getEnd());
513                    }
514                    catch (Exception e) {
515                            throw new SystemException(e);
516                    }
517                    finally {
518                            closeSession(session);
519                    }
520            }
521    
522            protected String getDDMStructureIds(long[] ddmStructureIds) {
523                    StringBundler sb = new StringBundler(
524                            (ddmStructureIds.length * 2 - 1) + 2);
525    
526                    sb.append(StringPool.OPEN_PARENTHESIS);
527    
528                    for (int i = 0; i < ddmStructureIds.length; i++) {
529                            sb.append("DLFileEntryTypes_DDMStructures.structureId = ?");
530    
531                            if ((i + 1) != ddmStructureIds.length) {
532                                    sb.append(WHERE_OR);
533                            }
534                    }
535    
536                    sb.append(StringPool.CLOSE_PARENTHESIS);
537    
538                    return sb.toString();
539            }
540    
541            protected String getFileEntriesSQL(
542                    String id, long groupId, List<Long> folderIds, String[] mimeTypes,
543                    QueryDefinition queryDefinition, boolean inlineSQLHelper) {
544    
545                    String tableName = DLFileVersionImpl.TABLE_NAME;
546    
547                    String sql = CustomSQLUtil.get(id, queryDefinition, tableName);
548    
549                    if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
550                            sql = StringUtil.replace(sql, "[$JOIN$]", StringPool.BLANK);
551    
552                            tableName = DLFileEntryImpl.TABLE_NAME;
553                    }
554                    else {
555                            sql = StringUtil.replace(
556                                    sql, "[$JOIN$]",
557                                    CustomSQLUtil.get(
558                                            DLFolderFinderImpl.JOIN_FE_BY_DL_FILE_VERSION));
559                    }
560    
561                    if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled()) {
562                            sql = InlineSQLHelperUtil.replacePermissionCheck(
563                                    sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
564                                    groupId);
565                    }
566    
567                    StringBundler sb = new StringBundler(7);
568    
569                    if (!folderIds.isEmpty()) {
570                            sb.append(StringPool.OPEN_PARENTHESIS);
571                            sb.append(getFolderIds(folderIds, tableName));
572                            sb.append(StringPool.CLOSE_PARENTHESIS);
573                    }
574    
575                    if (ArrayUtil.isNotEmpty(mimeTypes)) {
576                            sb.append(WHERE_AND);
577                            sb.append(StringPool.OPEN_PARENTHESIS);
578                            sb.append(getMimeTypes(mimeTypes, tableName));
579                            sb.append(StringPool.CLOSE_PARENTHESIS);
580                    }
581    
582                    return StringUtil.replace(sql, "[$FOLDER_ID$]", sb.toString());
583            }
584    
585            protected String getFolderIds(List<Long> folderIds, String tableName) {
586                    if (folderIds.isEmpty()) {
587                            return StringPool.BLANK;
588                    }
589    
590                    StringBundler sb = new StringBundler(folderIds.size() * 3 + 1);
591    
592                    sb.append(StringPool.OPEN_PARENTHESIS);
593    
594                    for (int i = 0; i < folderIds.size(); i++) {
595                            sb.append(tableName);
596                            sb.append(".folderId = ? ");
597    
598                            if ((i + 1) != folderIds.size()) {
599                                    sb.append(WHERE_OR);
600                            }
601                    }
602    
603                    sb.append(StringPool.CLOSE_PARENTHESIS);
604    
605                    return sb.toString();
606            }
607    
608            protected String getMimeTypes(String[] mimeTypes, String tableName) {
609                    if (mimeTypes.length == 0) {
610                            return StringPool.BLANK;
611                    }
612    
613                    StringBundler sb = new StringBundler(mimeTypes.length * 2 - 1);
614    
615                    for (int i = 0; i < mimeTypes.length; i++) {
616                            sb.append(tableName);
617                            sb.append(".mimeType = ?");
618    
619                            if ((i + 1) != mimeTypes.length) {
620                                    sb.append(WHERE_OR);
621                            }
622                    }
623    
624                    return sb.toString();
625            }
626    
627    }