001    /**
002     * Copyright (c) 2000-2011 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.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_WITHOUT_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_WITH_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            /**
153             * Creates a new counter with the primary key. Does not add the counter to the database.
154             *
155             * @param name the primary key for the new counter
156             * @return the new counter
157             */
158            public Counter create(String name) {
159                    Counter counter = new CounterImpl();
160    
161                    counter.setNew(true);
162                    counter.setPrimaryKey(name);
163    
164                    return counter;
165            }
166    
167            /**
168             * Removes the counter with the primary key from the database. Also notifies the appropriate model listeners.
169             *
170             * @param primaryKey the primary key of the counter
171             * @return the counter that was removed
172             * @throws com.liferay.portal.NoSuchModelException if a counter with the primary key could not be found
173             * @throws SystemException if a system exception occurred
174             */
175            @Override
176            public Counter remove(Serializable primaryKey)
177                    throws NoSuchModelException, SystemException {
178                    return remove((String)primaryKey);
179            }
180    
181            /**
182             * Removes the counter with the primary key from the database. Also notifies the appropriate model listeners.
183             *
184             * @param name the primary key of the counter
185             * @return the counter that was removed
186             * @throws com.liferay.counter.NoSuchCounterException if a counter with the primary key could not be found
187             * @throws SystemException if a system exception occurred
188             */
189            public Counter remove(String name)
190                    throws NoSuchCounterException, SystemException {
191                    Session session = null;
192    
193                    try {
194                            session = openSession();
195    
196                            Counter counter = (Counter)session.get(CounterImpl.class, name);
197    
198                            if (counter == null) {
199                                    if (_log.isWarnEnabled()) {
200                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
201                                    }
202    
203                                    throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
204                                            name);
205                            }
206    
207                            return counterPersistence.remove(counter);
208                    }
209                    catch (NoSuchCounterException nsee) {
210                            throw nsee;
211                    }
212                    catch (Exception e) {
213                            throw processException(e);
214                    }
215                    finally {
216                            closeSession(session);
217                    }
218            }
219    
220            /**
221             * Removes the counter from the database. Also notifies the appropriate model listeners.
222             *
223             * @param counter the counter
224             * @return the counter that was removed
225             * @throws SystemException if a system exception occurred
226             */
227            @Override
228            public Counter remove(Counter counter) throws SystemException {
229                    return super.remove(counter);
230            }
231    
232            @Override
233            protected Counter removeImpl(Counter counter) throws SystemException {
234                    counter = toUnwrappedModel(counter);
235    
236                    Session session = null;
237    
238                    try {
239                            session = openSession();
240    
241                            BatchSessionUtil.delete(session, counter);
242                    }
243                    catch (Exception e) {
244                            throw processException(e);
245                    }
246                    finally {
247                            closeSession(session);
248                    }
249    
250                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
251                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
252    
253                    EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
254                            CounterImpl.class, counter.getPrimaryKey());
255    
256                    return counter;
257            }
258    
259            @Override
260            public Counter updateImpl(com.liferay.counter.model.Counter counter,
261                    boolean merge) throws SystemException {
262                    counter = toUnwrappedModel(counter);
263    
264                    Session session = null;
265    
266                    try {
267                            session = openSession();
268    
269                            BatchSessionUtil.update(session, counter, merge);
270    
271                            counter.setNew(false);
272                    }
273                    catch (Exception e) {
274                            throw processException(e);
275                    }
276                    finally {
277                            closeSession(session);
278                    }
279    
280                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
281    
282                    EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
283                            CounterImpl.class, counter.getPrimaryKey(), counter);
284    
285                    return counter;
286            }
287    
288            protected Counter toUnwrappedModel(Counter counter) {
289                    if (counter instanceof CounterImpl) {
290                            return counter;
291                    }
292    
293                    CounterImpl counterImpl = new CounterImpl();
294    
295                    counterImpl.setNew(counter.isNew());
296                    counterImpl.setPrimaryKey(counter.getPrimaryKey());
297    
298                    counterImpl.setName(counter.getName());
299                    counterImpl.setCurrentId(counter.getCurrentId());
300    
301                    return counterImpl;
302            }
303    
304            /**
305             * Returns the counter with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
306             *
307             * @param primaryKey the primary key of the counter
308             * @return the counter
309             * @throws com.liferay.portal.NoSuchModelException if a counter with the primary key could not be found
310             * @throws SystemException if a system exception occurred
311             */
312            @Override
313            public Counter findByPrimaryKey(Serializable primaryKey)
314                    throws NoSuchModelException, SystemException {
315                    return findByPrimaryKey((String)primaryKey);
316            }
317    
318            /**
319             * Returns the counter with the primary key or throws a {@link com.liferay.counter.NoSuchCounterException} if it could not be found.
320             *
321             * @param name the primary key of the counter
322             * @return the counter
323             * @throws com.liferay.counter.NoSuchCounterException if a counter with the primary key could not be found
324             * @throws SystemException if a system exception occurred
325             */
326            public Counter findByPrimaryKey(String name)
327                    throws NoSuchCounterException, SystemException {
328                    Counter counter = fetchByPrimaryKey(name);
329    
330                    if (counter == null) {
331                            if (_log.isWarnEnabled()) {
332                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
333                            }
334    
335                            throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
336                                    name);
337                    }
338    
339                    return counter;
340            }
341    
342            /**
343             * Returns the counter with the primary key or returns <code>null</code> if it could not be found.
344             *
345             * @param primaryKey the primary key of the counter
346             * @return the counter, or <code>null</code> if a counter with the primary key could not be found
347             * @throws SystemException if a system exception occurred
348             */
349            @Override
350            public Counter fetchByPrimaryKey(Serializable primaryKey)
351                    throws SystemException {
352                    return fetchByPrimaryKey((String)primaryKey);
353            }
354    
355            /**
356             * Returns the counter with the primary key or returns <code>null</code> if it could not be found.
357             *
358             * @param name the primary key of the counter
359             * @return the counter, or <code>null</code> if a counter with the primary key could not be found
360             * @throws SystemException if a system exception occurred
361             */
362            public Counter fetchByPrimaryKey(String name) throws SystemException {
363                    Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
364                                    CounterImpl.class, name);
365    
366                    if (counter == _nullCounter) {
367                            return null;
368                    }
369    
370                    if (counter == null) {
371                            Session session = null;
372    
373                            boolean hasException = false;
374    
375                            try {
376                                    session = openSession();
377    
378                                    counter = (Counter)session.get(CounterImpl.class, name);
379                            }
380                            catch (Exception e) {
381                                    hasException = true;
382    
383                                    throw processException(e);
384                            }
385                            finally {
386                                    if (counter != null) {
387                                            cacheResult(counter);
388                                    }
389                                    else if (!hasException) {
390                                            EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
391                                                    CounterImpl.class, name, _nullCounter);
392                                    }
393    
394                                    closeSession(session);
395                            }
396                    }
397    
398                    return counter;
399            }
400    
401            /**
402             * Returns all the counters.
403             *
404             * @return the counters
405             * @throws SystemException if a system exception occurred
406             */
407            public List<Counter> findAll() throws SystemException {
408                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
409            }
410    
411            /**
412             * Returns a range of all the counters.
413             *
414             * <p>
415             * 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.
416             * </p>
417             *
418             * @param start the lower bound of the range of counters
419             * @param end the upper bound of the range of counters (not inclusive)
420             * @return the range of counters
421             * @throws SystemException if a system exception occurred
422             */
423            public List<Counter> findAll(int start, int end) throws SystemException {
424                    return findAll(start, end, null);
425            }
426    
427            /**
428             * Returns an ordered range of all the counters.
429             *
430             * <p>
431             * 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.
432             * </p>
433             *
434             * @param start the lower bound of the range of counters
435             * @param end the upper bound of the range of counters (not inclusive)
436             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
437             * @return the ordered range of counters
438             * @throws SystemException if a system exception occurred
439             */
440            public List<Counter> findAll(int start, int end,
441                    OrderByComparator orderByComparator) throws SystemException {
442                    FinderPath finderPath = null;
443                    Object[] finderArgs = new Object[] { start, end, orderByComparator };
444    
445                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
446                                    (orderByComparator == null)) {
447                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
448                            finderArgs = FINDER_ARGS_EMPTY;
449                    }
450                    else {
451                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
452                            finderArgs = new Object[] { start, end, orderByComparator };
453                    }
454    
455                    List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(finderPath,
456                                    finderArgs, this);
457    
458                    if (list == null) {
459                            StringBundler query = null;
460                            String sql = null;
461    
462                            if (orderByComparator != null) {
463                                    query = new StringBundler(2 +
464                                                    (orderByComparator.getOrderByFields().length * 3));
465    
466                                    query.append(_SQL_SELECT_COUNTER);
467    
468                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
469                                            orderByComparator);
470    
471                                    sql = query.toString();
472                            }
473                            else {
474                                    sql = _SQL_SELECT_COUNTER;
475                            }
476    
477                            Session session = null;
478    
479                            try {
480                                    session = openSession();
481    
482                                    Query q = session.createQuery(sql);
483    
484                                    if (orderByComparator == null) {
485                                            list = (List<Counter>)QueryUtil.list(q, getDialect(),
486                                                            start, end, false);
487    
488                                            Collections.sort(list);
489                                    }
490                                    else {
491                                            list = (List<Counter>)QueryUtil.list(q, getDialect(),
492                                                            start, end);
493                                    }
494                            }
495                            catch (Exception e) {
496                                    throw processException(e);
497                            }
498                            finally {
499                                    if (list == null) {
500                                            FinderCacheUtil.removeResult(finderPath, finderArgs);
501                                    }
502                                    else {
503                                            cacheResult(list);
504    
505                                            FinderCacheUtil.putResult(finderPath, finderArgs, list);
506                                    }
507    
508                                    closeSession(session);
509                            }
510                    }
511    
512                    return list;
513            }
514    
515            /**
516             * Removes all the counters from the database.
517             *
518             * @throws SystemException if a system exception occurred
519             */
520            public void removeAll() throws SystemException {
521                    for (Counter counter : findAll()) {
522                            counterPersistence.remove(counter);
523                    }
524            }
525    
526            /**
527             * Returns the number of counters.
528             *
529             * @return the number of counters
530             * @throws SystemException if a system exception occurred
531             */
532            public int countAll() throws SystemException {
533                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
534                                    FINDER_ARGS_EMPTY, this);
535    
536                    if (count == null) {
537                            Session session = null;
538    
539                            try {
540                                    session = openSession();
541    
542                                    Query q = session.createQuery(_SQL_COUNT_COUNTER);
543    
544                                    count = (Long)q.uniqueResult();
545                            }
546                            catch (Exception e) {
547                                    throw processException(e);
548                            }
549                            finally {
550                                    if (count == null) {
551                                            count = Long.valueOf(0);
552                                    }
553    
554                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
555                                            FINDER_ARGS_EMPTY, count);
556    
557                                    closeSession(session);
558                            }
559                    }
560    
561                    return count.intValue();
562            }
563    
564            /**
565             * Initializes the counter persistence.
566             */
567            public void afterPropertiesSet() {
568                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
569                                            com.liferay.portal.util.PropsUtil.get(
570                                                    "value.object.listener.com.liferay.counter.model.Counter")));
571    
572                    if (listenerClassNames.length > 0) {
573                            try {
574                                    List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
575    
576                                    for (String listenerClassName : listenerClassNames) {
577                                            listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
578                                                            listenerClassName));
579                                    }
580    
581                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
582                            }
583                            catch (Exception e) {
584                                    _log.error(e);
585                            }
586                    }
587            }
588    
589            public void destroy() {
590                    EntityCacheUtil.removeCache(CounterImpl.class.getName());
591                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
592                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
593            }
594    
595            @BeanReference(type = CounterPersistence.class)
596            protected CounterPersistence counterPersistence;
597            @BeanReference(type = ResourcePersistence.class)
598            protected ResourcePersistence resourcePersistence;
599            @BeanReference(type = UserPersistence.class)
600            protected UserPersistence userPersistence;
601            private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
602            private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
603            private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
604            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
605            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
606            private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
607            private static Counter _nullCounter = new CounterImpl() {
608                            @Override
609                            public Object clone() {
610                                    return this;
611                            }
612    
613                            @Override
614                            public CacheModel<Counter> toCacheModel() {
615                                    return _nullCounterCacheModel;
616                            }
617                    };
618    
619            private static CacheModel<Counter> _nullCounterCacheModel = new CacheModel<Counter>() {
620                            public Counter toEntityModel() {
621                                    return _nullCounter;
622                            }
623                    };
624    }