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