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