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