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.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.portlet.wiki.NoSuchPageException;
029    import com.liferay.portlet.wiki.model.WikiPage;
030    import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
031    import com.liferay.util.dao.orm.CustomSQLUtil;
032    
033    import java.sql.Timestamp;
034    
035    import java.util.Date;
036    import java.util.Iterator;
037    import java.util.List;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class WikiPageFinderImpl
043            extends BasePersistenceImpl<WikiPage> implements WikiPageFinder {
044    
045            public static final String COUNT_BY_CREATE_DATE =
046                    WikiPageFinder.class.getName() + ".countByCreateDate";
047    
048            public static final String FIND_BY_RESOURCE_PRIM_KEY =
049                    WikiPageFinder.class.getName() + ".findByResourcePrimKey";
050    
051            public static final String FIND_BY_CREATE_DATE =
052                    WikiPageFinder.class.getName() + ".findByCreateDate";
053    
054            public static final String FIND_BY_NO_ASSETS =
055                    WikiPageFinder.class.getName() + ".findByNoAssets";
056    
057            public int countByCreateDate(long nodeId, Date createDate, boolean before)
058                    throws SystemException {
059    
060                    return countByCreateDate(
061                            nodeId, new Timestamp(createDate.getTime()), before);
062            }
063    
064            public int countByCreateDate(
065                            long nodeId, Timestamp createDate, boolean before)
066                    throws SystemException {
067    
068                    Session session = null;
069    
070                    try {
071                            session = openSession();
072    
073                            String createDateComparator = StringPool.GREATER_THAN;
074    
075                            if (before) {
076                                    createDateComparator = StringPool.LESS_THAN;
077                            }
078    
079                            String sql = CustomSQLUtil.get(COUNT_BY_CREATE_DATE);
080    
081                            sql = StringUtil.replace(
082                                    sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
083    
084                            SQLQuery q = session.createSQLQuery(sql);
085    
086                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
087    
088                            QueryPos qPos = QueryPos.getInstance(q);
089    
090                            qPos.add(nodeId);
091                            qPos.add(createDate);
092                            qPos.add(true);
093                            qPos.add(WorkflowConstants.STATUS_APPROVED);
094    
095                            Iterator<Long> itr = q.iterate();
096    
097                            if (itr.hasNext()) {
098                                    Long count = itr.next();
099    
100                                    if (count != null) {
101                                            return count.intValue();
102                                    }
103                            }
104    
105                            return 0;
106                    }
107                    catch (Exception e) {
108                            throw new SystemException(e);
109                    }
110                    finally {
111                            closeSession(session);
112                    }
113            }
114    
115            public WikiPage findByResourcePrimKey(long resourcePrimKey)
116                    throws NoSuchPageException, SystemException {
117    
118                    Session session = null;
119    
120                    try {
121                            session = openSession();
122    
123                            String sql = CustomSQLUtil.get(FIND_BY_RESOURCE_PRIM_KEY);
124    
125                            SQLQuery q = session.createSQLQuery(sql);
126    
127                            q.addEntity("WikiPage", WikiPageImpl.class);
128    
129                            QueryPos qPos = QueryPos.getInstance(q);
130    
131                            qPos.add(resourcePrimKey);
132    
133                            List<WikiPage> pages = q.list();
134    
135                            if (!pages.isEmpty()) {
136                                    return pages.get(0);
137                            }
138                    }
139                    catch (Exception e) {
140                            throw new SystemException(e);
141                    }
142                    finally {
143                            closeSession(session);
144                    }
145    
146                    StringBundler sb = new StringBundler(3);
147    
148                    sb.append("No WikiPage exists with the key {resourcePrimKey");
149                    sb.append(resourcePrimKey);
150                    sb.append("}");
151    
152                    throw new NoSuchPageException(sb.toString());
153            }
154    
155            public List<WikiPage> findByCreateDate(
156                            long nodeId, Date createDate, boolean before, int start, int end)
157                    throws SystemException {
158    
159                    return findByCreateDate(
160                            nodeId, new Timestamp(createDate.getTime()), before, start, end);
161            }
162    
163            public List<WikiPage> findByCreateDate(
164                            long nodeId, Timestamp createDate, boolean before, int start,
165                            int end)
166                    throws SystemException {
167    
168                    Session session = null;
169    
170                    try {
171                            session = openSession();
172    
173                            String createDateComparator = StringPool.GREATER_THAN;
174    
175                            if (before) {
176                                    createDateComparator = StringPool.LESS_THAN;
177                            }
178    
179                            String sql = CustomSQLUtil.get(FIND_BY_CREATE_DATE);
180    
181                            sql = StringUtil.replace(
182                                    sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
183    
184                            SQLQuery q = session.createSQLQuery(sql);
185    
186                            q.addEntity("WikiPage", WikiPageImpl.class);
187    
188                            QueryPos qPos = QueryPos.getInstance(q);
189    
190                            qPos.add(nodeId);
191                            qPos.add(createDate);
192                            qPos.add(true);
193                            qPos.add(WorkflowConstants.STATUS_APPROVED);
194    
195                            return (List<WikiPage>)QueryUtil.list(q, getDialect(), start, end);
196                    }
197                    catch (Exception e) {
198                            throw new SystemException(e);
199                    }
200                    finally {
201                            closeSession(session);
202                    }
203            }
204    
205            public List<WikiPage> findByNoAssets() throws SystemException {
206                    Session session = null;
207    
208                    try {
209                            session = openSession();
210    
211                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
212    
213                            SQLQuery q = session.createSQLQuery(sql);
214    
215                            q.addEntity("WikiPage", WikiPageImpl.class);
216    
217                            return q.list(true);
218                    }
219                    catch (Exception e) {
220                            throw new SystemException(e);
221                    }
222                    finally {
223                            closeSession(session);
224                    }
225            }
226    
227    }