1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.wiki.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.kernel.annotation.BeanReference;
19  import com.liferay.portal.kernel.cache.CacheRegistry;
20  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
21  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
22  import com.liferay.portal.kernel.dao.orm.FinderPath;
23  import com.liferay.portal.kernel.dao.orm.Query;
24  import com.liferay.portal.kernel.dao.orm.QueryPos;
25  import com.liferay.portal.kernel.dao.orm.QueryUtil;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.exception.SystemException;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.InstanceFactory;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.StringBundler;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
38  import com.liferay.portal.model.ModelListener;
39  import com.liferay.portal.service.persistence.BatchSessionUtil;
40  import com.liferay.portal.service.persistence.ResourcePersistence;
41  import com.liferay.portal.service.persistence.UserPersistence;
42  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
43  
44  import com.liferay.portlet.wiki.NoSuchPageResourceException;
45  import com.liferay.portlet.wiki.model.WikiPageResource;
46  import com.liferay.portlet.wiki.model.impl.WikiPageResourceImpl;
47  import com.liferay.portlet.wiki.model.impl.WikiPageResourceModelImpl;
48  
49  import java.io.Serializable;
50  
51  import java.util.ArrayList;
52  import java.util.Collections;
53  import java.util.List;
54  
55  /**
56   * <a href="WikiPageResourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
57   *
58   * <p>
59   * ServiceBuilder generated this class. Modifications in this class will be
60   * overwritten the next time is generated.
61   * </p>
62   *
63   * @author    Brian Wing Shun Chan
64   * @see       WikiPageResourcePersistence
65   * @see       WikiPageResourceUtil
66   * @generated
67   */
68  public class WikiPageResourcePersistenceImpl extends BasePersistenceImpl<WikiPageResource>
69      implements WikiPageResourcePersistence {
70      public static final String FINDER_CLASS_NAME_ENTITY = WikiPageResourceImpl.class.getName();
71      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
72          ".List";
73      public static final FinderPath FINDER_PATH_FIND_BY_UUID = new FinderPath(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
74              WikiPageResourceModelImpl.FINDER_CACHE_ENABLED,
75              FINDER_CLASS_NAME_LIST, "findByUuid",
76              new String[] {
77                  String.class.getName(),
78                  
79              "java.lang.Integer", "java.lang.Integer",
80                  "com.liferay.portal.kernel.util.OrderByComparator"
81              });
82      public static final FinderPath FINDER_PATH_COUNT_BY_UUID = new FinderPath(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
83              WikiPageResourceModelImpl.FINDER_CACHE_ENABLED,
84              FINDER_CLASS_NAME_LIST, "countByUuid",
85              new String[] { String.class.getName() });
86      public static final FinderPath FINDER_PATH_FETCH_BY_N_T = new FinderPath(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
87              WikiPageResourceModelImpl.FINDER_CACHE_ENABLED,
88              FINDER_CLASS_NAME_ENTITY, "fetchByN_T",
89              new String[] { Long.class.getName(), String.class.getName() });
90      public static final FinderPath FINDER_PATH_COUNT_BY_N_T = new FinderPath(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
91              WikiPageResourceModelImpl.FINDER_CACHE_ENABLED,
92              FINDER_CLASS_NAME_LIST, "countByN_T",
93              new String[] { Long.class.getName(), String.class.getName() });
94      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
95              WikiPageResourceModelImpl.FINDER_CACHE_ENABLED,
96              FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
97      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
98              WikiPageResourceModelImpl.FINDER_CACHE_ENABLED,
99              FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
100 
101     public void cacheResult(WikiPageResource wikiPageResource) {
102         EntityCacheUtil.putResult(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
103             WikiPageResourceImpl.class, wikiPageResource.getPrimaryKey(),
104             wikiPageResource);
105 
106         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T,
107             new Object[] {
108                 new Long(wikiPageResource.getNodeId()),
109                 
110             wikiPageResource.getTitle()
111             }, wikiPageResource);
112     }
113 
114     public void cacheResult(List<WikiPageResource> wikiPageResources) {
115         for (WikiPageResource wikiPageResource : wikiPageResources) {
116             if (EntityCacheUtil.getResult(
117                         WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
118                         WikiPageResourceImpl.class,
119                         wikiPageResource.getPrimaryKey(), this) == null) {
120                 cacheResult(wikiPageResource);
121             }
122         }
123     }
124 
125     public void clearCache() {
126         CacheRegistry.clear(WikiPageResourceImpl.class.getName());
127         EntityCacheUtil.clearCache(WikiPageResourceImpl.class.getName());
128         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
129         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
130     }
131 
132     public void clearCache(WikiPageResource wikiPageResource) {
133         EntityCacheUtil.removeResult(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
134             WikiPageResourceImpl.class, wikiPageResource.getPrimaryKey());
135 
136         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_N_T,
137             new Object[] {
138                 new Long(wikiPageResource.getNodeId()),
139                 
140             wikiPageResource.getTitle()
141             });
142     }
143 
144     public WikiPageResource create(long resourcePrimKey) {
145         WikiPageResource wikiPageResource = new WikiPageResourceImpl();
146 
147         wikiPageResource.setNew(true);
148         wikiPageResource.setPrimaryKey(resourcePrimKey);
149 
150         String uuid = PortalUUIDUtil.generate();
151 
152         wikiPageResource.setUuid(uuid);
153 
154         return wikiPageResource;
155     }
156 
157     public WikiPageResource remove(Serializable primaryKey)
158         throws NoSuchModelException, SystemException {
159         return remove(((Long)primaryKey).longValue());
160     }
161 
162     public WikiPageResource remove(long resourcePrimKey)
163         throws NoSuchPageResourceException, SystemException {
164         Session session = null;
165 
166         try {
167             session = openSession();
168 
169             WikiPageResource wikiPageResource = (WikiPageResource)session.get(WikiPageResourceImpl.class,
170                     new Long(resourcePrimKey));
171 
172             if (wikiPageResource == null) {
173                 if (_log.isWarnEnabled()) {
174                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
175                         resourcePrimKey);
176                 }
177 
178                 throw new NoSuchPageResourceException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
179                     resourcePrimKey);
180             }
181 
182             return remove(wikiPageResource);
183         }
184         catch (NoSuchPageResourceException nsee) {
185             throw nsee;
186         }
187         catch (Exception e) {
188             throw processException(e);
189         }
190         finally {
191             closeSession(session);
192         }
193     }
194 
195     public WikiPageResource remove(WikiPageResource wikiPageResource)
196         throws SystemException {
197         for (ModelListener<WikiPageResource> listener : listeners) {
198             listener.onBeforeRemove(wikiPageResource);
199         }
200 
201         wikiPageResource = removeImpl(wikiPageResource);
202 
203         for (ModelListener<WikiPageResource> listener : listeners) {
204             listener.onAfterRemove(wikiPageResource);
205         }
206 
207         return wikiPageResource;
208     }
209 
210     protected WikiPageResource removeImpl(WikiPageResource wikiPageResource)
211         throws SystemException {
212         wikiPageResource = toUnwrappedModel(wikiPageResource);
213 
214         Session session = null;
215 
216         try {
217             session = openSession();
218 
219             if (wikiPageResource.isCachedModel() ||
220                     BatchSessionUtil.isEnabled()) {
221                 Object staleObject = session.get(WikiPageResourceImpl.class,
222                         wikiPageResource.getPrimaryKeyObj());
223 
224                 if (staleObject != null) {
225                     session.evict(staleObject);
226                 }
227             }
228 
229             session.delete(wikiPageResource);
230 
231             session.flush();
232         }
233         catch (Exception e) {
234             throw processException(e);
235         }
236         finally {
237             closeSession(session);
238         }
239 
240         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
241 
242         WikiPageResourceModelImpl wikiPageResourceModelImpl = (WikiPageResourceModelImpl)wikiPageResource;
243 
244         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_N_T,
245             new Object[] {
246                 new Long(wikiPageResourceModelImpl.getOriginalNodeId()),
247                 
248             wikiPageResourceModelImpl.getOriginalTitle()
249             });
250 
251         EntityCacheUtil.removeResult(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
252             WikiPageResourceImpl.class, wikiPageResource.getPrimaryKey());
253 
254         return wikiPageResource;
255     }
256 
257     public WikiPageResource updateImpl(
258         com.liferay.portlet.wiki.model.WikiPageResource wikiPageResource,
259         boolean merge) throws SystemException {
260         wikiPageResource = toUnwrappedModel(wikiPageResource);
261 
262         boolean isNew = wikiPageResource.isNew();
263 
264         WikiPageResourceModelImpl wikiPageResourceModelImpl = (WikiPageResourceModelImpl)wikiPageResource;
265 
266         if (Validator.isNull(wikiPageResource.getUuid())) {
267             String uuid = PortalUUIDUtil.generate();
268 
269             wikiPageResource.setUuid(uuid);
270         }
271 
272         Session session = null;
273 
274         try {
275             session = openSession();
276 
277             BatchSessionUtil.update(session, wikiPageResource, merge);
278 
279             wikiPageResource.setNew(false);
280         }
281         catch (Exception e) {
282             throw processException(e);
283         }
284         finally {
285             closeSession(session);
286         }
287 
288         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
289 
290         EntityCacheUtil.putResult(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
291             WikiPageResourceImpl.class, wikiPageResource.getPrimaryKey(),
292             wikiPageResource);
293 
294         if (!isNew &&
295                 ((wikiPageResource.getNodeId() != wikiPageResourceModelImpl.getOriginalNodeId()) ||
296                 !Validator.equals(wikiPageResource.getTitle(),
297                     wikiPageResourceModelImpl.getOriginalTitle()))) {
298             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_N_T,
299                 new Object[] {
300                     new Long(wikiPageResourceModelImpl.getOriginalNodeId()),
301                     
302                 wikiPageResourceModelImpl.getOriginalTitle()
303                 });
304         }
305 
306         if (isNew ||
307                 ((wikiPageResource.getNodeId() != wikiPageResourceModelImpl.getOriginalNodeId()) ||
308                 !Validator.equals(wikiPageResource.getTitle(),
309                     wikiPageResourceModelImpl.getOriginalTitle()))) {
310             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T,
311                 new Object[] {
312                     new Long(wikiPageResource.getNodeId()),
313                     
314                 wikiPageResource.getTitle()
315                 }, wikiPageResource);
316         }
317 
318         return wikiPageResource;
319     }
320 
321     protected WikiPageResource toUnwrappedModel(
322         WikiPageResource wikiPageResource) {
323         if (wikiPageResource instanceof WikiPageResourceImpl) {
324             return wikiPageResource;
325         }
326 
327         WikiPageResourceImpl wikiPageResourceImpl = new WikiPageResourceImpl();
328 
329         wikiPageResourceImpl.setNew(wikiPageResource.isNew());
330         wikiPageResourceImpl.setPrimaryKey(wikiPageResource.getPrimaryKey());
331 
332         wikiPageResourceImpl.setUuid(wikiPageResource.getUuid());
333         wikiPageResourceImpl.setResourcePrimKey(wikiPageResource.getResourcePrimKey());
334         wikiPageResourceImpl.setNodeId(wikiPageResource.getNodeId());
335         wikiPageResourceImpl.setTitle(wikiPageResource.getTitle());
336 
337         return wikiPageResourceImpl;
338     }
339 
340     public WikiPageResource findByPrimaryKey(Serializable primaryKey)
341         throws NoSuchModelException, SystemException {
342         return findByPrimaryKey(((Long)primaryKey).longValue());
343     }
344 
345     public WikiPageResource findByPrimaryKey(long resourcePrimKey)
346         throws NoSuchPageResourceException, SystemException {
347         WikiPageResource wikiPageResource = fetchByPrimaryKey(resourcePrimKey);
348 
349         if (wikiPageResource == null) {
350             if (_log.isWarnEnabled()) {
351                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + resourcePrimKey);
352             }
353 
354             throw new NoSuchPageResourceException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
355                 resourcePrimKey);
356         }
357 
358         return wikiPageResource;
359     }
360 
361     public WikiPageResource fetchByPrimaryKey(Serializable primaryKey)
362         throws SystemException {
363         return fetchByPrimaryKey(((Long)primaryKey).longValue());
364     }
365 
366     public WikiPageResource fetchByPrimaryKey(long resourcePrimKey)
367         throws SystemException {
368         WikiPageResource wikiPageResource = (WikiPageResource)EntityCacheUtil.getResult(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
369                 WikiPageResourceImpl.class, resourcePrimKey, this);
370 
371         if (wikiPageResource == null) {
372             Session session = null;
373 
374             try {
375                 session = openSession();
376 
377                 wikiPageResource = (WikiPageResource)session.get(WikiPageResourceImpl.class,
378                         new Long(resourcePrimKey));
379             }
380             catch (Exception e) {
381                 throw processException(e);
382             }
383             finally {
384                 if (wikiPageResource != null) {
385                     cacheResult(wikiPageResource);
386                 }
387 
388                 closeSession(session);
389             }
390         }
391 
392         return wikiPageResource;
393     }
394 
395     public List<WikiPageResource> findByUuid(String uuid)
396         throws SystemException {
397         return findByUuid(uuid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
398     }
399 
400     public List<WikiPageResource> findByUuid(String uuid, int start, int end)
401         throws SystemException {
402         return findByUuid(uuid, start, end, null);
403     }
404 
405     public List<WikiPageResource> findByUuid(String uuid, int start, int end,
406         OrderByComparator orderByComparator) throws SystemException {
407         Object[] finderArgs = new Object[] {
408                 uuid,
409                 
410                 String.valueOf(start), String.valueOf(end),
411                 String.valueOf(orderByComparator)
412             };
413 
414         List<WikiPageResource> list = (List<WikiPageResource>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_UUID,
415                 finderArgs, this);
416 
417         if (list == null) {
418             Session session = null;
419 
420             try {
421                 session = openSession();
422 
423                 StringBundler query = null;
424 
425                 if (orderByComparator != null) {
426                     query = new StringBundler(3 +
427                             (orderByComparator.getOrderByFields().length * 3));
428                 }
429                 else {
430                     query = new StringBundler(2);
431                 }
432 
433                 query.append(_SQL_SELECT_WIKIPAGERESOURCE_WHERE);
434 
435                 if (uuid == null) {
436                     query.append(_FINDER_COLUMN_UUID_UUID_1);
437                 }
438                 else {
439                     if (uuid.equals(StringPool.BLANK)) {
440                         query.append(_FINDER_COLUMN_UUID_UUID_3);
441                     }
442                     else {
443                         query.append(_FINDER_COLUMN_UUID_UUID_2);
444                     }
445                 }
446 
447                 if (orderByComparator != null) {
448                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
449                         orderByComparator);
450                 }
451 
452                 String sql = query.toString();
453 
454                 Query q = session.createQuery(sql);
455 
456                 QueryPos qPos = QueryPos.getInstance(q);
457 
458                 if (uuid != null) {
459                     qPos.add(uuid);
460                 }
461 
462                 list = (List<WikiPageResource>)QueryUtil.list(q, getDialect(),
463                         start, end);
464             }
465             catch (Exception e) {
466                 throw processException(e);
467             }
468             finally {
469                 if (list == null) {
470                     list = new ArrayList<WikiPageResource>();
471                 }
472 
473                 cacheResult(list);
474 
475                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_UUID, finderArgs,
476                     list);
477 
478                 closeSession(session);
479             }
480         }
481 
482         return list;
483     }
484 
485     public WikiPageResource findByUuid_First(String uuid,
486         OrderByComparator orderByComparator)
487         throws NoSuchPageResourceException, SystemException {
488         List<WikiPageResource> list = findByUuid(uuid, 0, 1, orderByComparator);
489 
490         if (list.isEmpty()) {
491             StringBundler msg = new StringBundler(4);
492 
493             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
494 
495             msg.append("uuid=");
496             msg.append(uuid);
497 
498             msg.append(StringPool.CLOSE_CURLY_BRACE);
499 
500             throw new NoSuchPageResourceException(msg.toString());
501         }
502         else {
503             return list.get(0);
504         }
505     }
506 
507     public WikiPageResource findByUuid_Last(String uuid,
508         OrderByComparator orderByComparator)
509         throws NoSuchPageResourceException, SystemException {
510         int count = countByUuid(uuid);
511 
512         List<WikiPageResource> list = findByUuid(uuid, count - 1, count,
513                 orderByComparator);
514 
515         if (list.isEmpty()) {
516             StringBundler msg = new StringBundler(4);
517 
518             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
519 
520             msg.append("uuid=");
521             msg.append(uuid);
522 
523             msg.append(StringPool.CLOSE_CURLY_BRACE);
524 
525             throw new NoSuchPageResourceException(msg.toString());
526         }
527         else {
528             return list.get(0);
529         }
530     }
531 
532     public WikiPageResource[] findByUuid_PrevAndNext(long resourcePrimKey,
533         String uuid, OrderByComparator orderByComparator)
534         throws NoSuchPageResourceException, SystemException {
535         WikiPageResource wikiPageResource = findByPrimaryKey(resourcePrimKey);
536 
537         Session session = null;
538 
539         try {
540             session = openSession();
541 
542             WikiPageResource[] array = new WikiPageResourceImpl[3];
543 
544             array[0] = getByUuid_PrevAndNext(session, wikiPageResource, uuid,
545                     orderByComparator, true);
546 
547             array[1] = wikiPageResource;
548 
549             array[2] = getByUuid_PrevAndNext(session, wikiPageResource, uuid,
550                     orderByComparator, false);
551 
552             return array;
553         }
554         catch (Exception e) {
555             throw processException(e);
556         }
557         finally {
558             closeSession(session);
559         }
560     }
561 
562     protected WikiPageResource getByUuid_PrevAndNext(Session session,
563         WikiPageResource wikiPageResource, String uuid,
564         OrderByComparator orderByComparator, boolean previous) {
565         StringBundler query = null;
566 
567         if (orderByComparator != null) {
568             query = new StringBundler(6 +
569                     (orderByComparator.getOrderByFields().length * 6));
570         }
571         else {
572             query = new StringBundler(3);
573         }
574 
575         query.append(_SQL_SELECT_WIKIPAGERESOURCE_WHERE);
576 
577         if (uuid == null) {
578             query.append(_FINDER_COLUMN_UUID_UUID_1);
579         }
580         else {
581             if (uuid.equals(StringPool.BLANK)) {
582                 query.append(_FINDER_COLUMN_UUID_UUID_3);
583             }
584             else {
585                 query.append(_FINDER_COLUMN_UUID_UUID_2);
586             }
587         }
588 
589         if (orderByComparator != null) {
590             String[] orderByFields = orderByComparator.getOrderByFields();
591 
592             if (orderByFields.length > 0) {
593                 query.append(WHERE_AND);
594             }
595 
596             for (int i = 0; i < orderByFields.length; i++) {
597                 query.append(_ORDER_BY_ENTITY_ALIAS);
598                 query.append(orderByFields[i]);
599 
600                 if ((i + 1) < orderByFields.length) {
601                     if (orderByComparator.isAscending() ^ previous) {
602                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
603                     }
604                     else {
605                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
606                     }
607                 }
608                 else {
609                     if (orderByComparator.isAscending() ^ previous) {
610                         query.append(WHERE_GREATER_THAN);
611                     }
612                     else {
613                         query.append(WHERE_LESSER_THAN);
614                     }
615                 }
616             }
617 
618             query.append(ORDER_BY_CLAUSE);
619 
620             for (int i = 0; i < orderByFields.length; i++) {
621                 query.append(_ORDER_BY_ENTITY_ALIAS);
622                 query.append(orderByFields[i]);
623 
624                 if ((i + 1) < orderByFields.length) {
625                     if (orderByComparator.isAscending() ^ previous) {
626                         query.append(ORDER_BY_ASC_HAS_NEXT);
627                     }
628                     else {
629                         query.append(ORDER_BY_DESC_HAS_NEXT);
630                     }
631                 }
632                 else {
633                     if (orderByComparator.isAscending() ^ previous) {
634                         query.append(ORDER_BY_ASC);
635                     }
636                     else {
637                         query.append(ORDER_BY_DESC);
638                     }
639                 }
640             }
641         }
642 
643         String sql = query.toString();
644 
645         Query q = session.createQuery(sql);
646 
647         q.setFirstResult(0);
648         q.setMaxResults(2);
649 
650         QueryPos qPos = QueryPos.getInstance(q);
651 
652         if (uuid != null) {
653             qPos.add(uuid);
654         }
655 
656         if (orderByComparator != null) {
657             Object[] values = orderByComparator.getOrderByValues(wikiPageResource);
658 
659             for (Object value : values) {
660                 qPos.add(value);
661             }
662         }
663 
664         List<WikiPageResource> list = q.list();
665 
666         if (list.size() == 2) {
667             return list.get(1);
668         }
669         else {
670             return null;
671         }
672     }
673 
674     public WikiPageResource findByN_T(long nodeId, String title)
675         throws NoSuchPageResourceException, SystemException {
676         WikiPageResource wikiPageResource = fetchByN_T(nodeId, title);
677 
678         if (wikiPageResource == null) {
679             StringBundler msg = new StringBundler(6);
680 
681             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
682 
683             msg.append("nodeId=");
684             msg.append(nodeId);
685 
686             msg.append(", title=");
687             msg.append(title);
688 
689             msg.append(StringPool.CLOSE_CURLY_BRACE);
690 
691             if (_log.isWarnEnabled()) {
692                 _log.warn(msg.toString());
693             }
694 
695             throw new NoSuchPageResourceException(msg.toString());
696         }
697 
698         return wikiPageResource;
699     }
700 
701     public WikiPageResource fetchByN_T(long nodeId, String title)
702         throws SystemException {
703         return fetchByN_T(nodeId, title, true);
704     }
705 
706     public WikiPageResource fetchByN_T(long nodeId, String title,
707         boolean retrieveFromCache) throws SystemException {
708         Object[] finderArgs = new Object[] { new Long(nodeId), title };
709 
710         Object result = null;
711 
712         if (retrieveFromCache) {
713             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_N_T,
714                     finderArgs, this);
715         }
716 
717         if (result == null) {
718             Session session = null;
719 
720             try {
721                 session = openSession();
722 
723                 StringBundler query = new StringBundler(3);
724 
725                 query.append(_SQL_SELECT_WIKIPAGERESOURCE_WHERE);
726 
727                 query.append(_FINDER_COLUMN_N_T_NODEID_2);
728 
729                 if (title == null) {
730                     query.append(_FINDER_COLUMN_N_T_TITLE_1);
731                 }
732                 else {
733                     if (title.equals(StringPool.BLANK)) {
734                         query.append(_FINDER_COLUMN_N_T_TITLE_3);
735                     }
736                     else {
737                         query.append(_FINDER_COLUMN_N_T_TITLE_2);
738                     }
739                 }
740 
741                 String sql = query.toString();
742 
743                 Query q = session.createQuery(sql);
744 
745                 QueryPos qPos = QueryPos.getInstance(q);
746 
747                 qPos.add(nodeId);
748 
749                 if (title != null) {
750                     qPos.add(title);
751                 }
752 
753                 List<WikiPageResource> list = q.list();
754 
755                 result = list;
756 
757                 WikiPageResource wikiPageResource = null;
758 
759                 if (list.isEmpty()) {
760                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T,
761                         finderArgs, list);
762                 }
763                 else {
764                     wikiPageResource = list.get(0);
765 
766                     cacheResult(wikiPageResource);
767 
768                     if ((wikiPageResource.getNodeId() != nodeId) ||
769                             (wikiPageResource.getTitle() == null) ||
770                             !wikiPageResource.getTitle().equals(title)) {
771                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T,
772                             finderArgs, wikiPageResource);
773                     }
774                 }
775 
776                 return wikiPageResource;
777             }
778             catch (Exception e) {
779                 throw processException(e);
780             }
781             finally {
782                 if (result == null) {
783                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T,
784                         finderArgs, new ArrayList<WikiPageResource>());
785                 }
786 
787                 closeSession(session);
788             }
789         }
790         else {
791             if (result instanceof List<?>) {
792                 return null;
793             }
794             else {
795                 return (WikiPageResource)result;
796             }
797         }
798     }
799 
800     public List<WikiPageResource> findAll() throws SystemException {
801         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
802     }
803 
804     public List<WikiPageResource> findAll(int start, int end)
805         throws SystemException {
806         return findAll(start, end, null);
807     }
808 
809     public List<WikiPageResource> findAll(int start, int end,
810         OrderByComparator orderByComparator) throws SystemException {
811         Object[] finderArgs = new Object[] {
812                 String.valueOf(start), String.valueOf(end),
813                 String.valueOf(orderByComparator)
814             };
815 
816         List<WikiPageResource> list = (List<WikiPageResource>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
817                 finderArgs, this);
818 
819         if (list == null) {
820             Session session = null;
821 
822             try {
823                 session = openSession();
824 
825                 StringBundler query = null;
826                 String sql = null;
827 
828                 if (orderByComparator != null) {
829                     query = new StringBundler(2 +
830                             (orderByComparator.getOrderByFields().length * 3));
831 
832                     query.append(_SQL_SELECT_WIKIPAGERESOURCE);
833 
834                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
835                         orderByComparator);
836 
837                     sql = query.toString();
838                 }
839 
840                 sql = _SQL_SELECT_WIKIPAGERESOURCE;
841 
842                 Query q = session.createQuery(sql);
843 
844                 if (orderByComparator == null) {
845                     list = (List<WikiPageResource>)QueryUtil.list(q,
846                             getDialect(), start, end, false);
847 
848                     Collections.sort(list);
849                 }
850                 else {
851                     list = (List<WikiPageResource>)QueryUtil.list(q,
852                             getDialect(), start, end);
853                 }
854             }
855             catch (Exception e) {
856                 throw processException(e);
857             }
858             finally {
859                 if (list == null) {
860                     list = new ArrayList<WikiPageResource>();
861                 }
862 
863                 cacheResult(list);
864 
865                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
866 
867                 closeSession(session);
868             }
869         }
870 
871         return list;
872     }
873 
874     public void removeByUuid(String uuid) throws SystemException {
875         for (WikiPageResource wikiPageResource : findByUuid(uuid)) {
876             remove(wikiPageResource);
877         }
878     }
879 
880     public void removeByN_T(long nodeId, String title)
881         throws NoSuchPageResourceException, SystemException {
882         WikiPageResource wikiPageResource = findByN_T(nodeId, title);
883 
884         remove(wikiPageResource);
885     }
886 
887     public void removeAll() throws SystemException {
888         for (WikiPageResource wikiPageResource : findAll()) {
889             remove(wikiPageResource);
890         }
891     }
892 
893     public int countByUuid(String uuid) throws SystemException {
894         Object[] finderArgs = new Object[] { uuid };
895 
896         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_UUID,
897                 finderArgs, this);
898 
899         if (count == null) {
900             Session session = null;
901 
902             try {
903                 session = openSession();
904 
905                 StringBundler query = new StringBundler(2);
906 
907                 query.append(_SQL_COUNT_WIKIPAGERESOURCE_WHERE);
908 
909                 if (uuid == null) {
910                     query.append(_FINDER_COLUMN_UUID_UUID_1);
911                 }
912                 else {
913                     if (uuid.equals(StringPool.BLANK)) {
914                         query.append(_FINDER_COLUMN_UUID_UUID_3);
915                     }
916                     else {
917                         query.append(_FINDER_COLUMN_UUID_UUID_2);
918                     }
919                 }
920 
921                 String sql = query.toString();
922 
923                 Query q = session.createQuery(sql);
924 
925                 QueryPos qPos = QueryPos.getInstance(q);
926 
927                 if (uuid != null) {
928                     qPos.add(uuid);
929                 }
930 
931                 count = (Long)q.uniqueResult();
932             }
933             catch (Exception e) {
934                 throw processException(e);
935             }
936             finally {
937                 if (count == null) {
938                     count = Long.valueOf(0);
939                 }
940 
941                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID,
942                     finderArgs, count);
943 
944                 closeSession(session);
945             }
946         }
947 
948         return count.intValue();
949     }
950 
951     public int countByN_T(long nodeId, String title) throws SystemException {
952         Object[] finderArgs = new Object[] { new Long(nodeId), title };
953 
954         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_N_T,
955                 finderArgs, this);
956 
957         if (count == null) {
958             Session session = null;
959 
960             try {
961                 session = openSession();
962 
963                 StringBundler query = new StringBundler(3);
964 
965                 query.append(_SQL_COUNT_WIKIPAGERESOURCE_WHERE);
966 
967                 query.append(_FINDER_COLUMN_N_T_NODEID_2);
968 
969                 if (title == null) {
970                     query.append(_FINDER_COLUMN_N_T_TITLE_1);
971                 }
972                 else {
973                     if (title.equals(StringPool.BLANK)) {
974                         query.append(_FINDER_COLUMN_N_T_TITLE_3);
975                     }
976                     else {
977                         query.append(_FINDER_COLUMN_N_T_TITLE_2);
978                     }
979                 }
980 
981                 String sql = query.toString();
982 
983                 Query q = session.createQuery(sql);
984 
985                 QueryPos qPos = QueryPos.getInstance(q);
986 
987                 qPos.add(nodeId);
988 
989                 if (title != null) {
990                     qPos.add(title);
991                 }
992 
993                 count = (Long)q.uniqueResult();
994             }
995             catch (Exception e) {
996                 throw processException(e);
997             }
998             finally {
999                 if (count == null) {
1000                    count = Long.valueOf(0);
1001                }
1002
1003                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_N_T, finderArgs,
1004                    count);
1005
1006                closeSession(session);
1007            }
1008        }
1009
1010        return count.intValue();
1011    }
1012
1013    public int countAll() throws SystemException {
1014        Object[] finderArgs = new Object[0];
1015
1016        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1017                finderArgs, this);
1018
1019        if (count == null) {
1020            Session session = null;
1021
1022            try {
1023                session = openSession();
1024
1025                Query q = session.createQuery(_SQL_COUNT_WIKIPAGERESOURCE);
1026
1027                count = (Long)q.uniqueResult();
1028            }
1029            catch (Exception e) {
1030                throw processException(e);
1031            }
1032            finally {
1033                if (count == null) {
1034                    count = Long.valueOf(0);
1035                }
1036
1037                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1038                    count);
1039
1040                closeSession(session);
1041            }
1042        }
1043
1044        return count.intValue();
1045    }
1046
1047    public void afterPropertiesSet() {
1048        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1049                    com.liferay.portal.util.PropsUtil.get(
1050                        "value.object.listener.com.liferay.portlet.wiki.model.WikiPageResource")));
1051
1052        if (listenerClassNames.length > 0) {
1053            try {
1054                List<ModelListener<WikiPageResource>> listenersList = new ArrayList<ModelListener<WikiPageResource>>();
1055
1056                for (String listenerClassName : listenerClassNames) {
1057                    listenersList.add((ModelListener<WikiPageResource>)InstanceFactory.newInstance(
1058                            listenerClassName));
1059                }
1060
1061                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1062            }
1063            catch (Exception e) {
1064                _log.error(e);
1065            }
1066        }
1067    }
1068
1069    @BeanReference(type = WikiNodePersistence.class)
1070    protected WikiNodePersistence wikiNodePersistence;
1071    @BeanReference(type = WikiPagePersistence.class)
1072    protected WikiPagePersistence wikiPagePersistence;
1073    @BeanReference(type = WikiPageResourcePersistence.class)
1074    protected WikiPageResourcePersistence wikiPageResourcePersistence;
1075    @BeanReference(type = ResourcePersistence.class)
1076    protected ResourcePersistence resourcePersistence;
1077    @BeanReference(type = UserPersistence.class)
1078    protected UserPersistence userPersistence;
1079    private static final String _SQL_SELECT_WIKIPAGERESOURCE = "SELECT wikiPageResource FROM WikiPageResource wikiPageResource";
1080    private static final String _SQL_SELECT_WIKIPAGERESOURCE_WHERE = "SELECT wikiPageResource FROM WikiPageResource wikiPageResource WHERE ";
1081    private static final String _SQL_COUNT_WIKIPAGERESOURCE = "SELECT COUNT(wikiPageResource) FROM WikiPageResource wikiPageResource";
1082    private static final String _SQL_COUNT_WIKIPAGERESOURCE_WHERE = "SELECT COUNT(wikiPageResource) FROM WikiPageResource wikiPageResource WHERE ";
1083    private static final String _FINDER_COLUMN_UUID_UUID_1 = "wikiPageResource.uuid IS NULL";
1084    private static final String _FINDER_COLUMN_UUID_UUID_2 = "wikiPageResource.uuid = ?";
1085    private static final String _FINDER_COLUMN_UUID_UUID_3 = "(wikiPageResource.uuid IS NULL OR wikiPageResource.uuid = ?)";
1086    private static final String _FINDER_COLUMN_N_T_NODEID_2 = "wikiPageResource.nodeId = ? AND ";
1087    private static final String _FINDER_COLUMN_N_T_TITLE_1 = "wikiPageResource.title IS NULL";
1088    private static final String _FINDER_COLUMN_N_T_TITLE_2 = "wikiPageResource.title = ?";
1089    private static final String _FINDER_COLUMN_N_T_TITLE_3 = "(wikiPageResource.title IS NULL OR wikiPageResource.title = ?)";
1090    private static final String _ORDER_BY_ENTITY_ALIAS = "wikiPageResource.";
1091    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No WikiPageResource exists with the primary key ";
1092    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No WikiPageResource exists with the key {";
1093    private static Log _log = LogFactoryUtil.getLog(WikiPageResourcePersistenceImpl.class);
1094}