001    /**
002     * Copyright (c) 2000-2010 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.enterpriseadmin.util;
016    
017    import com.liferay.portal.NoSuchCountryException;
018    import com.liferay.portal.NoSuchRegionException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.search.BooleanQuery;
022    import com.liferay.portal.kernel.search.Document;
023    import com.liferay.portal.kernel.search.DocumentImpl;
024    import com.liferay.portal.kernel.search.ExpandoIndexer;
025    import com.liferay.portal.kernel.search.Field;
026    import com.liferay.portal.kernel.search.SearchContext;
027    import com.liferay.portal.kernel.search.SearchEngineUtil;
028    import com.liferay.portal.kernel.search.Summary;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.SetUtil;
031    import com.liferay.portal.kernel.util.Validator;
032    import com.liferay.portal.model.Address;
033    import com.liferay.portal.model.Country;
034    import com.liferay.portal.model.Organization;
035    import com.liferay.portal.model.Region;
036    import com.liferay.portal.model.User;
037    import com.liferay.portal.service.CountryServiceUtil;
038    import com.liferay.portal.service.OrganizationLocalServiceUtil;
039    import com.liferay.portal.service.RegionServiceUtil;
040    import com.liferay.portal.util.PortletKeys;
041    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
042    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
043    import com.liferay.portlet.expando.model.ExpandoBridge;
044    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
045    import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
046    
047    import java.util.ArrayList;
048    import java.util.Collection;
049    import java.util.HashMap;
050    import java.util.LinkedHashMap;
051    import java.util.List;
052    import java.util.Map;
053    import java.util.Set;
054    
055    import javax.portlet.PortletURL;
056    
057    /**
058     * <a href="OrganzationIndexer.java.html"><b><i>View Source</i></b></a>
059     *
060     * @author Raymond Augé
061     * @author Zsigmond Rab
062     * @author Hugo Huijser
063     */
064    public class OrganizationIndexer extends ExpandoIndexer {
065    
066            public static final String[] CLASS_NAMES = {Organization.class.getName()};
067    
068            public static final String PORTLET_ID =
069                    PortletKeys.ENTERPRISE_ADMIN_ORGANIZATIONS;
070    
071            public String[] getClassNames() {
072                    return CLASS_NAMES;
073            }
074    
075            public Summary getSummary(
076                    Document document, String snippet, PortletURL portletURL) {
077    
078                    String title = document.get("name");
079    
080                    String content = null;
081    
082                    String organizationId = document.get(Field.ORGANIZATION_ID);
083    
084                    portletURL.setParameter(
085                            "struts_action", "/enterprise_admin/edit_organization");
086                    portletURL.setParameter("organizationId", organizationId);
087    
088                    return new Summary(title, content, portletURL);
089            }
090    
091            protected void doDelete(Object obj) throws Exception {
092                    Organization organization = (Organization)obj;
093    
094                    Document document = new DocumentImpl();
095    
096                    document.addUID(PORTLET_ID, organization.getOrganizationId());
097    
098                    SearchEngineUtil.deleteDocument(
099                            organization.getCompanyId(), document.get(Field.UID));
100            }
101    
102            protected Document doGetDocument(Object obj) throws Exception {
103                    Organization organization = (Organization)obj;
104    
105                    long companyId = organization.getCompanyId();
106                    long organizationId = organization.getOrganizationId();
107                    long parentOrganizationId = organization.getParentOrganizationId();
108                    String name = organization.getName();
109                    String type = organization.getType();
110                    long regionId = organization.getRegionId();
111                    long countryId = organization.getCountryId();
112    
113                    List<Address> addresses = organization.getAddresses();
114    
115                    List<String> streets = new ArrayList<String>();
116                    List<String> cities = new ArrayList<String>();
117                    List<String> zips = new ArrayList<String>();
118                    List<String> regions = new ArrayList<String>();
119                    List<String> countries = new ArrayList<String>();
120    
121                    if (regionId > 0) {
122                            try {
123                                    Region region = RegionServiceUtil.getRegion(regionId);
124    
125                                    regions.add(region.getName().toLowerCase());
126                            }
127                            catch (NoSuchRegionException nsre) {
128                                    if (_log.isWarnEnabled()) {
129                                            _log.warn(nsre.getMessage());
130                                    }
131                            }
132                    }
133    
134                    if (countryId > 0) {
135                            try {
136                                    Country country = CountryServiceUtil.getCountry(countryId);
137    
138                                    countries.add(country.getName().toLowerCase());
139                            }
140                            catch (NoSuchCountryException nsce) {
141                                    if (_log.isWarnEnabled()) {
142                                            _log.warn(nsce.getMessage());
143                                    }
144                            }
145                    }
146    
147                    for (Address address : addresses) {
148                            streets.add(address.getStreet1().toLowerCase());
149                            streets.add(address.getStreet2().toLowerCase());
150                            streets.add(address.getStreet3().toLowerCase());
151                            cities.add(address.getCity().toLowerCase());
152                            zips.add(address.getZip().toLowerCase());
153                            regions.add(address.getRegion().getName().toLowerCase());
154                            countries.add(address.getCountry().getName().toLowerCase());
155                    }
156    
157                    long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
158                            User.class.getName(), organizationId);
159                    String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
160                            User.class.getName(), organizationId);
161    
162                    ExpandoBridge expandoBridge = organization.getExpandoBridge();
163    
164                    Document document = new DocumentImpl();
165    
166                    document.addUID(PORTLET_ID, organizationId);
167    
168                    document.addKeyword(Field.COMPANY_ID, companyId);
169                    document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
170                    document.addKeyword(Field.ORGANIZATION_ID, organizationId);
171                    document.addKeyword("parentOrganizationId", parentOrganizationId);
172                    document.addKeyword("name", name, true);
173                    document.addKeyword("type", type);
174                    document.addKeyword(
175                            "street", streets.toArray(new String[streets.size()]));
176                    document.addKeyword(
177                            "city", cities.toArray(new String[cities.size()]));
178                    document.addKeyword(
179                            "zip", zips.toArray(new String[zips.size()]));
180                    document.addKeyword(
181                            "region", regions.toArray(new String[regions.size()]));
182                    document.addKeyword(
183                            "country", countries.toArray(new String[countries.size()]));
184    
185                    document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
186                    document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
187    
188                    document.addKeyword(
189                            Field.ENTRY_CLASS_NAME, Organization.class.getName());
190                    document.addKeyword(Field.ENTRY_CLASS_PK, organizationId);
191    
192                    ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
193    
194                    return document;
195            }
196    
197            protected void doReindex(Object obj) throws Exception {
198                    if (obj instanceof List<?>) {
199                            List<Organization> organizations = (List<Organization>)obj;
200    
201                            for (Organization organization : organizations) {
202                                    doReindex(organization);
203                            }
204                    }
205                    else if (obj instanceof Long) {
206                            long organizationId = (Long)obj;
207    
208                            Organization organization =
209                                    OrganizationLocalServiceUtil.getOrganization(organizationId);
210    
211                            doReindex(organization);
212                    }
213                    else if (obj instanceof long[]) {
214                            long[] organizationIds = (long[])obj;
215    
216                            Map<Long, Collection<Document>> documentsMap =
217                                    new HashMap<Long, Collection<Document>>();
218    
219                            for (long organizationId : organizationIds) {
220                                    Organization organization =
221                                            OrganizationLocalServiceUtil.getOrganization(
222                                                    organizationId);
223    
224                                    Document document = getDocument(organization);
225    
226                                    long companyId = organization.getCompanyId();
227    
228                                    Collection<Document> documents = documentsMap.get(companyId);
229    
230                                    if (documents == null) {
231                                            documents = new ArrayList<Document>();
232    
233                                            documentsMap.put(companyId, documents);
234                                    }
235    
236                                    documents.add(document);
237                            }
238    
239                            for (Map.Entry<Long, Collection<Document>> entry :
240                                            documentsMap.entrySet()) {
241    
242                                    long companyId = entry.getKey();
243                                    Collection<Document> documents = entry.getValue();
244    
245                                    SearchEngineUtil.updateDocuments(companyId, documents);
246                            }
247                    }
248                    else if (obj instanceof Organization) {
249                            Organization organization = (Organization)obj;
250    
251                            Document document = getDocument(organization);
252    
253                            SearchEngineUtil.updateDocument(
254                                    organization.getCompanyId(), document);
255                    }
256            }
257    
258            protected void doReindex(String className, long classPK) throws Exception {
259                    Organization organization =
260                            OrganizationLocalServiceUtil.getOrganization(classPK);
261    
262                    doReindex(organization);
263            }
264    
265            protected void doReindex(String[] ids) throws Exception {
266                    long companyId = GetterUtil.getLong(ids[0]);
267    
268                    reindexOrganizations(companyId);
269            }
270    
271            protected String getPortletId(SearchContext searchContext) {
272                    return PORTLET_ID;
273            }
274    
275            protected void postProcessSearchQuery(
276                            BooleanQuery searchQuery, SearchContext searchContext)
277                    throws Exception {
278    
279                    String city = (String)searchContext.getAttribute("city");
280    
281                    if (Validator.isNotNull(city)) {
282                            if (searchContext.isAndSearch()) {
283                                    searchQuery.addRequiredTerm("city", city, true);
284                            }
285                            else {
286                                    searchQuery.addTerm("city", city, true);
287                            }
288                    }
289    
290                    String country = (String)searchContext.getAttribute("country");
291    
292                    if (Validator.isNotNull(country)) {
293                            if (searchContext.isAndSearch()) {
294                                    searchQuery.addRequiredTerm("country", country, true);
295                            }
296                            else {
297                                    searchQuery.addTerm("country", country, true);
298                            }
299                    }
300    
301                    String name = (String)searchContext.getAttribute("name");
302    
303                    if (Validator.isNotNull(name)) {
304                            if (searchContext.isAndSearch()) {
305                                    searchQuery.addRequiredTerm("name", name, true);
306                            }
307                            else {
308                                    searchQuery.addTerm("name", name, true);
309                            }
310                    }
311    
312                    LinkedHashMap<String, Object> params =
313                            (LinkedHashMap<String, Object>)searchContext.getAttribute("params");
314    
315                    if (params != null) {
316                            ExpandoBridge expandoBridge =
317                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
318                                            searchContext.getCompanyId(), Organization.class.getName());
319    
320                            Set<String> attributeNames = SetUtil.fromEnumeration(
321                                    expandoBridge.getAttributeNames());
322    
323                            for (Map.Entry<String, Object> entry : params.entrySet()) {
324                                    String key = entry.getKey();
325                                    Object value = entry.getValue();
326    
327                                    addSearchQueryParams(
328                                            searchQuery, searchContext, expandoBridge, attributeNames,
329                                            key, value);
330                            }
331                    }
332    
333                    String parentOrganizationId = (String)searchContext.getAttribute(
334                            "parentOrganizationId");
335    
336                    if (Validator.isNotNull(parentOrganizationId)) {
337                            if (searchContext.isAndSearch()) {
338                                    searchQuery.addRequiredTerm(
339                                            "parentOrganizationId", parentOrganizationId, true);
340                            }
341                            else {
342                                    searchQuery.addTerm(
343                                            "parentOrganizationId", parentOrganizationId, true);
344                            }
345                    }
346    
347                    String region = (String)searchContext.getAttribute("region");
348    
349                    if (Validator.isNotNull(region)) {
350                            if (searchContext.isAndSearch()) {
351                                    searchQuery.addRequiredTerm("region", region, true);
352                            }
353                            else {
354                                    searchQuery.addTerm("region", region, true);
355                            }
356                    }
357    
358                    String street = (String)searchContext.getAttribute("street");
359    
360                    if (Validator.isNotNull(street)) {
361                            if (searchContext.isAndSearch()) {
362                                    searchQuery.addRequiredTerm("street", street, true);
363                            }
364                            else {
365                                    searchQuery.addTerm("street", street, true);
366                            }
367                    }
368    
369                    String type = (String)searchContext.getAttribute("type");
370    
371                    if (Validator.isNotNull(type)) {
372                            if (searchContext.isAndSearch()) {
373                                    searchQuery.addRequiredTerm("type", type, true);
374                            }
375                            else {
376                                    searchQuery.addTerm("type", type, true);
377                            }
378                    }
379    
380                    String zip = (String)searchContext.getAttribute("zip");
381    
382                    if (Validator.isNotNull(zip)) {
383                            if (searchContext.isAndSearch()) {
384                                    searchQuery.addRequiredTerm("zip", zip, true);
385                            }
386                            else {
387                                    searchQuery.addTerm("zip", zip, true);
388                            }
389                    }
390            }
391    
392            protected void reindexOrganizations(long companyId) throws Exception {
393                    int count = OrganizationLocalServiceUtil.getOrganizationsCount();
394    
395                    int pages = count / OrganizationIndexer.DEFAULT_INTERVAL;
396    
397                    for (int i = 0; i <= pages; i++) {
398                            int start = (i * OrganizationIndexer.DEFAULT_INTERVAL);
399                            int end = start + OrganizationIndexer.DEFAULT_INTERVAL;
400    
401                            reindexOrganizations(companyId, start, end);
402                    }
403            }
404    
405            protected void reindexOrganizations(long companyId, int start, int end)
406                    throws Exception {
407    
408                    List<Organization> organizations =
409                            OrganizationLocalServiceUtil.getOrganizations(start, end);
410    
411                    if (organizations.isEmpty()) {
412                            return;
413                    }
414    
415                    Collection<Document> documents = new ArrayList<Document>();
416    
417                    for (Organization organization : organizations) {
418                            Document document = getDocument(organization);
419    
420                            documents.add(document);
421                    }
422    
423                    SearchEngineUtil.updateDocuments(companyId, documents);
424            }
425    
426            private static Log _log = LogFactoryUtil.getLog(OrganizationIndexer.class);
427    
428    }