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