1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.wiki.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.QueryPos;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.kernel.dao.orm.SQLQuery;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.dao.orm.Type;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
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.orm.CustomSQLUtil;
35  
36  import java.sql.Timestamp;
37  
38  import java.util.Date;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * <a href="WikiPageFinderImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class WikiPageFinderImpl
49      extends BasePersistenceImpl implements WikiPageFinder {
50  
51      public static String COUNT_BY_CREATE_DATE =
52          WikiPageFinder.class.getName() + ".countByCreateDate";
53  
54      public static String FIND_BY_RESOURCE_PRIM_KEY =
55          WikiPageFinder.class.getName() + ".findByResourcePrimKey";
56  
57      public static String FIND_BY_CREATE_DATE =
58          WikiPageFinder.class.getName() + ".findByCreateDate";
59  
60      public static String FIND_BY_NO_ASSETS =
61          WikiPageFinder.class.getName() + ".findByNoAssets";
62  
63      public int countByCreateDate(long nodeId, Date createDate, boolean before)
64          throws SystemException {
65  
66          return countByCreateDate(
67              nodeId, new Timestamp(createDate.getTime()), before);
68      }
69  
70      public int countByCreateDate(
71              long nodeId, Timestamp createDate, boolean before)
72          throws SystemException {
73  
74          Session session = null;
75  
76          try {
77              session = openSession();
78  
79              String createDateComparator = StringPool.GREATER_THAN;
80  
81              if (before) {
82                  createDateComparator = StringPool.LESS_THAN;
83              }
84  
85              String sql = CustomSQLUtil.get(COUNT_BY_CREATE_DATE);
86  
87              sql = StringUtil.replace(
88                  sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
89  
90              SQLQuery q = session.createSQLQuery(sql);
91  
92              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
93  
94              QueryPos qPos = QueryPos.getInstance(q);
95  
96              qPos.add(nodeId);
97              qPos.add(createDate);
98              qPos.add(true);
99  
100             Iterator<Long> itr = q.list().iterator();
101 
102             if (itr.hasNext()) {
103                 Long count = itr.next();
104 
105                 if (count != null) {
106                     return count.intValue();
107                 }
108             }
109 
110             return 0;
111         }
112         catch (Exception e) {
113             throw new SystemException(e);
114         }
115         finally {
116             closeSession(session);
117         }
118     }
119 
120     public WikiPage findByResourcePrimKey(long resourcePrimKey)
121         throws NoSuchPageException, SystemException {
122 
123         Session session = null;
124 
125         try {
126             session = openSession();
127 
128             String sql = CustomSQLUtil.get(FIND_BY_RESOURCE_PRIM_KEY);
129 
130             SQLQuery q = session.createSQLQuery(sql);
131 
132             q.addEntity("WikiPage", WikiPageImpl.class);
133 
134             QueryPos qPos = QueryPos.getInstance(q);
135 
136             qPos.add(resourcePrimKey);
137 
138             List<WikiPage> list = q.list();
139 
140             if (list.size() == 0) {
141                 StringBuilder sb = new StringBuilder();
142 
143                 sb.append("No WikiPage exists with the key {resourcePrimKey");
144                 sb.append(resourcePrimKey);
145                 sb.append("}");
146 
147                 throw new NoSuchPageException(sb.toString());
148             }
149             else {
150                 return list.get(0);
151             }
152         }
153         catch (NoSuchPageException nspe) {
154             throw nspe;
155         }
156         catch (Exception e) {
157             throw new SystemException(e);
158         }
159         finally {
160             closeSession(session);
161         }
162     }
163 
164     public List<WikiPage> findByCreateDate(
165             long nodeId, Date createDate, boolean before, int start, int end)
166         throws SystemException {
167 
168         return findByCreateDate(
169             nodeId, new Timestamp(createDate.getTime()), before, start, end);
170     }
171 
172     public List<WikiPage> findByCreateDate(
173             long nodeId, Timestamp createDate, boolean before, int start,
174             int end)
175         throws SystemException {
176 
177         Session session = null;
178 
179         try {
180             session = openSession();
181 
182             String createDateComparator = StringPool.GREATER_THAN;
183 
184             if (before) {
185                 createDateComparator = StringPool.LESS_THAN;
186             }
187 
188             String sql = CustomSQLUtil.get(FIND_BY_CREATE_DATE);
189 
190             sql = StringUtil.replace(
191                 sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
192 
193             SQLQuery q = session.createSQLQuery(sql);
194 
195             q.addEntity("WikiPage", WikiPageImpl.class);
196 
197             QueryPos qPos = QueryPos.getInstance(q);
198 
199             qPos.add(nodeId);
200             qPos.add(createDate);
201             qPos.add(true);
202 
203             return (List<WikiPage>)QueryUtil.list(q, getDialect(), start, end);
204         }
205         catch (Exception e) {
206             throw new SystemException(e);
207         }
208         finally {
209             closeSession(session);
210         }
211     }
212 
213     public List<WikiPage> findByNoAssets() throws SystemException {
214         Session session = null;
215 
216         try {
217             session = openSession();
218 
219             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
220 
221             SQLQuery q = session.createSQLQuery(sql);
222 
223             q.addEntity("WikiPage", WikiPageImpl.class);
224 
225             return q.list();
226         }
227         catch (Exception e) {
228             throw new SystemException(e);
229         }
230         finally {
231             closeSession(session);
232         }
233     }
234 
235 }