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.kernel.util.InitialThreadLocal;
35  import com.liferay.portal.model.Contact;
36  import com.liferay.portal.model.ContactConstants;
37  import com.liferay.portal.model.Organization;
38  import com.liferay.portal.model.User;
39  import com.liferay.portal.service.OrganizationLocalServiceUtil;
40  import com.liferay.portal.service.UserLocalServiceUtil;
41  import com.liferay.portal.util.PortletKeys;
42  import com.liferay.portlet.expando.model.ExpandoBridge;
43  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
44  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
45  
46  import java.util.ArrayList;
47  import java.util.List;
48  
49  import javax.portlet.PortletURL;
50  
51  /**
52   * <a href="UserIndexer.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Raymond Augé
55   *
56   */
57  public class UserIndexer implements Indexer {
58  
59      public static final String PORTLET_ID = PortletKeys.ENTERPRISE_ADMIN_USERS;
60  
61      public static void deleteUser(long companyId, long userId)
62          throws SearchException {
63  
64          SearchEngineUtil.deleteDocument(companyId, getUserUID(userId));
65      }
66  
67      public static Document getUserDocument(
68          long companyId, long userId, String screenName, String emailAddress,
69          String firstName, String middleName, String lastName, String jobTitle,
70          boolean active, long[] groupIds, long[] organizationIds,
71          long[] roleIds, long[] userGroupIds, String[] tagsEntries,
72          ExpandoBridge expandoBridge) {
73  
74          Document doc = new DocumentImpl();
75  
76          doc.addUID(PORTLET_ID, String.valueOf(userId));
77  
78          doc.addModifiedDate();
79  
80          doc.addKeyword(Field.COMPANY_ID, companyId);
81          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
82          doc.addKeyword(Field.USER_ID, userId);
83  
84          doc.addKeyword("screenName", screenName);
85          doc.addKeyword("emailAddress", emailAddress);
86          doc.addKeyword("firstName", firstName, true);
87          doc.addKeyword("middleName", middleName, true);
88          doc.addKeyword("lastName", lastName, true);
89          doc.addKeyword("jobTitle", jobTitle);
90          doc.addKeyword("active", active);
91          doc.addKeyword("groupIds", groupIds);
92          doc.addKeyword("organizationIds", organizationIds);
93          doc.addKeyword(
94              "ancestorOrganizationIds",
95              _getAncestorOrganizationIds(userId, organizationIds));
96          doc.addKeyword("roleIds", roleIds);
97          doc.addKeyword("userGroupIds", userGroupIds);
98  
99          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
100 
101         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
102 
103         return doc;
104     }
105 
106     public static String getUserUID(long userId) {
107         Document doc = new DocumentImpl();
108 
109         doc.addUID(PORTLET_ID, String.valueOf(userId));
110 
111         return doc.get(Field.UID);
112     }
113 
114     public static void setEnabled(boolean enabled) {
115         _enabled.set(enabled);
116     }
117 
118     public static void updateUser(User user) throws SearchException {
119         if (!_enabled.get()) {
120             return;
121         }
122 
123         try {
124             if (user.isDefaultUser()) {
125                 return;
126             }
127 
128             Contact contact = user.getContact();
129 
130             String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
131                 User.class.getName(), user.getUserId());
132 
133             Document doc = getUserDocument(
134                 user.getCompanyId(), user.getUserId(), user.getScreenName(),
135                 user.getEmailAddress(), contact.getFirstName(),
136                 contact.getMiddleName(), contact.getLastName(),
137                 contact.getJobTitle(), user.getActive(),
138                 user.getGroupIds(), user.getOrganizationIds(),
139                 user.getRoleIds(), user.getUserGroupIds(), tagsEntries,
140                 user.getExpandoBridge());
141 
142             SearchEngineUtil.updateDocument(
143                 user.getCompanyId(), doc.get(Field.UID), doc);
144         }
145         catch (Exception e) {
146             throw new SearchException(e);
147         }
148     }
149 
150     public static void updateUsers(long[] userIds) throws SearchException {
151         for (long userId : userIds) {
152             try {
153                 User user = UserLocalServiceUtil.getUserById(userId);
154 
155                 updateUser(user);
156             }
157             catch (Exception e) {
158                 throw new SearchException(e);
159             }
160         }
161     }
162 
163     public static void updateUsers(List<User> users) throws SearchException {
164         for (User user : users) {
165             updateUser(user);
166         }
167     }
168 
169     public String[] getClassNames() {
170         return _CLASS_NAMES;
171     }
172 
173     public DocumentSummary getDocumentSummary(
174         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
175 
176         // Title
177 
178         String firstName = doc.get("firstName");
179         String middleName = doc.get("middleName");
180         String lastName = doc.get("lastName");
181 
182         String title = ContactConstants.getFullName(
183             firstName, middleName, lastName);
184 
185         // Content
186 
187         String content = null;
188 
189         // Portlet URL
190 
191         String userId = doc.get(Field.USER_ID);
192 
193         portletURL.setParameter("struts_action", "/enterprise_admin/edit_user");
194         portletURL.setParameter("p_u_i_d", userId);
195 
196         return new DocumentSummary(title, content, portletURL);
197     }
198 
199     public void reIndex(String className, long classPK) throws SearchException {
200         try {
201             UserLocalServiceUtil.reIndex(classPK);
202         }
203         catch (Exception e) {
204             throw new SearchException(e);
205         }
206     }
207 
208     public void reIndex(String[] ids) throws SearchException {
209         try {
210             UserLocalServiceUtil.reIndex(ids);
211         }
212         catch (Exception e) {
213             throw new SearchException(e);
214         }
215     }
216 
217     private static long[] _getAncestorOrganizationIds(
218         long userId, long[] organizationIds) {
219 
220         List<Organization> ancestorOrganizations =
221             new ArrayList<Organization>();
222 
223         for (long organizationId : organizationIds) {
224             try {
225                 Organization organization =
226                     OrganizationLocalServiceUtil.getOrganization(
227                         organizationId);
228 
229                 ancestorOrganizations.addAll(organization.getAncestors());
230             }
231             catch (Exception e) {
232                 _log.error("Error while indexing user " + userId, e);
233             }
234         }
235 
236         long[] ancestorOrganizationIds = new long[ancestorOrganizations.size()];
237 
238         for (int i = 0; i < ancestorOrganizations.size(); i++) {
239             Organization ancestorOrganization = ancestorOrganizations.get(i);
240 
241             ancestorOrganizationIds[i] =
242                 ancestorOrganization.getOrganizationId();
243         }
244 
245         return ancestorOrganizationIds;
246     }
247 
248     private static final String[] _CLASS_NAMES = new String[] {
249         User.class.getName()
250     };
251 
252     private static Log _log = LogFactoryUtil.getLog(UserIndexer.class);
253 
254     private static ThreadLocal<Boolean> _enabled =
255         new InitialThreadLocal<Boolean>(Boolean.TRUE);
256 
257 }