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.portlet.usersadmin.atom;
016    
017    import com.liferay.portal.atom.AtomPager;
018    import com.liferay.portal.atom.AtomUtil;
019    import com.liferay.portal.kernel.atom.AtomEntryContent;
020    import com.liferay.portal.kernel.atom.AtomRequestContext;
021    import com.liferay.portal.kernel.atom.BaseAtomCollectionAdapter;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.model.Address;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.security.auth.CompanyThreadLocal;
028    import com.liferay.portal.service.UserServiceUtil;
029    import com.liferay.portal.util.PortletKeys;
030    
031    import java.util.ArrayList;
032    import java.util.Collections;
033    import java.util.Date;
034    import java.util.List;
035    
036    /**
037     * @author Igor Spasic
038     */
039    public class UserAtomCollectionAdapter extends BaseAtomCollectionAdapter<User> {
040    
041            public String getCollectionName() {
042                    return _COLLECTION_NAME;
043            }
044    
045            public List<String> getEntryAuthors(User user) {
046                    List<String> authors = new ArrayList<String>();
047    
048                    authors.add(user.getFullName());
049    
050                    return authors;
051            }
052    
053            public AtomEntryContent getEntryContent(
054                    User user, AtomRequestContext atomRequestContext) {
055    
056                    StringBundler content = new StringBundler();
057    
058                    content.append(user.getScreenName());
059                    content.append(StringPool.NEW_LINE);
060                    content.append(user.getEmailAddress());
061                    content.append(StringPool.NEW_LINE);
062                    content.append(user.getFullName());
063                    content.append(StringPool.NEW_LINE);
064                    content.append(user.getJobTitle());
065                    content.append(StringPool.NEW_LINE);
066    
067                    try {
068                            List<Address> userAddresses = user.getAddresses();
069    
070                            for (Address address : userAddresses) {
071                                    content.append(address.getStreet1());
072                                    content.append(StringPool.NEW_LINE);
073                                    content.append(address.getStreet2());
074                                    content.append(StringPool.NEW_LINE);
075                                    content.append(address.getStreet3());
076                                    content.append(StringPool.NEW_LINE);
077                            }
078                    }
079                    catch (Exception e) {
080                    }
081    
082                    return new AtomEntryContent(content.toString());
083            }
084    
085            public String getEntryId(User user) {
086                    return String.valueOf(user.getUserId());
087            }
088    
089            public String getEntrySummary(User user) {
090                    return user.getFullName();
091            }
092    
093            public String getEntryTitle(User user) {
094                    return user.getScreenName();
095            }
096    
097            public Date getEntryUpdated(User user) {
098                    return user.getModifiedDate();
099            }
100    
101            public String getFeedTitle(AtomRequestContext atomRequestContext) {
102                    return AtomUtil.createFeedTitleFromPortletName(
103                            atomRequestContext, PortletKeys.USERS_ADMIN);
104            }
105    
106            @Override
107            protected User doGetEntry(
108                            String resourceName, AtomRequestContext atomRequestContext)
109                    throws Exception {
110    
111                    long userId = GetterUtil.getLong(resourceName);
112    
113                    return UserServiceUtil.getUserById(userId);
114            }
115    
116            @Override
117            protected Iterable<User> doGetFeedEntries(
118                            AtomRequestContext atomRequestContext)
119                    throws Exception {
120    
121                    long groupId = atomRequestContext.getLongParameter("groupId");
122    
123                    if (groupId > 0) {
124                            List<User> users = UserServiceUtil.getGroupUsers(groupId);
125    
126                            return users;
127                    }
128    
129                    long organizationId = atomRequestContext.getLongParameter(
130                            "organizationId");
131    
132                    if (organizationId > 0) {
133                            List<User> users = UserServiceUtil.getOrganizationUsers(
134                                    organizationId);
135    
136                            return users;
137                    }
138    
139                    long userGroupId = atomRequestContext.getLongParameter("userGroupId");
140    
141                    if (userGroupId > 0) {
142                            List<User> users = UserServiceUtil.getUserGroupUsers(userGroupId);
143    
144                            return users;
145                    }
146    
147                    long companyId = CompanyThreadLocal.getCompanyId();
148    
149                    if (companyId > 0) {
150                            int usersCount = UserServiceUtil.getCompanyUsersCount(companyId);
151    
152                            AtomPager atomPager = new AtomPager(atomRequestContext, usersCount);
153    
154                            AtomUtil.saveAtomPagerInRequest(atomRequestContext, atomPager);
155    
156                            List<User> users = UserServiceUtil.getCompanyUsers(
157                                    companyId, atomPager.getStart(), atomPager.getEnd() + 1);
158    
159                            return users;
160                    }
161    
162                    return Collections.emptyList();
163            }
164    
165            private static final String _COLLECTION_NAME = "users";
166    
167    }