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.setCompanyId(browserTracker.getCompanyId());
577                    browserTrackerImpl.setUserId(browserTracker.getUserId());
578                    browserTrackerImpl.setBrowserKey(browserTracker.getBrowserKey());
579    
580                    return browserTrackerImpl;
581            }
582    
583            /**
584             * Returns the browser tracker with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
585             *
586             * @param primaryKey the primary key of the browser tracker
587             * @return the browser tracker
588             * @throws NoSuchBrowserTrackerException if a browser tracker with the primary key could not be found
589             */
590            @Override
591            public BrowserTracker findByPrimaryKey(Serializable primaryKey)
592                    throws NoSuchBrowserTrackerException {
593                    BrowserTracker browserTracker = fetchByPrimaryKey(primaryKey);
594    
595                    if (browserTracker == null) {
596                            if (_log.isWarnEnabled()) {
597                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
598                            }
599    
600                            throw new NoSuchBrowserTrackerException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
601                                    primaryKey);
602                    }
603    
604                    return browserTracker;
605            }
606    
607            /**
608             * Returns the browser tracker with the primary key or throws a {@link NoSuchBrowserTrackerException} if it could not be found.
609             *
610             * @param browserTrackerId the primary key of the browser tracker
611             * @return the browser tracker
612             * @throws NoSuchBrowserTrackerException if a browser tracker with the primary key could not be found
613             */
614            @Override
615            public BrowserTracker findByPrimaryKey(long browserTrackerId)
616                    throws NoSuchBrowserTrackerException {
617                    return findByPrimaryKey((Serializable)browserTrackerId);
618            }
619    
620            /**
621             * Returns the browser tracker with the primary key or returns <code>null</code> if it could not be found.
622             *
623             * @param primaryKey the primary key of the browser tracker
624             * @return the browser tracker, or <code>null</code> if a browser tracker with the primary key could not be found
625             */
626            @Override
627            public BrowserTracker fetchByPrimaryKey(Serializable primaryKey) {
628                    BrowserTracker browserTracker = (BrowserTracker)entityCache.getResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
629                                    BrowserTrackerImpl.class, primaryKey);
630    
631                    if (browserTracker == _nullBrowserTracker) {
632                            return null;
633                    }
634    
635                    if (browserTracker == null) {
636                            Session session = null;
637    
638                            try {
639                                    session = openSession();
640    
641                                    browserTracker = (BrowserTracker)session.get(BrowserTrackerImpl.class,
642                                                    primaryKey);
643    
644                                    if (browserTracker != null) {
645                                            cacheResult(browserTracker);
646                                    }
647                                    else {
648                                            entityCache.putResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
649                                                    BrowserTrackerImpl.class, primaryKey,
650                                                    _nullBrowserTracker);
651                                    }
652                            }
653                            catch (Exception e) {
654                                    entityCache.removeResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
655                                            BrowserTrackerImpl.class, primaryKey);
656    
657                                    throw processException(e);
658                            }
659                            finally {
660                                    closeSession(session);
661                            }
662                    }
663    
664                    return browserTracker;
665            }
666    
667            /**
668             * Returns the browser tracker with the primary key or returns <code>null</code> if it could not be found.
669             *
670             * @param browserTrackerId the primary key of the browser tracker
671             * @return the browser tracker, or <code>null</code> if a browser tracker with the primary key could not be found
672             */
673            @Override
674            public BrowserTracker fetchByPrimaryKey(long browserTrackerId) {
675                    return fetchByPrimaryKey((Serializable)browserTrackerId);
676            }
677    
678            @Override
679            public Map<Serializable, BrowserTracker> fetchByPrimaryKeys(
680                    Set<Serializable> primaryKeys) {
681                    if (primaryKeys.isEmpty()) {
682                            return Collections.emptyMap();
683                    }
684    
685                    Map<Serializable, BrowserTracker> map = new HashMap<Serializable, BrowserTracker>();
686    
687                    if (primaryKeys.size() == 1) {
688                            Iterator<Serializable> iterator = primaryKeys.iterator();
689    
690                            Serializable primaryKey = iterator.next();
691    
692                            BrowserTracker browserTracker = fetchByPrimaryKey(primaryKey);
693    
694                            if (browserTracker != null) {
695                                    map.put(primaryKey, browserTracker);
696                            }
697    
698                            return map;
699                    }
700    
701                    Set<Serializable> uncachedPrimaryKeys = null;
702    
703                    for (Serializable primaryKey : primaryKeys) {
704                            BrowserTracker browserTracker = (BrowserTracker)entityCache.getResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
705                                            BrowserTrackerImpl.class, primaryKey);
706    
707                            if (browserTracker == null) {
708                                    if (uncachedPrimaryKeys == null) {
709                                            uncachedPrimaryKeys = new HashSet<Serializable>();
710                                    }
711    
712                                    uncachedPrimaryKeys.add(primaryKey);
713                            }
714                            else {
715                                    map.put(primaryKey, browserTracker);
716                            }
717                    }
718    
719                    if (uncachedPrimaryKeys == null) {
720                            return map;
721                    }
722    
723                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
724                                    1);
725    
726                    query.append(_SQL_SELECT_BROWSERTRACKER_WHERE_PKS_IN);
727    
728                    for (Serializable primaryKey : uncachedPrimaryKeys) {
729                            query.append(String.valueOf(primaryKey));
730    
731                            query.append(StringPool.COMMA);
732                    }
733    
734                    query.setIndex(query.index() - 1);
735    
736                    query.append(StringPool.CLOSE_PARENTHESIS);
737    
738                    String sql = query.toString();
739    
740                    Session session = null;
741    
742                    try {
743                            session = openSession();
744    
745                            Query q = session.createQuery(sql);
746    
747                            for (BrowserTracker browserTracker : (List<BrowserTracker>)q.list()) {
748                                    map.put(browserTracker.getPrimaryKeyObj(), browserTracker);
749    
750                                    cacheResult(browserTracker);
751    
752                                    uncachedPrimaryKeys.remove(browserTracker.getPrimaryKeyObj());
753                            }
754    
755                            for (Serializable primaryKey : uncachedPrimaryKeys) {
756                                    entityCache.putResult(BrowserTrackerModelImpl.ENTITY_CACHE_ENABLED,
757                                            BrowserTrackerImpl.class, primaryKey, _nullBrowserTracker);
758                            }
759                    }
760                    catch (Exception e) {
761                            throw processException(e);
762                    }
763                    finally {
764                            closeSession(session);
765                    }
766    
767                    return map;
768            }
769    
770            /**
771             * Returns all the browser trackers.
772             *
773             * @return the browser trackers
774             */
775            @Override
776            public List<BrowserTracker> findAll() {
777                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
778            }
779    
780            /**
781             * Returns a range of all the browser trackers.
782             *
783             * <p>
784             * 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.
785             * </p>
786             *
787             * @param start the lower bound of the range of browser trackers
788             * @param end the upper bound of the range of browser trackers (not inclusive)
789             * @return the range of browser trackers
790             */
791            @Override
792            public List<BrowserTracker> findAll(int start, int end) {
793                    return findAll(start, end, null);
794            }
795    
796            /**
797             * Returns an ordered range of all the browser trackers.
798             *
799             * <p>
800             * 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.
801             * </p>
802             *
803             * @param start the lower bound of the range of browser trackers
804             * @param end the upper bound of the range of browser trackers (not inclusive)
805             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
806             * @return the ordered range of browser trackers
807             */
808            @Override
809            public List<BrowserTracker> findAll(int start, int end,
810                    OrderByComparator<BrowserTracker> orderByComparator) {
811                    return findAll(start, end, orderByComparator, true);
812            }
813    
814            /**
815             * Returns an ordered range of all the browser trackers.
816             *
817             * <p>
818             * 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.
819             * </p>
820             *
821             * @param start the lower bound of the range of browser trackers
822             * @param end the upper bound of the range of browser trackers (not inclusive)
823             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
824             * @param retrieveFromCache whether to retrieve from the finder cache
825             * @return the ordered range of browser trackers
826             */
827            @Override
828            public List<BrowserTracker> findAll(int start, int end,
829                    OrderByComparator<BrowserTracker> orderByComparator,
830                    boolean retrieveFromCache) {
831                    boolean pagination = true;
832                    FinderPath finderPath = null;
833                    Object[] finderArgs = null;
834    
835                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
836                                    (orderByComparator == null)) {
837                            pagination = false;
838                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
839                            finderArgs = FINDER_ARGS_EMPTY;
840                    }
841                    else {
842                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
843                            finderArgs = new Object[] { start, end, orderByComparator };
844                    }
845    
846                    List<BrowserTracker> list = null;
847    
848                    if (retrieveFromCache) {
849                            list = (List<BrowserTracker>)finderCache.getResult(finderPath,
850                                            finderArgs, this);
851                    }
852    
853                    if (list == null) {
854                            StringBundler query = null;
855                            String sql = null;
856    
857                            if (orderByComparator != null) {
858                                    query = new StringBundler(2 +
859                                                    (orderByComparator.getOrderByFields().length * 3));
860    
861                                    query.append(_SQL_SELECT_BROWSERTRACKER);
862    
863                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
864                                            orderByComparator);
865    
866                                    sql = query.toString();
867                            }
868                            else {
869                                    sql = _SQL_SELECT_BROWSERTRACKER;
870    
871                                    if (pagination) {
872                                            sql = sql.concat(BrowserTrackerModelImpl.ORDER_BY_JPQL);
873                                    }
874                            }
875    
876                            Session session = null;
877    
878                            try {
879                                    session = openSession();
880    
881                                    Query q = session.createQuery(sql);
882    
883                                    if (!pagination) {
884                                            list = (List<BrowserTracker>)QueryUtil.list(q,
885                                                            getDialect(), start, end, false);
886    
887                                            Collections.sort(list);
888    
889                                            list = Collections.unmodifiableList(list);
890                                    }
891                                    else {
892                                            list = (List<BrowserTracker>)QueryUtil.list(q,
893                                                            getDialect(), start, end);
894                                    }
895    
896                                    cacheResult(list);
897    
898                                    finderCache.putResult(finderPath, finderArgs, list);
899                            }
900                            catch (Exception e) {
901                                    finderCache.removeResult(finderPath, finderArgs);
902    
903                                    throw processException(e);
904                            }
905                            finally {
906                                    closeSession(session);
907                            }
908                    }
909    
910                    return list;
911            }
912    
913            /**
914             * Removes all the browser trackers from the database.
915             *
916             */
917            @Override
918            public void removeAll() {
919                    for (BrowserTracker browserTracker : findAll()) {
920                            remove(browserTracker);
921                    }
922            }
923    
924            /**
925             * Returns the number of browser trackers.
926             *
927             * @return the number of browser trackers
928             */
929            @Override
930            public int countAll() {
931                    Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
932                                    FINDER_ARGS_EMPTY, this);
933    
934                    if (count == null) {
935                            Session session = null;
936    
937                            try {
938                                    session = openSession();
939    
940                                    Query q = session.createQuery(_SQL_COUNT_BROWSERTRACKER);
941    
942                                    count = (Long)q.uniqueResult();
943    
944                                    finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
945                                            count);
946                            }
947                            catch (Exception e) {
948                                    finderCache.removeResult(FINDER_PATH_COUNT_ALL,
949                                            FINDER_ARGS_EMPTY);
950    
951                                    throw processException(e);
952                            }
953                            finally {
954                                    closeSession(session);
955                            }
956                    }
957    
958                    return count.intValue();
959            }
960    
961            @Override
962            protected Map<String, Integer> getTableColumnsMap() {
963                    return BrowserTrackerModelImpl.TABLE_COLUMNS_MAP;
964            }
965    
966            /**
967             * Initializes the browser tracker persistence.
968             */
969            public void afterPropertiesSet() {
970            }
971    
972            public void destroy() {
973                    entityCache.removeCache(BrowserTrackerImpl.class.getName());
974                    finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
975                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
976                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
977            }
978    
979            protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
980            protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
981            private static final String _SQL_SELECT_BROWSERTRACKER = "SELECT browserTracker FROM BrowserTracker browserTracker";
982            private static final String _SQL_SELECT_BROWSERTRACKER_WHERE_PKS_IN = "SELECT browserTracker FROM BrowserTracker browserTracker WHERE browserTrackerId IN (";
983            private static final String _SQL_SELECT_BROWSERTRACKER_WHERE = "SELECT browserTracker FROM BrowserTracker browserTracker WHERE ";
984            private static final String _SQL_COUNT_BROWSERTRACKER = "SELECT COUNT(browserTracker) FROM BrowserTracker browserTracker";
985            private static final String _SQL_COUNT_BROWSERTRACKER_WHERE = "SELECT COUNT(browserTracker) FROM BrowserTracker browserTracker WHERE ";
986            private static final String _ORDER_BY_ENTITY_ALIAS = "browserTracker.";
987            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No BrowserTracker exists with the primary key ";
988            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No BrowserTracker exists with the key {";
989            private static final Log _log = LogFactoryUtil.getLog(BrowserTrackerPersistenceImpl.class);
990            private static final BrowserTracker _nullBrowserTracker = new BrowserTrackerImpl() {
991                            @Override
992                            public Object clone() {
993                                    return this;
994                            }
995    
996                            @Override
997                            public CacheModel<BrowserTracker> toCacheModel() {
998                                    return _nullBrowserTrackerCacheModel;
999                            }
1000                    };
1001    
1002            private static final CacheModel<BrowserTracker> _nullBrowserTrackerCacheModel =
1003                    new NullCacheModel();
1004    
1005            private static class NullCacheModel implements CacheModel<BrowserTracker>,
1006                    MVCCModel {
1007                    @Override
1008                    public long getMvccVersion() {
1009                            return -1;
1010                    }
1011    
1012                    @Override
1013                    public void setMvccVersion(long mvccVersion) {
1014                    }
1015    
1016                    @Override
1017                    public BrowserTracker toEntityModel() {
1018                            return _nullBrowserTracker;
1019                    }
1020            }
1021    }