1   /**
2    * Copyright (c) 2000-2007 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.DynamicQuery;
27  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.service.persistence.BasePersistence;
32  import com.liferay.portal.spring.hibernate.FinderCache;
33  import com.liferay.portal.spring.hibernate.HibernateUtil;
34  
35  import com.liferay.portlet.wiki.NoSuchPageResourceException;
36  import com.liferay.portlet.wiki.model.WikiPageResource;
37  import com.liferay.portlet.wiki.model.impl.WikiPageResourceImpl;
38  
39  import com.liferay.util.dao.hibernate.QueryUtil;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  import org.hibernate.Query;
45  import org.hibernate.Session;
46  
47  import java.util.Collections;
48  import java.util.Iterator;
49  import java.util.List;
50  
51  /**
52   * <a href="WikiPageResourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class WikiPageResourcePersistenceImpl extends BasePersistence
58      implements WikiPageResourcePersistence {
59      public WikiPageResource create(long resourcePrimKey) {
60          WikiPageResource wikiPageResource = new WikiPageResourceImpl();
61          wikiPageResource.setNew(true);
62          wikiPageResource.setPrimaryKey(resourcePrimKey);
63  
64          return wikiPageResource;
65      }
66  
67      public WikiPageResource remove(long resourcePrimKey)
68          throws NoSuchPageResourceException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              WikiPageResource wikiPageResource = (WikiPageResource)session.get(WikiPageResourceImpl.class,
75                      new Long(resourcePrimKey));
76  
77              if (wikiPageResource == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn(
80                          "No WikiPageResource exists with the primary key " +
81                          resourcePrimKey);
82                  }
83  
84                  throw new NoSuchPageResourceException(
85                      "No WikiPageResource exists with the primary key " +
86                      resourcePrimKey);
87              }
88  
89              return remove(wikiPageResource);
90          }
91          catch (NoSuchPageResourceException nsee) {
92              throw nsee;
93          }
94          catch (Exception e) {
95              throw HibernateUtil.processException(e);
96          }
97          finally {
98              closeSession(session);
99          }
100     }
101 
102     public WikiPageResource remove(WikiPageResource wikiPageResource)
103         throws SystemException {
104         Session session = null;
105 
106         try {
107             session = openSession();
108             session.delete(wikiPageResource);
109             session.flush();
110 
111             return wikiPageResource;
112         }
113         catch (Exception e) {
114             throw HibernateUtil.processException(e);
115         }
116         finally {
117             closeSession(session);
118             FinderCache.clearCache(WikiPageResource.class.getName());
119         }
120     }
121 
122     public WikiPageResource update(
123         com.liferay.portlet.wiki.model.WikiPageResource wikiPageResource)
124         throws SystemException {
125         return update(wikiPageResource, false);
126     }
127 
128     public WikiPageResource update(
129         com.liferay.portlet.wiki.model.WikiPageResource wikiPageResource,
130         boolean merge) throws SystemException {
131         Session session = null;
132 
133         try {
134             session = openSession();
135 
136             if (merge) {
137                 session.merge(wikiPageResource);
138             }
139             else {
140                 if (wikiPageResource.isNew()) {
141                     session.save(wikiPageResource);
142                 }
143             }
144 
145             session.flush();
146             wikiPageResource.setNew(false);
147 
148             return wikiPageResource;
149         }
150         catch (Exception e) {
151             throw HibernateUtil.processException(e);
152         }
153         finally {
154             closeSession(session);
155             FinderCache.clearCache(WikiPageResource.class.getName());
156         }
157     }
158 
159     public WikiPageResource findByPrimaryKey(long resourcePrimKey)
160         throws NoSuchPageResourceException, SystemException {
161         WikiPageResource wikiPageResource = fetchByPrimaryKey(resourcePrimKey);
162 
163         if (wikiPageResource == null) {
164             if (_log.isWarnEnabled()) {
165                 _log.warn("No WikiPageResource exists with the primary key " +
166                     resourcePrimKey);
167             }
168 
169             throw new NoSuchPageResourceException(
170                 "No WikiPageResource exists with the primary key " +
171                 resourcePrimKey);
172         }
173 
174         return wikiPageResource;
175     }
176 
177     public WikiPageResource fetchByPrimaryKey(long resourcePrimKey)
178         throws SystemException {
179         Session session = null;
180 
181         try {
182             session = openSession();
183 
184             return (WikiPageResource)session.get(WikiPageResourceImpl.class,
185                 new Long(resourcePrimKey));
186         }
187         catch (Exception e) {
188             throw HibernateUtil.processException(e);
189         }
190         finally {
191             closeSession(session);
192         }
193     }
194 
195     public WikiPageResource findByN_T(long nodeId, String title)
196         throws NoSuchPageResourceException, SystemException {
197         WikiPageResource wikiPageResource = fetchByN_T(nodeId, title);
198 
199         if (wikiPageResource == null) {
200             StringMaker msg = new StringMaker();
201             msg.append("No WikiPageResource exists with the key ");
202             msg.append(StringPool.OPEN_CURLY_BRACE);
203             msg.append("nodeId=");
204             msg.append(nodeId);
205             msg.append(", ");
206             msg.append("title=");
207             msg.append(title);
208             msg.append(StringPool.CLOSE_CURLY_BRACE);
209 
210             if (_log.isWarnEnabled()) {
211                 _log.warn(msg.toString());
212             }
213 
214             throw new NoSuchPageResourceException(msg.toString());
215         }
216 
217         return wikiPageResource;
218     }
219 
220     public WikiPageResource fetchByN_T(long nodeId, String title)
221         throws SystemException {
222         String finderClassName = WikiPageResource.class.getName();
223         String finderMethodName = "fetchByN_T";
224         String[] finderParams = new String[] {
225                 Long.class.getName(), String.class.getName()
226             };
227         Object[] finderArgs = new Object[] { new Long(nodeId), title };
228         Object result = FinderCache.getResult(finderClassName,
229                 finderMethodName, finderParams, finderArgs, getSessionFactory());
230 
231         if (result == null) {
232             Session session = null;
233 
234             try {
235                 session = openSession();
236 
237                 StringMaker query = new StringMaker();
238                 query.append(
239                     "FROM com.liferay.portlet.wiki.model.WikiPageResource WHERE ");
240                 query.append("nodeId = ?");
241                 query.append(" AND ");
242 
243                 if (title == null) {
244                     query.append("title IS NULL");
245                 }
246                 else {
247                     query.append("title = ?");
248                 }
249 
250                 query.append(" ");
251 
252                 Query q = session.createQuery(query.toString());
253                 int queryPos = 0;
254                 q.setLong(queryPos++, nodeId);
255 
256                 if (title != null) {
257                     q.setString(queryPos++, title);
258                 }
259 
260                 List list = q.list();
261                 FinderCache.putResult(finderClassName, finderMethodName,
262                     finderParams, finderArgs, list);
263 
264                 if (list.size() == 0) {
265                     return null;
266                 }
267                 else {
268                     return (WikiPageResource)list.get(0);
269                 }
270             }
271             catch (Exception e) {
272                 throw HibernateUtil.processException(e);
273             }
274             finally {
275                 closeSession(session);
276             }
277         }
278         else {
279             List list = (List)result;
280 
281             if (list.size() == 0) {
282                 return null;
283             }
284             else {
285                 return (WikiPageResource)list.get(0);
286             }
287         }
288     }
289 
290     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
291         throws SystemException {
292         Session session = null;
293 
294         try {
295             session = openSession();
296 
297             DynamicQuery query = queryInitializer.initialize(session);
298 
299             return query.list();
300         }
301         catch (Exception e) {
302             throw HibernateUtil.processException(e);
303         }
304         finally {
305             closeSession(session);
306         }
307     }
308 
309     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
310         int begin, int end) throws SystemException {
311         Session session = null;
312 
313         try {
314             session = openSession();
315 
316             DynamicQuery query = queryInitializer.initialize(session);
317             query.setLimit(begin, end);
318 
319             return query.list();
320         }
321         catch (Exception e) {
322             throw HibernateUtil.processException(e);
323         }
324         finally {
325             closeSession(session);
326         }
327     }
328 
329     public List findAll() throws SystemException {
330         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
331     }
332 
333     public List findAll(int begin, int end) throws SystemException {
334         return findAll(begin, end, null);
335     }
336 
337     public List findAll(int begin, int end, OrderByComparator obc)
338         throws SystemException {
339         String finderClassName = WikiPageResource.class.getName();
340         String finderMethodName = "findAll";
341         String[] finderParams = new String[] {
342                 "java.lang.Integer", "java.lang.Integer",
343                 "com.liferay.portal.kernel.util.OrderByComparator"
344             };
345         Object[] finderArgs = new Object[] {
346                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
347             };
348         Object result = FinderCache.getResult(finderClassName,
349                 finderMethodName, finderParams, finderArgs, getSessionFactory());
350 
351         if (result == null) {
352             Session session = null;
353 
354             try {
355                 session = openSession();
356 
357                 StringMaker query = new StringMaker();
358                 query.append(
359                     "FROM com.liferay.portlet.wiki.model.WikiPageResource ");
360 
361                 if (obc != null) {
362                     query.append("ORDER BY ");
363                     query.append(obc.getOrderBy());
364                 }
365 
366                 Query q = session.createQuery(query.toString());
367                 List list = QueryUtil.list(q, getDialect(), begin, end);
368 
369                 if (obc == null) {
370                     Collections.sort(list);
371                 }
372 
373                 FinderCache.putResult(finderClassName, finderMethodName,
374                     finderParams, finderArgs, list);
375 
376                 return list;
377             }
378             catch (Exception e) {
379                 throw HibernateUtil.processException(e);
380             }
381             finally {
382                 closeSession(session);
383             }
384         }
385         else {
386             return (List)result;
387         }
388     }
389 
390     public void removeByN_T(long nodeId, String title)
391         throws NoSuchPageResourceException, SystemException {
392         WikiPageResource wikiPageResource = findByN_T(nodeId, title);
393         remove(wikiPageResource);
394     }
395 
396     public void removeAll() throws SystemException {
397         Iterator itr = findAll().iterator();
398 
399         while (itr.hasNext()) {
400             remove((WikiPageResource)itr.next());
401         }
402     }
403 
404     public int countByN_T(long nodeId, String title) throws SystemException {
405         String finderClassName = WikiPageResource.class.getName();
406         String finderMethodName = "countByN_T";
407         String[] finderParams = new String[] {
408                 Long.class.getName(), String.class.getName()
409             };
410         Object[] finderArgs = new Object[] { new Long(nodeId), title };
411         Object result = FinderCache.getResult(finderClassName,
412                 finderMethodName, finderParams, finderArgs, getSessionFactory());
413 
414         if (result == null) {
415             Session session = null;
416 
417             try {
418                 session = openSession();
419 
420                 StringMaker query = new StringMaker();
421                 query.append("SELECT COUNT(*) ");
422                 query.append(
423                     "FROM com.liferay.portlet.wiki.model.WikiPageResource WHERE ");
424                 query.append("nodeId = ?");
425                 query.append(" AND ");
426 
427                 if (title == null) {
428                     query.append("title IS NULL");
429                 }
430                 else {
431                     query.append("title = ?");
432                 }
433 
434                 query.append(" ");
435 
436                 Query q = session.createQuery(query.toString());
437                 int queryPos = 0;
438                 q.setLong(queryPos++, nodeId);
439 
440                 if (title != null) {
441                     q.setString(queryPos++, title);
442                 }
443 
444                 Long count = null;
445                 Iterator itr = q.list().iterator();
446 
447                 if (itr.hasNext()) {
448                     count = (Long)itr.next();
449                 }
450 
451                 if (count == null) {
452                     count = new Long(0);
453                 }
454 
455                 FinderCache.putResult(finderClassName, finderMethodName,
456                     finderParams, finderArgs, count);
457 
458                 return count.intValue();
459             }
460             catch (Exception e) {
461                 throw HibernateUtil.processException(e);
462             }
463             finally {
464                 closeSession(session);
465             }
466         }
467         else {
468             return ((Long)result).intValue();
469         }
470     }
471 
472     public int countAll() throws SystemException {
473         String finderClassName = WikiPageResource.class.getName();
474         String finderMethodName = "countAll";
475         String[] finderParams = new String[] {  };
476         Object[] finderArgs = new Object[] {  };
477         Object result = FinderCache.getResult(finderClassName,
478                 finderMethodName, finderParams, finderArgs, getSessionFactory());
479 
480         if (result == null) {
481             Session session = null;
482 
483             try {
484                 session = openSession();
485 
486                 StringMaker query = new StringMaker();
487                 query.append("SELECT COUNT(*) ");
488                 query.append(
489                     "FROM com.liferay.portlet.wiki.model.WikiPageResource");
490 
491                 Query q = session.createQuery(query.toString());
492                 Long count = null;
493                 Iterator itr = q.list().iterator();
494 
495                 if (itr.hasNext()) {
496                     count = (Long)itr.next();
497                 }
498 
499                 if (count == null) {
500                     count = new Long(0);
501                 }
502 
503                 FinderCache.putResult(finderClassName, finderMethodName,
504                     finderParams, finderArgs, count);
505 
506                 return count.intValue();
507             }
508             catch (Exception e) {
509                 throw HibernateUtil.processException(e);
510             }
511             finally {
512                 closeSession(session);
513             }
514         }
515         else {
516             return ((Long)result).intValue();
517         }
518     }
519 
520     protected void initDao() {
521     }
522 
523     private static Log _log = LogFactory.getLog(WikiPageResourcePersistenceImpl.class);
524 }