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 int countByCreateDate(long nodeId, Date createDate, boolean before)
67          throws SystemException {
68  
69          return countByCreateDate(
70              nodeId, new Timestamp(createDate.getTime()), before);
71      }
72  
73      public int countByCreateDate(
74              long nodeId, Timestamp createDate, boolean before)
75          throws SystemException {
76  
77          Session session = null;
78  
79          try {
80              session = openSession();
81  
82              String createDateComparator = StringPool.GREATER_THAN;
83  
84              if (before) {
85                  createDateComparator = StringPool.LESS_THAN;
86              }
87  
88              String sql = CustomSQLUtil.get(COUNT_BY_CREATE_DATE);
89  
90              sql = StringUtil.replace(
91                  sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
92  
93              SQLQuery q = session.createSQLQuery(sql);
94  
95              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
96  
97              QueryPos qPos = QueryPos.getInstance(q);
98  
99              qPos.add(nodeId);
100             qPos.add(createDate);
101             qPos.add(true);
102 
103             Iterator<Long> itr = q.list().iterator();
104 
105             if (itr.hasNext()) {
106                 Long count = itr.next();
107 
108                 if (count != null) {
109                     return count.intValue();
110                 }
111             }
112 
113             return 0;
114         }
115         catch (Exception e) {
116             throw new SystemException(e);
117         }
118         finally {
119             closeSession(session);
120         }
121     }
122 
123     public WikiPage findByResourcePrimKey(long resourcePrimKey)
124         throws NoSuchPageException, SystemException {
125 
126         Session session = null;
127 
128         try {
129             session = openSession();
130 
131             String sql = CustomSQLUtil.get(FIND_BY_RESOURCE_PRIM_KEY);
132 
133             SQLQuery q = session.createSQLQuery(sql);
134 
135             q.addEntity("WikiPage", WikiPageImpl.class);
136 
137             QueryPos qPos = QueryPos.getInstance(q);
138 
139             qPos.add(resourcePrimKey);
140 
141             List<WikiPage> list = q.list();
142 
143             if (list.size() == 0) {
144                 StringBuilder sb = new StringBuilder();
145 
146                 sb.append("No WikiPage exists with the key {resourcePrimKey");
147                 sb.append(resourcePrimKey);
148                 sb.append("}");
149 
150                 throw new NoSuchPageException(sb.toString());
151             }
152             else {
153                 return list.get(0);
154             }
155         }
156         catch (NoSuchPageException nspe) {
157             throw nspe;
158         }
159         catch (Exception e) {
160             throw new SystemException(e);
161         }
162         finally {
163             closeSession(session);
164         }
165     }
166 
167     public List<WikiPage> findByCreateDate(
168             long nodeId, Date createDate, boolean before, int start, int end)
169         throws SystemException {
170 
171         return findByCreateDate(
172             nodeId, new Timestamp(createDate.getTime()), before, start, end);
173     }
174 
175     public List<WikiPage> findByCreateDate(
176             long nodeId, Timestamp createDate, boolean before, int start,
177             int end)
178         throws SystemException {
179 
180         Session session = null;
181 
182         try {
183             session = openSession();
184 
185             String createDateComparator = StringPool.GREATER_THAN;
186 
187             if (before) {
188                 createDateComparator = StringPool.LESS_THAN;
189             }
190 
191             String sql = CustomSQLUtil.get(FIND_BY_CREATE_DATE);
192 
193             sql = StringUtil.replace(
194                 sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
195 
196             SQLQuery q = session.createSQLQuery(sql);
197 
198             q.addEntity("WikiPage", WikiPageImpl.class);
199 
200             QueryPos qPos = QueryPos.getInstance(q);
201 
202             qPos.add(nodeId);
203             qPos.add(createDate);
204             qPos.add(true);
205 
206             return (List<WikiPage>)QueryUtil.list(q, getDialect(), start, end);
207         }
208         catch (Exception e) {
209             throw new SystemException(e);
210         }
211         finally {
212             closeSession(session);
213         }
214     }
215 
216     public List<WikiPage> findByNoAssets() throws SystemException {
217         Session session = null;
218 
219         try {
220             session = openSession();
221 
222             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
223 
224             SQLQuery q = session.createSQLQuery(sql);
225 
226             q.addEntity("WikiPage", WikiPageImpl.class);
227 
228             return q.list();
229         }
230         catch (Exception e) {
231             throw new SystemException(e);
232         }
233         finally {
234             closeSession(session);
235         }
236     }
237 
238 }