001    /**
002     * Copyright (c) 2000-2012 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            @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    }