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