1   /**
2    * Copyright (c) 2000-2008 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.StringMaker;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
30  import com.liferay.portal.spring.hibernate.HibernateUtil;
31  import com.liferay.portlet.wiki.NoSuchPageException;
32  import com.liferay.portlet.wiki.model.WikiPage;
33  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
34  import com.liferay.util.dao.hibernate.QueryPos;
35  import com.liferay.util.dao.hibernate.QueryUtil;
36  
37  import java.sql.Timestamp;
38  
39  import java.util.Date;
40  import java.util.Iterator;
41  import java.util.List;
42  
43  import org.hibernate.Hibernate;
44  import org.hibernate.SQLQuery;
45  import org.hibernate.Session;
46  
47  /**
48   * <a href="WikiPageFinderImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class WikiPageFinderImpl implements WikiPageFinder {
54  
55      public static String COUNT_BY_CREATEDATE =
56          WikiPageFinder.class.getName() + ".countByCreateDate";
57  
58      public static String FIND_BY_CREATEDATE =
59          WikiPageFinder.class.getName() + ".findByCreateDate";
60  
61      public static String FIND_BY_NO_ASSETS =
62          WikiPageFinder.class.getName() + ".findByNoAssets";
63  
64      public static String FIND_BY_UUID_G =
65          WikiPageFinder.class.getName() + ".findByUuid_G";
66  
67      public int countByCreateDate(long nodeId, Date createDate, boolean before)
68          throws SystemException {
69  
70          return countByCreateDate(
71              nodeId, new Timestamp(createDate.getTime()), before);
72      }
73  
74      public int countByCreateDate(
75              long nodeId, Timestamp createDate, boolean before)
76          throws SystemException {
77  
78          Session session = null;
79  
80          try {
81              session = HibernateUtil.openSession();
82  
83              String createDateComparator = StringPool.GREATER_THAN;
84  
85              if (before) {
86                  createDateComparator = StringPool.LESS_THAN;
87              }
88  
89              String sql = CustomSQLUtil.get(COUNT_BY_CREATEDATE);
90  
91              sql = StringUtil.replace(
92                  sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
93  
94              SQLQuery q = session.createSQLQuery(sql);
95  
96              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
97  
98              QueryPos qPos = QueryPos.getInstance(q);
99  
100             qPos.add(nodeId);
101             qPos.add(createDate);
102             qPos.add(true);
103 
104             Iterator<Long> itr = q.list().iterator();
105 
106             if (itr.hasNext()) {
107                 Long count = itr.next();
108 
109                 if (count != null) {
110                     return count.intValue();
111                 }
112             }
113 
114             return 0;
115         }
116         catch (Exception e) {
117             throw new SystemException(e);
118         }
119         finally {
120             HibernateUtil.closeSession(session);
121         }
122     }
123 
124     public List<WikiPage> findByCreateDate(
125             long nodeId, Date createDate, boolean before, int begin, int end)
126         throws SystemException {
127 
128         return findByCreateDate(
129             nodeId, new Timestamp(createDate.getTime()), before, begin, end);
130     }
131 
132     public List<WikiPage> findByCreateDate(
133             long nodeId, Timestamp createDate, boolean before, int begin,
134             int end)
135         throws SystemException {
136 
137         Session session = null;
138 
139         try {
140             session = HibernateUtil.openSession();
141 
142             String createDateComparator = StringPool.GREATER_THAN;
143 
144             if (before) {
145                 createDateComparator = StringPool.LESS_THAN;
146             }
147 
148             String sql = CustomSQLUtil.get(FIND_BY_CREATEDATE);
149 
150             sql = StringUtil.replace(
151                 sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
152 
153             SQLQuery q = session.createSQLQuery(sql);
154 
155             q.addEntity("WikiPage", WikiPageImpl.class);
156 
157             QueryPos qPos = QueryPos.getInstance(q);
158 
159             qPos.add(nodeId);
160             qPos.add(createDate);
161             qPos.add(true);
162 
163             return (List<WikiPage>)QueryUtil.list(
164                 q, HibernateUtil.getDialect(), begin, end);
165         }
166         catch (Exception e) {
167             throw new SystemException(e);
168         }
169         finally {
170             HibernateUtil.closeSession(session);
171         }
172     }
173 
174     public List<WikiPage> findByNoAssets() throws SystemException {
175         Session session = null;
176 
177         try {
178             session = HibernateUtil.openSession();
179 
180             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
181 
182             SQLQuery q = session.createSQLQuery(sql);
183 
184             q.addEntity("WikiPage", WikiPageImpl.class);
185 
186             return q.list();
187         }
188         catch (Exception e) {
189             throw new SystemException(e);
190         }
191         finally {
192             HibernateUtil.closeSession(session);
193         }
194     }
195 
196     public WikiPage findByUuid_G(String uuid, long groupId)
197         throws NoSuchPageException, SystemException {
198 
199         Session session = null;
200 
201         try {
202             session = HibernateUtil.openSession();
203 
204             String sql = CustomSQLUtil.get(FIND_BY_UUID_G);
205 
206             SQLQuery q = session.createSQLQuery(sql);
207 
208             q.addEntity("WikiPage", WikiPageImpl.class);
209 
210             QueryPos qPos = QueryPos.getInstance(q);
211 
212             qPos.add(uuid);
213             qPos.add(groupId);
214 
215             List<WikiPage> list = q.list();
216 
217             if (list.size() == 0) {
218                 StringMaker sm = new StringMaker();
219 
220                 sm.append("No WikiPage exists with the key {uuid=");
221                 sm.append(uuid);
222                 sm.append(", groupId=");
223                 sm.append(groupId);
224                 sm.append("}");
225 
226                 throw new NoSuchPageException(sm.toString());
227             }
228             else {
229                 return list.get(0);
230             }
231         }
232         catch (NoSuchPageException nspe) {
233             throw nspe;
234         }
235         catch (Exception e) {
236             throw new SystemException(e);
237         }
238         finally {
239             HibernateUtil.closeSession(session);
240         }
241     }
242 
243 }