1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.wiki.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
29  import com.liferay.portal.spring.hibernate.HibernateUtil;
30  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
31  import com.liferay.util.dao.hibernate.QueryPos;
32  import com.liferay.util.dao.hibernate.QueryUtil;
33  
34  import java.sql.Timestamp;
35  
36  import java.util.Date;
37  import java.util.Iterator;
38  import java.util.List;
39  
40  import org.hibernate.Hibernate;
41  import org.hibernate.SQLQuery;
42  import org.hibernate.Session;
43  
44  /**
45   * <a href="WikiPageFinder.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class WikiPageFinder {
51  
52      public static String COUNT_BY_CREATEDATE =
53          WikiPageFinder.class.getName() + ".countByCreateDate";
54  
55      public static String FIND_BY_CREATEDATE =
56          WikiPageFinder.class.getName() + ".findByCreateDate";
57  
58      public static int countByCreateDate(
59              long nodeId, Date createDate, boolean before)
60          throws SystemException {
61  
62          return countByCreateDate(
63              nodeId, new Timestamp(createDate.getTime()), before);
64      }
65  
66      public static int countByCreateDate(
67              long nodeId, Timestamp createDate, boolean before)
68          throws SystemException {
69  
70          Session session = null;
71  
72          try {
73              session = HibernateUtil.openSession();
74  
75              String createDateComparator = StringPool.GREATER_THAN;
76  
77              if (before) {
78                  createDateComparator = StringPool.LESS_THAN;
79              }
80  
81              String sql = CustomSQLUtil.get(COUNT_BY_CREATEDATE);
82  
83              sql = StringUtil.replace(
84                  sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
85  
86              SQLQuery q = session.createSQLQuery(sql);
87  
88              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
89  
90              QueryPos qPos = QueryPos.getInstance(q);
91  
92              qPos.add(nodeId);
93              qPos.add(createDate);
94              qPos.add(true);
95  
96              Iterator itr = q.list().iterator();
97  
98              if (itr.hasNext()) {
99                  Long count = (Long)itr.next();
100 
101                 if (count != null) {
102                     return count.intValue();
103                 }
104             }
105 
106             return 0;
107         }
108         catch (Exception e) {
109             throw new SystemException(e);
110         }
111         finally {
112             HibernateUtil.closeSession(session);
113         }
114     }
115 
116     public static List findByCreateDate(
117             long nodeId, Date createDate, boolean before, int begin, int end)
118         throws SystemException {
119 
120         return findByCreateDate(
121             nodeId, new Timestamp(createDate.getTime()), before, begin, end);
122     }
123 
124     public static List findByCreateDate(
125             long nodeId, Timestamp createDate, boolean before, int begin,
126             int end)
127         throws SystemException {
128 
129         Session session = null;
130 
131         try {
132             session = HibernateUtil.openSession();
133 
134             String createDateComparator = StringPool.GREATER_THAN;
135 
136             if (before) {
137                 createDateComparator = StringPool.LESS_THAN;
138             }
139 
140             String sql = CustomSQLUtil.get(FIND_BY_CREATEDATE);
141 
142             sql = StringUtil.replace(
143                 sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
144 
145             SQLQuery q = session.createSQLQuery(sql);
146 
147             q.addEntity("WikiPage", WikiPageImpl.class);
148 
149             QueryPos qPos = QueryPos.getInstance(q);
150 
151             qPos.add(nodeId);
152             qPos.add(createDate);
153             qPos.add(true);
154 
155             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
156         }
157         catch (Exception e) {
158             throw new SystemException(e);
159         }
160         finally {
161             HibernateUtil.closeSession(session);
162         }
163     }
164 
165 }