001    /**
002     * Copyright (c) 2000-present 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.portal.service.persistence.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.NoSuchBrowserTrackerException;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.util.OrderByComparator;
030    import com.liferay.portal.kernel.util.StringBundler;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.model.BrowserTracker;
033    import com.liferay.portal.model.CacheModel;
034    import com.liferay.portal.model.MVCCModel;
035    import com.liferay.portal.model.impl.BrowserTrackerImpl;
036    import com.liferay.portal.model.impl.BrowserTrackerModelImpl;
037    import com.liferay.portal.service.persistence.BrowserTrackerPersistence;
038    
039    import java.io.Serializable;
040    
041    import java.util.Collections;
042    import java.util.HashMap;
043    import java.util.HashSet;
044    import java.util.Iterator;
045    import java.util.List;
046    import java.util.Map;
047    import java.util.Set;
048    
049    /**
050     * The persistence implementation for the browser tracker service.
051     *
052     * <p>
053     * Caching information and settings can be found in <code>portal.properties</code>
054     * </p>
055     *
056     * @author Brian Wing Shun Chan
057     * @see BrowserTrackerPersistence
058     * @see com.liferay.portal.service.persistence.BrowserTrackerUtil
059     * @generated
060     */
061    @ProviderType
062    public class BrowserTrackerPersistenceImpl extends BasePersistenceImpl<BrowserTracker>
063            implements BrowserTrackerPersistence {
064            /*
065             * NOTE FOR DEVELOPERS:
066             *
067             * Never modify or reference this class directly. Always use {@link BrowserTrackerUtil} to access the browser tracker persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
068             */
069            public static final String FINDER_CLASS_NAME_ENTITY = BrowserTrackerImpl.class.getName();
070            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
071                    ".List1";
072            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
073                    ".List2";
074            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
075                            BrowserTrackerModelImpl.FINDER_CACHE_ENABLED,
076                            BrowserTrackerImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
077                            "findAll", new String[0]);
078            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
079                            BrowserTrackerModelImpl.FINDER_CACHE_ENABLED,
080                            BrowserTrackerImpl.class,
081                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
082            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
083                            BrowserTrackerModelImpl.FINDER_CACHE_ENABLED, Long.class,
084                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
085            public static final FinderPath FINDER_PATH_FETCH_BY_USERID = new FinderPath(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
086                            BrowserTrackerModelImpl.FINDER_CACHE_ENABLED,
087                            BrowserTrackerImpl.class, FINDER_CLASS_NAME_ENTITY,
088                            "fetchByUserId", new String[] { Long.class.getName() },
089                            BrowserTrackerModelImpl.USERID_COLUMN_BITMASK);
090            public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
091                            BrowserTrackerModelImpl.FINDER_CACHE_ENABLED, Long.class,
092                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUserId",
093                            new String[] { Long.class.getName() });
094    
095            /**
096             * Returns the browser tracker where userId = &#63; or throws a {@link NoSuchBrowserTrackerException} if it could not be found.
097             *
098             * @param userId the user ID
099             * @return the matching browser tracker
100             * @throws NoSuchBrowserTrackerException if a matching browser tracker could not be found
101             */
102            @Override
103            public BrowserTracker findByUserId(long userId)
104                    throws NoSuchBrowserTrackerException {
105                    BrowserTracker browserTracker = fetchByUserId(userId);
106    
107                    if (browserTracker == null) {
108                            StringBundler msg = new StringBundler(4);
109    
110                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
111    
112                            msg.append("userId=");
113                            msg.append(userId);
114    
115                            msg.append(StringPool.CLOSE_CURLY_BRACE);
116    
117                            if (_log.isWarnEnabled()) {
118                                    _log.warn(msg.toString());
119                            }
120    
121                            throw new NoSuchBrowserTrackerException(msg.toString());
122                    }
123    
124                    return browserTracker;
125            }
126    
127            /**
128             * Returns the browser tracker where userId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
129             *
130             * @param userId the user ID
131             * @return the matching browser tracker, or <code>null</code> if a matching browser tracker could not be found
132             */
133            @Override
134            public BrowserTracker fetchByUserId(long userId) {
135                    return fetchByUserId(userId, true);
136            }
137    
138            /**
139             * Returns the browser tracker where userId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
140             *
141             * @param userId the user ID
142             * @param retrieveFromCache whether to use the finder cache
143             * @return the matching browser tracker, or <code>null</code> if a matching browser tracker could not be found
144             */
145            @Override
146            public BrowserTracker fetchByUserId(long userId, boolean retrieveFromCache) {
147                    Object[] finderArgs = new Object[] { userId };
148    
149                    Object result = null;
150    
151                    if (retrieveFromCache) {
152                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_USERID,
153                                            finderArgs, this);
154                    }
155    
156                    if (result instanceof BrowserTracker) {
157                            BrowserTracker browserTracker = (BrowserTracker)result;
158    
159                            if ((userId != browserTracker.getUserId())) {
160                                    result = null;
161                            }
162                    }
163    
164                    if (result == null) {
165                            StringBundler query = new StringBundler(3);
166    
167                            query.append(_SQL_SELECT_BROWSERTRACKER_WHERE);
168    
169                            query.append(_FINDER_COLUMN_USERID_USERID_2);
170    
171                            String sql = query.toString();
172    
173                            Session session = null;
174    
175                            try {
176                                    session = openSession();
177    
178                                    Query q = session.createQuery(sql);
179    
180                                    QueryPos qPos = QueryPos.getInstance(q);
181    
182                                    qPos.add(userId);
183    
184                                    List<BrowserTracker> list = q.list();
185    
186                                    if (list.isEmpty()) {
187                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_USERID,
188                                                    finderArgs, list);
189                                    }
190                                    else {
191                                            BrowserTracker browserTracker = list.get(0);
192    
193                                            result = browserTracker;
194    
195                                            cacheResult(browserTracker);
196    
197                                            if ((browserTracker.getUserId() != userId)) {
198                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_USERID,
199                                                            finderArgs, browserTracker);
200                                            }
201                                    }
202                            }
203                            catch (Exception e) {
204                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_USERID,
205                                            finderArgs);
206    
207                                    throw processException(e);
208                            }
209                            finally {
210                                    closeSession(session);
211                            }
212                    }
213    
214                    if (result instanceof List<?>) {
215                            return null;
216                    }
217                    else {
218                            return (BrowserTracker)result;
219                    }
220            }
221    
222            /**
223             * Removes the browser tracker where userId = &#63; from the database.
224             *
225             * @param userId the user ID
226             * @return the browser tracker that was removed
227             */
228            @Override
229            public BrowserTracker removeByUserId(long userId)
230                    throws NoSuchBrowserTrackerException {
231                    BrowserTracker browserTracker = findByUserId(userId);
232    
233                    return remove(browserTracker);
234            }
235    
236            /**
237             * Returns the number of browser trackers where userId = &#63;.
238             *
239             * @param userId the user ID
240             * @return the number of matching browser trackers
241             */
242            @Override
243            public int countByUserId(long userId) {
244                    FinderPath finderPath = FINDER_PATH_COUNT_BY_USERID;
245    
246                    Object[] finderArgs = new Object[] { userId };
247    
248                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
249                                    this);
250    
251                    if (count == null) {
252                            StringBundler query = new StringBundler(2);
253    
254                            query.append(_SQL_COUNT_BROWSERTRACKER_WHERE);
255    
256                            query.append(_FINDER_COLUMN_USERID_USERID_2);
257    
258                            String sql = query.toString();
259    
260                            Session session = null;
261    
262                            try {
263                                    session = openSession();
264    
265                                    Query q = session.createQuery(sql);
266    
267                                    QueryPos qPos = QueryPos.getInstance(q);
268    
269                                    qPos.add(userId);
270    
271                                    count = (Long)q.uniqueResult();
272    
273                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
274                            }
275                            catch (Exception e) {
276                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
277    
278                                    throw processException(e);
279                            }
280                            finally {
281                                    closeSession(session);
282                            }
283                    }
284    
285                    return count.intValue();
286            }
287    
288            private static final String _FINDER_COLUMN_USERID_USERID_2 = "browserTracker.userId = ?";
289    
290            public BrowserTrackerPersistenceImpl() {
291                    setModelClass(BrowserTracker.class);
292            }
293    
294            /**
295             * Caches the browser tracker in the entity cache if it is enabled.
296             *
297             * @param browserTracker the browser tracker
298             */
299            @Override
300            public void cacheResult(BrowserTracker browserTracker) {
301                    EntityCacheUtil.putResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
302                            BrowserTrackerImpl.class, browserTracker.getPrimaryKey(),
303                            browserTracker);
304    
305                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_USERID,
306                            new Object[] { browserTracker.getUserId() }, browserTracker);
307    
308                    browserTracker.resetOriginalValues();
309            }
310    
311            /**
312             * Caches the browser trackers in the entity cache if it is enabled.
313             *
314             * @param browserTrackers the browser trackers
315             */
316            @Override
317            public void cacheResult(List<BrowserTracker> browserTrackers) {
318                    for (BrowserTracker browserTracker : browserTrackers) {
319                            if (EntityCacheUtil.getResult(
320                                                    BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
321                                                    BrowserTrackerImpl.class, browserTracker.getPrimaryKey()) == null) {
322                                    cacheResult(browserTracker);
323                            }
324                            else {
325                                    browserTracker.resetOriginalValues();
326                            }
327                    }
328            }
329    
330            /**
331             * Clears the cache for all browser trackers.
332             *
333             * <p>
334             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
335             * </p>
336             */
337            @Override
338            public void clearCache() {
339                    EntityCacheUtil.clearCache(BrowserTrackerImpl.class);
340    
341                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
342                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
343                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
344            }
345    
346            /**
347             * Clears the cache for the browser tracker.
348             *
349             * <p>
350             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
351             * </p>
352             */
353            @Override
354            public void clearCache(BrowserTracker browserTracker) {
355                    EntityCacheUtil.removeResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
356                            BrowserTrackerImpl.class, browserTracker.getPrimaryKey());
357    
358                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
359                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
360    
361                    clearUniqueFindersCache(browserTracker);
362            }
363    
364            @Override
365            public void clearCache(List<BrowserTracker> browserTrackers) {
366                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
367                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
368    
369                    for (BrowserTracker browserTracker : browserTrackers) {
370                            EntityCacheUtil.removeResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
371                                    BrowserTrackerImpl.class, browserTracker.getPrimaryKey());
372    
373                            clearUniqueFindersCache(browserTracker);
374                    }
375            }
376    
377            protected void cacheUniqueFindersCache(BrowserTracker browserTracker) {
378                    if (browserTracker.isNew()) {
379                            Object[] args = new Object[] { browserTracker.getUserId() };
380    
381                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_USERID, args,
382                                    Long.valueOf(1));
383                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_USERID, args,
384                                    browserTracker);
385                    }
386                    else {
387                            BrowserTrackerModelImpl browserTrackerModelImpl = (BrowserTrackerModelImpl)browserTracker;
388    
389                            if ((browserTrackerModelImpl.getColumnBitmask() &
390                                            FINDER_PATH_FETCH_BY_USERID.getColumnBitmask()) != 0) {
391                                    Object[] args = new Object[] { browserTracker.getUserId() };
392    
393                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_USERID, args,
394                                            Long.valueOf(1));
395                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_USERID, args,
396                                            browserTracker);
397                            }
398                    }
399            }
400    
401            protected void clearUniqueFindersCache(BrowserTracker browserTracker) {
402                    BrowserTrackerModelImpl browserTrackerModelImpl = (BrowserTrackerModelImpl)browserTracker;
403    
404                    Object[] args = new Object[] { browserTracker.getUserId() };
405    
406                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
407                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_USERID, args);
408    
409                    if ((browserTrackerModelImpl.getColumnBitmask() &
410                                    FINDER_PATH_FETCH_BY_USERID.getColumnBitmask()) != 0) {
411                            args = new Object[] { browserTrackerModelImpl.getOriginalUserId() };
412    
413                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
414                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_USERID, args);
415                    }
416            }
417    
418            /**
419             * Creates a new browser tracker with the primary key. Does not add the browser tracker to the database.
420             *
421             * @param browserTrackerId the primary key for the new browser tracker
422             * @return the new browser tracker
423             */
424            @Override
425            public BrowserTracker create(long browserTrackerId) {
426                    BrowserTracker browserTracker = new BrowserTrackerImpl();
427    
428                    browserTracker.setNew(true);
429                    browserTracker.setPrimaryKey(browserTrackerId);
430    
431                    return browserTracker;
432            }
433    
434            /**
435             * Removes the browser tracker with the primary key from the database. Also notifies the appropriate model listeners.
436             *
437             * @param browserTrackerId the primary key of the browser tracker
438             * @return the browser tracker that was removed
439             * @throws NoSuchBrowserTrackerException if a browser tracker with the primary key could not be found
440             */
441            @Override
442            public BrowserTracker remove(long browserTrackerId)
443                    throws NoSuchBrowserTrackerException {
444                    return remove((Serializable)browserTrackerId);
445            }
446    
447            /**
448             * Removes the browser tracker with the primary key from the database. Also notifies the appropriate model listeners.
449             *
450             * @param primaryKey the primary key of the browser tracker
451             * @return the browser tracker that was removed
452             * @throws NoSuchBrowserTrackerException if a browser tracker with the primary key could not be found
453             */
454            @Override
455            public BrowserTracker remove(Serializable primaryKey)
456                    throws NoSuchBrowserTrackerException {
457                    Session session = null;
458    
459                    try {
460                            session = openSession();
461    
462                            BrowserTracker browserTracker = (BrowserTracker)session.get(BrowserTrackerImpl.class,
463                                            primaryKey);
464    
465                            if (browserTracker == null) {
466                                    if (_log.isWarnEnabled()) {
467                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
468                                    }
469    
470                                    throw new NoSuchBrowserTrackerException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
471                                            primaryKey);
472                            }
473    
474                            return remove(browserTracker);
475                    }
476                    catch (NoSuchBrowserTrackerException nsee) {
477                            throw nsee;
478                    }
479                    catch (Exception e) {
480                            throw processException(e);
481                    }
482                    finally {
483                            closeSession(session);
484                    }
485            }
486    
487            @Override
488            protected BrowserTracker removeImpl(BrowserTracker browserTracker) {
489                    browserTracker = toUnwrappedModel(browserTracker);
490    
491                    Session session = null;
492    
493                    try {
494                            session = openSession();
495    
496                            if (!session.contains(browserTracker)) {
497                                    browserTracker = (BrowserTracker)session.get(BrowserTrackerImpl.class,
498                                                    browserTracker.getPrimaryKeyObj());
499                            }
500    
501                            if (browserTracker != null) {
502                                    session.delete(browserTracker);
503                            }
504                    }
505                    catch (Exception e) {
506                            throw processException(e);
507                    }
508                    finally {
509                            closeSession(session);
510                    }
511    
512                    if (browserTracker != null) {
513                            clearCache(browserTracker);
514                    }
515    
516                    return browserTracker;
517            }
518    
519            @Override
520            public BrowserTracker updateImpl(BrowserTracker browserTracker) {
521                    browserTracker = toUnwrappedModel(browserTracker);
522    
523                    boolean isNew = browserTracker.isNew();
524    
525                    Session session = null;
526    
527                    try {
528                            session = openSession();
529    
530                            if (browserTracker.isNew()) {
531                                    session.save(browserTracker);
532    
533                                    browserTracker.setNew(false);
534                            }
535                            else {
536                                    session.merge(browserTracker);
537                            }
538                    }
539                    catch (Exception e) {
540                            throw processException(e);
541                    }
542                    finally {
543                            closeSession(session);
544                    }
545    
546                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
547    
548                    if (isNew || !BrowserTrackerModelImpl.COLUMN_BITMASK_ENABLED) {
549                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
550                    }
551    
552                    EntityCacheUtil.putResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
553                            BrowserTrackerImpl.class, browserTracker.getPrimaryKey(),
554                            browserTracker, false);
555    
556                    clearUniqueFindersCache(browserTracker);
557                    cacheUniqueFindersCache(browserTracker);
558    
559                    browserTracker.resetOriginalValues();
560    
561                    return browserTracker;
562            }
563    
564            protected BrowserTracker toUnwrappedModel(BrowserTracker browserTracker) {
565                    if (browserTracker instanceof BrowserTrackerImpl) {
566                            return browserTracker;
567                    }
568    
569                    BrowserTrackerImpl browserTrackerImpl = new BrowserTrackerImpl();
570    
571                    browserTrackerImpl.setNew(browserTracker.isNew());
572                    browserTrackerImpl.setPrimaryKey(browserTracker.getPrimaryKey());
573    
574                    browserTrackerImpl.setMvccVersion(browserTracker.getMvccVersion());
575                    browserTrackerImpl.setBrowserTrackerId(browserTracker.getBrowserTrackerId());
576                    browserTrackerImpl.setUserId(browserTracker.getUserId());
577                    browserTrackerImpl.setBrowserKey(browserTracker.getBrowserKey());
578    
579                    return browserTrackerImpl;
580            }
581    
582            /**
583             * Returns the browser tracker with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
584             *
585             * @param primaryKey the primary key of the browser tracker
586             * @return the browser tracker
587             * @throws NoSuchBrowserTrackerException if a browser tracker with the primary key could not be found
588             */
589            @Override
590            public BrowserTracker findByPrimaryKey(Serializable primaryKey)
591                    throws NoSuchBrowserTrackerException {
592                    BrowserTracker browserTracker = fetchByPrimaryKey(primaryKey);
593    
594                    if (browserTracker == null) {
595                            if (_log.isWarnEnabled()) {
596                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
597                            }
598    
599                            throw new NoSuchBrowserTrackerException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
600                                    primaryKey);
601                    }
602    
603                    return browserTracker;
604            }
605    
606            /**
607             * Returns the browser tracker with the primary key or throws a {@link NoSuchBrowserTrackerException} if it could not be found.
608             *
609             * @param browserTrackerId the primary key of the browser tracker
610             * @return the browser tracker
611             * @throws NoSuchBrowserTrackerException if a browser tracker with the primary key could not be found
612             */
613            @Override
614            public BrowserTracker findByPrimaryKey(long browserTrackerId)
615                    throws NoSuchBrowserTrackerException {
616                    return findByPrimaryKey((Serializable)browserTrackerId);
617            }
618    
619            /**
620             * Returns the browser tracker with the primary key or returns <code>null</code> if it could not be found.
621             *
622             * @param primaryKey the primary key of the browser tracker
623             * @return the browser tracker, or <code>null</code> if a browser tracker with the primary key could not be found
624             */
625            @Override
626            public BrowserTracker fetchByPrimaryKey(Serializable primaryKey) {
627                    BrowserTracker browserTracker = (BrowserTracker)EntityCacheUtil.getResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
628                                    BrowserTrackerImpl.class, primaryKey);
629    
630                    if (browserTracker == _nullBrowserTracker) {
631                            return null;
632                    }
633    
634                    if (browserTracker == null) {
635                            Session session = null;
636    
637                            try {
638                                    session = openSession();
639    
640                                    browserTracker = (BrowserTracker)session.get(BrowserTrackerImpl.class,
641                                                    primaryKey);
642    
643                                    if (browserTracker != null) {
644                                            cacheResult(browserTracker);
645                                    }
646                                    else {
647                                            EntityCacheUtil.putResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
648                                                    BrowserTrackerImpl.class, primaryKey,
649                                                    _nullBrowserTracker);
650                                    }
651                            }
652                            catch (Exception e) {
653                                    EntityCacheUtil.removeResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
654                                            BrowserTrackerImpl.class, primaryKey);
655    
656                                    throw processException(e);
657                            }
658                            finally {
659                                    closeSession(session);
660                            }
661                    }
662    
663                    return browserTracker;
664            }
665    
666            /**
667             * Returns the browser tracker with the primary key or returns <code>null</code> if it could not be found.
668             *
669             * @param browserTrackerId the primary key of the browser tracker
670             * @return the browser tracker, or <code>null</code> if a browser tracker with the primary key could not be found
671             */
672            @Override
673            public BrowserTracker fetchByPrimaryKey(long browserTrackerId) {
674                    return fetchByPrimaryKey((Serializable)browserTrackerId);
675            }
676    
677            @Override
678            public Map<Serializable, BrowserTracker> fetchByPrimaryKeys(
679                    Set<Serializable> primaryKeys) {
680                    if (primaryKeys.isEmpty()) {
681                            return Collections.emptyMap();
682                    }
683    
684                    Map<Serializable, BrowserTracker> map = new HashMap<Serializable, BrowserTracker>();
685    
686                    if (primaryKeys.size() == 1) {
687                            Iterator<Serializable> iterator = primaryKeys.iterator();
688    
689                            Serializable primaryKey = iterator.next();
690    
691                            BrowserTracker browserTracker = fetchByPrimaryKey(primaryKey);
692    
693                            if (browserTracker != null) {
694                                    map.put(primaryKey, browserTracker);
695                            }
696    
697                            return map;
698                    }
699    
700                    Set<Serializable> uncachedPrimaryKeys = null;
701    
702                    for (Serializable primaryKey : primaryKeys) {
703                            BrowserTracker browserTracker = (BrowserTracker)EntityCacheUtil.getResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
704                                            BrowserTrackerImpl.class, primaryKey);
705    
706                            if (browserTracker == null) {
707                                    if (uncachedPrimaryKeys == null) {
708                                            uncachedPrimaryKeys = new HashSet<Serializable>();
709                                    }
710    
711                                    uncachedPrimaryKeys.add(primaryKey);
712                            }
713                            else {
714                                    map.put(primaryKey, browserTracker);
715                            }
716                    }
717    
718                    if (uncachedPrimaryKeys == null) {
719                            return map;
720                    }
721    
722                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
723                                    1);
724    
725                    query.append(_SQL_SELECT_BROWSERTRACKER_WHERE_PKS_IN);
726    
727                    for (Serializable primaryKey : uncachedPrimaryKeys) {
728                            query.append(String.valueOf(primaryKey));
729    
730                            query.append(StringPool.COMMA);
731                    }
732    
733                    query.setIndex(query.index() - 1);
734    
735                    query.append(StringPool.CLOSE_PARENTHESIS);
736    
737                    String sql = query.toString();
738    
739                    Session session = null;
740    
741                    try {
742                            session = openSession();
743    
744                            Query q = session.createQuery(sql);
745    
746                            for (BrowserTracker browserTracker : (List<BrowserTracker>)q.list()) {
747                                    map.put(browserTracker.getPrimaryKeyObj(), browserTracker);
748    
749                                    cacheResult(browserTracker);
750    
751                                    uncachedPrimaryKeys.remove(browserTracker.getPrimaryKeyObj());
752                            }
753    
754                            for (Serializable primaryKey : uncachedPrimaryKeys) {
755                                    EntityCacheUtil.putResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
756                                            BrowserTrackerImpl.class, primaryKey, _nullBrowserTracker);
757                            }
758                    }
759                    catch (Exception e) {
760                            throw processException(e);
761                    }
762                    finally {
763                            closeSession(session);
764                    }
765    
766                    return map;
767            }
768    
769            /**
770             * Returns all the browser trackers.
771             *
772             * @return the browser trackers
773             */
774            @Override
775            public List<BrowserTracker> findAll() {
776                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
777            }
778    
779            /**
780             * Returns a range of all the browser trackers.
781             *
782             * <p>
783             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link BrowserTrackerModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
784             * </p>
785             *
786             * @param start the lower bound of the range of browser trackers
787             * @param end the upper bound of the range of browser trackers (not inclusive)
788             * @return the range of browser trackers
789             */
790            @Override
791            public List<BrowserTracker> findAll(int start, int end) {
792                    return findAll(start, end, null);
793            }
794    
795            /**
796             * Returns an ordered range of all the browser trackers.
797             *
798             * <p>
799             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link BrowserTrackerModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
800             * </p>
801             *
802             * @param start the lower bound of the range of browser trackers
803             * @param end the upper bound of the range of browser trackers (not inclusive)
804             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
805             * @return the ordered range of browser trackers
806             */
807            @Override
808            public List<BrowserTracker> findAll(int start, int end,
809                    OrderByComparator<BrowserTracker> orderByComparator) {
810                    boolean pagination = true;
811                    FinderPath finderPath = null;
812                    Object[] finderArgs = null;
813    
814                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
815                                    (orderByComparator == null)) {
816                            pagination = false;
817                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
818                            finderArgs = FINDER_ARGS_EMPTY;
819                    }
820                    else {
821                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
822                            finderArgs = new Object[] { start, end, orderByComparator };
823                    }
824    
825                    List<BrowserTracker> list = (List<BrowserTracker>)FinderCacheUtil.getResult(finderPath,
826                                    finderArgs, this);
827    
828                    if (list == null) {
829                            StringBundler query = null;
830                            String sql = null;
831    
832                            if (orderByComparator != null) {
833                                    query = new StringBundler(2 +
834                                                    (orderByComparator.getOrderByFields().length * 3));
835    
836                                    query.append(_SQL_SELECT_BROWSERTRACKER);
837    
838                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
839                                            orderByComparator);
840    
841                                    sql = query.toString();
842                            }
843                            else {
844                                    sql = _SQL_SELECT_BROWSERTRACKER;
845    
846                                    if (pagination) {
847                                            sql = sql.concat(BrowserTrackerModelImpl.ORDER_BY_JPQL);
848                                    }
849                            }
850    
851                            Session session = null;
852    
853                            try {
854                                    session = openSession();
855    
856                                    Query q = session.createQuery(sql);
857    
858                                    if (!pagination) {
859                                            list = (List<BrowserTracker>)QueryUtil.list(q,
860                                                            getDialect(), start, end, false);
861    
862                                            Collections.sort(list);
863    
864                                            list = Collections.unmodifiableList(list);
865                                    }
866                                    else {
867                                            list = (List<BrowserTracker>)QueryUtil.list(q,
868                                                            getDialect(), start, end);
869                                    }
870    
871                                    cacheResult(list);
872    
873                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
874                            }
875                            catch (Exception e) {
876                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
877    
878                                    throw processException(e);
879                            }
880                            finally {
881                                    closeSession(session);
882                            }
883                    }
884    
885                    return list;
886            }
887    
888            /**
889             * Removes all the browser trackers from the database.
890             *
891             */
892            @Override
893            public void removeAll() {
894                    for (BrowserTracker browserTracker : findAll()) {
895                            remove(browserTracker);
896                    }
897            }
898    
899            /**
900             * Returns the number of browser trackers.
901             *
902             * @return the number of browser trackers
903             */
904            @Override
905            public int countAll() {
906                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
907                                    FINDER_ARGS_EMPTY, this);
908    
909                    if (count == null) {
910                            Session session = null;
911    
912                            try {
913                                    session = openSession();
914    
915                                    Query q = session.createQuery(_SQL_COUNT_BROWSERTRACKER);
916    
917                                    count = (Long)q.uniqueResult();
918    
919                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
920                                            FINDER_ARGS_EMPTY, count);
921                            }
922                            catch (Exception e) {
923                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
924                                            FINDER_ARGS_EMPTY);
925    
926                                    throw processException(e);
927                            }
928                            finally {
929                                    closeSession(session);
930                            }
931                    }
932    
933                    return count.intValue();
934            }
935    
936            /**
937             * Initializes the browser tracker persistence.
938             */
939            public void afterPropertiesSet() {
940            }
941    
942            public void destroy() {
943                    EntityCacheUtil.removeCache(BrowserTrackerImpl.class.getName());
944                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
945                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
946                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
947            }
948    
949            private static final String _SQL_SELECT_BROWSERTRACKER = "SELECT browserTracker FROM BrowserTracker browserTracker";
950            private static final String _SQL_SELECT_BROWSERTRACKER_WHERE_PKS_IN = "SELECT browserTracker FROM BrowserTracker browserTracker WHERE browserTrackerId IN (";
951            private static final String _SQL_SELECT_BROWSERTRACKER_WHERE = "SELECT browserTracker FROM BrowserTracker browserTracker WHERE ";
952            private static final String _SQL_COUNT_BROWSERTRACKER = "SELECT COUNT(browserTracker) FROM BrowserTracker browserTracker";
953            private static final String _SQL_COUNT_BROWSERTRACKER_WHERE = "SELECT COUNT(browserTracker) FROM BrowserTracker browserTracker WHERE ";
954            private static final String _ORDER_BY_ENTITY_ALIAS = "browserTracker.";
955            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No BrowserTracker exists with the primary key ";
956            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No BrowserTracker exists with the key {";
957            private static final Log _log = LogFactoryUtil.getLog(BrowserTrackerPersistenceImpl.class);
958            private static final BrowserTracker _nullBrowserTracker = new BrowserTrackerImpl() {
959                            @Override
960                            public Object clone() {
961                                    return this;
962                            }
963    
964                            @Override
965                            public CacheModel<BrowserTracker> toCacheModel() {
966                                    return _nullBrowserTrackerCacheModel;
967                            }
968                    };
969    
970            private static final CacheModel<BrowserTracker> _nullBrowserTrackerCacheModel =
971                    new NullCacheModel();
972    
973            private static class NullCacheModel implements CacheModel<BrowserTracker>,
974                    MVCCModel {
975                    @Override
976                    public long getMvccVersion() {
977                            return -1;
978                    }
979    
980                    @Override
981                    public void setMvccVersion(long mvccVersion) {
982                    }
983    
984                    @Override
985                    public BrowserTracker toEntityModel() {
986                            return _nullBrowserTracker;
987                    }
988            }
989    }