1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.enterpriseadmin.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.search.Document;
20  import com.liferay.portal.kernel.search.DocumentImpl;
21  import com.liferay.portal.kernel.search.DocumentSummary;
22  import com.liferay.portal.kernel.search.Field;
23  import com.liferay.portal.kernel.search.Indexer;
24  import com.liferay.portal.kernel.search.SearchEngineUtil;
25  import com.liferay.portal.kernel.search.SearchException;
26  import com.liferay.portal.model.Organization;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.security.auth.FullNameGenerator;
29  import com.liferay.portal.security.auth.FullNameGeneratorFactory;
30  import com.liferay.portal.service.OrganizationLocalServiceUtil;
31  import com.liferay.portal.service.UserLocalServiceUtil;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portlet.expando.model.ExpandoBridge;
34  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
35  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
36  
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  import javax.portlet.PortletURL;
41  
42  /**
43   * <a href="UserIndexer.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Raymond Augé
46   */
47  public class UserIndexer implements Indexer {
48  
49      public static final String PORTLET_ID = PortletKeys.ENTERPRISE_ADMIN_USERS;
50  
51      public static void deleteUser(long companyId, long userId)
52          throws SearchException {
53  
54          SearchEngineUtil.deleteDocument(companyId, getUserUID(userId));
55      }
56  
57      public static Document getUserDocument(
58          long companyId, long userId, String screenName, String emailAddress,
59          String firstName, String middleName, String lastName, String jobTitle,
60          boolean active, long[] groupIds, long[] organizationIds,
61          long[] roleIds, long[] userGroupIds, String[] tagsEntries,
62          ExpandoBridge expandoBridge) {
63  
64          Document doc = new DocumentImpl();
65  
66          doc.addUID(PORTLET_ID, String.valueOf(userId));
67  
68          doc.addModifiedDate();
69  
70          doc.addKeyword(Field.COMPANY_ID, companyId);
71          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
72          doc.addKeyword(Field.USER_ID, userId);
73  
74          doc.addKeyword("screenName", screenName);
75          doc.addKeyword("emailAddress", emailAddress);
76          doc.addKeyword("firstName", firstName, true);
77          doc.addKeyword("middleName", middleName, true);
78          doc.addKeyword("lastName", lastName, true);
79          doc.addKeyword("jobTitle", jobTitle);
80          doc.addKeyword("active", active);
81          doc.addKeyword("groupIds", groupIds);
82          doc.addKeyword("organizationIds", organizationIds);
83          doc.addKeyword(
84              "ancestorOrganizationIds",
85              _getAncestorOrganizationIds(userId, organizationIds));
86          doc.addKeyword("roleIds", roleIds);
87          doc.addKeyword("userGroupIds", userGroupIds);
88  
89          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
90  
91          doc.addKeyword(Field.ENTRY_CLASS_NAME, User.class.getName());
92          doc.addKeyword(Field.ENTRY_CLASS_PK, userId);
93  
94          ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
95  
96          return doc;
97      }
98  
99      public static String getUserUID(long userId) {
100         Document doc = new DocumentImpl();
101 
102         doc.addUID(PORTLET_ID, String.valueOf(userId));
103 
104         return doc.get(Field.UID);
105     }
106 
107     public static void updateUser(User user) throws SearchException {
108         try {
109             if (user.isDefaultUser()) {
110                 return;
111             }
112 
113             String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
114                 User.class.getName(), user.getUserId());
115 
116             Document doc = getUserDocument(
117                 user.getCompanyId(), user.getUserId(), user.getScreenName(),
118                 user.getEmailAddress(), user.getFirstName(),
119                 user.getMiddleName(), user.getLastName(), user.getJobTitle(),
120                 user.getActive(), user.getGroupIds(), user.getOrganizationIds(),
121                 user.getRoleIds(), user.getUserGroupIds(), tagsEntries,
122                 user.getExpandoBridge());
123 
124             SearchEngineUtil.updateDocument(
125                 user.getCompanyId(), doc.get(Field.UID), doc);
126         }
127         catch (Exception e) {
128             throw new SearchException(e);
129         }
130     }
131 
132     public static void updateUsers(long[] userIds) throws SearchException {
133         for (long userId : userIds) {
134             try {
135                 User user = UserLocalServiceUtil.getUserById(userId);
136 
137                 updateUser(user);
138             }
139             catch (Exception e) {
140                 throw new SearchException(e);
141             }
142         }
143     }
144 
145     public static void updateUsers(List<User> users) throws SearchException {
146         for (User user : users) {
147             updateUser(user);
148         }
149     }
150 
151     public String[] getClassNames() {
152         return _CLASS_NAMES;
153     }
154 
155     public DocumentSummary getDocumentSummary(
156         Document doc, String snippet, PortletURL portletURL) {
157 
158         // Title
159 
160         String emailAddress = doc.get("emailAddress");
161         String firstName = doc.get("firstName");
162         String middleName = doc.get("middleName");
163         String lastName = doc.get("lastName");
164 
165         FullNameGenerator fullNameGenerator =
166             FullNameGeneratorFactory.getInstance();
167 
168         String title = fullNameGenerator.getFullName(
169             firstName, middleName, lastName);
170 
171         // Content
172 
173         String content = title + " <" + emailAddress + ">";
174 
175         // Portlet URL
176 
177         String userId = doc.get(Field.USER_ID);
178 
179         portletURL.setParameter("struts_action", "/enterprise_admin/edit_user");
180         portletURL.setParameter("p_u_i_d", userId);
181 
182         return new DocumentSummary(title, content, portletURL);
183     }
184 
185     public void reIndex(String className, long classPK) throws SearchException {
186         try {
187             UserLocalServiceUtil.reIndex(classPK);
188         }
189         catch (Exception e) {
190             throw new SearchException(e);
191         }
192     }
193 
194     public void reIndex(String[] ids) throws SearchException {
195         try {
196             UserLocalServiceUtil.reIndex(ids);
197         }
198         catch (Exception e) {
199             throw new SearchException(e);
200         }
201     }
202 
203     private static long[] _getAncestorOrganizationIds(
204         long userId, long[] organizationIds) {
205 
206         List<Organization> ancestorOrganizations =
207             new ArrayList<Organization>();
208 
209         for (long organizationId : organizationIds) {
210             try {
211                 Organization organization =
212                     OrganizationLocalServiceUtil.getOrganization(
213                         organizationId);
214 
215                 ancestorOrganizations.addAll(organization.getAncestors());
216             }
217             catch (Exception e) {
218                 _log.error("Error while indexing user " + userId, e);
219             }
220         }
221 
222         long[] ancestorOrganizationIds = new long[ancestorOrganizations.size()];
223 
224         for (int i = 0; i < ancestorOrganizations.size(); i++) {
225             Organization ancestorOrganization = ancestorOrganizations.get(i);
226 
227             ancestorOrganizationIds[i] =
228                 ancestorOrganization.getOrganizationId();
229         }
230 
231         return ancestorOrganizationIds;
232     }
233 
234     private static final String[] _CLASS_NAMES = new String[] {
235         User.class.getName()
236     };
237 
238     private static Log _log = LogFactoryUtil.getLog(UserIndexer.class);
239 
240 }