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