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