001    /**
002     * Copyright (c) 2000-2010 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 String COUNT_BY_CREATE_DATE =
046                    WikiPageFinder.class.getName() + ".countByCreateDate";
047    
048            public static String FIND_BY_RESOURCE_PRIM_KEY =
049                    WikiPageFinder.class.getName() + ".findByResourcePrimKey";
050    
051            public static String FIND_BY_CREATE_DATE =
052                    WikiPageFinder.class.getName() + ".findByCreateDate";
053    
054            public static 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.list().iterator();
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> list = q.list();
134    
135                            if (list.size() == 0) {
136                                    StringBundler sb = new StringBundler(3);
137    
138                                    sb.append("No WikiPage exists with the key {resourcePrimKey");
139                                    sb.append(resourcePrimKey);
140                                    sb.append("}");
141    
142                                    throw new NoSuchPageException(sb.toString());
143                            }
144                            else {
145                                    return list.get(0);
146                            }
147                    }
148                    catch (NoSuchPageException nspe) {
149                            throw nspe;
150                    }
151                    catch (Exception e) {
152                            throw new SystemException(e);
153                    }
154                    finally {
155                            closeSession(session);
156                    }
157            }
158    
159            public List<WikiPage> findByCreateDate(
160                            long nodeId, Date createDate, boolean before, int start, int end)
161                    throws SystemException {
162    
163                    return findByCreateDate(
164                            nodeId, new Timestamp(createDate.getTime()), before, start, end);
165            }
166    
167            public List<WikiPage> findByCreateDate(
168                            long nodeId, Timestamp createDate, boolean before, int start,
169                            int end)
170                    throws SystemException {
171    
172                    Session session = null;
173    
174                    try {
175                            session = openSession();
176    
177                            String createDateComparator = StringPool.GREATER_THAN;
178    
179                            if (before) {
180                                    createDateComparator = StringPool.LESS_THAN;
181                            }
182    
183                            String sql = CustomSQLUtil.get(FIND_BY_CREATE_DATE);
184    
185                            sql = StringUtil.replace(
186                                    sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
187    
188                            SQLQuery q = session.createSQLQuery(sql);
189    
190                            q.addEntity("WikiPage", WikiPageImpl.class);
191    
192                            QueryPos qPos = QueryPos.getInstance(q);
193    
194                            qPos.add(nodeId);
195                            qPos.add(createDate);
196                            qPos.add(true);
197                            qPos.add(WorkflowConstants.STATUS_APPROVED);
198    
199                            return (List<WikiPage>)QueryUtil.list(q, getDialect(), start, end);
200                    }
201                    catch (Exception e) {
202                            throw new SystemException(e);
203                    }
204                    finally {
205                            closeSession(session);
206                    }
207            }
208    
209            public List<WikiPage> findByNoAssets() throws SystemException {
210                    Session session = null;
211    
212                    try {
213                            session = openSession();
214    
215                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
216    
217                            SQLQuery q = session.createSQLQuery(sql);
218    
219                            q.addEntity("WikiPage", WikiPageImpl.class);
220    
221                            return q.list();
222                    }
223                    catch (Exception e) {
224                            throw new SystemException(e);
225                    }
226                    finally {
227                            closeSession(session);
228                    }
229            }
230    
231    }