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