1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.enterpriseadmin.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.search.Document;
28  import com.liferay.portal.kernel.search.DocumentImpl;
29  import com.liferay.portal.kernel.search.DocumentSummary;
30  import com.liferay.portal.kernel.search.Field;
31  import com.liferay.portal.kernel.search.Indexer;
32  import com.liferay.portal.kernel.search.SearchEngineUtil;
33  import com.liferay.portal.kernel.search.SearchException;
34  import com.liferay.portal.model.Contact;
35  import com.liferay.portal.model.ContactConstants;
36  import com.liferay.portal.model.Organization;
37  import com.liferay.portal.model.User;
38  import com.liferay.portal.service.OrganizationLocalServiceUtil;
39  import com.liferay.portal.service.UserLocalServiceUtil;
40  import com.liferay.portal.util.PortletKeys;
41  import com.liferay.portlet.expando.model.ExpandoBridge;
42  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
43  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
44  
45  import java.util.ArrayList;
46  import java.util.List;
47  
48  import javax.portlet.PortletURL;
49  
50  /**
51   * <a href="UserIndexer.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Raymond Augé
54   *
55   */
56  public class UserIndexer implements Indexer {
57  
58      public static final String PORTLET_ID = PortletKeys.ENTERPRISE_ADMIN_USERS;
59  
60      public static void deleteUser(long companyId, long userId)
61          throws SearchException {
62  
63          SearchEngineUtil.deleteDocument(companyId, getUserUID(userId));
64      }
65  
66      public static Document getUserDocument(
67          long companyId, long userId, String screenName, String emailAddress,
68          String firstName, String middleName, String lastName, String jobTitle,
69          boolean active, long[] groupIds, long[] organizationIds,
70          long[] roleIds, long[] userGroupIds, String[] tagsEntries,
71          ExpandoBridge expandoBridge) {
72  
73          Document doc = new DocumentImpl();
74  
75          doc.addUID(PORTLET_ID, String.valueOf(userId));
76  
77          doc.addModifiedDate();
78  
79          doc.addKeyword(Field.COMPANY_ID, companyId);
80          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
81          doc.addKeyword(Field.USER_ID, userId);
82  
83          doc.addKeyword("screenName", screenName);
84          doc.addKeyword("emailAddress", emailAddress);
85          doc.addKeyword("firstName", firstName, true);
86          doc.addKeyword("middleName", middleName, true);
87          doc.addKeyword("lastName", lastName, true);
88          doc.addKeyword("jobTitle", jobTitle);
89          doc.addKeyword("active", active);
90          doc.addKeyword("groupIds", groupIds);
91          doc.addKeyword("organizationIds", organizationIds);
92          doc.addKeyword(
93              "ancestorOrganizationIds",
94              _getAncestorOrganizationIds(userId, organizationIds));
95          doc.addKeyword("roleIds", roleIds);
96          doc.addKeyword("userGroupIds", userGroupIds);
97  
98          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
99  
100         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
101 
102         return doc;
103     }
104 
105     public static String getUserUID(long userId) {
106         Document doc = new DocumentImpl();
107 
108         doc.addUID(PORTLET_ID, String.valueOf(userId));
109 
110         return doc.get(Field.UID);
111     }
112 
113     public static void setEnabled(boolean enabled) {
114         _enabled.set(enabled);
115     }
116 
117     public static void updateUser(User user) throws SearchException {
118         if (!_enabled.get()) {
119             return;
120         }
121 
122         try {
123             if (user.isDefaultUser()) {
124                 return;
125             }
126 
127             Contact contact = user.getContact();
128 
129             String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
130                 User.class.getName(), user.getUserId());
131 
132             Document doc = getUserDocument(
133                 user.getCompanyId(), user.getUserId(), user.getScreenName(),
134                 user.getEmailAddress(), contact.getFirstName(),
135                 contact.getMiddleName(), contact.getLastName(),
136                 contact.getJobTitle(), user.getActive(),
137                 user.getGroupIds(), user.getOrganizationIds(),
138                 user.getRoleIds(), user.getUserGroupIds(), tagsEntries,
139                 user.getExpandoBridge());
140 
141             SearchEngineUtil.updateDocument(
142                 user.getCompanyId(), doc.get(Field.UID), doc);
143         }
144         catch (Exception e) {
145             throw new SearchException(e);
146         }
147     }
148 
149     public static void updateUsers(long[] userIds) throws SearchException {
150         for (long userId : userIds) {
151             try {
152                 User user = UserLocalServiceUtil.getUserById(userId);
153 
154                 updateUser(user);
155             }
156             catch (Exception e) {
157                 throw new SearchException(e);
158             }
159         }
160     }
161 
162     public static void updateUsers(List<User> users) throws SearchException {
163         for (User user : users) {
164             updateUser(user);
165         }
166     }
167 
168     public String[] getClassNames() {
169         return _CLASS_NAMES;
170     }
171 
172     public DocumentSummary getDocumentSummary(
173         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
174 
175         // Title
176 
177         String firstName = doc.get("firstName");
178         String middleName = doc.get("middleName");
179         String lastName = doc.get("lastName");
180 
181         String title = ContactConstants.getFullName(
182             firstName, middleName, lastName);
183 
184         // Content
185 
186         String content = null;
187 
188         // Portlet URL
189 
190         String userId = doc.get(Field.USER_ID);
191 
192         portletURL.setParameter("struts_action", "/enterprise_admin/edit_user");
193         portletURL.setParameter("p_u_i_d", userId);
194 
195         return new DocumentSummary(title, content, portletURL);
196     }
197 
198     public void reIndex(String className, long classPK) throws SearchException {
199         try {
200             UserLocalServiceUtil.reIndex(classPK);
201         }
202         catch (Exception e) {
203             throw new SearchException(e);
204         }
205     }
206 
207     public void reIndex(String[] ids) throws SearchException {
208         try {
209             UserLocalServiceUtil.reIndex(ids);
210         }
211         catch (Exception e) {
212             throw new SearchException(e);
213         }
214     }
215 
216     private static long[] _getAncestorOrganizationIds(
217         long userId, long[] organizationIds) {
218 
219         List<Organization> ancestorOrganizations =
220             new ArrayList<Organization>();
221 
222         for (long organizationId : organizationIds) {
223             try {
224                 Organization organization =
225                     OrganizationLocalServiceUtil.getOrganization(
226                         organizationId);
227 
228                 ancestorOrganizations.addAll(organization.getAncestors());
229             }
230             catch (Exception e) {
231                 _log.error("Error while indexing user " + userId, e);
232             }
233         }
234 
235         long[] ancestorOrganizationIds = new long[ancestorOrganizations.size()];
236 
237         for (int i = 0; i < ancestorOrganizations.size(); i++) {
238             Organization ancestorOrganization = ancestorOrganizations.get(i);
239 
240             ancestorOrganizationIds[i] =
241                 ancestorOrganization.getOrganizationId();
242         }
243 
244         return ancestorOrganizationIds;
245     }
246 
247     private static final String[] _CLASS_NAMES = new String[] {
248         User.class.getName()
249     };
250 
251     private static UserIndexerEnabled _enabled = new UserIndexerEnabled();
252     private static Log _log = LogFactoryUtil.getLog(UserIndexer.class);
253 
254 }