001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.usersadmin.util;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.IndexableActionableDynamicQuery;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.search.BaseIndexer;
023    import com.liferay.portal.kernel.search.BooleanClauseOccur;
024    import com.liferay.portal.kernel.search.BooleanQuery;
025    import com.liferay.portal.kernel.search.Document;
026    import com.liferay.portal.kernel.search.Field;
027    import com.liferay.portal.kernel.search.IndexWriterHelperUtil;
028    import com.liferay.portal.kernel.search.SearchContext;
029    import com.liferay.portal.kernel.search.Summary;
030    import com.liferay.portal.kernel.search.WildcardQuery;
031    import com.liferay.portal.kernel.search.filter.BooleanFilter;
032    import com.liferay.portal.kernel.search.filter.QueryFilter;
033    import com.liferay.portal.kernel.search.filter.TermsFilter;
034    import com.liferay.portal.kernel.search.generic.WildcardQueryImpl;
035    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
036    import com.liferay.portal.kernel.util.ArrayUtil;
037    import com.liferay.portal.kernel.util.GetterUtil;
038    import com.liferay.portal.kernel.util.ListUtil;
039    import com.liferay.portal.kernel.util.Validator;
040    import com.liferay.portal.model.Organization;
041    import com.liferay.portal.model.OrganizationConstants;
042    import com.liferay.portal.service.OrganizationLocalServiceUtil;
043    
044    import java.util.LinkedHashMap;
045    import java.util.List;
046    import java.util.Locale;
047    
048    import javax.portlet.PortletRequest;
049    import javax.portlet.PortletResponse;
050    
051    /**
052     * @author Raymond Aug??
053     * @author Zsigmond Rab
054     * @author Hugo Huijser
055     */
056    @OSGiBeanProperties
057    public class OrganizationIndexer extends BaseIndexer<Organization> {
058    
059            public static final String CLASS_NAME = Organization.class.getName();
060    
061            public OrganizationIndexer() {
062                    setDefaultSelectedFieldNames(
063                            Field.COMPANY_ID, Field.ORGANIZATION_ID, Field.UID);
064                    setPermissionAware(true);
065                    setStagingAware(false);
066            }
067    
068            @Override
069            public String getClassName() {
070                    return CLASS_NAME;
071            }
072    
073            @Override
074            public void postProcessContextBooleanFilter(
075                            BooleanFilter contextBooleanFilter, SearchContext searchContext)
076                    throws Exception {
077    
078                    LinkedHashMap<String, Object> params =
079                            (LinkedHashMap<String, Object>)searchContext.getAttribute("params");
080    
081                    if (params == null) {
082                            return;
083                    }
084    
085                    List<Long> excludedOrganizationIds = (List<Long>)params.get(
086                            "excludedOrganizationIds");
087    
088                    if (ListUtil.isNotEmpty(excludedOrganizationIds)) {
089                            TermsFilter termsFilter = new TermsFilter("organizationId");
090    
091                            termsFilter.addValues(
092                                    ArrayUtil.toStringArray(
093                                            excludedOrganizationIds.toArray(
094                                                    new Long[excludedOrganizationIds.size()])));
095    
096                            contextBooleanFilter.add(termsFilter, BooleanClauseOccur.MUST_NOT);
097                    }
098    
099                    List<Organization> organizationsTree = (List<Organization>)params.get(
100                            "organizationsTree");
101    
102                    if (ListUtil.isNotEmpty(organizationsTree)) {
103                            BooleanFilter booleanFilter = new BooleanFilter();
104    
105                            for (Organization organization : organizationsTree) {
106                                    String treePath = organization.buildTreePath();
107    
108                                    WildcardQuery wildcardQuery = new WildcardQueryImpl(
109                                            Field.TREE_PATH, treePath);
110    
111                                    booleanFilter.add(new QueryFilter(wildcardQuery));
112                            }
113    
114                            contextBooleanFilter.add(booleanFilter, BooleanClauseOccur.MUST);
115                    }
116                    else {
117                            long parentOrganizationId = GetterUtil.getLong(
118                                    searchContext.getAttribute("parentOrganizationId"));
119    
120                            if (parentOrganizationId !=
121                                            OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
122    
123                                    contextBooleanFilter.addRequiredTerm(
124                                            "parentOrganizationId", parentOrganizationId);
125                            }
126                    }
127            }
128    
129            @Override
130            public void postProcessSearchQuery(
131                            BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
132                            SearchContext searchContext)
133                    throws Exception {
134    
135                    addSearchTerm(searchQuery, searchContext, "city", false);
136                    addSearchTerm(searchQuery, searchContext, "country", false);
137                    addSearchTerm(searchQuery, searchContext, "name", false);
138                    addSearchTerm(searchQuery, searchContext, "region", false);
139                    addSearchTerm(searchQuery, searchContext, "street", false);
140                    addSearchTerm(searchQuery, searchContext, "type", false);
141                    addSearchTerm(searchQuery, searchContext, "zip", false);
142    
143                    LinkedHashMap<String, Object> params =
144                            (LinkedHashMap<String, Object>)searchContext.getAttribute("params");
145    
146                    if (params != null) {
147                            String expandoAttributes = (String)params.get("expandoAttributes");
148    
149                            if (Validator.isNotNull(expandoAttributes)) {
150                                    addSearchExpando(searchQuery, searchContext, expandoAttributes);
151                            }
152                    }
153            }
154    
155            @Override
156            protected void doDelete(Organization organization) throws Exception {
157                    deleteDocument(
158                            organization.getCompanyId(), organization.getOrganizationId());
159            }
160    
161            @Override
162            protected Document doGetDocument(Organization organization)
163                    throws Exception {
164    
165                    Document document = getBaseModelDocument(CLASS_NAME, organization);
166    
167                    document.addKeyword(Field.COMPANY_ID, organization.getCompanyId());
168                    document.addText(Field.NAME, organization.getName());
169                    document.addKeyword(
170                            Field.ORGANIZATION_ID, organization.getOrganizationId());
171                    document.addKeyword(Field.TREE_PATH, organization.buildTreePath());
172                    document.addKeyword(Field.TYPE, organization.getType());
173    
174                    document.addKeyword(
175                            "parentOrganizationId", organization.getParentOrganizationId());
176    
177                    populateAddresses(
178                            document, organization.getAddresses(), organization.getRegionId(),
179                            organization.getCountryId());
180    
181                    return document;
182            }
183    
184            @Override
185            protected String doGetSortField(String orderByCol) {
186                    if (orderByCol.equals("name")) {
187                            return "name";
188                    }
189                    else if (orderByCol.equals("type")) {
190                            return "type";
191                    }
192                    else {
193                            return orderByCol;
194                    }
195            }
196    
197            @Override
198            protected Summary doGetSummary(
199                    Document document, Locale locale, String snippet,
200                    PortletRequest portletRequest, PortletResponse portletResponse) {
201    
202                    String title = document.get("name");
203    
204                    String content = null;
205    
206                    return new Summary(title, content);
207            }
208    
209            @Override
210            protected void doReindex(Organization organization) throws Exception {
211                    Document document = getDocument(organization);
212    
213                    IndexWriterHelperUtil.updateDocument(
214                            getSearchEngineId(), organization.getCompanyId(), document,
215                            isCommitImmediately());
216            }
217    
218            @Override
219            protected void doReindex(String className, long classPK) throws Exception {
220                    Organization organization =
221                            OrganizationLocalServiceUtil.getOrganization(classPK);
222    
223                    doReindex(organization);
224            }
225    
226            @Override
227            protected void doReindex(String[] ids) throws Exception {
228                    long companyId = GetterUtil.getLong(ids[0]);
229    
230                    reindexOrganizations(companyId);
231            }
232    
233            protected void reindexOrganizations(long companyId) throws Exception {
234                    final IndexableActionableDynamicQuery indexableActionableDynamicQuery =
235                            OrganizationLocalServiceUtil.getIndexableActionableDynamicQuery();
236    
237                    indexableActionableDynamicQuery.setCompanyId(companyId);
238                    indexableActionableDynamicQuery.setPerformActionMethod(
239                            new ActionableDynamicQuery.PerformActionMethod<Organization>() {
240    
241                                    @Override
242                                    public void performAction(Organization organization) {
243                                            try {
244                                                    Document document = getDocument(organization);
245    
246                                                    indexableActionableDynamicQuery.addDocuments(document);
247                                            }
248                                            catch (PortalException pe) {
249                                                    if (_log.isWarnEnabled()) {
250                                                            _log.warn(
251                                                                    "Unable to index organization " +
252                                                                            organization.getOrganizationId(),
253                                                                    pe);
254                                                    }
255                                            }
256                                    }
257    
258                            });
259                    indexableActionableDynamicQuery.setSearchEngineId(getSearchEngineId());
260    
261                    indexableActionableDynamicQuery.performActions();
262            }
263    
264            private static final Log _log = LogFactoryUtil.getLog(
265                    OrganizationIndexer.class);
266    
267    }