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.wiki.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.wiki.NoSuchPageException;
031    import com.liferay.portlet.wiki.model.WikiPage;
032    import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
033    import com.liferay.util.dao.orm.CustomSQLUtil;
034    
035    import java.sql.Timestamp;
036    
037    import java.util.Date;
038    import java.util.Iterator;
039    import java.util.List;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class WikiPageFinderImpl
045            extends BasePersistenceImpl<WikiPage> implements WikiPageFinder {
046    
047            public static final String COUNT_BY_CREATE_DATE =
048                    WikiPageFinder.class.getName() + ".countByCreateDate";
049    
050            public static final String COUNT_BY_G_N_H_S =
051                    WikiPageFinder.class.getName() + ".countByG_N_H_S";
052    
053            public static final String FIND_BY_RESOURCE_PRIM_KEY =
054                    WikiPageFinder.class.getName() + ".findByResourcePrimKey";
055    
056            public static final String FIND_BY_CREATE_DATE =
057                    WikiPageFinder.class.getName() + ".findByCreateDate";
058    
059            public static final String FIND_BY_G_N_H_S =
060                    WikiPageFinder.class.getName() + ".findByG_N_H_S";
061    
062            public static final String FIND_BY_NO_ASSETS =
063                    WikiPageFinder.class.getName() + ".findByNoAssets";
064    
065            @Override
066            public int countByCreateDate(
067                            long groupId, long nodeId, Date createDate, boolean before)
068                    throws SystemException {
069    
070                    return countByCreateDate(
071                            groupId, nodeId, new Timestamp(createDate.getTime()), before);
072            }
073    
074            @Override
075            public int countByCreateDate(
076                            long groupId, long nodeId, Timestamp createDate, boolean before)
077                    throws SystemException {
078    
079                    return doCountByCreateDate(groupId, nodeId, createDate, before, false);
080            }
081    
082            @Override
083            public int countByG_N_H_S(
084                            long groupId, long nodeId, boolean head,
085                            QueryDefinition queryDefinition)
086                    throws SystemException {
087    
088                    return doCountByG_N_H_S(groupId, nodeId, head, queryDefinition, false);
089            }
090    
091            @Override
092            public int filterCountByCreateDate(
093                            long groupId, long nodeId, Date createDate, boolean before)
094                    throws SystemException {
095    
096                    return doCountByCreateDate(
097                            groupId, nodeId, new Timestamp(createDate.getTime()), before, true);
098            }
099    
100            @Override
101            public int filterCountByCreateDate(
102                            long groupId, long nodeId, Timestamp createDate, boolean before)
103                    throws SystemException {
104    
105                    return doCountByCreateDate(groupId, nodeId, createDate, before, true);
106            }
107    
108            @Override
109            public int filterCountByG_N_H_S(
110                            long groupId, long nodeId, boolean head,
111                            QueryDefinition queryDefinition)
112                    throws SystemException {
113    
114                    return doCountByG_N_H_S(groupId, nodeId, head, queryDefinition, true);
115            }
116    
117            @Override
118            public List<WikiPage> filterFindByCreateDate(
119                            long groupId, long nodeId, Date createDate, boolean before,
120                            int start, int end)
121                    throws SystemException {
122    
123                    return doFindByCreateDate(
124                            groupId, nodeId, new Timestamp(createDate.getTime()), before, start,
125                            end, true);
126            }
127    
128            @Override
129            public List<WikiPage> filterFindByCreateDate(
130                            long groupId, long nodeId, Timestamp createDate, boolean before,
131                            int start, int end)
132                    throws SystemException {
133    
134                    return doFindByCreateDate(
135                            groupId, nodeId, createDate, before, start, end, true);
136            }
137    
138            @Override
139            public List<WikiPage> filterFindByG_N_H_S(
140                            long groupId, long nodeId, boolean head,
141                            QueryDefinition queryDefinition)
142                    throws SystemException {
143    
144                    return doFindByG_N_H_S(groupId, nodeId, head, queryDefinition, true);
145            }
146    
147            @Override
148            public WikiPage findByResourcePrimKey(long resourcePrimKey)
149                    throws NoSuchPageException, SystemException {
150    
151                    Session session = null;
152    
153                    try {
154                            session = openSession();
155    
156                            String sql = CustomSQLUtil.get(FIND_BY_RESOURCE_PRIM_KEY);
157    
158                            SQLQuery q = session.createSQLQuery(sql);
159    
160                            q.addEntity("WikiPage", WikiPageImpl.class);
161    
162                            QueryPos qPos = QueryPos.getInstance(q);
163    
164                            qPos.add(resourcePrimKey);
165    
166                            List<WikiPage> pages = q.list();
167    
168                            if (!pages.isEmpty()) {
169                                    return pages.get(0);
170                            }
171                    }
172                    catch (Exception e) {
173                            throw new SystemException(e);
174                    }
175                    finally {
176                            closeSession(session);
177                    }
178    
179                    StringBundler sb = new StringBundler(3);
180    
181                    sb.append("No WikiPage exists with the key {resourcePrimKey");
182                    sb.append(resourcePrimKey);
183                    sb.append("}");
184    
185                    throw new NoSuchPageException(sb.toString());
186            }
187    
188            @Override
189            public List<WikiPage> findByCreateDate(
190                            long groupId, long nodeId, Date createDate, boolean before,
191                            int start, int end)
192                    throws SystemException {
193    
194                    return doFindByCreateDate(
195                            groupId, nodeId, new Timestamp(createDate.getTime()), before, start,
196                            end, false);
197            }
198    
199            @Override
200            public List<WikiPage> findByCreateDate(
201                            long groupId, long nodeId, Timestamp createDate, boolean before,
202                            int start, int end)
203                    throws SystemException {
204    
205                    return doFindByCreateDate(
206                            groupId, nodeId, createDate, before, start, end, false);
207            }
208    
209            @Override
210            public List<WikiPage> findByNoAssets() throws SystemException {
211                    Session session = null;
212    
213                    try {
214                            session = openSession();
215    
216                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
217    
218                            SQLQuery q = session.createSQLQuery(sql);
219    
220                            q.addEntity("WikiPage", WikiPageImpl.class);
221    
222                            return q.list(true);
223                    }
224                    catch (Exception e) {
225                            throw new SystemException(e);
226                    }
227                    finally {
228                            closeSession(session);
229                    }
230            }
231    
232            @Override
233            public List<WikiPage> findByG_N_H_S(
234                            long groupId, long nodeId, boolean head,
235                            QueryDefinition queryDefinition)
236                    throws SystemException {
237    
238                    return doFindByG_N_H_S(groupId, nodeId, head, queryDefinition, false);
239            }
240    
241            protected int doCountByCreateDate(
242                            long groupId, long nodeId, Timestamp createDate, boolean before,
243                            boolean inlineSQLHelper)
244                    throws SystemException {
245    
246                    Session session = null;
247    
248                    try {
249                            session = openSession();
250    
251                            String sql = CustomSQLUtil.get(COUNT_BY_CREATE_DATE);
252    
253                            String createDateComparator = StringPool.GREATER_THAN;
254    
255                            if (before) {
256                                    createDateComparator = StringPool.LESS_THAN;
257                            }
258    
259                            sql = StringUtil.replace(
260                                    sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
261    
262                            if (inlineSQLHelper) {
263                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
264                                            sql, WikiPage.class.getName(), "WikiPage.resourcePrimKey",
265                                            groupId);
266                            }
267    
268                            SQLQuery q = session.createSQLQuery(sql);
269    
270                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
271    
272                            QueryPos qPos = QueryPos.getInstance(q);
273    
274                            qPos.add(groupId);
275                            qPos.add(nodeId);
276                            qPos.add(createDate);
277                            qPos.add(true);
278                            qPos.add(WorkflowConstants.STATUS_APPROVED);
279    
280                            Iterator<Long> itr = q.iterate();
281    
282                            if (itr.hasNext()) {
283                                    Long count = itr.next();
284    
285                                    if (count != null) {
286                                            return count.intValue();
287                                    }
288                            }
289    
290                            return 0;
291                    }
292                    catch (Exception e) {
293                            throw new SystemException(e);
294                    }
295                    finally {
296                            closeSession(session);
297                    }
298            }
299    
300            protected int doCountByG_N_H_S(
301                            long groupId, long nodeId, boolean head,
302                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
303                    throws SystemException {
304    
305                    Session session = null;
306    
307                    try {
308                            session = openSession();
309    
310                            String sql = CustomSQLUtil.get(
311                                    COUNT_BY_G_N_H_S, queryDefinition, "WikiPage");
312    
313                            if (inlineSQLHelper) {
314                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
315                                            sql, WikiPage.class.getName(), "WikiPage.resourcePrimKey",
316                                            groupId);
317                            }
318    
319                            SQLQuery q = session.createSQLQuery(sql);
320    
321                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
322    
323                            QueryPos qPos = QueryPos.getInstance(q);
324    
325                            qPos.add(groupId);
326                            qPos.add(nodeId);
327                            qPos.add(head);
328    
329                            if (queryDefinition.getOwnerUserId() > 0) {
330                                    qPos.add(queryDefinition.getOwnerUserId());
331    
332                                    if (queryDefinition.isIncludeOwner()) {
333                                            qPos.add(WorkflowConstants.STATUS_IN_TRASH);
334                                    }
335                            }
336    
337                            qPos.add(queryDefinition.getStatus());
338    
339                            Iterator<Long> itr = q.iterate();
340    
341                            if (itr.hasNext()) {
342                                    Long count = itr.next();
343    
344                                    if (count != null) {
345                                            return count.intValue();
346                                    }
347                            }
348    
349                            return 0;
350                    }
351                    catch (Exception e) {
352                            throw new SystemException(e);
353                    }
354                    finally {
355                            closeSession(session);
356                    }
357            }
358    
359            protected List<WikiPage> doFindByCreateDate(
360                            long groupId, long nodeId, Timestamp createDate, boolean before,
361                            int start, int end, boolean inlineSQLHelper)
362                    throws SystemException {
363    
364                    Session session = null;
365    
366                    try {
367                            session = openSession();
368    
369                            String sql = CustomSQLUtil.get(FIND_BY_CREATE_DATE);
370    
371                            String createDateComparator = StringPool.GREATER_THAN;
372    
373                            if (before) {
374                                    createDateComparator = StringPool.LESS_THAN;
375                            }
376    
377                            sql = StringUtil.replace(
378                                    sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
379    
380                            if (inlineSQLHelper) {
381                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
382                                            sql, WikiPage.class.getName(), "WikiPage.resourcePrimKey",
383                                            groupId);
384                            }
385    
386                            SQLQuery q = session.createSQLQuery(sql);
387    
388                            q.addEntity("WikiPage", WikiPageImpl.class);
389    
390                            QueryPos qPos = QueryPos.getInstance(q);
391    
392                            qPos.add(groupId);
393                            qPos.add(nodeId);
394                            qPos.add(createDate);
395                            qPos.add(true);
396                            qPos.add(WorkflowConstants.STATUS_APPROVED);
397    
398                            return (List<WikiPage>)QueryUtil.list(q, getDialect(), start, end);
399                    }
400                    catch (Exception e) {
401                            throw new SystemException(e);
402                    }
403                    finally {
404                            closeSession(session);
405                    }
406            }
407    
408            protected List<WikiPage> doFindByG_N_H_S(
409                            long groupId, long nodeId, boolean head,
410                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
411                    throws SystemException {
412    
413                    Session session = null;
414    
415                    try {
416                            session = openSession();
417    
418                            String sql = CustomSQLUtil.get(
419                                    FIND_BY_G_N_H_S, queryDefinition, "WikiPage");
420    
421                            if (inlineSQLHelper) {
422                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
423                                            sql, WikiPage.class.getName(), "WikiPage.resourcePrimKey",
424                                            groupId);
425                            }
426    
427                            CustomSQLUtil.replaceOrderBy(
428                                    sql, queryDefinition.getOrderByComparator("WikiPage"));
429    
430                            SQLQuery q = session.createSQLQuery(sql);
431    
432                            q.addEntity("WikiPage", WikiPageImpl.class);
433    
434                            QueryPos qPos = QueryPos.getInstance(q);
435    
436                            qPos.add(groupId);
437                            qPos.add(nodeId);
438                            qPos.add(head);
439    
440                            if (queryDefinition.getOwnerUserId() > 0) {
441                                    qPos.add(queryDefinition.getOwnerUserId());
442    
443                                    if (queryDefinition.isIncludeOwner()) {
444                                            qPos.add(WorkflowConstants.STATUS_IN_TRASH);
445                                    }
446                            }
447    
448                            qPos.add(queryDefinition.getStatus());
449    
450                            return (List<WikiPage>)QueryUtil.list(
451                                    q, getDialect(), queryDefinition.getStart(),
452                                    queryDefinition.getEnd());
453                    }
454                    catch (Exception e) {
455                            throw new SystemException(e);
456                    }
457                    finally {
458                            closeSession(session);
459                    }
460            }
461    
462    }