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.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            @Override
037            public void addPath(UserTrackerPath path) {
038                    try {
039                            _paths.add(path);
040                    }
041                    catch (ArrayIndexOutOfBoundsException aioobe) {
042                            if (_log.isWarnEnabled()) {
043                                    _log.warn(aioobe);
044                            }
045                    }
046    
047                    setModifiedDate(path.getPathDate());
048            }
049    
050            @Override
051            public int compareTo(UserTracker userTracker) {
052                    String userName1 = getFullName().toLowerCase();
053                    String userName2 = userTracker.getFullName().toLowerCase();
054    
055                    int value = userName1.compareTo(userName2);
056    
057                    if (value == 0) {
058                            value = getModifiedDate().compareTo(userTracker.getModifiedDate());
059                    }
060    
061                    return value;
062            }
063    
064            @Override
065            public String getEmailAddress() {
066                    if (_emailAddress == null) {
067                            try {
068                                    if (_user == null) {
069                                            _user = UserLocalServiceUtil.getUserById(getUserId());
070                                    }
071    
072                                    _emailAddress = _user.getEmailAddress();
073                            }
074                            catch (Exception e) {
075                            }
076                    }
077    
078                    if (_emailAddress == null) {
079                            _emailAddress = StringPool.BLANK;
080                    }
081    
082                    return _emailAddress;
083            }
084    
085            @Override
086            public String getFullName() {
087                    if (_fullName == null) {
088                            try {
089                                    if (_user == null) {
090                                            _user = UserLocalServiceUtil.getUserById(getUserId());
091                                    }
092    
093                                    _fullName = _user.getFullName();
094                            }
095                            catch (Exception e) {
096                            }
097                    }
098    
099                    if (_fullName == null) {
100                            _fullName = StringPool.BLANK;
101                    }
102    
103                    return _fullName;
104            }
105    
106            @Override
107            public int getHits() {
108                    return _paths.size();
109            }
110    
111            @Override
112            public List<UserTrackerPath> getPaths() {
113                    return _paths;
114            }
115    
116            private static Log _log = LogFactoryUtil.getLog(UserTrackerImpl.class);
117    
118            private String _emailAddress;
119            private String _fullName;
120            private List<UserTrackerPath> _paths = new ArrayList<UserTrackerPath>();
121            private User _user;
122    
123    }