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