1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class WikiPageFinderImpl
51      extends BasePersistenceImpl implements WikiPageFinder {
52  
53      public static String COUNT_BY_CREATE_DATE =
54          WikiPageFinder.class.getName() + ".countByCreateDate";
55  
56      public static String FIND_BY_RESOURCE_PRIM_KEY =
57          WikiPageFinder.class.getName() + ".findByResourcePrimKey";
58  
59      public static String FIND_BY_CREATE_DATE =
60          WikiPageFinder.class.getName() + ".findByCreateDate";
61  
62      public static String FIND_BY_NO_ASSETS =
63          WikiPageFinder.class.getName() + ".findByNoAssets";
64  
65      public int countByCreateDate(long nodeId, Date createDate, boolean before)
66          throws SystemException {
67  
68          return countByCreateDate(
69              nodeId, new Timestamp(createDate.getTime()), before);
70      }
71  
72      public int countByCreateDate(
73              long nodeId, Timestamp createDate, boolean before)
74          throws SystemException {
75  
76          Session session = null;
77  
78          try {
79              session = openSession();
80  
81              String createDateComparator = StringPool.GREATER_THAN;
82  
83              if (before) {
84                  createDateComparator = StringPool.LESS_THAN;
85              }
86  
87              String sql = CustomSQLUtil.get(COUNT_BY_CREATE_DATE);
88  
89              sql = StringUtil.replace(
90                  sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
91  
92              SQLQuery q = session.createSQLQuery(sql);
93  
94              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
95  
96              QueryPos qPos = QueryPos.getInstance(q);
97  
98              qPos.add(nodeId);
99              qPos.add(createDate);
100             qPos.add(true);
101 
102             Iterator<Long> itr = q.list().iterator();
103 
104             if (itr.hasNext()) {
105                 Long count = itr.next();
106 
107                 if (count != null) {
108                     return count.intValue();
109                 }
110             }
111 
112             return 0;
113         }
114         catch (Exception e) {
115             throw new SystemException(e);
116         }
117         finally {
118             closeSession(session);
119         }
120     }
121 
122     public WikiPage findByResourcePrimKey(long resourcePrimKey)
123         throws NoSuchPageException, SystemException {
124 
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             String sql = CustomSQLUtil.get(FIND_BY_RESOURCE_PRIM_KEY);
131 
132             SQLQuery q = session.createSQLQuery(sql);
133 
134             q.addEntity("WikiPage", WikiPageImpl.class);
135 
136             QueryPos qPos = QueryPos.getInstance(q);
137 
138             qPos.add(resourcePrimKey);
139 
140             List<WikiPage> list = q.list();
141 
142             if (list.size() == 0) {
143                 StringBuilder sb = new StringBuilder();
144 
145                 sb.append("No WikiPage exists with the key {resourcePrimKey");
146                 sb.append(resourcePrimKey);
147                 sb.append("}");
148 
149                 throw new NoSuchPageException(sb.toString());
150             }
151             else {
152                 return list.get(0);
153             }
154         }
155         catch (NoSuchPageException nspe) {
156             throw nspe;
157         }
158         catch (Exception e) {
159             throw new SystemException(e);
160         }
161         finally {
162             closeSession(session);
163         }
164     }
165 
166     public List<WikiPage> findByCreateDate(
167             long nodeId, Date createDate, boolean before, int start, int end)
168         throws SystemException {
169 
170         return findByCreateDate(
171             nodeId, new Timestamp(createDate.getTime()), before, start, end);
172     }
173 
174     public List<WikiPage> findByCreateDate(
175             long nodeId, Timestamp createDate, boolean before, int start,
176             int end)
177         throws SystemException {
178 
179         Session session = null;
180 
181         try {
182             session = openSession();
183 
184             String createDateComparator = StringPool.GREATER_THAN;
185 
186             if (before) {
187                 createDateComparator = StringPool.LESS_THAN;
188             }
189 
190             String sql = CustomSQLUtil.get(FIND_BY_CREATE_DATE);
191 
192             sql = StringUtil.replace(
193                 sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
194 
195             SQLQuery q = session.createSQLQuery(sql);
196 
197             q.addEntity("WikiPage", WikiPageImpl.class);
198 
199             QueryPos qPos = QueryPos.getInstance(q);
200 
201             qPos.add(nodeId);
202             qPos.add(createDate);
203             qPos.add(true);
204 
205             return (List<WikiPage>)QueryUtil.list(q, getDialect(), start, end);
206         }
207         catch (Exception e) {
208             throw new SystemException(e);
209         }
210         finally {
211             closeSession(session);
212         }
213     }
214 
215     public List<WikiPage> findByNoAssets() throws SystemException {
216         Session session = null;
217 
218         try {
219             session = openSession();
220 
221             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
222 
223             SQLQuery q = session.createSQLQuery(sql);
224 
225             q.addEntity("WikiPage", WikiPageImpl.class);
226 
227             return q.list();
228         }
229         catch (Exception e) {
230             throw new SystemException(e);
231         }
232         finally {
233             closeSession(session);
234         }
235     }
236 
237 }