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