1   /**
2    * Copyright (c) 2000-2008 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_CREATE_DATE =
58          WikiPageFinder.class.getName() + ".findByCreateDate";
59  
60      public static String FIND_BY_NO_ASSETS =
61          WikiPageFinder.class.getName() + ".findByNoAssets";
62  
63      public static String FIND_BY_UUID_G =
64          WikiPageFinder.class.getName() + ".findByUuid_G";
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 List<WikiPage> findByCreateDate(
124             long nodeId, Date createDate, boolean before, int start, int end)
125         throws SystemException {
126 
127         return findByCreateDate(
128             nodeId, new Timestamp(createDate.getTime()), before, start, end);
129     }
130 
131     public List<WikiPage> findByCreateDate(
132             long nodeId, Timestamp createDate, boolean before, int start,
133             int end)
134         throws SystemException {
135 
136         Session session = null;
137 
138         try {
139             session = openSession();
140 
141             String createDateComparator = StringPool.GREATER_THAN;
142 
143             if (before) {
144                 createDateComparator = StringPool.LESS_THAN;
145             }
146 
147             String sql = CustomSQLUtil.get(FIND_BY_CREATE_DATE);
148 
149             sql = StringUtil.replace(
150                 sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);
151 
152             SQLQuery q = session.createSQLQuery(sql);
153 
154             q.addEntity("WikiPage", WikiPageImpl.class);
155 
156             QueryPos qPos = QueryPos.getInstance(q);
157 
158             qPos.add(nodeId);
159             qPos.add(createDate);
160             qPos.add(true);
161 
162             return (List<WikiPage>)QueryUtil.list(q, getDialect(), start, end);
163         }
164         catch (Exception e) {
165             throw new SystemException(e);
166         }
167         finally {
168             closeSession(session);
169         }
170     }
171 
172     public List<WikiPage> findByNoAssets() throws SystemException {
173         Session session = null;
174 
175         try {
176             session = openSession();
177 
178             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
179 
180             SQLQuery q = session.createSQLQuery(sql);
181 
182             q.addEntity("WikiPage", WikiPageImpl.class);
183 
184             return q.list();
185         }
186         catch (Exception e) {
187             throw new SystemException(e);
188         }
189         finally {
190             closeSession(session);
191         }
192     }
193 
194     public WikiPage findByUuid_G(String uuid, long groupId)
195         throws NoSuchPageException, SystemException {
196 
197         Session session = null;
198 
199         try {
200             session = openSession();
201 
202             String sql = CustomSQLUtil.get(FIND_BY_UUID_G);
203 
204             SQLQuery q = session.createSQLQuery(sql);
205 
206             q.addEntity("WikiPage", WikiPageImpl.class);
207 
208             QueryPos qPos = QueryPos.getInstance(q);
209 
210             qPos.add(uuid);
211             qPos.add(groupId);
212 
213             List<WikiPage> list = q.list();
214 
215             if (list.size() == 0) {
216                 StringBuilder sb = new StringBuilder();
217 
218                 sb.append("No WikiPage exists with the key {uuid=");
219                 sb.append(uuid);
220                 sb.append(", groupId=");
221                 sb.append(groupId);
222                 sb.append("}");
223 
224                 throw new NoSuchPageException(sb.toString());
225             }
226             else {
227                 return list.get(0);
228             }
229         }
230         catch (NoSuchPageException nspe) {
231             throw nspe;
232         }
233         catch (Exception e) {
234             throw new SystemException(e);
235         }
236         finally {
237             closeSession(session);
238         }
239     }
240 
241 }