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