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.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            public void deleteUserBrowserTracker(long userId) throws SystemException {
031                    try {
032                            browserTrackerPersistence.removeByUserId(userId);
033                    }
034                    catch (NoSuchBrowserTrackerException nsbte) {
035                    }
036            }
037    
038            public BrowserTracker getBrowserTracker(long userId, long browserKey)
039                    throws SystemException {
040    
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            public BrowserTracker updateBrowserTracker(long userId, long browserKey)
053                    throws SystemException {
054    
055                    BrowserTracker browserTracker = browserTrackerPersistence.fetchByUserId(
056                            userId);
057    
058                    if (browserTracker == null) {
059                            long browserTrackerId = counterLocalService.increment();
060    
061                            browserTracker = browserTrackerPersistence.create(browserTrackerId);
062    
063                            browserTracker.setUserId(userId);
064                    }
065    
066                    browserTracker.setBrowserKey(browserKey);
067    
068                    try {
069                            browserTrackerPersistence.update(browserTracker);
070                    }
071                    catch (SystemException se) {
072                            if (_log.isWarnEnabled()) {
073                                    _log.warn("Add failed, fetch {userId=" + userId + "}");
074                            }
075    
076                            browserTracker = browserTrackerPersistence.fetchByUserId(
077                                    userId, false);
078    
079                            if (browserTracker == null) {
080                                    throw se;
081                            }
082                    }
083    
084                    return browserTracker;
085            }
086    
087            private static Log _log = LogFactoryUtil.getLog(
088                    BrowserTrackerLocalServiceImpl.class);
089    
090    }