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