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.EntityCache;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCache;
023    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
024    import com.liferay.portal.kernel.dao.orm.FinderPath;
025    import com.liferay.portal.kernel.dao.orm.Query;
026    import com.liferay.portal.kernel.dao.orm.QueryPos;
027    import com.liferay.portal.kernel.dao.orm.QueryUtil;
028    import com.liferay.portal.kernel.dao.orm.Session;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.util.OrderByComparator;
032    import com.liferay.portal.kernel.util.StringBundler;
033    import com.liferay.portal.kernel.util.StringPool;
034    import com.liferay.portal.model.BrowserTracker;
035    import com.liferay.portal.model.CacheModel;
036    import com.liferay.portal.model.MVCCModel;
037    import com.liferay.portal.model.impl.BrowserTrackerImpl;
038    import com.liferay.portal.model.impl.BrowserTrackerModelImpl;
039    import com.liferay.portal.service.persistence.BrowserTrackerPersistence;
040    
041    import java.io.Serializable;
042    
043    import java.util.Collections;
044    import java.util.HashMap;
045    import java.util.HashSet;
046    import java.util.Iterator;
047    import java.util.List;
048    import java.util.Map;
049    import java.util.Set;
050    
051    /**
052     * The persistence implementation for the browser tracker service.
053     *
054     * <p>
055     * Caching information and settings can be found in <code>portal.properties</code>
056     * </p>
057     *
058     * @author Brian Wing Shun Chan
059     * @see BrowserTrackerPersistence
060     * @see com.liferay.portal.service.persistence.BrowserTrackerUtil
061     * @generated
062     */
063    @ProviderType
064    public class BrowserTrackerPersistenceImpl extends BasePersistenceImpl<BrowserTracker>
065            implements BrowserTrackerPersistence {
066            /*
067             * NOTE FOR DEVELOPERS:
068             *
069             * 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.
070             */
071            public static final String FINDER_CLASS_NAME_ENTITY = BrowserTrackerImpl.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(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
077                            BrowserTrackerModelImpl.FINDER_CACHE_ENABLED,
078                            BrowserTrackerImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
079                            "findAll", new String[0]);
080            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
081                            BrowserTrackerModelImpl.FINDER_CACHE_ENABLED,
082                            BrowserTrackerImpl.class,
083                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
084            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
085                            BrowserTrackerModelImpl.FINDER_CACHE_ENABLED, Long.class,
086                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
087            public static final FinderPath FINDER_PATH_FETCH_BY_USERID = new FinderPath(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
088                            BrowserTrackerModelImpl.FINDER_CACHE_ENABLED,
089                            BrowserTrackerImpl.class, FINDER_CLASS_NAME_ENTITY,
090                            "fetchByUserId", new String[] { Long.class.getName() },
091                            BrowserTrackerModelImpl.USERID_COLUMN_BITMASK);
092            public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
093                            BrowserTrackerModelImpl.FINDER_CACHE_ENABLED, Long.class,
094                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUserId",
095                            new String[] { Long.class.getName() });
096    
097            /**
098             * Returns the browser tracker where userId = &#63; or throws a {@link NoSuchBrowserTrackerException} if it could not be found.
099             *
100             * @param userId the user ID
101             * @return the matching browser tracker
102             * @throws NoSuchBrowserTrackerException if a matching browser tracker could not be found
103             */
104            @Override
105            public BrowserTracker findByUserId(long userId)
106                    throws NoSuchBrowserTrackerException {
107                    BrowserTracker browserTracker = fetchByUserId(userId);
108    
109                    if (browserTracker == null) {
110                            StringBundler msg = new StringBundler(4);
111    
112                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
113    
114                            msg.append("userId=");
115                            msg.append(userId);
116    
117                            msg.append(StringPool.CLOSE_CURLY_BRACE);
118    
119                            if (_log.isWarnEnabled()) {
120                                    _log.warn(msg.toString());
121                            }
122    
123                            throw new NoSuchBrowserTrackerException(msg.toString());
124                    }
125    
126                    return browserTracker;
127            }
128    
129            /**
130             * Returns the browser tracker where userId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
131             *
132             * @param userId the user ID
133             * @return the matching browser tracker, or <code>null</code> if a matching browser tracker could not be found
134             */
135            @Override
136            public BrowserTracker fetchByUserId(long userId) {
137                    return fetchByUserId(userId, true);
138            }
139    
140            /**
141             * Returns the browser tracker where userId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
142             *
143             * @param userId the user ID
144             * @param retrieveFromCache whether to retrieve from the finder cache
145             * @return the matching browser tracker, or <code>null</code> if a matching browser tracker could not be found
146             */
147            @Override
148            public BrowserTracker fetchByUserId(long userId, boolean retrieveFromCache) {
149                    Object[] finderArgs = new Object[] { userId };
150    
151                    Object result = null;
152    
153                    if (retrieveFromCache) {
154                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_USERID,
155                                            finderArgs, this);
156                    }
157    
158                    if (result instanceof BrowserTracker) {
159                            BrowserTracker browserTracker = (BrowserTracker)result;
160    
161                            if ((userId != browserTracker.getUserId())) {
162                                    result = null;
163                            }
164                    }
165    
166                    if (result == null) {
167                            StringBundler query = new StringBundler(3);
168    
169                            query.append(_SQL_SELECT_BROWSERTRACKER_WHERE);
170    
171                            query.append(_FINDER_COLUMN_USERID_USERID_2);
172    
173                            String sql = query.toString();
174    
175                            Session session = null;
176    
177                            try {
178                                    session = openSession();
179    
180                                    Query q = session.createQuery(sql);
181    
182                                    QueryPos qPos = QueryPos.getInstance(q);
183    
184                                    qPos.add(userId);
185    
186                                    List<BrowserTracker> list = q.list();
187    
188                                    if (list.isEmpty()) {
189                                            finderCache.putResult(FINDER_PATH_FETCH_BY_USERID,
190                                                    finderArgs, list);
191                                    }
192                                    else {
193                                            BrowserTracker browserTracker = list.get(0);
194    
195                                            result = browserTracker;
196    
197                                            cacheResult(browserTracker);
198    
199                                            if ((browserTracker.getUserId() != userId)) {
200                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_USERID,
201                                                            finderArgs, browserTracker);
202                                            }
203                                    }
204                            }
205                            catch (Exception e) {
206                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_USERID, finderArgs);
207    
208                                    throw processException(e);
209                            }
210                            finally {
211                                    closeSession(session);
212                            }
213                    }
214    
215                    if (result instanceof List<?>) {
216                            return null;
217                    }
218                    else {
219                            return (BrowserTracker)result;
220                    }
221            }
222    
223            /**
224             * Removes the browser tracker where userId = &#63; from the database.
225             *
226             * @param userId the user ID
227             * @return the browser tracker that was removed
228             */
229            @Override
230            public BrowserTracker removeByUserId(long userId)
231                    throws NoSuchBrowserTrackerException {
232                    BrowserTracker browserTracker = findByUserId(userId);
233    
234                    return remove(browserTracker);
235            }
236    
237            /**
238             * Returns the number of browser trackers where userId = &#63;.
239             *
240             * @param userId the user ID
241             * @return the number of matching browser trackers
242             */
243            @Override
244            public int countByUserId(long userId) {
245                    FinderPath finderPath = FINDER_PATH_COUNT_BY_USERID;
246    
247                    Object[] finderArgs = new Object[] { userId };
248    
249                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, 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                                    finderCache.putResult(finderPath, finderArgs, count);
274                            }
275                            catch (Exception e) {
276                                    finderCache.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                    entityCache.putResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
302                            BrowserTrackerImpl.class, browserTracker.getPrimaryKey(),
303                            browserTracker);
304    
305                    finderCache.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 (entityCache.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 EntityCache} and {@link FinderCache} are both cleared by this method.
335             * </p>
336             */
337            @Override
338            public void clearCache() {
339                    entityCache.clearCache(BrowserTrackerImpl.class);
340    
341                    finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
342                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
343                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
344            }
345    
346            /**
347             * Clears the cache for the browser tracker.
348             *
349             * <p>
350             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
351             * </p>
352             */
353            @Override
354            public void clearCache(BrowserTracker browserTracker) {
355                    entityCache.removeResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
356                            BrowserTrackerImpl.class, browserTracker.getPrimaryKey());
357    
358                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
359                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
360    
361                    clearUniqueFindersCache((BrowserTrackerModelImpl)browserTracker);
362            }
363    
364            @Override
365            public void clearCache(List<BrowserTracker> browserTrackers) {
366                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
367                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
368    
369                    for (BrowserTracker browserTracker : browserTrackers) {
370                            entityCache.removeResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
371                                    BrowserTrackerImpl.class, browserTracker.getPrimaryKey());
372    
373                            clearUniqueFindersCache((BrowserTrackerModelImpl)browserTracker);
374                    }
375            }
376    
377            protected void cacheUniqueFindersCache(
378                    BrowserTrackerModelImpl browserTrackerModelImpl, boolean isNew) {
379                    if (isNew) {
380                            Object[] args = new Object[] { browserTrackerModelImpl.getUserId() };
381    
382                            finderCache.putResult(FINDER_PATH_COUNT_BY_USERID, args,
383                                    Long.valueOf(1));
384                            finderCache.putResult(FINDER_PATH_FETCH_BY_USERID, args,
385                                    browserTrackerModelImpl);
386                    }
387                    else {
388                            if ((browserTrackerModelImpl.getColumnBitmask() &
389                                            FINDER_PATH_FETCH_BY_USERID.getColumnBitmask()) != 0) {
390                                    Object[] args = new Object[] { browserTrackerModelImpl.getUserId() };
391    
392                                    finderCache.putResult(FINDER_PATH_COUNT_BY_USERID, args,
393                                            Long.valueOf(1));
394                                    finderCache.putResult(FINDER_PATH_FETCH_BY_USERID, args,
395                                            browserTrackerModelImpl);
396                            }
397                    }
398            }
399    
400            protected void clearUniqueFindersCache(
401                    BrowserTrackerModelImpl browserTrackerModelImpl) {
402                    Object[] args = new Object[] { browserTrackerModelImpl.getUserId() };
403    
404                    finderCache.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
405                    finderCache.removeResult(FINDER_PATH_FETCH_BY_USERID, args);
406    
407                    if ((browserTrackerModelImpl.getColumnBitmask() &
408                                    FINDER_PATH_FETCH_BY_USERID.getColumnBitmask()) != 0) {
409                            args = new Object[] { browserTrackerModelImpl.getOriginalUserId() };
410    
411                            finderCache.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
412                            finderCache.removeResult(FINDER_PATH_FETCH_BY_USERID, args);
413                    }
414            }
415    
416            /**
417             * Creates a new browser tracker with the primary key. Does not add the browser tracker to the database.
418             *
419             * @param browserTrackerId the primary key for the new browser tracker
420             * @return the new browser tracker
421             */
422            @Override
423            public BrowserTracker create(long browserTrackerId) {
424                    BrowserTracker browserTracker = new BrowserTrackerImpl();
425    
426                    browserTracker.setNew(true);
427                    browserTracker.setPrimaryKey(browserTrackerId);
428    
429                    return browserTracker;
430            }
431    
432            /**
433             * Removes the browser tracker with the primary key from the database. Also notifies the appropriate model listeners.
434             *
435             * @param browserTrackerId the primary key of the browser tracker
436             * @return the browser tracker that was removed
437             * @throws NoSuchBrowserTrackerException if a browser tracker with the primary key could not be found
438             */
439            @Override
440            public BrowserTracker remove(long browserTrackerId)
441                    throws NoSuchBrowserTrackerException {
442                    return remove((Serializable)browserTrackerId);
443            }
444    
445            /**
446             * Removes the browser tracker with the primary key from the database. Also notifies the appropriate model listeners.
447             *
448             * @param primaryKey the primary key of the browser tracker
449             * @return the browser tracker that was removed
450             * @throws NoSuchBrowserTrackerException if a browser tracker with the primary key could not be found
451             */
452            @Override
453            public BrowserTracker remove(Serializable primaryKey)
454                    throws NoSuchBrowserTrackerException {
455                    Session session = null;
456    
457                    try {
458                            session = openSession();
459    
460                            BrowserTracker browserTracker = (BrowserTracker)session.get(BrowserTrackerImpl.class,
461                                            primaryKey);
462    
463                            if (browserTracker == null) {
464                                    if (_log.isWarnEnabled()) {
465                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
466                                    }
467    
468                                    throw new NoSuchBrowserTrackerException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
469                                            primaryKey);
470                            }
471    
472                            return remove(browserTracker);
473                    }
474                    catch (NoSuchBrowserTrackerException nsee) {
475                            throw nsee;
476                    }
477                    catch (Exception e) {
478                            throw processException(e);
479                    }
480                    finally {
481                            closeSession(session);
482                    }
483            }
484    
485            @Override
486            protected BrowserTracker removeImpl(BrowserTracker browserTracker) {
487                    browserTracker = toUnwrappedModel(browserTracker);
488    
489                    Session session = null;
490    
491                    try {
492                            session = openSession();
493    
494                            if (!session.contains(browserTracker)) {
495                                    browserTracker = (BrowserTracker)session.get(BrowserTrackerImpl.class,
496                                                    browserTracker.getPrimaryKeyObj());
497                            }
498    
499                            if (browserTracker != null) {
500                                    session.delete(browserTracker);
501                            }
502                    }
503                    catch (Exception e) {
504                            throw processException(e);
505                    }
506                    finally {
507                            closeSession(session);
508                    }
509    
510                    if (browserTracker != null) {
511                            clearCache(browserTracker);
512                    }
513    
514                    return browserTracker;
515            }
516    
517            @Override
518            public BrowserTracker updateImpl(BrowserTracker browserTracker) {
519                    browserTracker = toUnwrappedModel(browserTracker);
520    
521                    boolean isNew = browserTracker.isNew();
522    
523                    BrowserTrackerModelImpl browserTrackerModelImpl = (BrowserTrackerModelImpl)browserTracker;
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                                    browserTracker = (BrowserTracker)session.merge(browserTracker);
537                            }
538                    }
539                    catch (Exception e) {
540                            throw processException(e);
541                    }
542                    finally {
543                            closeSession(session);
544                    }
545    
546                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
547    
548                    if (isNew || !BrowserTrackerModelImpl.COLUMN_BITMASK_ENABLED) {
549                            finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
550                    }
551    
552                    entityCache.putResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
553                            BrowserTrackerImpl.class, browserTracker.getPrimaryKey(),
554                            browserTracker, false);
555    
556                    clearUniqueFindersCache(browserTrackerModelImpl);
557                    cacheUniqueFindersCache(browserTrackerModelImpl, isNew);
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)entityCache.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                                            entityCache.putResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
648                                                    BrowserTrackerImpl.class, primaryKey,
649                                                    _nullBrowserTracker);
650                                    }
651                            }
652                            catch (Exception e) {
653                                    entityCache.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)entityCache.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                                    entityCache.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                    return findAll(start, end, orderByComparator, true);
811            }
812    
813            /**
814             * Returns an ordered range of all the browser trackers.
815             *
816             * <p>
817             * 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.
818             * </p>
819             *
820             * @param start the lower bound of the range of browser trackers
821             * @param end the upper bound of the range of browser trackers (not inclusive)
822             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
823             * @param retrieveFromCache whether to retrieve from the finder cache
824             * @return the ordered range of browser trackers
825             */
826            @Override
827            public List<BrowserTracker> findAll(int start, int end,
828                    OrderByComparator<BrowserTracker> orderByComparator,
829                    boolean retrieveFromCache) {
830                    boolean pagination = true;
831                    FinderPath finderPath = null;
832                    Object[] finderArgs = null;
833    
834                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
835                                    (orderByComparator == null)) {
836                            pagination = false;
837                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
838                            finderArgs = FINDER_ARGS_EMPTY;
839                    }
840                    else {
841                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
842                            finderArgs = new Object[] { start, end, orderByComparator };
843                    }
844    
845                    List<BrowserTracker> list = null;
846    
847                    if (retrieveFromCache) {
848                            list = (List<BrowserTracker>)finderCache.getResult(finderPath,
849                                            finderArgs, this);
850                    }
851    
852                    if (list == null) {
853                            StringBundler query = null;
854                            String sql = null;
855    
856                            if (orderByComparator != null) {
857                                    query = new StringBundler(2 +
858                                                    (orderByComparator.getOrderByFields().length * 3));
859    
860                                    query.append(_SQL_SELECT_BROWSERTRACKER);
861    
862                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
863                                            orderByComparator);
864    
865                                    sql = query.toString();
866                            }
867                            else {
868                                    sql = _SQL_SELECT_BROWSERTRACKER;
869    
870                                    if (pagination) {
871                                            sql = sql.concat(BrowserTrackerModelImpl.ORDER_BY_JPQL);
872                                    }
873                            }
874    
875                            Session session = null;
876    
877                            try {
878                                    session = openSession();
879    
880                                    Query q = session.createQuery(sql);
881    
882                                    if (!pagination) {
883                                            list = (List<BrowserTracker>)QueryUtil.list(q,
884                                                            getDialect(), start, end, false);
885    
886                                            Collections.sort(list);
887    
888                                            list = Collections.unmodifiableList(list);
889                                    }
890                                    else {
891                                            list = (List<BrowserTracker>)QueryUtil.list(q,
892                                                            getDialect(), start, end);
893                                    }
894    
895                                    cacheResult(list);
896    
897                                    finderCache.putResult(finderPath, finderArgs, list);
898                            }
899                            catch (Exception e) {
900                                    finderCache.removeResult(finderPath, finderArgs);
901    
902                                    throw processException(e);
903                            }
904                            finally {
905                                    closeSession(session);
906                            }
907                    }
908    
909                    return list;
910            }
911    
912            /**
913             * Removes all the browser trackers from the database.
914             *
915             */
916            @Override
917            public void removeAll() {
918                    for (BrowserTracker browserTracker : findAll()) {
919                            remove(browserTracker);
920                    }
921            }
922    
923            /**
924             * Returns the number of browser trackers.
925             *
926             * @return the number of browser trackers
927             */
928            @Override
929            public int countAll() {
930                    Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
931                                    FINDER_ARGS_EMPTY, this);
932    
933                    if (count == null) {
934                            Session session = null;
935    
936                            try {
937                                    session = openSession();
938    
939                                    Query q = session.createQuery(_SQL_COUNT_BROWSERTRACKER);
940    
941                                    count = (Long)q.uniqueResult();
942    
943                                    finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
944                                            count);
945                            }
946                            catch (Exception e) {
947                                    finderCache.removeResult(FINDER_PATH_COUNT_ALL,
948                                            FINDER_ARGS_EMPTY);
949    
950                                    throw processException(e);
951                            }
952                            finally {
953                                    closeSession(session);
954                            }
955                    }
956    
957                    return count.intValue();
958            }
959    
960            @Override
961            protected Map<String, Integer> getTableColumnsMap() {
962                    return BrowserTrackerModelImpl.TABLE_COLUMNS_MAP;
963            }
964    
965            /**
966             * Initializes the browser tracker persistence.
967             */
968            public void afterPropertiesSet() {
969            }
970    
971            public void destroy() {
972                    entityCache.removeCache(BrowserTrackerImpl.class.getName());
973                    finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
974                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
975                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
976            }
977    
978            protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
979            protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
980            private static final String _SQL_SELECT_BROWSERTRACKER = "SELECT browserTracker FROM BrowserTracker browserTracker";
981            private static final String _SQL_SELECT_BROWSERTRACKER_WHERE_PKS_IN = "SELECT browserTracker FROM BrowserTracker browserTracker WHERE browserTrackerId IN (";
982            private static final String _SQL_SELECT_BROWSERTRACKER_WHERE = "SELECT browserTracker FROM BrowserTracker browserTracker WHERE ";
983            private static final String _SQL_COUNT_BROWSERTRACKER = "SELECT COUNT(browserTracker) FROM BrowserTracker browserTracker";
984            private static final String _SQL_COUNT_BROWSERTRACKER_WHERE = "SELECT COUNT(browserTracker) FROM BrowserTracker browserTracker WHERE ";
985            private static final String _ORDER_BY_ENTITY_ALIAS = "browserTracker.";
986            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No BrowserTracker exists with the primary key ";
987            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No BrowserTracker exists with the key {";
988            private static final Log _log = LogFactoryUtil.getLog(BrowserTrackerPersistenceImpl.class);
989            private static final BrowserTracker _nullBrowserTracker = new BrowserTrackerImpl() {
990                            @Override
991                            public Object clone() {
992                                    return this;
993                            }
994    
995                            @Override
996                            public CacheModel<BrowserTracker> toCacheModel() {
997                                    return _nullBrowserTrackerCacheModel;
998                            }
999                    };
1000    
1001            private static final CacheModel<BrowserTracker> _nullBrowserTrackerCacheModel =
1002                    new NullCacheModel();
1003    
1004            private static class NullCacheModel implements CacheModel<BrowserTracker>,
1005                    MVCCModel {
1006                    @Override
1007                    public long getMvccVersion() {
1008                            return -1;
1009                    }
1010    
1011                    @Override
1012                    public void setMvccVersion(long mvccVersion) {
1013                    }
1014    
1015                    @Override
1016                    public BrowserTracker toEntityModel() {
1017                            return _nullBrowserTracker;
1018                    }
1019            }
1020    }