1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.NoSuchBrowserTrackerException;
18  import com.liferay.portal.kernel.exception.PortalException;
19  import com.liferay.portal.kernel.exception.SystemException;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.model.BrowserTracker;
23  import com.liferay.portal.service.base.BrowserTrackerLocalServiceBaseImpl;
24  
25  /**
26   * <a href="BrowserTrackerLocalServiceImpl.java.html"><b><i>View Source</i></b>
27   * </a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class BrowserTrackerLocalServiceImpl
32      extends BrowserTrackerLocalServiceBaseImpl {
33  
34      public void deleteUserBrowserTracker(long userId)
35          throws SystemException {
36  
37          try {
38              browserTrackerPersistence.removeByUserId(userId);
39          }
40          catch (NoSuchBrowserTrackerException nsbte) {
41          }
42      }
43  
44      public BrowserTracker getBrowserTracker(long browserTrackerId)
45          throws PortalException, SystemException {
46  
47          return browserTrackerPersistence.findByPrimaryKey(browserTrackerId);
48      }
49  
50      public BrowserTracker getBrowserTracker(long userId, long browserKey)
51          throws SystemException {
52  
53          BrowserTracker browserTracker = browserTrackerPersistence.fetchByUserId(
54              userId);
55  
56          if (browserTracker == null) {
57              browserTracker = browserTrackerLocalService.updateBrowserTracker(
58                  userId, browserKey);
59          }
60  
61          return browserTracker;
62      }
63  
64      public BrowserTracker updateBrowserTracker(long userId, long browserKey)
65          throws SystemException {
66  
67          BrowserTracker browserTracker = browserTrackerPersistence.fetchByUserId(
68              userId);
69  
70          if (browserTracker == null) {
71              long browserTrackerId = counterLocalService.increment();
72  
73              browserTracker = browserTrackerPersistence.create(browserTrackerId);
74  
75              browserTracker.setUserId(userId);
76          }
77  
78          browserTracker.setBrowserKey(browserKey);
79  
80          try {
81              browserTrackerPersistence.update(browserTracker, false);
82          }
83          catch (SystemException se) {
84              if (_log.isWarnEnabled()) {
85                  _log.warn("Add failed, fetch {userId=" + userId + "}");
86              }
87  
88              browserTracker = browserTrackerPersistence.fetchByUserId(userId);
89  
90              if (browserTracker == null) {
91                  throw se;
92              }
93          }
94  
95          return browserTracker;
96      }
97  
98      private static Log _log = LogFactoryUtil.getLog(
99          BrowserTrackerLocalServiceImpl.class);
100 
101 }