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