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