1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.NoSuchBrowserTrackerException;
18  import com.liferay.portal.PortalException;
19  import com.liferay.portal.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(
89                  userId, false);
90  
91              if (browserTracker == null) {
92                  throw se;
93              }
94          }
95  
96          return browserTracker;
97      }
98  
99      private static Log _log = LogFactoryUtil.getLog(
100         BrowserTrackerLocalServiceImpl.class);
101 
102 }