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