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