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