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.journal.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.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
028    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
029    import com.liferay.portlet.journal.model.JournalArticle;
030    import com.liferay.portlet.journal.model.JournalFolder;
031    import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
032    import com.liferay.portlet.journal.model.impl.JournalFolderImpl;
033    import com.liferay.util.dao.orm.CustomSQLUtil;
034    
035    import java.util.ArrayList;
036    import java.util.Iterator;
037    import java.util.List;
038    
039    /**
040     * @author Juan Fernández
041     * @author Zsolt Berentey
042     */
043    public class JournalFolderFinderImpl extends BasePersistenceImpl<JournalFolder>
044            implements JournalFolderFinder {
045    
046            public static final String COUNT_A_BY_G_F =
047                    JournalFolderFinder.class.getName() + ".countA_ByG_F";
048    
049            public static final String COUNT_F_BY_G_F =
050                    JournalFolderFinder.class.getName() + ".countF_ByG_F";
051    
052            public static final String FIND_A_BY_G_F =
053                    JournalFolderFinder.class.getName() + ".findA_ByG_F";
054    
055            public static final String FIND_F_BY_NO_ASSETS =
056                    JournalFolderFinder.class.getName() + ".findByF_ByNoAssets";
057    
058            public static final String FIND_F_BY_G_F =
059                    JournalFolderFinder.class.getName() + ".findF_ByG_F";
060    
061            public int countF_A_ByG_F(
062                            long groupId, long folderId, QueryDefinition queryDefinition)
063                    throws SystemException {
064    
065                    return doCountF_A_ByG_F(groupId, folderId, queryDefinition, false);
066            }
067    
068            public int filterCountF_A_ByG_F(
069                            long groupId, long folderId, QueryDefinition queryDefinition)
070                    throws SystemException {
071    
072                    return doCountF_A_ByG_F(groupId, folderId, queryDefinition, true);
073            }
074    
075            public List<Object> filterFindF_A_ByG_F(
076                            long groupId, long folderId, QueryDefinition queryDefinition)
077                    throws SystemException {
078    
079                    return doFindF_A_ByG_F(groupId, folderId, queryDefinition, true);
080            }
081    
082            public List<JournalFolder> findF_ByNoAssets() throws SystemException {
083                    Session session = null;
084    
085                    try {
086                            session = openSession();
087    
088                            String sql = CustomSQLUtil.get(FIND_F_BY_NO_ASSETS);
089    
090                            SQLQuery q = session.createSQLQuery(sql);
091    
092                            q.addEntity(JournalFolderImpl.TABLE_NAME, JournalFolderImpl.class);
093    
094                            return q.list(true);
095                    }
096                    catch (Exception e) {
097                            throw new SystemException(e);
098                    }
099                    finally {
100                            closeSession(session);
101                    }
102            }
103    
104            public List<Object> findF_A_ByG_F(
105                            long groupId, long folderId, QueryDefinition queryDefinition)
106                    throws SystemException {
107    
108                    return doFindF_A_ByG_F(groupId, folderId, queryDefinition, false);
109            }
110    
111            protected int doCountF_A_ByG_F(
112                            long groupId, long folderId, QueryDefinition queryDefinition,
113                            boolean inlineSQLHelper)
114                    throws SystemException {
115    
116                    Session session = null;
117    
118                    try {
119                            session = openSession();
120    
121                            StringBundler sb = new StringBundler(5);
122    
123                            sb.append(StringPool.OPEN_PARENTHESIS);
124                            sb.append(
125                                    getFoldersSQL(
126                                            COUNT_F_BY_G_F, groupId, queryDefinition, inlineSQLHelper));
127                            sb.append(") UNION ALL (");
128                            sb.append(
129                                    getArticlesSQL(
130                                            COUNT_A_BY_G_F, groupId, queryDefinition, inlineSQLHelper));
131                            sb.append(StringPool.CLOSE_PARENTHESIS);
132    
133                            String sql = updateSQL(sb.toString(), folderId);
134    
135                            SQLQuery q = session.createSQLQuery(sql);
136    
137                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
138    
139                            QueryPos qPos = QueryPos.getInstance(q);
140    
141                            qPos.add(groupId);
142                            qPos.add(queryDefinition.getStatus());
143    
144                            if (folderId >= 0) {
145                                    qPos.add(folderId);
146                            }
147    
148                            qPos.add(groupId);
149                            qPos.add(queryDefinition.getStatus());
150    
151                            if (folderId >= 0) {
152                                    qPos.add(folderId);
153                            }
154    
155                            int count = 0;
156    
157                            Iterator<Long> itr = q.iterate();
158    
159                            while (itr.hasNext()) {
160                                    Long l = itr.next();
161    
162                                    if (l != null) {
163                                            count += l.intValue();
164                                    }
165                            }
166    
167                            return count;
168                    }
169                    catch (Exception e) {
170                            throw new SystemException(e);
171                    }
172                    finally {
173                            closeSession(session);
174                    }
175            }
176    
177            protected List<Object> doFindF_A_ByG_F(
178                            long groupId, long folderId, QueryDefinition queryDefinition,
179                            boolean inlineSQLHelper)
180                    throws SystemException {
181    
182                    Session session = null;
183    
184                    try {
185                            session = openSession();
186    
187                            StringBundler sb = new StringBundler(5);
188    
189                            sb.append(StringPool.OPEN_PARENTHESIS);
190                            sb.append(
191                                    getFoldersSQL(
192                                            FIND_F_BY_G_F, groupId, queryDefinition, inlineSQLHelper));
193                            sb.append(") UNION ALL (");
194                            sb.append(
195                                    getArticlesSQL(
196                                            FIND_A_BY_G_F, groupId, queryDefinition, inlineSQLHelper));
197                            sb.append(StringPool.CLOSE_PARENTHESIS);
198    
199                            String sql = updateSQL(sb.toString(), folderId);
200    
201                            sql = CustomSQLUtil.replaceOrderBy(
202                                    sql, queryDefinition.getOrderByComparator());
203    
204                            SQLQuery q = session.createSQLQuery(sql);
205    
206                            q.addScalar("modelFolderId", Type.LONG);
207                            q.addScalar("modelFolder", Type.LONG);
208                            q.addScalar("articleId", Type.STRING);
209                            q.addScalar("version", Type.DOUBLE);
210    
211                            QueryPos qPos = QueryPos.getInstance(q);
212    
213                            qPos.add(groupId);
214                            qPos.add(queryDefinition.getStatus());
215    
216                            if (folderId >= 0) {
217                                    qPos.add(folderId);
218                            }
219    
220                            qPos.add(groupId);
221                            qPos.add(queryDefinition.getStatus());
222    
223                            if (folderId >= 0) {
224                                    qPos.add(folderId);
225                            }
226    
227                            List<Object> models = new ArrayList<Object>();
228    
229                            Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
230                                    q, getDialect(), queryDefinition.getStart(),
231                                    queryDefinition.getEnd());
232    
233                            while (itr.hasNext()) {
234                                    Object[] array = itr.next();
235    
236                                    long curFolderId = (Long)array[0];
237                                    long modelFolder = (Long)array[1];
238    
239                                    Object obj = null;
240    
241                                    if (modelFolder == 1) {
242                                            obj = JournalFolderUtil.findByPrimaryKey(curFolderId);
243                                    }
244                                    else {
245                                            String articleId = (String)array[2];
246                                            double version = (Double)array[3];
247    
248                                            obj = JournalArticleUtil.findByG_A_V(
249                                                    groupId, articleId, version);
250                                    }
251    
252                                    models.add(obj);
253                            }
254    
255                            return models;
256                    }
257                    catch (Exception e) {
258                            throw new SystemException(e);
259                    }
260                    finally {
261                            closeSession(session);
262                    }
263            }
264    
265            protected String getArticlesSQL(
266                    String id, long groupId, QueryDefinition queryDefinition,
267                    boolean inlineSQLHelper) {
268    
269                    String sql = CustomSQLUtil.get(
270                            id, queryDefinition, JournalArticleImpl.TABLE_NAME);
271    
272                    if (inlineSQLHelper) {
273                            sql = InlineSQLHelperUtil.replacePermissionCheck(
274                                    sql, JournalArticle.class.getName(),
275                                    "JournalArticle.resourcePrimKey", groupId);
276                    }
277    
278                    return sql;
279            }
280    
281            protected String getFolderId(long folderId, String tableName) {
282                    if (folderId < 0) {
283                            return StringPool.BLANK;
284                    }
285    
286                    StringBundler sb = new StringBundler(5);
287    
288                    sb.append(" AND ");
289                    sb.append(tableName);
290                    sb.append(".");
291    
292                    if (tableName.equals(JournalFolderImpl.TABLE_NAME)) {
293                            sb.append("parentFolderId");
294                    }
295                    else {
296                            sb.append("folderId");
297                    }
298    
299                    sb.append(" = ? ");
300    
301                    return sb.toString();
302            }
303    
304            protected String getFoldersSQL(
305                    String id, long groupId, QueryDefinition queryDefinition,
306                    boolean inlineSQLHelper) {
307    
308                    String sql = CustomSQLUtil.get(
309                            id, queryDefinition, JournalFolderImpl.TABLE_NAME);
310    
311                    if (inlineSQLHelper) {
312                            sql = InlineSQLHelperUtil.replacePermissionCheck(
313                                    sql, JournalFolder.class.getName(), "JournalFolder.folderId",
314                                    groupId);
315                    }
316    
317                    return sql;
318            }
319    
320            protected String updateSQL(String sql, long folderId) {
321                    sql = StringUtil.replace(
322                            sql,
323                            new String[] {
324                                    "[$ARTICLE_FOLDER_ID$]", "[$FOLDER_PARENT_FOLDER_ID$]"
325                            },
326                            new String[] {
327                                    getFolderId(folderId, JournalArticleImpl.TABLE_NAME),
328                                    getFolderId(folderId, JournalFolderImpl.TABLE_NAME)
329                            });
330    
331                    return sql;
332            }
333    
334    }