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