001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                            Session session = null;
400    
401                            try {
402                                    session = openSession();
403    
404                                    StringBundler query = null;
405                                    String sql = null;
406    
407                                    if (orderByComparator != null) {
408                                            query = new StringBundler(2 +
409                                                            (orderByComparator.getOrderByFields().length * 3));
410    
411                                            query.append(_SQL_SELECT_COUNTER);
412    
413                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
414                                                    orderByComparator);
415    
416                                            sql = query.toString();
417                                    }
418                                    else {
419                                            sql = _SQL_SELECT_COUNTER;
420                                    }
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                                            list = new ArrayList<Counter>();
441                                    }
442    
443                                    cacheResult(list);
444    
445                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
446    
447                                    closeSession(session);
448                            }
449                    }
450    
451                    return list;
452            }
453    
454            /**
455             * Removes all the counters from the database.
456             *
457             * @throws SystemException if a system exception occurred
458             */
459            public void removeAll() throws SystemException {
460                    for (Counter counter : findAll()) {
461                            remove(counter);
462                    }
463            }
464    
465            /**
466             * Counts all the counters.
467             *
468             * @return the number of counters
469             * @throws SystemException if a system exception occurred
470             */
471            public int countAll() throws SystemException {
472                    Object[] finderArgs = new Object[0];
473    
474                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
475                                    finderArgs, this);
476    
477                    if (count == null) {
478                            Session session = null;
479    
480                            try {
481                                    session = openSession();
482    
483                                    Query q = session.createQuery(_SQL_COUNT_COUNTER);
484    
485                                    count = (Long)q.uniqueResult();
486                            }
487                            catch (Exception e) {
488                                    throw processException(e);
489                            }
490                            finally {
491                                    if (count == null) {
492                                            count = Long.valueOf(0);
493                                    }
494    
495                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
496                                            count);
497    
498                                    closeSession(session);
499                            }
500                    }
501    
502                    return count.intValue();
503            }
504    
505            /**
506             * Initializes the counter persistence.
507             */
508            public void afterPropertiesSet() {
509                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
510                                            com.liferay.portal.util.PropsUtil.get(
511                                                    "value.object.listener.com.liferay.counter.model.Counter")));
512    
513                    if (listenerClassNames.length > 0) {
514                            try {
515                                    List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
516    
517                                    for (String listenerClassName : listenerClassNames) {
518                                            listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
519                                                            listenerClassName));
520                                    }
521    
522                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
523                            }
524                            catch (Exception e) {
525                                    _log.error(e);
526                            }
527                    }
528            }
529    
530            public void destroy() {
531                    EntityCacheUtil.removeCache(CounterImpl.class.getName());
532                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
533                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
534            }
535    
536            @BeanReference(type = CounterPersistence.class)
537            protected CounterPersistence counterPersistence;
538            @BeanReference(type = ResourcePersistence.class)
539            protected ResourcePersistence resourcePersistence;
540            @BeanReference(type = UserPersistence.class)
541            protected UserPersistence userPersistence;
542            private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
543            private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
544            private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
545            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
546            private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
547    }