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