001    /**
002     * Copyright (c) 2000-2010 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.impl;
016    
017    import com.liferay.portal.NoSuchBrowserTrackerException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.model.BrowserTracker;
023    import com.liferay.portal.service.base.BrowserTrackerLocalServiceBaseImpl;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class BrowserTrackerLocalServiceImpl
029            extends BrowserTrackerLocalServiceBaseImpl {
030    
031            public void deleteUserBrowserTracker(long userId)
032                    throws SystemException {
033    
034                    try {
035                            browserTrackerPersistence.removeByUserId(userId);
036                    }
037                    catch (NoSuchBrowserTrackerException nsbte) {
038                    }
039            }
040    
041            public BrowserTracker getBrowserTracker(long browserTrackerId)
042                    throws PortalException, SystemException {
043    
044                    return browserTrackerPersistence.findByPrimaryKey(browserTrackerId);
045            }
046    
047            public BrowserTracker getBrowserTracker(long userId, long browserKey)
048                    throws SystemException {
049    
050                    BrowserTracker browserTracker = browserTrackerPersistence.fetchByUserId(
051                            userId);
052    
053                    if (browserTracker == null) {
054                            browserTracker = browserTrackerLocalService.updateBrowserTracker(
055                                    userId, browserKey);
056                    }
057    
058                    return browserTracker;
059            }
060    
061            public BrowserTracker updateBrowserTracker(long userId, long browserKey)
062                    throws SystemException {
063    
064                    BrowserTracker browserTracker = browserTrackerPersistence.fetchByUserId(
065                            userId);
066    
067                    if (browserTracker == null) {
068                            long browserTrackerId = counterLocalService.increment();
069    
070                            browserTracker = browserTrackerPersistence.create(browserTrackerId);
071    
072                            browserTracker.setUserId(userId);
073                    }
074    
075                    browserTracker.setBrowserKey(browserKey);
076    
077                    try {
078                            browserTrackerPersistence.update(browserTracker, false);
079                    }
080                    catch (SystemException se) {
081                            if (_log.isWarnEnabled()) {
082                                    _log.warn("Add failed, fetch {userId=" + userId + "}");
083                            }
084    
085                            browserTracker = browserTrackerPersistence.fetchByUserId(
086                                    userId, false);
087    
088                            if (browserTracker == null) {
089                                    throw se;
090                            }
091                    }
092    
093                    return browserTracker;
094            }
095    
096            private static Log _log = LogFactoryUtil.getLog(
097                    BrowserTrackerLocalServiceImpl.class);
098    
099    }