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