001    /**
002     * Copyright (c) 2000-2011 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.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
028    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
029    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
030    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
031    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Shuyang Zhou
040     */
041    public class DLFileEntryFinderImpl
042            extends BasePersistenceImpl<DLFileEntry> implements DLFileEntryFinder {
043    
044            public static String COUNT_BY_EXTRA_SETTINGS =
045                    DLFileEntryFinder.class.getName() + ".countByExtraSettings";
046    
047            public static String COUNT_BY_G_F =
048                    DLFileEntryFinder.class.getName() + ".countByG_F";
049    
050            public static String COUNT_BY_G_F_S =
051                    DLFileEntryFinder.class.getName() + ".countByG_F_S";
052    
053            public static String FIND_BY_ANY_IMAGE_ID =
054                    DLFileEntryFinder.class.getName() + ".findByAnyImageId";
055    
056            public static String FIND_BY_EXTRA_SETTINGS =
057                    DLFileEntryFinder.class.getName() + ".findByExtraSettings";
058    
059            public static String FIND_BY_NO_ASSETS =
060                    DLFileEntryFinder.class.getName() + ".findByNoAssets";
061    
062            public static String FIND_BY_ORPHANED_FILE_ENTRIES =
063                    DLFileEntryFinder.class.getName() + ".findByOrphanedFileEntries";
064    
065            public int countByExtraSettings() throws SystemException {
066                    Session session = null;
067    
068                    try {
069                            session = openSession();
070    
071                            String sql = CustomSQLUtil.get(COUNT_BY_EXTRA_SETTINGS);
072    
073                            SQLQuery q = session.createSQLQuery(sql);
074    
075                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
076    
077                            Iterator<Long> itr = q.iterate();
078    
079                            if (itr.hasNext()) {
080                                    Long count = itr.next();
081    
082                                    if (count != null) {
083                                            return count.intValue();
084                                    }
085                            }
086    
087                            return 0;
088                    }
089                    catch (Exception e) {
090                            throw new SystemException(e);
091                    }
092                    finally {
093                            closeSession(session);
094                    }
095            }
096    
097            public int countByG_F_S(long groupId, List<Long> folderIds, int status)
098                    throws SystemException {
099    
100                    return doCountByG_F_S(groupId, folderIds, status, false);
101            }
102    
103            public DLFileEntry fetchByAnyImageId(long imageId) throws SystemException {
104                    Session session = null;
105    
106                    try {
107                            session = openSession();
108    
109                            String sql = CustomSQLUtil.get(FIND_BY_ANY_IMAGE_ID);
110    
111                            SQLQuery q = session.createSQLQuery(sql);
112    
113                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
114    
115                            QueryPos qPos = QueryPos.getInstance(q);
116    
117                            qPos.add(imageId);
118                            qPos.add(imageId);
119                            qPos.add(imageId);
120                            qPos.add(imageId);
121    
122                            List<DLFileEntry> dlFileEntries = q.list();
123    
124                            if (!dlFileEntries.isEmpty()) {
125                                    return dlFileEntries.get(0);
126                            }
127    
128                            return null;
129                    }
130                    catch (Exception e) {
131                            throw new SystemException(e);
132                    }
133                    finally {
134                            closeSession(session);
135                    }
136            }
137    
138            public int filterCountByG_F_S(
139                            long groupId, List<Long> folderIds, int status)
140                    throws SystemException {
141    
142                    return doCountByG_F_S(groupId, folderIds, status, true);
143            }
144    
145            public DLFileEntry findByAnyImageId(long imageId)
146                    throws NoSuchFileEntryException, SystemException {
147    
148                    DLFileEntry dlFileEntry = fetchByAnyImageId(imageId);
149    
150                    if (dlFileEntry != null) {
151                            return dlFileEntry;
152                    }
153    
154                    throw new NoSuchFileEntryException(
155                            "No DLFileEntry exists with the imageId " + imageId);
156            }
157    
158            public List<DLFileEntry> findByExtraSettings(int start, int end)
159                    throws SystemException {
160    
161                    Session session = null;
162    
163                    try {
164                            session = openSession();
165    
166                            String sql = CustomSQLUtil.get(FIND_BY_EXTRA_SETTINGS);
167    
168                            SQLQuery q = session.createSQLQuery(sql);
169    
170                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
171    
172                            return (List<DLFileEntry>)QueryUtil.list(
173                                    q, getDialect(), start, end);
174                    }
175                    catch (Exception e) {
176                            throw new SystemException(e);
177                    }
178                    finally {
179                            closeSession(session);
180                    }
181            }
182    
183            public List<DLFileEntry> findByNoAssets() throws SystemException {
184                    Session session = null;
185    
186                    try {
187                            session = openSession();
188    
189                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
190    
191                            SQLQuery q = session.createSQLQuery(sql);
192    
193                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
194    
195                            return q.list(true);
196                    }
197                    catch (Exception e) {
198                            throw new SystemException(e);
199                    }
200                    finally {
201                            closeSession(session);
202                    }
203            }
204    
205            public List<DLFileEntry> findByOrphanedFileEntries()
206                    throws SystemException {
207    
208                    Session session = null;
209    
210                    try {
211                            session = openSession();
212    
213                            String sql = CustomSQLUtil.get(FIND_BY_ORPHANED_FILE_ENTRIES);
214    
215                            SQLQuery q = session.createSQLQuery(sql);
216    
217                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
218    
219                            return q.list(true);
220                    }
221                    catch (Exception e) {
222                            throw new SystemException(e);
223                    }
224                    finally {
225                            closeSession(session);
226                    }
227            }
228    
229            protected int doCountByG_F_S(
230                            long groupId, List<Long> folderIds, int status,
231                            boolean inlineSQLHelper)
232                    throws SystemException {
233    
234                    Session session = null;
235    
236                    try {
237                            session = openSession();
238    
239                            String sql = null;
240    
241                            String table = "DLFileEntry";
242    
243                            if (status == WorkflowConstants.STATUS_ANY) {
244                                    sql = CustomSQLUtil.get(COUNT_BY_G_F);
245                            }
246                            else {
247                                    sql = CustomSQLUtil.get(COUNT_BY_G_F_S);
248    
249                                    if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled()) {
250    
251                                            sql = StringUtil.replace(sql, "[$JOIN$]",
252                                                    CustomSQLUtil.get(
253                                                            DLFolderFinderImpl.JOIN_FV_BY_DL_FILE_ENTRY));
254                                    }
255                                    else {
256                                            table = "DLFileVersion";
257    
258                                            sql = StringUtil.replace(sql, "[$JOIN$]", "");
259                                    }
260                            }
261    
262                            if (inlineSQLHelper) {
263                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
264                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
265                                            groupId);
266                            }
267    
268                            sql = StringUtil.replace(
269                                    sql, "[$FOLDER_ID$]", getFolderIds(folderIds, table));
270    
271                            SQLQuery q = session.createSQLQuery(sql);
272    
273                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
274    
275                            QueryPos qPos = QueryPos.getInstance(q);
276    
277                            qPos.add(groupId);
278    
279                            if (status != WorkflowConstants.STATUS_ANY) {
280                                    qPos.add(status);
281                            }
282    
283                            for (int i = 0; i < folderIds.size(); i++) {
284                                    Long folderId = folderIds.get(i);
285    
286                                    qPos.add(folderId);
287                            }
288    
289                            Iterator<Long> itr = q.iterate();
290    
291                            if (itr.hasNext()) {
292                                    Long count = itr.next();
293    
294                                    if (count != null) {
295                                            return count.intValue();
296                                    }
297                            }
298    
299                            return 0;
300                    }
301                    catch (Exception e) {
302                            throw new SystemException(e);
303                    }
304                    finally {
305                            closeSession(session);
306                    }
307            }
308    
309            protected String getFolderIds(List<Long> folderIds, String table) {
310                    if (folderIds.isEmpty()) {
311                            return StringPool.BLANK;
312                    }
313    
314                    StringBundler sb = new StringBundler(folderIds.size() * 2 - 1);
315    
316                    for (int i = 0; i < folderIds.size(); i++) {
317                            sb.append(table);
318                            sb.append(".folderId = ? ");
319    
320                            if ((i + 1) != folderIds.size()) {
321                                    sb.append("OR ");
322                            }
323                    }
324    
325                    return sb.toString();
326            }
327    
328    }