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.model.ContactConstants;
35  import com.liferay.portal.model.Organization;
36  import com.liferay.portal.model.User;
37  import com.liferay.portal.service.OrganizationLocalServiceUtil;
38  import com.liferay.portal.service.UserLocalServiceUtil;
39  import com.liferay.portal.util.PortletKeys;
40  import com.liferay.portlet.expando.model.ExpandoBridge;
41  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
42  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
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  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 updateUser(User user) throws SearchException {
112         try {
113             if (user.isDefaultUser()) {
114                 return;
115             }
116 
117             String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
118                 User.class.getName(), user.getUserId());
119 
120             Document doc = getUserDocument(
121                 user.getCompanyId(), user.getUserId(), user.getScreenName(),
122                 user.getEmailAddress(), user.getFirstName(),
123                 user.getMiddleName(), user.getLastName(), user.getJobTitle(),
124                 user.getActive(), user.getGroupIds(), user.getOrganizationIds(),
125                 user.getRoleIds(), user.getUserGroupIds(), tagsEntries,
126                 user.getExpandoBridge());
127 
128             SearchEngineUtil.updateDocument(
129                 user.getCompanyId(), doc.get(Field.UID), doc);
130         }
131         catch (Exception e) {
132             throw new SearchException(e);
133         }
134     }
135 
136     public static void updateUsers(long[] userIds) throws SearchException {
137         for (long userId : userIds) {
138             try {
139                 User user = UserLocalServiceUtil.getUserById(userId);
140 
141                 updateUser(user);
142             }
143             catch (Exception e) {
144                 throw new SearchException(e);
145             }
146         }
147     }
148 
149     public static void updateUsers(List<User> users) throws SearchException {
150         for (User user : users) {
151             updateUser(user);
152         }
153     }
154 
155     public String[] getClassNames() {
156         return _CLASS_NAMES;
157     }
158 
159     public DocumentSummary getDocumentSummary(
160         Document doc, String snippet, PortletURL portletURL) {
161 
162         // Title
163 
164         String firstName = doc.get("firstName");
165         String middleName = doc.get("middleName");
166         String lastName = doc.get("lastName");
167 
168         String title = ContactConstants.getFullName(
169             firstName, middleName, lastName);
170 
171         // Content
172 
173         String content = null;
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 }