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.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.UserTracker;
020    import com.liferay.portal.model.UserTrackerPath;
021    import com.liferay.portal.service.base.UserTrackerLocalServiceBaseImpl;
022    import com.liferay.portal.util.PropsValues;
023    
024    import java.util.Date;
025    import java.util.Iterator;
026    import java.util.List;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class UserTrackerLocalServiceImpl
032            extends UserTrackerLocalServiceBaseImpl {
033    
034            @Override
035            public UserTracker addUserTracker(
036                            long companyId, long userId, Date modifiedDate, String sessionId,
037                            String remoteAddr, String remoteHost, String userAgent,
038                            List<UserTrackerPath> userTrackerPaths)
039                    throws SystemException {
040    
041                    if (PropsValues.SESSION_TRACKER_PERSISTENCE_ENABLED) {
042                            long userTrackerId = counterLocalService.increment(
043                                    UserTracker.class.getName());
044    
045                            UserTracker userTracker = userTrackerPersistence.create(
046                                    userTrackerId);
047    
048                            userTracker.setCompanyId(companyId);
049                            userTracker.setUserId(userId);
050                            userTracker.setModifiedDate(modifiedDate);
051                            userTracker.setSessionId(sessionId);
052                            userTracker.setRemoteAddr(remoteAddr);
053                            userTracker.setRemoteHost(remoteHost);
054                            userTracker.setUserAgent(userAgent);
055    
056                            userTrackerPersistence.update(userTracker, false);
057    
058                            Iterator<UserTrackerPath> itr = userTrackerPaths.iterator();
059    
060                            while (itr.hasNext()) {
061                                    UserTrackerPath userTrackerPath = itr.next();
062    
063                                    long pathId = counterLocalService.increment(
064                                            UserTrackerPath.class.getName());
065    
066                                    userTrackerPath.setUserTrackerPathId(pathId);
067                                    userTrackerPath.setUserTrackerId(userTrackerId);
068    
069                                    userTrackerPathPersistence.update(userTrackerPath, false);
070                            }
071    
072                            return userTracker;
073                    }
074                    else {
075                            return null;
076                    }
077            }
078    
079            @Override
080            public UserTracker deleteUserTracker(long userTrackerId)
081                    throws PortalException, SystemException {
082    
083                    UserTracker userTracker = userTrackerPersistence.findByPrimaryKey(
084                            userTrackerId);
085    
086                    return deleteUserTracker(userTracker);
087            }
088    
089            @Override
090            public UserTracker deleteUserTracker(UserTracker userTracker)
091                    throws SystemException {
092    
093                    // Paths
094    
095                    userTrackerPathPersistence.removeByUserTrackerId(
096                            userTracker.getUserTrackerId());
097    
098                    // User tracker
099    
100                    return userTrackerPersistence.remove(userTracker);
101            }
102    
103            @Override
104            public List<UserTracker> getUserTrackers(long companyId, int start, int end)
105                    throws SystemException {
106    
107                    return userTrackerPersistence.findByCompanyId(companyId, start, end);
108            }
109    
110    }