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