001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.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            @Override
042            public BrowserTracker getBrowserTracker(long browserTrackerId)
043                    throws PortalException, SystemException {
044    
045                    return browserTrackerPersistence.findByPrimaryKey(browserTrackerId);
046            }
047    
048            public BrowserTracker getBrowserTracker(long userId, long browserKey)
049                    throws SystemException {
050    
051                    BrowserTracker browserTracker = browserTrackerPersistence.fetchByUserId(
052                            userId);
053    
054                    if (browserTracker == null) {
055                            browserTracker = browserTrackerLocalService.updateBrowserTracker(
056                                    userId, browserKey);
057                    }
058    
059                    return browserTracker;
060            }
061    
062            public BrowserTracker updateBrowserTracker(long userId, long browserKey)
063                    throws SystemException {
064    
065                    BrowserTracker browserTracker = browserTrackerPersistence.fetchByUserId(
066                            userId);
067    
068                    if (browserTracker == null) {
069                            long browserTrackerId = counterLocalService.increment();
070    
071                            browserTracker = browserTrackerPersistence.create(browserTrackerId);
072    
073                            browserTracker.setUserId(userId);
074                    }
075    
076                    browserTracker.setBrowserKey(browserKey);
077    
078                    try {
079                            browserTrackerPersistence.update(browserTracker, false);
080                    }
081                    catch (SystemException se) {
082                            if (_log.isWarnEnabled()) {
083                                    _log.warn("Add failed, fetch {userId=" + userId + "}");
084                            }
085    
086                            browserTracker = browserTrackerPersistence.fetchByUserId(
087                                    userId, false);
088    
089                            if (browserTracker == null) {
090                                    throw se;
091                            }
092                    }
093    
094                    return browserTracker;
095            }
096    
097            private static Log _log = LogFactoryUtil.getLog(
098                    BrowserTrackerLocalServiceImpl.class);
099    
100    }