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