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