001    /**
002     * Copyright (c) 2000-2013 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) throws SystemException {
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                    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);
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    }