001    /**
002     * Copyright (c) 2000-2010 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.SQLQuery;
019    import com.liferay.portal.kernel.dao.orm.Session;
020    import com.liferay.portal.kernel.dao.orm.Type;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
027    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
029    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
030    import com.liferay.util.dao.orm.CustomSQLUtil;
031    
032    import java.util.Iterator;
033    import java.util.List;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class DLFileEntryFinderImpl
039            extends BasePersistenceImpl<DLFileEntry> implements DLFileEntryFinder {
040    
041            public static String COUNT_BY_G_F_S =
042                    DLFileEntryFinder.class.getName() + ".countByG_F_S";
043    
044            public static String FIND_BY_NO_ASSETS =
045                    DLFileEntryFinder.class.getName() + ".findByNoAssets";
046    
047            public int countByG_F_S(long groupId, List<Long> folderIds, int status)
048                    throws SystemException {
049    
050                    return doCountByG_F_S(groupId, folderIds, status, false);
051            }
052    
053            public int filterCountByG_F_S(
054                            long groupId, List<Long> folderIds, int status)
055                    throws SystemException {
056    
057                    return doCountByG_F_S(groupId, folderIds, status, true);
058            }
059    
060            public List<DLFileEntry> findByNoAssets() throws SystemException {
061                    Session session = null;
062    
063                    try {
064                            session = openSession();
065    
066                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
067    
068                            SQLQuery q = session.createSQLQuery(sql);
069    
070                            q.addEntity("DLFileEntry", DLFileEntryImpl.class);
071    
072                            return q.list();
073                    }
074                    catch (Exception e) {
075                            throw new SystemException(e);
076                    }
077                    finally {
078                            closeSession(session);
079                    }
080            }
081    
082            protected int doCountByG_F_S(
083                            long groupId, List<Long> folderIds, int status,
084                            boolean inlineSQLHelper)
085                    throws SystemException {
086    
087                    Session session = null;
088    
089                    try {
090                            session = openSession();
091    
092                            String sql = CustomSQLUtil.get(COUNT_BY_G_F_S);
093    
094                            if (inlineSQLHelper) {
095                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
096                                            sql, DLFileEntry.class.getName(), "DLFileEntry.fileEntryId",
097                                            "DLFileEntry.userId", groupId);
098                            }
099    
100                            sql = StringUtil.replace(
101                                    sql, "[$FOLDER_ID$]", getFolderIds(folderIds));
102    
103                            if (status == WorkflowConstants.STATUS_ANY) {
104                                    sql = StringUtil.replace(
105                                            sql, "(DLFileVersion.status = ?) AND", "");
106                            }
107    
108                            SQLQuery q = session.createSQLQuery(sql);
109    
110                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
111    
112                            QueryPos qPos = QueryPos.getInstance(q);
113    
114                            qPos.add(groupId);
115    
116                            if (status != WorkflowConstants.STATUS_ANY) {
117                                    qPos.add(status);
118                            }
119    
120                            for (int i = 0; i < folderIds.size(); i++) {
121                                    Long folderId = folderIds.get(i);
122    
123                                    qPos.add(folderId);
124                            }
125    
126                            Iterator<Long> itr = q.list().iterator();
127    
128                            if (itr.hasNext()) {
129                                    Long count = itr.next();
130    
131                                    if (count != null) {
132                                            return count.intValue();
133                                    }
134                            }
135    
136                            return 0;
137                    }
138                    catch (Exception e) {
139                            throw new SystemException(e);
140                    }
141                    finally {
142                            closeSession(session);
143                    }
144            }
145    
146            protected String getFolderIds(List<Long> folderIds) {
147                    if (folderIds.isEmpty()) {
148                            return StringPool.BLANK;
149                    }
150    
151                    StringBundler sb = new StringBundler(folderIds.size() * 2 - 1);
152    
153                    for (int i = 0; i < folderIds.size(); i++) {
154                            sb.append("DLFileEntry.folderId = ? ");
155    
156                            if ((i + 1) != folderIds.size()) {
157                                    sb.append("OR ");
158                            }
159                    }
160    
161                    return sb.toString();
162            }
163    
164    }