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