001    /**
002     * Copyright (c) 2000-present 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.Field;
025    import com.liferay.portal.kernel.search.SearchContext;
026    import com.liferay.portal.kernel.search.facet.config.FacetConfiguration;
027    import com.liferay.portal.kernel.search.filter.BooleanFilter;
028    import com.liferay.portal.kernel.search.filter.Filter;
029    import com.liferay.portal.kernel.search.filter.TermsFilter;
030    import com.liferay.portal.kernel.util.ArrayUtil;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.Validator;
033    import com.liferay.portal.model.Group;
034    import com.liferay.portal.model.Layout;
035    import com.liferay.portal.service.GroupLocalServiceUtil;
036    
037    import java.util.ArrayList;
038    import java.util.List;
039    
040    /**
041     * @author Raymond Aug??
042     */
043    public class ScopeFacet extends MultiValueFacet {
044    
045            public ScopeFacet(SearchContext searchContext) {
046                    super(searchContext);
047    
048                    setFieldName(Field.GROUP_ID);
049            }
050    
051            protected long[] addScopeGroup(long groupId) {
052                    try {
053                            List<Long> groupIds = new ArrayList<>();
054    
055                            groupIds.add(groupId);
056    
057                            Group group = GroupLocalServiceUtil.getGroup(groupId);
058    
059                            List<Group> groups = GroupLocalServiceUtil.getGroups(
060                                    group.getCompanyId(), Layout.class.getName(),
061                                    group.getGroupId());
062    
063                            for (Group scopeGroup : groups) {
064                                    groupIds.add(scopeGroup.getGroupId());
065                            }
066    
067                            return ArrayUtil.toLongArray(groupIds);
068                    }
069                    catch (Exception e) {
070                            _log.error(e, e);
071                    }
072    
073                    return new long[] {groupId};
074            }
075    
076            @Override
077            protected BooleanClause<Filter> doGetFacetFilterBooleanClause() {
078                    SearchContext searchContext = getSearchContext();
079    
080                    FacetConfiguration facetConfiguration = getFacetConfiguration();
081    
082                    JSONObject dataJSONObject = facetConfiguration.getData();
083    
084                    long[] groupIds = null;
085    
086                    if (dataJSONObject.has("values")) {
087                            JSONArray valuesJSONArray = dataJSONObject.getJSONArray("values");
088    
089                            groupIds = new long[valuesJSONArray.length()];
090    
091                            for (int i = 0; i < valuesJSONArray.length(); i++) {
092                                    groupIds[i] = valuesJSONArray.getLong(i);
093                            }
094                    }
095    
096                    if (ArrayUtil.isEmpty(groupIds)) {
097                            groupIds = searchContext.getGroupIds();
098                    }
099    
100                    String groupIdParam = GetterUtil.getString(
101                            searchContext.getAttribute("groupId"));
102    
103                    if (Validator.isNotNull(groupIdParam)) {
104                            long groupId = GetterUtil.getLong(groupIdParam);
105    
106                            groupIds = addScopeGroup(groupId);
107                    }
108    
109                    if (ArrayUtil.isEmpty(groupIds) ||
110                            ((groupIds.length == 1) && (groupIds[0] == 0))) {
111    
112                            return null;
113                    }
114    
115                    BooleanFilter facetBooleanFilter = new BooleanFilter();
116    
117                    long ownerUserId = searchContext.getOwnerUserId();
118    
119                    if (ownerUserId > 0) {
120                            facetBooleanFilter.addRequiredTerm(Field.USER_ID, ownerUserId);
121                    }
122    
123                    TermsFilter groupIdsTermsFilter = new TermsFilter(Field.GROUP_ID);
124                    TermsFilter scopeGroupIdsTermsFilter = new TermsFilter(
125                            Field.SCOPE_GROUP_ID);
126    
127                    for (int i = 0; i < groupIds.length; i ++) {
128                            long groupId = groupIds[i];
129    
130                            if (groupId <= 0) {
131                                    continue;
132                            }
133    
134                            try {
135                                    Group group = GroupLocalServiceUtil.getGroup(groupId);
136    
137                                    if (!group.isActive()) {
138                                            continue;
139                                    }
140    
141                                    long parentGroupId = groupId;
142    
143                                    if (group.isLayout()) {
144                                            parentGroupId = group.getParentGroupId();
145                                    }
146    
147                                    groupIdsTermsFilter.addValue(String.valueOf(parentGroupId));
148    
149                                    groupIds[i] = parentGroupId;
150    
151                                    if (group.isLayout() || searchContext.isScopeStrict()) {
152                                            scopeGroupIdsTermsFilter.addValue(String.valueOf(groupId));
153                                    }
154                            }
155                            catch (Exception e) {
156                                    if (_log.isDebugEnabled()) {
157                                            _log.debug(e, e);
158                                    }
159                            }
160                    }
161    
162                    searchContext.setGroupIds(groupIds);
163    
164                    if (!groupIdsTermsFilter.isEmpty()) {
165                            facetBooleanFilter.add(
166                                    groupIdsTermsFilter, BooleanClauseOccur.MUST);
167                    }
168    
169                    if (!scopeGroupIdsTermsFilter.isEmpty()) {
170                            facetBooleanFilter.add(
171                                    scopeGroupIdsTermsFilter, BooleanClauseOccur.MUST);
172                    }
173    
174                    return BooleanClauseFactoryUtil.createFilter(
175                            searchContext, facetBooleanFilter, BooleanClauseOccur.MUST);
176            }
177    
178            private static final Log _log = LogFactoryUtil.getLog(ScopeFacet.class);
179    
180    }