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