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