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.JSONObject;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.search.BooleanClause;
022    import com.liferay.portal.kernel.search.BooleanClauseFactoryUtil;
023    import com.liferay.portal.kernel.search.BooleanClauseOccur;
024    import com.liferay.portal.kernel.search.BooleanQuery;
025    import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
026    import com.liferay.portal.kernel.search.Field;
027    import com.liferay.portal.kernel.search.ParseException;
028    import com.liferay.portal.kernel.search.SearchContext;
029    import com.liferay.portal.kernel.search.facet.config.FacetConfiguration;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.Validator;
032    import com.liferay.portal.model.Group;
033    import com.liferay.portal.service.GroupLocalServiceUtil;
034    
035    /**
036     * @author Raymond Augé
037     */
038    public class ScopeFacet extends MultiValueFacet {
039    
040            public ScopeFacet(SearchContext searchContext) {
041                    super(searchContext);
042    
043                    setFieldName(Field.GROUP_ID);
044            }
045    
046            @Override
047            protected BooleanClause doGetFacetClause() {
048                    SearchContext searchContext = getSearchContext();
049    
050                    FacetConfiguration facetConfiguration = getFacetConfiguration();
051    
052                    JSONObject dataJSONObject = facetConfiguration.getData();
053    
054                    long[] groupIds = null;
055    
056                    if (dataJSONObject.has("values")) {
057                            JSONArray valuesJSONArray = dataJSONObject.getJSONArray("values");
058    
059                            groupIds = new long[valuesJSONArray.length()];
060    
061                            for (int i = 0; i < valuesJSONArray.length(); i++) {
062                                    groupIds[i] = valuesJSONArray.getLong(i);
063                            }
064                    }
065    
066                    if ((groupIds == null) || (groupIds.length == 0)) {
067                            groupIds = searchContext.getGroupIds();
068                    }
069    
070                    String groupIdParam = GetterUtil.getString(
071                            searchContext.getAttribute("groupId"));
072    
073                    if (Validator.isNotNull(groupIdParam)) {
074                            groupIds = new long[] {GetterUtil.getLong(groupIdParam)};
075                    }
076    
077                    if ((groupIds == null) || (groupIds.length == 0) ||
078                            ((groupIds.length == 1) && (groupIds[0] == 0))) {
079    
080                            return null;
081                    }
082    
083                    BooleanQuery facetQuery = BooleanQueryFactoryUtil.create(searchContext);
084    
085                    long ownerUserId = searchContext.getOwnerUserId();
086    
087                    if (ownerUserId > 0) {
088                            facetQuery.addRequiredTerm(Field.USER_ID, ownerUserId);
089                    }
090    
091                    BooleanQuery groupIdsQuery = BooleanQueryFactoryUtil.create(
092                            searchContext);
093                    BooleanQuery scopeGroupIdsQuery = BooleanQueryFactoryUtil.create(
094                            searchContext);
095    
096                    for (int i = 0; i < groupIds.length; i ++) {
097                            long groupId = groupIds[i];
098    
099                            if (groupId <= 0) {
100                                    continue;
101                            }
102    
103                            try {
104                                    Group group = GroupLocalServiceUtil.getGroup(groupId);
105    
106                                    if (!group.isActive()) {
107                                            continue;
108                                    }
109    
110                                    long parentGroupId = groupId;
111    
112                                    if (group.isLayout()) {
113                                            parentGroupId = group.getParentGroupId();
114                                    }
115    
116                                    groupIdsQuery.addTerm(Field.GROUP_ID, parentGroupId);
117    
118                                    groupIds[i] = parentGroupId;
119    
120                                    if (group.isLayout() || searchContext.isScopeStrict()) {
121                                            scopeGroupIdsQuery.addTerm(Field.SCOPE_GROUP_ID, groupId);
122                                    }
123                            }
124                            catch (Exception e) {
125                                    continue;
126                            }
127                    }
128    
129                    searchContext.setGroupIds(groupIds);
130    
131                    if (groupIdsQuery.hasClauses()) {
132                            try {
133                                    facetQuery.add(groupIdsQuery, BooleanClauseOccur.MUST);
134                            }
135                            catch (ParseException pe) {
136                                    _log.error(pe, pe);
137                            }
138                    }
139    
140                    if (scopeGroupIdsQuery.hasClauses()) {
141                            try {
142                                    facetQuery.add(scopeGroupIdsQuery, BooleanClauseOccur.MUST);
143                            }
144                            catch (ParseException pe) {
145                                    _log.error(pe, pe);
146                            }
147                    }
148    
149                    return BooleanClauseFactoryUtil.create(
150                            facetQuery, BooleanClauseOccur.MUST.getName());
151            }
152    
153            private static Log _log = LogFactoryUtil.getLog(ScopeFacet.class);
154    
155    }