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