001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public int countByExtraSettings() throws SystemException {
101                    Session session = null;
102    
103                    try {
104                            session = openSession();
105    
106                            String sql = CustomSQLUtil.get(COUNT_BY_EXTRA_SETTINGS);
107    
108                            SQLQuery q = session.createSQLQuery(sql);
109    
110                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
111    
112                            Iterator<Long> itr = q.iterate();
113    
114                            if (itr.hasNext()) {
115                                    Long count = itr.next();
116    
117                                    if (count != null) {
118                                            return count.intValue();
119                                    }
120                            }
121    
122                            return 0;
123                    }
124                    catch (Exception e) {
125                            throw new SystemException(e);
126                    }
127                    finally {
128                            closeSession(session);
129                    }
130            }
131    
132            public int countByG_F_S(long groupId, List<Long> folderIds, int status)
133                    throws SystemException {
134    
135                    return doCountByG_F_S(groupId, folderIds, status, false);
136            }
137    
138            public int countByG_U_F_M_S(
139                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
140                            int status)
141                    throws SystemException {
142    
143                    Session session = null;
144    
145                    String table = DLFileVersionImpl.TABLE_NAME;
146    
147                    try {
148                            session = openSession();
149    
150                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_F_S);
151    
152                            if (userId <= 0) {
153                                    if (status == WorkflowConstants.STATUS_ANY) {
154                                            table = DLFileEntryImpl.TABLE_NAME;
155    
156                                            sql = CustomSQLUtil.get(COUNT_BY_G_F);
157                                    }
158                                    else {
159                                            sql = CustomSQLUtil.get(COUNT_BY_G_F_S);
160    
161                                            sql = StringUtil.replace(sql, "[$JOIN$]", "");
162                                    }
163                            }
164                            else {
165                                    if (status == WorkflowConstants.STATUS_ANY) {
166                                            table = DLFileEntryImpl.TABLE_NAME;
167    
168                                            sql = CustomSQLUtil.get(COUNT_BY_G_U_F);
169                                    }
170                            }
171    
172                            StringBundler sb = new StringBundler();
173    
174                            if (folderIds.size() > 0) {
175                                    sb.append(StringPool.OPEN_PARENTHESIS);
176                                    sb.append(getFolderIds(folderIds, table));
177                                    sb.append(StringPool.CLOSE_PARENTHESIS);
178                            }
179    
180                            if ((mimeTypes != null) && (mimeTypes.length > 0)) {
181                                    sb.append(WHERE_AND);
182                                    sb.append(StringPool.OPEN_PARENTHESIS);
183                                    sb.append(getMimeTypes(mimeTypes, table));
184                                    sb.append(StringPool.CLOSE_PARENTHESIS);
185                            }
186    
187                            sql = StringUtil.replace(sql, "[$FOLDER_ID$]", sb.toString());
188    
189                            SQLQuery q = session.createSQLQuery(sql);
190    
191                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
192    
193                            QueryPos qPos = QueryPos.getInstance(q);
194    
195                            qPos.add(groupId);
196    
197                            if (userId > 0) {
198                                    qPos.add(userId);
199                            }
200    
201                            if (status != WorkflowConstants.STATUS_ANY) {
202                                    qPos.add(status);
203                            }
204    
205                            for (Long folderId : folderIds) {
206                                    qPos.add(folderId);
207                            }
208    
209                            if (mimeTypes != null) {
210                                    qPos.add(mimeTypes);
211                            }
212    
213                            Iterator<Long> itr = q.iterate();
214    
215                            if (itr.hasNext()) {
216                                    Long count = itr.next();
217    
218                                    if (count != null) {
219                                            return count.intValue();
220                                    }
221                            }
222    
223                            return 0;
224                    }
225                    catch (Exception e) {
226                            throw new SystemException(e);
227                    }
228                    finally {
229                            closeSession(session);
230                    }
231            }
232    
233            public DLFileEntry fetchByAnyImageId(long imageId) throws SystemException {
234                    Session session = null;
235    
236                    try {
237                            session = openSession();
238    
239                            String sql = CustomSQLUtil.get(FIND_BY_ANY_IMAGE_ID);
240    
241                            SQLQuery q = session.createSQLQuery(sql);
242    
243                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
244    
245                            QueryPos qPos = QueryPos.getInstance(q);
246    
247                            qPos.add(imageId);
248                            qPos.add(imageId);
249                            qPos.add(imageId);
250                            qPos.add(imageId);
251    
252                            List<DLFileEntry> dlFileEntries = q.list();
253    
254                            if (!dlFileEntries.isEmpty()) {
255                                    return dlFileEntries.get(0);
256                            }
257    
258                            return null;
259                    }
260                    catch (Exception e) {
261                            throw new SystemException(e);
262                    }
263                    finally {
264                            closeSession(session);
265                    }
266            }
267    
268            public int filterCountByG_F_S(
269                            long groupId, List<Long> folderIds, int status)
270                    throws SystemException {
271    
272                    return doCountByG_F_S(groupId, folderIds, status, true);
273            }
274    
275            public DLFileEntry findByAnyImageId(long imageId)
276                    throws NoSuchFileEntryException, SystemException {
277    
278                    DLFileEntry dlFileEntry = fetchByAnyImageId(imageId);
279    
280                    if (dlFileEntry != null) {
281                            return dlFileEntry;
282                    }
283    
284                    throw new NoSuchFileEntryException(
285                            "No DLFileEntry exists with the imageId " + imageId);
286            }
287    
288            public List<DLFileEntry> findByExtraSettings(int start, int end)
289                    throws SystemException {
290    
291                    Session session = null;
292    
293                    try {
294                            session = openSession();
295    
296                            String sql = CustomSQLUtil.get(FIND_BY_EXTRA_SETTINGS);
297    
298                            SQLQuery q = session.createSQLQuery(sql);
299    
300                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
301    
302                            return (List<DLFileEntry>)QueryUtil.list(
303                                    q, getDialect(), start, end);
304                    }
305                    catch (Exception e) {
306                            throw new SystemException(e);
307                    }
308                    finally {
309                            closeSession(session);
310                    }
311            }
312    
313            public List<DLFileEntry> findByMisversioned() throws SystemException {
314                    Session session = null;
315    
316                    try {
317                            session = openSession();
318    
319                            String sql = CustomSQLUtil.get(FIND_BY_MISVERSIONED);
320    
321                            SQLQuery q = session.createSQLQuery(sql);
322    
323                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
324    
325                            return q.list(true);
326                    }
327                    catch (Exception e) {
328                            throw new SystemException(e);
329                    }
330                    finally {
331                            closeSession(session);
332                    }
333            }
334    
335            public List<DLFileEntry> findByNoAssets() throws SystemException {
336                    Session session = null;
337    
338                    try {
339                            session = openSession();
340    
341                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
342    
343                            SQLQuery q = session.createSQLQuery(sql);
344    
345                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
346    
347                            return q.list(true);
348                    }
349                    catch (Exception e) {
350                            throw new SystemException(e);
351                    }
352                    finally {
353                            closeSession(session);
354                    }
355            }
356    
357            public List<DLFileEntry> findByOrphanedFileEntries()
358                    throws SystemException {
359    
360                    Session session = null;
361    
362                    try {
363                            session = openSession();
364    
365                            String sql = CustomSQLUtil.get(FIND_BY_ORPHANED_FILE_ENTRIES);
366    
367                            SQLQuery q = session.createSQLQuery(sql);
368    
369                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
370    
371                            return q.list(true);
372                    }
373                    catch (Exception e) {
374                            throw new SystemException(e);
375                    }
376                    finally {
377                            closeSession(session);
378                    }
379            }
380    
381            public List<DLFileEntry> findByG_U_F_M_S(
382                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
383                            int status, int start, int end, OrderByComparator obc)
384                    throws SystemException {
385    
386                    Session session = null;
387    
388                    String table = DLFileVersionImpl.TABLE_NAME;
389    
390                    try {
391                            session = openSession();
392    
393                            String sql = StringPool.BLANK;
394    
395                            if ((mimeTypes == null) || (mimeTypes.length == 0)) {
396                                    sql = CustomSQLUtil.get(FIND_BY_G_U_F_S);
397                            }
398                            else {
399                                    sql = CustomSQLUtil.get(FIND_BY_G_U_F_S_M);
400                            }
401    
402                            if (userId <= 0) {
403                                    if (status == WorkflowConstants.STATUS_ANY) {
404                                            table = DLFileEntryImpl.TABLE_NAME;
405    
406                                            if ((mimeTypes == null) || (mimeTypes.length == 0)) {
407                                                    sql = CustomSQLUtil.get(FIND_BY_G_F);
408                                            }
409                                            else {
410                                                    sql = CustomSQLUtil.get(FIND_BY_G_F_M);
411                                            }
412                                    }
413                                    else if ((mimeTypes == null) || (mimeTypes.length == 0)) {
414                                            sql = CustomSQLUtil.get(FIND_BY_G_F_S);
415                                    }
416                                    else {
417                                            sql = CustomSQLUtil.get(FIND_BY_G_F_S_M);
418                                    }
419                            }
420                            else if (status == WorkflowConstants.STATUS_ANY) {
421                                    table = DLFileEntryImpl.TABLE_NAME;
422    
423                                    if ((mimeTypes == null) || (mimeTypes.length == 0)) {
424                                            sql = CustomSQLUtil.get(FIND_BY_G_U_F);
425                                    }
426                                    else {
427                                            sql = CustomSQLUtil.get(FIND_BY_G_U_F_M);
428                                    }
429                            }
430    
431                            StringBundler sb = new StringBundler(3);
432    
433                            if (folderIds.size() > 0) {
434                                    sb.append(StringPool.OPEN_PARENTHESIS);
435                                    sb.append(getFolderIds(folderIds, table));
436                                    sb.append(StringPool.CLOSE_PARENTHESIS);
437                            }
438    
439                            sql = StringUtil.replace(sql, "[$FOLDER_ID$]", sb.toString());
440    
441                            sb.setIndex(0);
442    
443                            if ((mimeTypes != null) && (mimeTypes.length > 0)) {
444                                    sb.append(StringPool.OPEN_PARENTHESIS);
445                                    sb.append(getMimeTypes(mimeTypes, DLFileEntryImpl.TABLE_NAME));
446                                    sb.append(StringPool.CLOSE_PARENTHESIS);
447                            }
448    
449                            sql = StringUtil.replace(sql, "[$MIME_TYPE$]", sb.toString());
450                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
451    
452                            SQLQuery q = session.createSQLQuery(sql);
453    
454                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
455    
456                            QueryPos qPos = QueryPos.getInstance(q);
457    
458                            qPos.add(groupId);
459    
460                            if (userId > 0) {
461                                    qPos.add(userId);
462                            }
463    
464                            if (status != WorkflowConstants.STATUS_ANY) {
465                                    qPos.add(status);
466                            }
467    
468                            for (Long folderId : folderIds) {
469                                    qPos.add(folderId);
470                            }
471    
472                            if (mimeTypes != null) {
473                                    qPos.add(mimeTypes);
474                            }
475    
476                            return (List<DLFileEntry>)QueryUtil.list(
477                                    q, getDialect(), start, end);
478                    }
479                    catch (Exception e) {
480                            throw new SystemException(e);
481                    }
482                    finally {
483                            closeSession(session);
484                    }
485            }
486    
487            protected int doCountByG_F_S(
488                            long groupId, List<Long> folderIds, int status,
489                            boolean inlineSQLHelper)
490                    throws SystemException {
491    
492                    Session session = null;
493    
494                    try {
495                            session = openSession();
496    
497                            String sql = null;
498    
499                            String table = "DLFileEntry";
500    
501                            if (status == WorkflowConstants.STATUS_ANY) {
502                                    sql = CustomSQLUtil.get(COUNT_BY_G_F);
503                            }
504                            else {
505                                    sql = CustomSQLUtil.get(COUNT_BY_G_F_S);
506    
507                                    if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled()) {
508    
509                                            sql = StringUtil.replace(
510                                                    sql, "[$JOIN$]",
511                                                    CustomSQLUtil.get(
512                                                            DLFolderFinderImpl.JOIN_FV_BY_DL_FILE_ENTRY));
513                                    }
514                                    else {
515                                            table = "DLFileVersion";
516    
517                                            sql = StringUtil.replace(sql, "[$JOIN$]", "");
518                                    }
519                            }
520    
521                            if (inlineSQLHelper) {
522                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
523                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
524                                            groupId);
525                            }
526    
527                            sql = StringUtil.replace(
528                                    sql, "[$FOLDER_ID$]", getFolderIds(folderIds, table));
529    
530                            SQLQuery q = session.createSQLQuery(sql);
531    
532                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
533    
534                            QueryPos qPos = QueryPos.getInstance(q);
535    
536                            qPos.add(groupId);
537    
538                            if (status != WorkflowConstants.STATUS_ANY) {
539                                    qPos.add(status);
540                            }
541    
542                            for (int i = 0; i < folderIds.size(); i++) {
543                                    Long folderId = folderIds.get(i);
544    
545                                    qPos.add(folderId);
546                            }
547    
548                            Iterator<Long> itr = q.iterate();
549    
550                            if (itr.hasNext()) {
551                                    Long count = itr.next();
552    
553                                    if (count != null) {
554                                            return count.intValue();
555                                    }
556                            }
557    
558                            return 0;
559                    }
560                    catch (Exception e) {
561                            throw new SystemException(e);
562                    }
563                    finally {
564                            closeSession(session);
565                    }
566            }
567    
568            protected String getFolderIds(List<Long> folderIds, String table) {
569                    if (folderIds.isEmpty()) {
570                            return StringPool.BLANK;
571                    }
572    
573                    StringBundler sb = new StringBundler(folderIds.size() * 2 - 1);
574    
575                    for (int i = 0; i < folderIds.size(); i++) {
576                            sb.append(table);
577                            sb.append(".folderId = ? ");
578    
579                            if ((i + 1) != folderIds.size()) {
580                                    sb.append(WHERE_OR);
581                            }
582                    }
583    
584                    return sb.toString();
585            }
586    
587            protected String getMimeTypes(String[] mimeTypes, String table) {
588                    if (mimeTypes.length == 0) {
589                            return StringPool.BLANK;
590                    }
591    
592                    StringBundler sb = new StringBundler(mimeTypes.length * 2 - 1);
593    
594                    for (int i = 0; i < mimeTypes.length; i++) {
595                            sb.append(table);
596                            sb.append(".mimeType = ?");
597    
598                            if ((i + 1) != mimeTypes.length) {
599                                    sb.append(WHERE_OR);
600                            }
601                    }
602    
603                    return sb.toString();
604            }
605    
606    }