001    /**
002     * Copyright (c) 2000-2010 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                            if (counter.isCachedModel() || BatchSessionUtil.isEnabled()) {
206                                    Object staleObject = session.get(CounterImpl.class,
207                                                    counter.getPrimaryKeyObj());
208    
209                                    if (staleObject != null) {
210                                            session.evict(staleObject);
211                                    }
212                            }
213    
214                            session.delete(counter);
215    
216                            session.flush();
217                    }
218                    catch (Exception e) {
219                            throw processException(e);
220                    }
221                    finally {
222                            closeSession(session);
223                    }
224    
225                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
226    
227                    EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
228                            CounterImpl.class, counter.getPrimaryKey());
229    
230                    return counter;
231            }
232    
233            public Counter updateImpl(com.liferay.counter.model.Counter counter,
234                    boolean merge) throws SystemException {
235                    counter = toUnwrappedModel(counter);
236    
237                    Session session = null;
238    
239                    try {
240                            session = openSession();
241    
242                            BatchSessionUtil.update(session, counter, merge);
243    
244                            counter.setNew(false);
245                    }
246                    catch (Exception e) {
247                            throw processException(e);
248                    }
249                    finally {
250                            closeSession(session);
251                    }
252    
253                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
254    
255                    EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
256                            CounterImpl.class, counter.getPrimaryKey(), counter);
257    
258                    return counter;
259            }
260    
261            protected Counter toUnwrappedModel(Counter counter) {
262                    if (counter instanceof CounterImpl) {
263                            return counter;
264                    }
265    
266                    CounterImpl counterImpl = new CounterImpl();
267    
268                    counterImpl.setNew(counter.isNew());
269                    counterImpl.setPrimaryKey(counter.getPrimaryKey());
270    
271                    counterImpl.setName(counter.getName());
272                    counterImpl.setCurrentId(counter.getCurrentId());
273    
274                    return counterImpl;
275            }
276    
277            /**
278             * Finds the counter with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
279             *
280             * @param primaryKey the primary key of the counter to find
281             * @return the counter
282             * @throws com.liferay.portal.NoSuchModelException if a counter with the primary key could not be found
283             * @throws SystemException if a system exception occurred
284             */
285            public Counter findByPrimaryKey(Serializable primaryKey)
286                    throws NoSuchModelException, SystemException {
287                    return findByPrimaryKey((String)primaryKey);
288            }
289    
290            /**
291             * Finds the counter with the primary key or throws a {@link com.liferay.counter.NoSuchCounterException} if it could not be found.
292             *
293             * @param name the primary key of the counter to find
294             * @return the counter
295             * @throws com.liferay.counter.NoSuchCounterException if a counter with the primary key could not be found
296             * @throws SystemException if a system exception occurred
297             */
298            public Counter findByPrimaryKey(String name)
299                    throws NoSuchCounterException, SystemException {
300                    Counter counter = fetchByPrimaryKey(name);
301    
302                    if (counter == null) {
303                            if (_log.isWarnEnabled()) {
304                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
305                            }
306    
307                            throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
308                                    name);
309                    }
310    
311                    return counter;
312            }
313    
314            /**
315             * Finds the counter with the primary key or returns <code>null</code> if it could not be found.
316             *
317             * @param primaryKey the primary key of the counter to find
318             * @return the counter, or <code>null</code> if a counter with the primary key could not be found
319             * @throws SystemException if a system exception occurred
320             */
321            public Counter fetchByPrimaryKey(Serializable primaryKey)
322                    throws SystemException {
323                    return fetchByPrimaryKey((String)primaryKey);
324            }
325    
326            /**
327             * Finds the counter with the primary key or returns <code>null</code> if it could not be found.
328             *
329             * @param name the primary key of the counter to find
330             * @return the counter, or <code>null</code> if a counter with the primary key could not be found
331             * @throws SystemException if a system exception occurred
332             */
333            public Counter fetchByPrimaryKey(String name) throws SystemException {
334                    Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
335                                    CounterImpl.class, name, this);
336    
337                    if (counter == null) {
338                            Session session = null;
339    
340                            try {
341                                    session = openSession();
342    
343                                    counter = (Counter)session.get(CounterImpl.class, name);
344                            }
345                            catch (Exception e) {
346                                    throw processException(e);
347                            }
348                            finally {
349                                    if (counter != null) {
350                                            cacheResult(counter);
351                                    }
352    
353                                    closeSession(session);
354                            }
355                    }
356    
357                    return counter;
358            }
359    
360            /**
361             * Finds all the counters.
362             *
363             * @return the counters
364             * @throws SystemException if a system exception occurred
365             */
366            public List<Counter> findAll() throws SystemException {
367                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
368            }
369    
370            /**
371             * Finds a range of all the counters.
372             *
373             * <p>
374             * 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.
375             * </p>
376             *
377             * @param start the lower bound of the range of counters to return
378             * @param end the upper bound of the range of counters to return (not inclusive)
379             * @return the range of counters
380             * @throws SystemException if a system exception occurred
381             */
382            public List<Counter> findAll(int start, int end) throws SystemException {
383                    return findAll(start, end, null);
384            }
385    
386            /**
387             * Finds an ordered range of all the counters.
388             *
389             * <p>
390             * 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.
391             * </p>
392             *
393             * @param start the lower bound of the range of counters to return
394             * @param end the upper bound of the range of counters to return (not inclusive)
395             * @param orderByComparator the comparator to order the results by
396             * @return the ordered range of counters
397             * @throws SystemException if a system exception occurred
398             */
399            public List<Counter> findAll(int start, int end,
400                    OrderByComparator orderByComparator) throws SystemException {
401                    Object[] finderArgs = new Object[] {
402                                    String.valueOf(start), String.valueOf(end),
403                                    String.valueOf(orderByComparator)
404                            };
405    
406                    List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
407                                    finderArgs, this);
408    
409                    if (list == null) {
410                            Session session = null;
411    
412                            try {
413                                    session = openSession();
414    
415                                    StringBundler query = null;
416                                    String sql = null;
417    
418                                    if (orderByComparator != null) {
419                                            query = new StringBundler(2 +
420                                                            (orderByComparator.getOrderByFields().length * 3));
421    
422                                            query.append(_SQL_SELECT_COUNTER);
423    
424                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
425                                                    orderByComparator);
426    
427                                            sql = query.toString();
428                                    }
429                                    else {
430                                            sql = _SQL_SELECT_COUNTER;
431                                    }
432    
433                                    Query q = session.createQuery(sql);
434    
435                                    if (orderByComparator == null) {
436                                            list = (List<Counter>)QueryUtil.list(q, getDialect(),
437                                                            start, end, false);
438    
439                                            Collections.sort(list);
440                                    }
441                                    else {
442                                            list = (List<Counter>)QueryUtil.list(q, getDialect(),
443                                                            start, end);
444                                    }
445                            }
446                            catch (Exception e) {
447                                    throw processException(e);
448                            }
449                            finally {
450                                    if (list == null) {
451                                            list = new ArrayList<Counter>();
452                                    }
453    
454                                    cacheResult(list);
455    
456                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
457    
458                                    closeSession(session);
459                            }
460                    }
461    
462                    return list;
463            }
464    
465            /**
466             * Removes all the counters from the database.
467             *
468             * @throws SystemException if a system exception occurred
469             */
470            public void removeAll() throws SystemException {
471                    for (Counter counter : findAll()) {
472                            remove(counter);
473                    }
474            }
475    
476            /**
477             * Counts all the counters.
478             *
479             * @return the number of counters
480             * @throws SystemException if a system exception occurred
481             */
482            public int countAll() throws SystemException {
483                    Object[] finderArgs = new Object[0];
484    
485                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
486                                    finderArgs, this);
487    
488                    if (count == null) {
489                            Session session = null;
490    
491                            try {
492                                    session = openSession();
493    
494                                    Query q = session.createQuery(_SQL_COUNT_COUNTER);
495    
496                                    count = (Long)q.uniqueResult();
497                            }
498                            catch (Exception e) {
499                                    throw processException(e);
500                            }
501                            finally {
502                                    if (count == null) {
503                                            count = Long.valueOf(0);
504                                    }
505    
506                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
507                                            count);
508    
509                                    closeSession(session);
510                            }
511                    }
512    
513                    return count.intValue();
514            }
515    
516            /**
517             * Initializes the counter persistence.
518             */
519            public void afterPropertiesSet() {
520                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
521                                            com.liferay.portal.util.PropsUtil.get(
522                                                    "value.object.listener.com.liferay.counter.model.Counter")));
523    
524                    if (listenerClassNames.length > 0) {
525                            try {
526                                    List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
527    
528                                    for (String listenerClassName : listenerClassNames) {
529                                            listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
530                                                            listenerClassName));
531                                    }
532    
533                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
534                            }
535                            catch (Exception e) {
536                                    _log.error(e);
537                            }
538                    }
539            }
540    
541            @BeanReference(type = CounterPersistence.class)
542            protected CounterPersistence counterPersistence;
543            @BeanReference(type = ResourcePersistence.class)
544            protected ResourcePersistence resourcePersistence;
545            @BeanReference(type = UserPersistence.class)
546            protected UserPersistence userPersistence;
547            private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
548            private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
549            private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
550            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
551            private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
552    }