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