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