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