1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.wiki.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
24  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
25  import com.liferay.portal.kernel.dao.orm.Query;
26  import com.liferay.portal.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.Session;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.StringUtil;
35  import com.liferay.portal.model.ModelListener;
36  import com.liferay.portal.service.persistence.BatchSessionUtil;
37  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
38  
39  import com.liferay.portlet.wiki.NoSuchPageResourceException;
40  import com.liferay.portlet.wiki.model.WikiPageResource;
41  import com.liferay.portlet.wiki.model.impl.WikiPageResourceImpl;
42  import com.liferay.portlet.wiki.model.impl.WikiPageResourceModelImpl;
43  
44  import java.util.ArrayList;
45  import java.util.Collections;
46  import java.util.Iterator;
47  import java.util.List;
48  
49  /**
50   * <a href="WikiPageResourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public class WikiPageResourcePersistenceImpl extends BasePersistenceImpl
56      implements WikiPageResourcePersistence {
57      public WikiPageResource create(long resourcePrimKey) {
58          WikiPageResource wikiPageResource = new WikiPageResourceImpl();
59  
60          wikiPageResource.setNew(true);
61          wikiPageResource.setPrimaryKey(resourcePrimKey);
62  
63          return wikiPageResource;
64      }
65  
66      public WikiPageResource remove(long resourcePrimKey)
67          throws NoSuchPageResourceException, SystemException {
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              WikiPageResource wikiPageResource = (WikiPageResource)session.get(WikiPageResourceImpl.class,
74                      new Long(resourcePrimKey));
75  
76              if (wikiPageResource == null) {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn(
79                          "No WikiPageResource exists with the primary key " +
80                          resourcePrimKey);
81                  }
82  
83                  throw new NoSuchPageResourceException(
84                      "No WikiPageResource exists with the primary key " +
85                      resourcePrimKey);
86              }
87  
88              return remove(wikiPageResource);
89          }
90          catch (NoSuchPageResourceException nsee) {
91              throw nsee;
92          }
93          catch (Exception e) {
94              throw processException(e);
95          }
96          finally {
97              closeSession(session);
98          }
99      }
100 
101     public WikiPageResource remove(WikiPageResource wikiPageResource)
102         throws SystemException {
103         for (ModelListener listener : listeners) {
104             listener.onBeforeRemove(wikiPageResource);
105         }
106 
107         wikiPageResource = removeImpl(wikiPageResource);
108 
109         for (ModelListener listener : listeners) {
110             listener.onAfterRemove(wikiPageResource);
111         }
112 
113         return wikiPageResource;
114     }
115 
116     protected WikiPageResource removeImpl(WikiPageResource wikiPageResource)
117         throws SystemException {
118         Session session = null;
119 
120         try {
121             session = openSession();
122 
123             if (BatchSessionUtil.isEnabled()) {
124                 Object staleObject = session.get(WikiPageResourceImpl.class,
125                         wikiPageResource.getPrimaryKeyObj());
126 
127                 if (staleObject != null) {
128                     session.evict(staleObject);
129                 }
130             }
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         for (ModelListener listener : listeners) {
179             if (isNew) {
180                 listener.onBeforeCreate(wikiPageResource);
181             }
182             else {
183                 listener.onBeforeUpdate(wikiPageResource);
184             }
185         }
186 
187         wikiPageResource = updateImpl(wikiPageResource, merge);
188 
189         for (ModelListener listener : listeners) {
190             if (isNew) {
191                 listener.onAfterCreate(wikiPageResource);
192             }
193             else {
194                 listener.onAfterUpdate(wikiPageResource);
195             }
196         }
197 
198         return wikiPageResource;
199     }
200 
201     public WikiPageResource updateImpl(
202         com.liferay.portlet.wiki.model.WikiPageResource wikiPageResource,
203         boolean merge) throws SystemException {
204         Session session = null;
205 
206         try {
207             session = openSession();
208 
209             BatchSessionUtil.update(session, wikiPageResource, merge);
210 
211             wikiPageResource.setNew(false);
212 
213             return wikiPageResource;
214         }
215         catch (Exception e) {
216             throw processException(e);
217         }
218         finally {
219             closeSession(session);
220 
221             FinderCacheUtil.clearCache(WikiPageResource.class.getName());
222         }
223     }
224 
225     public WikiPageResource findByPrimaryKey(long resourcePrimKey)
226         throws NoSuchPageResourceException, SystemException {
227         WikiPageResource wikiPageResource = fetchByPrimaryKey(resourcePrimKey);
228 
229         if (wikiPageResource == null) {
230             if (_log.isWarnEnabled()) {
231                 _log.warn("No WikiPageResource exists with the primary key " +
232                     resourcePrimKey);
233             }
234 
235             throw new NoSuchPageResourceException(
236                 "No WikiPageResource exists with the primary key " +
237                 resourcePrimKey);
238         }
239 
240         return wikiPageResource;
241     }
242 
243     public WikiPageResource fetchByPrimaryKey(long resourcePrimKey)
244         throws SystemException {
245         Session session = null;
246 
247         try {
248             session = openSession();
249 
250             return (WikiPageResource)session.get(WikiPageResourceImpl.class,
251                 new Long(resourcePrimKey));
252         }
253         catch (Exception e) {
254             throw processException(e);
255         }
256         finally {
257             closeSession(session);
258         }
259     }
260 
261     public WikiPageResource findByN_T(long nodeId, String title)
262         throws NoSuchPageResourceException, SystemException {
263         WikiPageResource wikiPageResource = fetchByN_T(nodeId, title);
264 
265         if (wikiPageResource == null) {
266             StringBuilder msg = new StringBuilder();
267 
268             msg.append("No WikiPageResource exists with the key {");
269 
270             msg.append("nodeId=" + nodeId);
271 
272             msg.append(", ");
273             msg.append("title=" + title);
274 
275             msg.append(StringPool.CLOSE_CURLY_BRACE);
276 
277             if (_log.isWarnEnabled()) {
278                 _log.warn(msg.toString());
279             }
280 
281             throw new NoSuchPageResourceException(msg.toString());
282         }
283 
284         return wikiPageResource;
285     }
286 
287     public WikiPageResource fetchByN_T(long nodeId, String title)
288         throws SystemException {
289         boolean finderClassNameCacheEnabled = WikiPageResourceModelImpl.CACHE_ENABLED;
290         String finderClassName = WikiPageResource.class.getName();
291         String finderMethodName = "fetchByN_T";
292         String[] finderParams = new String[] {
293                 Long.class.getName(), String.class.getName()
294             };
295         Object[] finderArgs = new Object[] { new Long(nodeId), title };
296 
297         Object result = null;
298 
299         if (finderClassNameCacheEnabled) {
300             result = FinderCacheUtil.getResult(finderClassName,
301                     finderMethodName, finderParams, finderArgs, this);
302         }
303 
304         if (result == null) {
305             Session session = null;
306 
307             try {
308                 session = openSession();
309 
310                 StringBuilder query = new StringBuilder();
311 
312                 query.append(
313                     "FROM com.liferay.portlet.wiki.model.WikiPageResource WHERE ");
314 
315                 query.append("nodeId = ?");
316 
317                 query.append(" AND ");
318 
319                 if (title == null) {
320                     query.append("title IS NULL");
321                 }
322                 else {
323                     query.append("title = ?");
324                 }
325 
326                 query.append(" ");
327 
328                 Query q = session.createQuery(query.toString());
329 
330                 QueryPos qPos = QueryPos.getInstance(q);
331 
332                 qPos.add(nodeId);
333 
334                 if (title != null) {
335                     qPos.add(title);
336                 }
337 
338                 List<WikiPageResource> list = q.list();
339 
340                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
341                     finderClassName, finderMethodName, finderParams,
342                     finderArgs, list);
343 
344                 if (list.size() == 0) {
345                     return null;
346                 }
347                 else {
348                     return list.get(0);
349                 }
350             }
351             catch (Exception e) {
352                 throw processException(e);
353             }
354             finally {
355                 closeSession(session);
356             }
357         }
358         else {
359             List<WikiPageResource> list = (List<WikiPageResource>)result;
360 
361             if (list.size() == 0) {
362                 return null;
363             }
364             else {
365                 return list.get(0);
366             }
367         }
368     }
369 
370     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
371         throws SystemException {
372         Session session = null;
373 
374         try {
375             session = openSession();
376 
377             dynamicQuery.compile(session);
378 
379             return dynamicQuery.list();
380         }
381         catch (Exception e) {
382             throw processException(e);
383         }
384         finally {
385             closeSession(session);
386         }
387     }
388 
389     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
390         int start, int end) throws SystemException {
391         Session session = null;
392 
393         try {
394             session = openSession();
395 
396             dynamicQuery.setLimit(start, end);
397 
398             dynamicQuery.compile(session);
399 
400             return dynamicQuery.list();
401         }
402         catch (Exception e) {
403             throw processException(e);
404         }
405         finally {
406             closeSession(session);
407         }
408     }
409 
410     public List<WikiPageResource> findAll() throws SystemException {
411         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
412     }
413 
414     public List<WikiPageResource> findAll(int start, int end)
415         throws SystemException {
416         return findAll(start, end, null);
417     }
418 
419     public List<WikiPageResource> findAll(int start, int end,
420         OrderByComparator obc) throws SystemException {
421         boolean finderClassNameCacheEnabled = WikiPageResourceModelImpl.CACHE_ENABLED;
422         String finderClassName = WikiPageResource.class.getName();
423         String finderMethodName = "findAll";
424         String[] finderParams = new String[] {
425                 "java.lang.Integer", "java.lang.Integer",
426                 "com.liferay.portal.kernel.util.OrderByComparator"
427             };
428         Object[] finderArgs = new Object[] {
429                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
430             };
431 
432         Object result = null;
433 
434         if (finderClassNameCacheEnabled) {
435             result = FinderCacheUtil.getResult(finderClassName,
436                     finderMethodName, finderParams, finderArgs, this);
437         }
438 
439         if (result == null) {
440             Session session = null;
441 
442             try {
443                 session = openSession();
444 
445                 StringBuilder query = new StringBuilder();
446 
447                 query.append(
448                     "FROM com.liferay.portlet.wiki.model.WikiPageResource ");
449 
450                 if (obc != null) {
451                     query.append("ORDER BY ");
452                     query.append(obc.getOrderBy());
453                 }
454 
455                 Query q = session.createQuery(query.toString());
456 
457                 List<WikiPageResource> list = null;
458 
459                 if (obc == null) {
460                     list = (List<WikiPageResource>)QueryUtil.list(q,
461                             getDialect(), start, end, false);
462 
463                     Collections.sort(list);
464                 }
465                 else {
466                     list = (List<WikiPageResource>)QueryUtil.list(q,
467                             getDialect(), start, end);
468                 }
469 
470                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
471                     finderClassName, finderMethodName, finderParams,
472                     finderArgs, list);
473 
474                 return list;
475             }
476             catch (Exception e) {
477                 throw processException(e);
478             }
479             finally {
480                 closeSession(session);
481             }
482         }
483         else {
484             return (List<WikiPageResource>)result;
485         }
486     }
487 
488     public void removeByN_T(long nodeId, String title)
489         throws NoSuchPageResourceException, SystemException {
490         WikiPageResource wikiPageResource = findByN_T(nodeId, title);
491 
492         remove(wikiPageResource);
493     }
494 
495     public void removeAll() throws SystemException {
496         for (WikiPageResource wikiPageResource : findAll()) {
497             remove(wikiPageResource);
498         }
499     }
500 
501     public int countByN_T(long nodeId, String title) throws SystemException {
502         boolean finderClassNameCacheEnabled = WikiPageResourceModelImpl.CACHE_ENABLED;
503         String finderClassName = WikiPageResource.class.getName();
504         String finderMethodName = "countByN_T";
505         String[] finderParams = new String[] {
506                 Long.class.getName(), String.class.getName()
507             };
508         Object[] finderArgs = new Object[] { new Long(nodeId), title };
509 
510         Object result = null;
511 
512         if (finderClassNameCacheEnabled) {
513             result = FinderCacheUtil.getResult(finderClassName,
514                     finderMethodName, finderParams, finderArgs, this);
515         }
516 
517         if (result == null) {
518             Session session = null;
519 
520             try {
521                 session = openSession();
522 
523                 StringBuilder query = new StringBuilder();
524 
525                 query.append("SELECT COUNT(*) ");
526                 query.append(
527                     "FROM com.liferay.portlet.wiki.model.WikiPageResource WHERE ");
528 
529                 query.append("nodeId = ?");
530 
531                 query.append(" AND ");
532 
533                 if (title == null) {
534                     query.append("title IS NULL");
535                 }
536                 else {
537                     query.append("title = ?");
538                 }
539 
540                 query.append(" ");
541 
542                 Query q = session.createQuery(query.toString());
543 
544                 QueryPos qPos = QueryPos.getInstance(q);
545 
546                 qPos.add(nodeId);
547 
548                 if (title != null) {
549                     qPos.add(title);
550                 }
551 
552                 Long count = null;
553 
554                 Iterator<Long> itr = q.list().iterator();
555 
556                 if (itr.hasNext()) {
557                     count = itr.next();
558                 }
559 
560                 if (count == null) {
561                     count = new Long(0);
562                 }
563 
564                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
565                     finderClassName, finderMethodName, finderParams,
566                     finderArgs, count);
567 
568                 return count.intValue();
569             }
570             catch (Exception e) {
571                 throw processException(e);
572             }
573             finally {
574                 closeSession(session);
575             }
576         }
577         else {
578             return ((Long)result).intValue();
579         }
580     }
581 
582     public int countAll() throws SystemException {
583         boolean finderClassNameCacheEnabled = WikiPageResourceModelImpl.CACHE_ENABLED;
584         String finderClassName = WikiPageResource.class.getName();
585         String finderMethodName = "countAll";
586         String[] finderParams = new String[] {  };
587         Object[] finderArgs = new Object[] {  };
588 
589         Object result = null;
590 
591         if (finderClassNameCacheEnabled) {
592             result = FinderCacheUtil.getResult(finderClassName,
593                     finderMethodName, finderParams, finderArgs, this);
594         }
595 
596         if (result == null) {
597             Session session = null;
598 
599             try {
600                 session = openSession();
601 
602                 Query q = session.createQuery(
603                         "SELECT COUNT(*) FROM com.liferay.portlet.wiki.model.WikiPageResource");
604 
605                 Long count = null;
606 
607                 Iterator<Long> itr = q.list().iterator();
608 
609                 if (itr.hasNext()) {
610                     count = itr.next();
611                 }
612 
613                 if (count == null) {
614                     count = new Long(0);
615                 }
616 
617                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
618                     finderClassName, finderMethodName, finderParams,
619                     finderArgs, count);
620 
621                 return count.intValue();
622             }
623             catch (Exception e) {
624                 throw processException(e);
625             }
626             finally {
627                 closeSession(session);
628             }
629         }
630         else {
631             return ((Long)result).intValue();
632         }
633     }
634 
635     public void afterPropertiesSet() {
636         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
637                     com.liferay.portal.util.PropsUtil.get(
638                         "value.object.listener.com.liferay.portlet.wiki.model.WikiPageResource")));
639 
640         if (listenerClassNames.length > 0) {
641             try {
642                 List<ModelListener> listenersList = new ArrayList<ModelListener>();
643 
644                 for (String listenerClassName : listenerClassNames) {
645                     listenersList.add((ModelListener)Class.forName(
646                             listenerClassName).newInstance());
647                 }
648 
649                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
650             }
651             catch (Exception e) {
652                 _log.error(e);
653             }
654         }
655     }
656 
657     private static Log _log = LogFactoryUtil.getLog(WikiPageResourcePersistenceImpl.class);
658 }