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