001
014
015 package com.liferay.portlet.usersadmin.util;
016
017 import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.search.BaseIndexer;
020 import com.liferay.portal.kernel.search.BooleanClauseOccur;
021 import com.liferay.portal.kernel.search.BooleanQuery;
022 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
023 import com.liferay.portal.kernel.search.Document;
024 import com.liferay.portal.kernel.search.Field;
025 import com.liferay.portal.kernel.search.SearchContext;
026 import com.liferay.portal.kernel.search.SearchEngineUtil;
027 import com.liferay.portal.kernel.search.Summary;
028 import com.liferay.portal.kernel.util.GetterUtil;
029 import com.liferay.portal.kernel.util.Validator;
030 import com.liferay.portal.model.Organization;
031 import com.liferay.portal.model.OrganizationConstants;
032 import com.liferay.portal.service.OrganizationLocalServiceUtil;
033 import com.liferay.portal.service.persistence.OrganizationActionableDynamicQuery;
034 import com.liferay.portal.util.PortletKeys;
035 import com.liferay.portal.util.PropsValues;
036
037 import java.util.ArrayList;
038 import java.util.Collection;
039 import java.util.HashMap;
040 import java.util.LinkedHashMap;
041 import java.util.List;
042 import java.util.Locale;
043 import java.util.Map;
044
045 import javax.portlet.PortletURL;
046
047
052 public class OrganizationIndexer extends BaseIndexer {
053
054 public static final String[] CLASS_NAMES = {Organization.class.getName()};
055
056 public static final String PORTLET_ID = PortletKeys.USERS_ADMIN;
057
058 public OrganizationIndexer() {
059 setStagingAware(false);
060 }
061
062 @Override
063 public String[] getClassNames() {
064 return CLASS_NAMES;
065 }
066
067 @Override
068 public String getPortletId() {
069 return PORTLET_ID;
070 }
071
072 @Override
073 public boolean isIndexerEnabled() {
074 return PropsValues.ORGANIZATIONS_INDEXER_ENABLED;
075 }
076
077 @Override
078 public boolean isPermissionAware() {
079 return _PERMISSION_AWARE;
080 }
081
082 @Override
083 public void postProcessContextQuery(
084 BooleanQuery contextQuery, SearchContext searchContext)
085 throws Exception {
086
087 LinkedHashMap<String, Object> params =
088 (LinkedHashMap<String, Object>)searchContext.getAttribute("params");
089
090 if (params == null) {
091 return;
092 }
093
094 List<Long> excludedOrganizationIds = (List<Long>)params.get(
095 "excludedOrganizationIds");
096
097 if ((excludedOrganizationIds != null) &&
098 !excludedOrganizationIds.isEmpty()) {
099
100 BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create(
101 searchContext);
102
103 for (long excludedOrganizationId : excludedOrganizationIds) {
104 booleanQuery.addTerm(
105 "organizationId", String.valueOf(excludedOrganizationId));
106 }
107
108 contextQuery.add(booleanQuery, BooleanClauseOccur.MUST_NOT);
109 }
110
111 List<Organization> organizationsTree = (List<Organization>)params.get(
112 "organizationsTree");
113
114 if ((organizationsTree != null) && !organizationsTree.isEmpty()) {
115 BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create(
116 searchContext);
117
118 for (Organization organization : organizationsTree) {
119 String treePath = organization.buildTreePath();
120
121 booleanQuery.addTerm("treePath", treePath, true);
122 }
123
124 contextQuery.add(booleanQuery, BooleanClauseOccur.MUST);
125 }
126 else {
127 long parentOrganizationId = GetterUtil.getLong(
128 searchContext.getAttribute("parentOrganizationId"));
129
130 if (parentOrganizationId !=
131 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
132
133 contextQuery.addRequiredTerm(
134 "parentOrganizationId", parentOrganizationId);
135 }
136 }
137 }
138
139 @Override
140 public void postProcessSearchQuery(
141 BooleanQuery searchQuery, SearchContext searchContext)
142 throws Exception {
143
144 addSearchTerm(searchQuery, searchContext, "city", false);
145 addSearchTerm(searchQuery, searchContext, "country", false);
146 addSearchTerm(searchQuery, searchContext, "name", false);
147 addSearchTerm(searchQuery, searchContext, "region", false);
148 addSearchTerm(searchQuery, searchContext, "street", false);
149 addSearchTerm(searchQuery, searchContext, "type", false);
150 addSearchTerm(searchQuery, searchContext, "zip", false);
151
152 LinkedHashMap<String, Object> params =
153 (LinkedHashMap<String, Object>)searchContext.getAttribute("params");
154
155 if (params != null) {
156 String expandoAttributes = (String)params.get("expandoAttributes");
157
158 if (Validator.isNotNull(expandoAttributes)) {
159 addSearchExpando(searchQuery, searchContext, expandoAttributes);
160 }
161 }
162 }
163
164 @Override
165 protected void doDelete(Object obj) throws Exception {
166 Organization organization = (Organization)obj;
167
168 deleteDocument(
169 organization.getCompanyId(), organization.getOrganizationId());
170 }
171
172 @Override
173 protected Document doGetDocument(Object obj) throws Exception {
174 Organization organization = (Organization)obj;
175
176 Document document = getBaseModelDocument(PORTLET_ID, organization);
177
178 document.addKeyword(Field.COMPANY_ID, organization.getCompanyId());
179 document.addText(Field.NAME, organization.getName());
180 document.addKeyword(
181 Field.ORGANIZATION_ID, organization.getOrganizationId());
182 document.addKeyword(Field.TYPE, organization.getType());
183
184 document.addKeyword(
185 "parentOrganizationId", organization.getParentOrganizationId());
186
187 String treePath = organization.buildTreePath();
188
189 document.addKeyword("treePath", treePath);
190
191 populateAddresses(
192 document, organization.getAddresses(), organization.getRegionId(),
193 organization.getCountryId());
194
195 return document;
196 }
197
198 @Override
199 protected String doGetSortField(String orderByCol) {
200 if (orderByCol.equals("name")) {
201 return "name";
202 }
203 else if (orderByCol.equals("type")) {
204 return "type";
205 }
206 else {
207 return orderByCol;
208 }
209 }
210
211 @Override
212 protected Summary doGetSummary(
213 Document document, Locale locale, String snippet,
214 PortletURL portletURL) {
215
216 String title = document.get("name");
217
218 String content = null;
219
220 String organizationId = document.get(Field.ORGANIZATION_ID);
221
222 portletURL.setParameter(
223 "struts_action", "/users_admin/edit_organization");
224 portletURL.setParameter("organizationId", organizationId);
225
226 return new Summary(title, content, portletURL);
227 }
228
229 @Override
230 protected void doReindex(Object obj) throws Exception {
231 if (obj instanceof List<?>) {
232 List<Organization> organizations = (List<Organization>)obj;
233
234 for (Organization organization : organizations) {
235 doReindex(organization);
236 }
237 }
238 else if (obj instanceof Long) {
239 long organizationId = (Long)obj;
240
241 Organization organization =
242 OrganizationLocalServiceUtil.getOrganization(organizationId);
243
244 doReindex(organization);
245 }
246 else if (obj instanceof long[]) {
247 long[] organizationIds = (long[])obj;
248
249 Map<Long, Collection<Document>> documentsMap =
250 new HashMap<Long, Collection<Document>>();
251
252 for (long organizationId : organizationIds) {
253 Organization organization =
254 OrganizationLocalServiceUtil.fetchOrganization(
255 organizationId);
256
257 if (organization == null) {
258 continue;
259 }
260
261 Document document = getDocument(organization);
262
263 long companyId = organization.getCompanyId();
264
265 Collection<Document> documents = documentsMap.get(companyId);
266
267 if (documents == null) {
268 documents = new ArrayList<Document>();
269
270 documentsMap.put(companyId, documents);
271 }
272
273 documents.add(document);
274 }
275
276 for (Map.Entry<Long, Collection<Document>> entry :
277 documentsMap.entrySet()) {
278
279 long companyId = entry.getKey();
280 Collection<Document> documents = entry.getValue();
281
282 SearchEngineUtil.updateDocuments(
283 getSearchEngineId(), companyId, documents);
284 }
285 }
286 else if (obj instanceof Organization) {
287 Organization organization = (Organization)obj;
288
289 Document document = getDocument(organization);
290
291 SearchEngineUtil.updateDocument(
292 getSearchEngineId(), organization.getCompanyId(), document);
293 }
294 }
295
296 @Override
297 protected void doReindex(String className, long classPK) throws Exception {
298 Organization organization =
299 OrganizationLocalServiceUtil.getOrganization(classPK);
300
301 doReindex(organization);
302 }
303
304 @Override
305 protected void doReindex(String[] ids) throws Exception {
306 long companyId = GetterUtil.getLong(ids[0]);
307
308 reindexOrganizations(companyId);
309 }
310
311 @Override
312 protected String getPortletId(SearchContext searchContext) {
313 return PORTLET_ID;
314 }
315
316 protected void reindexOrganizations(long companyId) throws Exception {
317 final Collection<Document> documents = new ArrayList<Document>();
318
319 ActionableDynamicQuery actionableDynamicQuery =
320 new OrganizationActionableDynamicQuery() {
321
322 @Override
323 protected void performAction(Object object) throws PortalException {
324 Organization organization = (Organization)object;
325
326 Document document = getDocument(organization);
327
328 documents.add(document);
329 }
330
331 };
332
333 actionableDynamicQuery.setCompanyId(companyId);
334
335 actionableDynamicQuery.performActions();
336
337 SearchEngineUtil.updateDocuments(
338 getSearchEngineId(), companyId, documents);
339 }
340
341 private static final boolean _PERMISSION_AWARE = true;
342
343 }