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