001    /**
002     * Copyright (c) 2000-2011 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.counter.service.persistence;
016    
017    import com.liferay.counter.NoSuchCounterException;
018    import com.liferay.counter.model.Counter;
019    import com.liferay.counter.model.impl.CounterImpl;
020    import com.liferay.counter.model.impl.CounterModelImpl;
021    
022    import com.liferay.portal.NoSuchModelException;
023    import com.liferay.portal.kernel.bean.BeanReference;
024    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
025    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
026    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
027    import com.liferay.portal.kernel.dao.orm.FinderPath;
028    import com.liferay.portal.kernel.dao.orm.Query;
029    import com.liferay.portal.kernel.dao.orm.QueryUtil;
030    import com.liferay.portal.kernel.dao.orm.Session;
031    import com.liferay.portal.kernel.exception.SystemException;
032    import com.liferay.portal.kernel.log.Log;
033    import com.liferay.portal.kernel.log.LogFactoryUtil;
034    import com.liferay.portal.kernel.util.GetterUtil;
035    import com.liferay.portal.kernel.util.InstanceFactory;
036    import com.liferay.portal.kernel.util.OrderByComparator;
037    import com.liferay.portal.kernel.util.StringBundler;
038    import com.liferay.portal.kernel.util.StringUtil;
039    import com.liferay.portal.model.CacheModel;
040    import com.liferay.portal.model.ModelListener;
041    import com.liferay.portal.service.persistence.BatchSessionUtil;
042    import com.liferay.portal.service.persistence.ResourcePersistence;
043    import com.liferay.portal.service.persistence.UserPersistence;
044    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
045    
046    import java.io.Serializable;
047    
048    import java.util.ArrayList;
049    import java.util.Collections;
050    import java.util.List;
051    
052    /**
053     * The persistence implementation for the counter service.
054     *
055     * <p>
056     * Caching information and settings can be found in <code>portal.properties</code>
057     * </p>
058     *
059     * @author Brian Wing Shun Chan
060     * @see CounterPersistence
061     * @see CounterUtil
062     * @generated
063     */
064    public class CounterPersistenceImpl extends BasePersistenceImpl<Counter>
065            implements CounterPersistence {
066            /*
067             * NOTE FOR DEVELOPERS:
068             *
069             * Never modify or reference this class directly. Always use {@link CounterUtil} to access the counter persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
070             */
071            public static final String FINDER_CLASS_NAME_ENTITY = CounterImpl.class.getName();
072            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
073                    ".List1";
074            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
075                    ".List2";
076            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
077                            CounterModelImpl.FINDER_CACHE_ENABLED, CounterImpl.class,
078                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
079            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
080                            CounterModelImpl.FINDER_CACHE_ENABLED, CounterImpl.class,
081                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
082            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
083                            CounterModelImpl.FINDER_CACHE_ENABLED, Long.class,
084                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
085    
086            /**
087             * Caches the counter in the entity cache if it is enabled.
088             *
089             * @param counter the counter
090             */
091            public void cacheResult(Counter counter) {
092                    EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
093                            CounterImpl.class, counter.getPrimaryKey(), counter);
094    
095                    counter.resetOriginalValues();
096            }
097    
098            /**
099             * Caches the counters in the entity cache if it is enabled.
100             *
101             * @param counters the counters
102             */
103            public void cacheResult(List<Counter> counters) {
104                    for (Counter counter : counters) {
105                            if (EntityCacheUtil.getResult(
106                                                    CounterModelImpl.ENTITY_CACHE_ENABLED,
107                                                    CounterImpl.class, counter.getPrimaryKey()) == null) {
108                                    cacheResult(counter);
109                            }
110                            else {
111                                    counter.resetOriginalValues();
112                            }
113                    }
114            }
115    
116            /**
117             * Clears the cache for all counters.
118             *
119             * <p>
120             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
121             * </p>
122             */
123            @Override
124            public void clearCache() {
125                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
126                            CacheRegistryUtil.clear(CounterImpl.class.getName());
127                    }
128    
129                    EntityCacheUtil.clearCache(CounterImpl.class.getName());
130    
131                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
132                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
133                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
134            }
135    
136            /**
137             * Clears the cache for the counter.
138             *
139             * <p>
140             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
141             * </p>
142             */
143            @Override
144            public void clearCache(Counter counter) {
145                    EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
146                            CounterImpl.class, counter.getPrimaryKey());
147    
148                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
149                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
150            }
151    
152            @Override
153            public void clearCache(List<Counter> counters) {
154                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
155                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
156    
157                    for (Counter counter : counters) {
158                            EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
159                                    CounterImpl.class, counter.getPrimaryKey());
160                    }
161            }
162    
163            /**
164             * Creates a new counter with the primary key. Does not add the counter to the database.
165             *
166             * @param name the primary key for the new counter
167             * @return the new counter
168             */
169            public Counter create(String name) {
170                    Counter counter = new CounterImpl();
171    
172                    counter.setNew(true);
173                    counter.setPrimaryKey(name);
174    
175                    return counter;
176            }
177    
178            /**
179             * Removes the counter with the primary key from the database. Also notifies the appropriate model listeners.
180             *
181             * @param name the primary key of the counter
182             * @return the counter that was removed
183             * @throws com.liferay.counter.NoSuchCounterException if a counter with the primary key could not be found
184             * @throws SystemException if a system exception occurred
185             */
186            public Counter remove(String name)
187                    throws NoSuchCounterException, SystemException {
188                    return remove((Serializable)name);
189            }
190    
191            /**
192             * Removes the counter with the primary key from the database. Also notifies the appropriate model listeners.
193             *
194             * @param primaryKey the primary key of the counter
195             * @return the counter that was removed
196             * @throws com.liferay.counter.NoSuchCounterException if a counter with the primary key could not be found
197             * @throws SystemException if a system exception occurred
198             */
199            @Override
200            public Counter remove(Serializable primaryKey)
201                    throws NoSuchCounterException, SystemException {
202                    Session session = null;
203    
204                    try {
205                            session = openSession();
206    
207                            Counter counter = (Counter)session.get(CounterImpl.class, primaryKey);
208    
209                            if (counter == null) {
210                                    if (_log.isWarnEnabled()) {
211                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
212                                    }
213    
214                                    throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
215                                            primaryKey);
216                            }
217    
218                            return remove(counter);
219                    }
220                    catch (NoSuchCounterException nsee) {
221                            throw nsee;
222                    }
223                    catch (Exception e) {
224                            throw processException(e);
225                    }
226                    finally {
227                            closeSession(session);
228                    }
229            }
230    
231            @Override
232            protected Counter removeImpl(Counter counter) throws SystemException {
233                    counter = toUnwrappedModel(counter);
234    
235                    Session session = null;
236    
237                    try {
238                            session = openSession();
239    
240                            BatchSessionUtil.delete(session, counter);
241                    }
242                    catch (Exception e) {
243                            throw processException(e);
244                    }
245                    finally {
246                            closeSession(session);
247                    }
248    
249                    clearCache(counter);
250    
251                    return counter;
252            }
253    
254            @Override
255            public Counter updateImpl(com.liferay.counter.model.Counter counter,
256                    boolean merge) throws SystemException {
257                    counter = toUnwrappedModel(counter);
258    
259                    Session session = null;
260    
261                    try {
262                            session = openSession();
263    
264                            BatchSessionUtil.update(session, counter, merge);
265    
266                            counter.setNew(false);
267                    }
268                    catch (Exception e) {
269                            throw processException(e);
270                    }
271                    finally {
272                            closeSession(session);
273                    }
274    
275                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
276    
277                    EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
278                            CounterImpl.class, counter.getPrimaryKey(), counter);
279    
280                    return counter;
281            }
282    
283            protected Counter toUnwrappedModel(Counter counter) {
284                    if (counter instanceof CounterImpl) {
285                            return counter;
286                    }
287    
288                    CounterImpl counterImpl = new CounterImpl();
289    
290                    counterImpl.setNew(counter.isNew());
291                    counterImpl.setPrimaryKey(counter.getPrimaryKey());
292    
293                    counterImpl.setName(counter.getName());
294                    counterImpl.setCurrentId(counter.getCurrentId());
295    
296                    return counterImpl;
297            }
298    
299            /**
300             * Returns the counter with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
301             *
302             * @param primaryKey the primary key of the counter
303             * @return the counter
304             * @throws com.liferay.portal.NoSuchModelException if a counter with the primary key could not be found
305             * @throws SystemException if a system exception occurred
306             */
307            @Override
308            public Counter findByPrimaryKey(Serializable primaryKey)
309                    throws NoSuchModelException, SystemException {
310                    return findByPrimaryKey((String)primaryKey);
311            }
312    
313            /**
314             * Returns the counter with the primary key or throws a {@link com.liferay.counter.NoSuchCounterException} if it could not be found.
315             *
316             * @param name the primary key of the counter
317             * @return the counter
318             * @throws com.liferay.counter.NoSuchCounterException if a counter with the primary key could not be found
319             * @throws SystemException if a system exception occurred
320             */
321            public Counter findByPrimaryKey(String name)
322                    throws NoSuchCounterException, SystemException {
323                    Counter counter = fetchByPrimaryKey(name);
324    
325                    if (counter == null) {
326                            if (_log.isWarnEnabled()) {
327                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
328                            }
329    
330                            throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
331                                    name);
332                    }
333    
334                    return counter;
335            }
336    
337            /**
338             * Returns the counter with the primary key or returns <code>null</code> if it could not be found.
339             *
340             * @param primaryKey the primary key of the counter
341             * @return the counter, or <code>null</code> if a counter with the primary key could not be found
342             * @throws SystemException if a system exception occurred
343             */
344            @Override
345            public Counter fetchByPrimaryKey(Serializable primaryKey)
346                    throws SystemException {
347                    return fetchByPrimaryKey((String)primaryKey);
348            }
349    
350            /**
351             * Returns the counter with the primary key or returns <code>null</code> if it could not be found.
352             *
353             * @param name the primary key of the counter
354             * @return the counter, or <code>null</code> if a counter with the primary key could not be found
355             * @throws SystemException if a system exception occurred
356             */
357            public Counter fetchByPrimaryKey(String name) throws SystemException {
358                    Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
359                                    CounterImpl.class, name);
360    
361                    if (counter == _nullCounter) {
362                            return null;
363                    }
364    
365                    if (counter == null) {
366                            Session session = null;
367    
368                            boolean hasException = false;
369    
370                            try {
371                                    session = openSession();
372    
373                                    counter = (Counter)session.get(CounterImpl.class, name);
374                            }
375                            catch (Exception e) {
376                                    hasException = true;
377    
378                                    throw processException(e);
379                            }
380                            finally {
381                                    if (counter != null) {
382                                            cacheResult(counter);
383                                    }
384                                    else if (!hasException) {
385                                            EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
386                                                    CounterImpl.class, name, _nullCounter);
387                                    }
388    
389                                    closeSession(session);
390                            }
391                    }
392    
393                    return counter;
394            }
395    
396            /**
397             * Returns all the counters.
398             *
399             * @return the counters
400             * @throws SystemException if a system exception occurred
401             */
402            public List<Counter> findAll() throws SystemException {
403                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
404            }
405    
406            /**
407             * Returns a range of all the counters.
408             *
409             * <p>
410             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
411             * </p>
412             *
413             * @param start the lower bound of the range of counters
414             * @param end the upper bound of the range of counters (not inclusive)
415             * @return the range of counters
416             * @throws SystemException if a system exception occurred
417             */
418            public List<Counter> findAll(int start, int end) throws SystemException {
419                    return findAll(start, end, null);
420            }
421    
422            /**
423             * Returns an ordered range of all the counters.
424             *
425             * <p>
426             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
427             * </p>
428             *
429             * @param start the lower bound of the range of counters
430             * @param end the upper bound of the range of counters (not inclusive)
431             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
432             * @return the ordered range of counters
433             * @throws SystemException if a system exception occurred
434             */
435            public List<Counter> findAll(int start, int end,
436                    OrderByComparator orderByComparator) throws SystemException {
437                    FinderPath finderPath = null;
438                    Object[] finderArgs = new Object[] { start, end, orderByComparator };
439    
440                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
441                                    (orderByComparator == null)) {
442                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
443                            finderArgs = FINDER_ARGS_EMPTY;
444                    }
445                    else {
446                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
447                            finderArgs = new Object[] { start, end, orderByComparator };
448                    }
449    
450                    List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(finderPath,
451                                    finderArgs, this);
452    
453                    if (list == null) {
454                            StringBundler query = null;
455                            String sql = null;
456    
457                            if (orderByComparator != null) {
458                                    query = new StringBundler(2 +
459                                                    (orderByComparator.getOrderByFields().length * 3));
460    
461                                    query.append(_SQL_SELECT_COUNTER);
462    
463                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
464                                            orderByComparator);
465    
466                                    sql = query.toString();
467                            }
468                            else {
469                                    sql = _SQL_SELECT_COUNTER;
470                            }
471    
472                            Session session = null;
473    
474                            try {
475                                    session = openSession();
476    
477                                    Query q = session.createQuery(sql);
478    
479                                    if (orderByComparator == null) {
480                                            list = (List<Counter>)QueryUtil.list(q, getDialect(),
481                                                            start, end, false);
482    
483                                            Collections.sort(list);
484                                    }
485                                    else {
486                                            list = (List<Counter>)QueryUtil.list(q, getDialect(),
487                                                            start, end);
488                                    }
489                            }
490                            catch (Exception e) {
491                                    throw processException(e);
492                            }
493                            finally {
494                                    if (list == null) {
495                                            FinderCacheUtil.removeResult(finderPath, finderArgs);
496                                    }
497                                    else {
498                                            cacheResult(list);
499    
500                                            FinderCacheUtil.putResult(finderPath, finderArgs, list);
501                                    }
502    
503                                    closeSession(session);
504                            }
505                    }
506    
507                    return list;
508            }
509    
510            /**
511             * Removes all the counters from the database.
512             *
513             * @throws SystemException if a system exception occurred
514             */
515            public void removeAll() throws SystemException {
516                    for (Counter counter : findAll()) {
517                            remove(counter);
518                    }
519            }
520    
521            /**
522             * Returns the number of counters.
523             *
524             * @return the number of counters
525             * @throws SystemException if a system exception occurred
526             */
527            public int countAll() throws SystemException {
528                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
529                                    FINDER_ARGS_EMPTY, this);
530    
531                    if (count == null) {
532                            Session session = null;
533    
534                            try {
535                                    session = openSession();
536    
537                                    Query q = session.createQuery(_SQL_COUNT_COUNTER);
538    
539                                    count = (Long)q.uniqueResult();
540                            }
541                            catch (Exception e) {
542                                    throw processException(e);
543                            }
544                            finally {
545                                    if (count == null) {
546                                            count = Long.valueOf(0);
547                                    }
548    
549                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
550                                            FINDER_ARGS_EMPTY, count);
551    
552                                    closeSession(session);
553                            }
554                    }
555    
556                    return count.intValue();
557            }
558    
559            /**
560             * Initializes the counter persistence.
561             */
562            public void afterPropertiesSet() {
563                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
564                                            com.liferay.portal.util.PropsUtil.get(
565                                                    "value.object.listener.com.liferay.counter.model.Counter")));
566    
567                    if (listenerClassNames.length > 0) {
568                            try {
569                                    List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
570    
571                                    for (String listenerClassName : listenerClassNames) {
572                                            listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
573                                                            listenerClassName));
574                                    }
575    
576                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
577                            }
578                            catch (Exception e) {
579                                    _log.error(e);
580                            }
581                    }
582            }
583    
584            public void destroy() {
585                    EntityCacheUtil.removeCache(CounterImpl.class.getName());
586                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
587                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
588            }
589    
590            @BeanReference(type = CounterPersistence.class)
591            protected CounterPersistence counterPersistence;
592            @BeanReference(type = ResourcePersistence.class)
593            protected ResourcePersistence resourcePersistence;
594            @BeanReference(type = UserPersistence.class)
595            protected UserPersistence userPersistence;
596            private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
597            private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
598            private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
599            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
600            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
601            private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
602            private static Counter _nullCounter = new CounterImpl() {
603                            @Override
604                            public Object clone() {
605                                    return this;
606                            }
607    
608                            @Override
609                            public CacheModel<Counter> toCacheModel() {
610                                    return _nullCounterCacheModel;
611                            }
612                    };
613    
614            private static CacheModel<Counter> _nullCounterCacheModel = new CacheModel<Counter>() {
615                            public Counter toEntityModel() {
616                                    return _nullCounter;
617                            }
618                    };
619    }