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.wiki.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
028    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
029    import com.liferay.portlet.wiki.NoSuchPageException;
030    import com.liferay.portlet.wiki.model.WikiPage;
031    import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.sql.Timestamp;
035    
036    import java.util.Date;
037    import java.util.Iterator;
038    import java.util.List;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class WikiPageFinderImpl
044            extends BasePersistenceImpl<WikiPage> implements WikiPageFinder {
045    
046            public static final String COUNT_BY_CREATE_DATE =
047                    WikiPageFinder.class.getName() + ".countByCreateDate";
048    
049            public static final String FIND_BY_RESOURCE_PRIM_KEY =
050                    WikiPageFinder.class.getName() + ".findByResourcePrimKey";
051    
052            public static final String FIND_BY_CREATE_DATE =
053                    WikiPageFinder.class.getName() + ".findByCreateDate";
054    
055            public static final String FIND_BY_NO_ASSETS =
056                    WikiPageFinder.class.getName() + ".findByNoAssets";
057    
058            public int countByCreateDate(
059                            long groupId, long nodeId, Date createDate, boolean before)
060                    throws SystemException {
061    
062                    return countByCreateDate(
063                            groupId, nodeId, new Timestamp(createDate.getTime()), before);
064            }
065    
066            public int countByCreateDate(
067                            long groupId, long nodeId, Timestamp createDate, boolean before)
068                    throws SystemException {
069    
070                    return doCountByCreateDate(groupId, nodeId, createDate, before, false);
071            }
072    
073            public int filterCountByCreateDate(
074                            long groupId, long nodeId, Date createDate, boolean before)
075                    throws SystemException {
076    
077                    return doCountByCreateDate(
078                            groupId, nodeId, new Timestamp(createDate.getTime()), before, true);
079            }
080    
081            public int filterCountByCreateDate(
082                            long groupId, long nodeId, Timestamp createDate, boolean before)
083                    throws SystemException {
084    
085                    return doCountByCreateDate(groupId, nodeId, createDate, before, true);
086            }
087    
088            public List<WikiPage> filterFindByCreateDate(
089                            long groupId, long nodeId, Date createDate, boolean before,
090                            int start, int end)
091                    throws SystemException {
092    
093                    return doFindByCreateDate(
094                            groupId, nodeId, new Timestamp(createDate.getTime()), before, start,
095                            end, true);
096            }
097    
098            public List<WikiPage> filterFindByCreateDate(
099                            long groupId, long nodeId, Timestamp createDate, boolean before,
100                            int start, int end)
101                    throws SystemException {
102    
103                    return doFindByCreateDate(
104                            groupId, nodeId, createDate, before, start, end, true);
105            }
106    
107            public WikiPage findByResourcePrimKey(long resourcePrimKey)
108                    throws NoSuchPageException, SystemException {
109    
110                    Session session = null;
111    
112                    try {
113                            session = openSession();
114    
115                            String sql = CustomSQLUtil.get(FIND_BY_RESOURCE_PRIM_KEY);
116    
117                            SQLQuery q = session.createSQLQuery(sql);
118    
119                            q.addEntity("WikiPage", WikiPageImpl.class);
120    
121                            QueryPos qPos = QueryPos.getInstance(q);
122    
123                            qPos.add(resourcePrimKey);
124    
125                            List<WikiPage> pages = q.list();
126    
127                            if (!pages.isEmpty()) {
128                                    return pages.get(0);
129                            }
130                    }
131                    catch (Exception e) {
132                            throw new SystemException(e);
133                    }
134                    finally {
135                            closeSession(session);
136                    }
137    
138                    StringBundler sb = new StringBundler(3);
139    
140                    sb.append("No WikiPage exists with the key {resourcePrimKey");
141                    sb.append(resourcePrimKey);
142                    sb.append("}");
143    
144                    throw new NoSuchPageException(sb.toString());
145            }
146    
147            public List<WikiPage> findByCreateDate(
148                            long groupId, long nodeId, Date createDate, boolean before,
149                            int start, int end)
150                    throws SystemException {
151    
152                    return doFindByCreateDate(
153                            groupId, nodeId, new Timestamp(createDate.getTime()), before, start,
154                            end, false);
155            }
156    
157            public List<WikiPage> findByCreateDate(
158                            long groupId, long nodeId, Timestamp createDate, boolean before,
159                            int start, int end)
160                    throws SystemException {
161    
162                    return doFindByCreateDate(
163                            groupId, nodeId, createDate, before, start, end, false);
164            }
165    
166            public List<WikiPage> findByNoAssets() throws SystemException {
167                    Session session = null;
168    
169                    try {
170                            session = openSession();
171    
172                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
173    
174                            SQLQuery q = session.createSQLQuery(sql);
175    
176                            q.addEntity("WikiPage", WikiPageImpl.class);
177    
178                            return q.list(true);
179                    }
180                    catch (Exception e) {
181                            throw new SystemException(e);
182                    }
183                    finally {
184                            closeSession(session);
185                    }
186            }
187    
188            protected int doCountByCreateDate(
189                            long groupId, long nodeId, Timestamp createDate, boolean before,
190                            boolean inlineSQLHelper)
191                    throws SystemException {
192    
193                    Session session = null;
194    
195                    try {
196                            session = openSession();
197    
198                            String sql = CustomSQLUtil.get(COUNT_BY_CREATE_DATE);
199    
200                            String createDateComparator = StringPool.GREATER_THAN;
201    
202                            if (before) {
203                                    createDateComparator = StringPool.LESS_THAN;
204                            }
205    
206                            sql = StringUtil.replace(
207                                    sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
208    
209                            if (inlineSQLHelper) {
210                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
211                                            sql, WikiPage.class.getName(), "WikiPage.resourcePrimKey",
212                                            groupId);
213                            }
214    
215                            SQLQuery q = session.createSQLQuery(sql);
216    
217                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
218    
219                            QueryPos qPos = QueryPos.getInstance(q);
220    
221                            qPos.add(groupId);
222                            qPos.add(nodeId);
223                            qPos.add(createDate);
224                            qPos.add(true);
225                            qPos.add(WorkflowConstants.STATUS_APPROVED);
226    
227                            Iterator<Long> itr = q.iterate();
228    
229                            if (itr.hasNext()) {
230                                    Long count = itr.next();
231    
232                                    if (count != null) {
233                                            return count.intValue();
234                                    }
235                            }
236    
237                            return 0;
238                    }
239                    catch (Exception e) {
240                            throw new SystemException(e);
241                    }
242                    finally {
243                            closeSession(session);
244                    }
245            }
246    
247            protected List<WikiPage> doFindByCreateDate(
248                            long groupId, long nodeId, Timestamp createDate, boolean before,
249                            int start, int end, boolean inlineSQLHelper)
250                    throws SystemException {
251    
252                    Session session = null;
253    
254                    try {
255                            session = openSession();
256    
257                            String sql = CustomSQLUtil.get(FIND_BY_CREATE_DATE);
258    
259                            String createDateComparator = StringPool.GREATER_THAN;
260    
261                            if (before) {
262                                    createDateComparator = StringPool.LESS_THAN;
263                            }
264    
265                            sql = StringUtil.replace(
266                                    sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
267    
268                            if (inlineSQLHelper) {
269                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
270                                            sql, WikiPage.class.getName(), "WikiPage.resourcePrimKey",
271                                            groupId);
272                            }
273    
274                            SQLQuery q = session.createSQLQuery(sql);
275    
276                            q.addEntity("WikiPage", WikiPageImpl.class);
277    
278                            QueryPos qPos = QueryPos.getInstance(q);
279    
280                            qPos.add(groupId);
281                            qPos.add(nodeId);
282                            qPos.add(createDate);
283                            qPos.add(true);
284                            qPos.add(WorkflowConstants.STATUS_APPROVED);
285    
286                            return (List<WikiPage>)QueryUtil.list(q, getDialect(), start, end);
287                    }
288                    catch (Exception e) {
289                            throw new SystemException(e);
290                    }
291                    finally {
292                            closeSession(session);
293                    }
294            }
295    
296    }