001    /**
002     * Copyright (c) 2000-2012 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.portal.kernel.search.facet;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.search.BooleanClause;
023    import com.liferay.portal.kernel.search.BooleanClauseFactoryUtil;
024    import com.liferay.portal.kernel.search.BooleanClauseOccur;
025    import com.liferay.portal.kernel.search.BooleanQuery;
026    import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
027    import com.liferay.portal.kernel.search.Field;
028    import com.liferay.portal.kernel.search.Indexer;
029    import com.liferay.portal.kernel.search.IndexerPostProcessor;
030    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
031    import com.liferay.portal.kernel.search.SearchContext;
032    import com.liferay.portal.kernel.search.SearchEngineUtil;
033    import com.liferay.portal.kernel.search.facet.config.FacetConfiguration;
034    import com.liferay.portal.kernel.util.GetterUtil;
035    import com.liferay.portal.kernel.util.StringUtil;
036    
037    /**
038     * @author Raymond Augé
039     */
040    public class AssetEntriesFacet extends MultiValueFacet {
041    
042            public AssetEntriesFacet(SearchContext searchContext) {
043                    super(searchContext);
044    
045                    setFieldName(Field.ENTRY_CLASS_NAME);
046    
047                    initFacetClause();
048            }
049    
050            @Override
051            public void setFacetConfiguration(FacetConfiguration facetConfiguration) {
052                    super.setFacetConfiguration(facetConfiguration);
053    
054                    initFacetClause();
055            }
056    
057            @Override
058            protected BooleanClause doGetFacetClause() {
059                    SearchContext searchContext = getSearchContext();
060    
061                    String[] entryClassNames = searchContext.getEntryClassNames();
062    
063                    BooleanQuery facetQuery = BooleanQueryFactoryUtil.create(searchContext);
064    
065                    for (String entryClassName : entryClassNames) {
066                            Indexer indexer = IndexerRegistryUtil.getIndexer(entryClassName);
067    
068                            if (indexer == null) {
069                                    continue;
070                            }
071    
072                            try {
073                                    BooleanQuery indexerBooleanQuery = indexer.getFacetQuery(
074                                            entryClassName, searchContext);
075    
076                                    if ((indexerBooleanQuery == null) ||
077                                            !indexerBooleanQuery.hasClauses()) {
078    
079                                            continue;
080                                    }
081    
082                                    BooleanQuery entityQuery = BooleanQueryFactoryUtil.create(
083                                            searchContext);
084    
085                                    entityQuery.add(indexerBooleanQuery, BooleanClauseOccur.MUST);
086    
087                                    indexer.postProcessContextQuery(entityQuery, searchContext);
088    
089                                    for (IndexerPostProcessor indexerPostProcessor :
090                                                    indexer.getIndexerPostProcessors()) {
091    
092                                            indexerPostProcessor.postProcessContextQuery(
093                                                    entityQuery, searchContext);
094                                    }
095    
096                                    if (indexer.isStagingAware()) {
097                                            if (!searchContext.isIncludeLiveGroups() &&
098                                                    searchContext.isIncludeStagingGroups()) {
099    
100                                                    entityQuery.addRequiredTerm(Field.STAGING_GROUP, true);
101                                            }
102                                            else if (searchContext.isIncludeLiveGroups() &&
103                                                             !searchContext.isIncludeStagingGroups()) {
104    
105                                                    entityQuery.addRequiredTerm(Field.STAGING_GROUP, false);
106                                            }
107                                    }
108    
109                                    if (entityQuery.hasClauses()) {
110                                            facetQuery.add(entityQuery, BooleanClauseOccur.SHOULD);
111                                    }
112                            }
113                            catch (Exception e) {
114                                    _log.error(e, e);
115                            }
116                    }
117    
118                    if (!facetQuery.hasClauses()) {
119                            return null;
120                    }
121    
122                    return BooleanClauseFactoryUtil.create(
123                            facetQuery, BooleanClauseOccur.MUST.getName());
124            }
125    
126            protected void initFacetClause() {
127                    SearchContext searchContext = getSearchContext();
128    
129                    FacetConfiguration facetConfiguration = getFacetConfiguration();
130    
131                    JSONObject dataJSONObject = facetConfiguration.getData();
132    
133                    String[] entryClassNames = null;
134    
135                    if (dataJSONObject.has("values")) {
136                            JSONArray valuesJSONArray = dataJSONObject.getJSONArray("values");
137    
138                            entryClassNames = new String[valuesJSONArray.length()];
139    
140                            for (int i = 0; i < valuesJSONArray.length(); i++) {
141                                    entryClassNames[i] = valuesJSONArray.getString(i);
142                            }
143                    }
144    
145                    if ((entryClassNames == null) || (entryClassNames.length == 0)) {
146                            entryClassNames = searchContext.getEntryClassNames();
147                    }
148    
149                    if (!isStatic()) {
150                            String[] entryClassNameParam = StringUtil.split(
151                                    GetterUtil.getString(
152                                            searchContext.getAttribute(getFieldName())));
153    
154                            if ((entryClassNameParam != null) &&
155                                    (entryClassNameParam.length > 0)) {
156    
157                                    entryClassNames = entryClassNameParam;
158                            }
159                    }
160    
161                    if ((entryClassNames == null) || (entryClassNames.length == 0)) {
162                            entryClassNames = SearchEngineUtil.getEntryClassNames();
163    
164                            if (!dataJSONObject.has("values")) {
165                                    JSONArray entriesJSONArray = JSONFactoryUtil.createJSONArray();
166    
167                                    for (String entryClassName : entryClassNames) {
168                                            entriesJSONArray.put(entryClassName);
169                                    }
170    
171                                    dataJSONObject.put("values", entriesJSONArray);
172                            }
173                    }
174    
175                    searchContext.setEntryClassNames(entryClassNames);
176            }
177    
178            private static Log _log = LogFactoryUtil.getLog(AssetEntriesFacet.class);
179    
180    }