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