001    /**
002     * Copyright (c) 2000-2012 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.model.impl;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.model.User;
021    import com.liferay.portal.model.UserTracker;
022    import com.liferay.portal.model.UserTrackerPath;
023    import com.liferay.portal.service.UserLocalServiceUtil;
024    
025    import java.util.ArrayList;
026    import java.util.List;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class UserTrackerImpl extends UserTrackerBaseImpl {
032    
033            public UserTrackerImpl() {
034            }
035    
036            public void addPath(UserTrackerPath path) {
037                    try {
038                            _paths.add(path);
039                    }
040                    catch (ArrayIndexOutOfBoundsException aioobe) {
041                            if (_log.isWarnEnabled()) {
042                                    _log.warn(aioobe);
043                            }
044                    }
045    
046                    setModifiedDate(path.getPathDate());
047            }
048    
049            @Override
050            public int compareTo(UserTracker userTracker) {
051                    String userName1 = getFullName().toLowerCase();
052                    String userName2 = userTracker.getFullName().toLowerCase();
053    
054                    int value = userName1.compareTo(userName2);
055    
056                    if (value == 0) {
057                            value = getModifiedDate().compareTo(userTracker.getModifiedDate());
058                    }
059    
060                    return value;
061            }
062    
063            public String getEmailAddress() {
064                    if (_emailAddress == null) {
065                            try {
066                                    if (_user == null) {
067                                            _user = UserLocalServiceUtil.getUserById(getUserId());
068                                    }
069    
070                                    _emailAddress = _user.getEmailAddress();
071                            }
072                            catch (Exception e) {
073                            }
074                    }
075    
076                    if (_emailAddress == null) {
077                            _emailAddress = StringPool.BLANK;
078                    }
079    
080                    return _emailAddress;
081            }
082    
083            public String getFullName() {
084                    if (_fullName == null) {
085                            try {
086                                    if (_user == null) {
087                                            _user = UserLocalServiceUtil.getUserById(getUserId());
088                                    }
089    
090                                    _fullName = _user.getFullName();
091                            }
092                            catch (Exception e) {
093                            }
094                    }
095    
096                    if (_fullName == null) {
097                            _fullName = StringPool.BLANK;
098                    }
099    
100                    return _fullName;
101            }
102    
103            public int getHits() {
104                    return _paths.size();
105            }
106    
107            public List<UserTrackerPath> getPaths() {
108                    return _paths;
109            }
110    
111            private static Log _log = LogFactoryUtil.getLog(UserTrackerImpl.class);
112    
113            private String _emailAddress;
114            private String _fullName;
115            private List<UserTrackerPath> _paths = new ArrayList<UserTrackerPath>();
116            private User _user;
117    
118    }