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.BatchSessionUtil;
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 {
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             if (BatchSessionUtil.isEnabled()) {
133                 Object staleObject = session.get(WikiPageResourceImpl.class,
134                         wikiPageResource.getPrimaryKeyObj());
135 
136                 if (staleObject != null) {
137                     session.evict(staleObject);
138                 }
139             }
140 
141             session.delete(wikiPageResource);
142 
143             session.flush();
144 
145             return wikiPageResource;
146         }
147         catch (Exception e) {
148             throw processException(e);
149         }
150         finally {
151             closeSession(session);
152 
153             FinderCacheUtil.clearCache(WikiPageResource.class.getName());
154         }
155     }
156 
157     /**
158      * @deprecated Use <code>update(WikiPageResource wikiPageResource, boolean merge)</code>.
159      */
160     public WikiPageResource update(WikiPageResource wikiPageResource)
161         throws SystemException {
162         if (_log.isWarnEnabled()) {
163             _log.warn(
164                 "Using the deprecated update(WikiPageResource wikiPageResource) method. Use update(WikiPageResource wikiPageResource, boolean merge) instead.");
165         }
166 
167         return update(wikiPageResource, false);
168     }
169 
170     /**
171      * Add, update, or merge, the entity. This method also calls the model
172      * listeners to trigger the proper events associated with adding, deleting,
173      * or updating an entity.
174      *
175      * @param        wikiPageResource the entity to add, update, or merge
176      * @param        merge boolean value for whether to merge the entity. The
177      *                default value is false. Setting merge to true is more
178      *                expensive and should only be true when wikiPageResource is
179      *                transient. See LEP-5473 for a detailed discussion of this
180      *                method.
181      * @return        true if the portlet can be displayed via Ajax
182      */
183     public WikiPageResource update(WikiPageResource wikiPageResource,
184         boolean merge) throws SystemException {
185         boolean isNew = wikiPageResource.isNew();
186 
187         if (_listeners.length > 0) {
188             for (ModelListener listener : _listeners) {
189                 if (isNew) {
190                     listener.onBeforeCreate(wikiPageResource);
191                 }
192                 else {
193                     listener.onBeforeUpdate(wikiPageResource);
194                 }
195             }
196         }
197 
198         wikiPageResource = updateImpl(wikiPageResource, merge);
199 
200         if (_listeners.length > 0) {
201             for (ModelListener listener : _listeners) {
202                 if (isNew) {
203                     listener.onAfterCreate(wikiPageResource);
204                 }
205                 else {
206                     listener.onAfterUpdate(wikiPageResource);
207                 }
208             }
209         }
210 
211         return wikiPageResource;
212     }
213 
214     public WikiPageResource updateImpl(
215         com.liferay.portlet.wiki.model.WikiPageResource wikiPageResource,
216         boolean merge) throws SystemException {
217         Session session = null;
218 
219         try {
220             session = openSession();
221 
222             BatchSessionUtil.update(session, wikiPageResource, merge);
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 = null;
471 
472                 if (obc == null) {
473                     list = (List<WikiPageResource>)QueryUtil.list(q,
474                             getDialect(), start, end, false);
475 
476                     Collections.sort(list);
477                 }
478                 else {
479                     list = (List<WikiPageResource>)QueryUtil.list(q,
480                             getDialect(), start, end);
481                 }
482 
483                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
484                     finderClassName, finderMethodName, finderParams,
485                     finderArgs, list);
486 
487                 return list;
488             }
489             catch (Exception e) {
490                 throw processException(e);
491             }
492             finally {
493                 closeSession(session);
494             }
495         }
496         else {
497             return (List<WikiPageResource>)result;
498         }
499     }
500 
501     public void removeByN_T(long nodeId, String title)
502         throws NoSuchPageResourceException, SystemException {
503         WikiPageResource wikiPageResource = findByN_T(nodeId, title);
504 
505         remove(wikiPageResource);
506     }
507 
508     public void removeAll() throws SystemException {
509         for (WikiPageResource wikiPageResource : findAll()) {
510             remove(wikiPageResource);
511         }
512     }
513 
514     public int countByN_T(long nodeId, String title) throws SystemException {
515         boolean finderClassNameCacheEnabled = WikiPageResourceModelImpl.CACHE_ENABLED;
516         String finderClassName = WikiPageResource.class.getName();
517         String finderMethodName = "countByN_T";
518         String[] finderParams = new String[] {
519                 Long.class.getName(), String.class.getName()
520             };
521         Object[] finderArgs = new Object[] { new Long(nodeId), title };
522 
523         Object result = null;
524 
525         if (finderClassNameCacheEnabled) {
526             result = FinderCacheUtil.getResult(finderClassName,
527                     finderMethodName, finderParams, finderArgs, this);
528         }
529 
530         if (result == null) {
531             Session session = null;
532 
533             try {
534                 session = openSession();
535 
536                 StringBuilder query = new StringBuilder();
537 
538                 query.append("SELECT COUNT(*) ");
539                 query.append(
540                     "FROM com.liferay.portlet.wiki.model.WikiPageResource WHERE ");
541 
542                 query.append("nodeId = ?");
543 
544                 query.append(" AND ");
545 
546                 if (title == null) {
547                     query.append("title IS NULL");
548                 }
549                 else {
550                     query.append("title = ?");
551                 }
552 
553                 query.append(" ");
554 
555                 Query q = session.createQuery(query.toString());
556 
557                 QueryPos qPos = QueryPos.getInstance(q);
558 
559                 qPos.add(nodeId);
560 
561                 if (title != null) {
562                     qPos.add(title);
563                 }
564 
565                 Long count = null;
566 
567                 Iterator<Long> itr = q.list().iterator();
568 
569                 if (itr.hasNext()) {
570                     count = itr.next();
571                 }
572 
573                 if (count == null) {
574                     count = new Long(0);
575                 }
576 
577                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
578                     finderClassName, finderMethodName, finderParams,
579                     finderArgs, count);
580 
581                 return count.intValue();
582             }
583             catch (Exception e) {
584                 throw processException(e);
585             }
586             finally {
587                 closeSession(session);
588             }
589         }
590         else {
591             return ((Long)result).intValue();
592         }
593     }
594 
595     public int countAll() throws SystemException {
596         boolean finderClassNameCacheEnabled = WikiPageResourceModelImpl.CACHE_ENABLED;
597         String finderClassName = WikiPageResource.class.getName();
598         String finderMethodName = "countAll";
599         String[] finderParams = new String[] {  };
600         Object[] finderArgs = new Object[] {  };
601 
602         Object result = null;
603 
604         if (finderClassNameCacheEnabled) {
605             result = FinderCacheUtil.getResult(finderClassName,
606                     finderMethodName, finderParams, finderArgs, this);
607         }
608 
609         if (result == null) {
610             Session session = null;
611 
612             try {
613                 session = openSession();
614 
615                 Query q = session.createQuery(
616                         "SELECT COUNT(*) FROM com.liferay.portlet.wiki.model.WikiPageResource");
617 
618                 Long count = null;
619 
620                 Iterator<Long> itr = q.list().iterator();
621 
622                 if (itr.hasNext()) {
623                     count = itr.next();
624                 }
625 
626                 if (count == null) {
627                     count = new Long(0);
628                 }
629 
630                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
631                     finderClassName, finderMethodName, finderParams,
632                     finderArgs, count);
633 
634                 return count.intValue();
635             }
636             catch (Exception e) {
637                 throw processException(e);
638             }
639             finally {
640                 closeSession(session);
641             }
642         }
643         else {
644             return ((Long)result).intValue();
645         }
646     }
647 
648     public void registerListener(ModelListener listener) {
649         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
650 
651         listeners.add(listener);
652 
653         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
654     }
655 
656     public void unregisterListener(ModelListener listener) {
657         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
658 
659         listeners.remove(listener);
660 
661         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
662     }
663 
664     public void afterPropertiesSet() {
665         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
666                     com.liferay.portal.util.PropsUtil.get(
667                         "value.object.listener.com.liferay.portlet.wiki.model.WikiPageResource")));
668 
669         if (listenerClassNames.length > 0) {
670             try {
671                 List<ModelListener> listeners = new ArrayList<ModelListener>();
672 
673                 for (String listenerClassName : listenerClassNames) {
674                     listeners.add((ModelListener)Class.forName(
675                             listenerClassName).newInstance());
676                 }
677 
678                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
679             }
680             catch (Exception e) {
681                 _log.error(e);
682             }
683         }
684     }
685 
686     private static Log _log = LogFactory.getLog(WikiPageResourcePersistenceImpl.class);
687     private ModelListener[] _listeners = new ModelListener[0];
688 }