001    /**
002     * Copyright (c) 2000-2013 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.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    import com.liferay.portal.service.LayoutLocalServiceUtil;
037    
038    import java.util.ArrayList;
039    import java.util.List;
040    
041    /**
042     * @author Raymond Aug??
043     */
044    public class ScopeFacet extends MultiValueFacet {
045    
046            public ScopeFacet(SearchContext searchContext) {
047                    super(searchContext);
048    
049                    setFieldName(Field.GROUP_ID);
050            }
051    
052            protected long[] addScopeGroup(long groupId) {
053                    try {
054                            List<Long> groupIds = new ArrayList<Long>();
055    
056                            groupIds.add(groupId);
057    
058                            List<Layout> publicLayouts =
059                                    LayoutLocalServiceUtil.getScopeGroupLayouts(groupId, false);
060    
061                            for (Layout layout :publicLayouts) {
062                                    Group group = layout.getScopeGroup();
063    
064                                    groupIds.add(group.getGroupId());
065                            }
066    
067                            List<Layout> privateLayouts =
068                                    LayoutLocalServiceUtil.getScopeGroupLayouts(groupId, true);
069    
070                            for (Layout layout : privateLayouts) {
071                                    Group group = layout.getScopeGroup();
072    
073                                    groupIds.add(group.getGroupId());
074                            }
075    
076                            return ArrayUtil.toLongArray(groupIds);
077                    }
078                    catch (Exception e) {
079                            _log.error(e, e);
080                    }
081    
082                    return new long[] {groupId};
083            }
084    
085            @Override
086            protected BooleanClause doGetFacetClause() {
087                    SearchContext searchContext = getSearchContext();
088    
089                    FacetConfiguration facetConfiguration = getFacetConfiguration();
090    
091                    JSONObject dataJSONObject = facetConfiguration.getData();
092    
093                    long[] groupIds = null;
094    
095                    if (dataJSONObject.has("values")) {
096                            JSONArray valuesJSONArray = dataJSONObject.getJSONArray("values");
097    
098                            groupIds = new long[valuesJSONArray.length()];
099    
100                            for (int i = 0; i < valuesJSONArray.length(); i++) {
101                                    groupIds[i] = valuesJSONArray.getLong(i);
102                            }
103                    }
104    
105                    if (ArrayUtil.isEmpty(groupIds)) {
106                            groupIds = searchContext.getGroupIds();
107                    }
108    
109                    String groupIdParam = GetterUtil.getString(
110                            searchContext.getAttribute("groupId"));
111    
112                    if (Validator.isNotNull(groupIdParam)) {
113                            long groupId = GetterUtil.getLong(groupIdParam);
114    
115                            groupIds = addScopeGroup(groupId);
116                    }
117    
118                    if (ArrayUtil.isEmpty(groupIds) ||
119                            ((groupIds.length == 1) && (groupIds[0] == 0))) {
120    
121                            return null;
122                    }
123    
124                    BooleanQuery facetQuery = BooleanQueryFactoryUtil.create(searchContext);
125    
126                    long ownerUserId = searchContext.getOwnerUserId();
127    
128                    if (ownerUserId > 0) {
129                            facetQuery.addRequiredTerm(Field.USER_ID, ownerUserId);
130                    }
131    
132                    BooleanQuery groupIdsQuery = BooleanQueryFactoryUtil.create(
133                            searchContext);
134                    BooleanQuery scopeGroupIdsQuery = BooleanQueryFactoryUtil.create(
135                            searchContext);
136    
137                    for (int i = 0; i < groupIds.length; i ++) {
138                            long groupId = groupIds[i];
139    
140                            if (groupId <= 0) {
141                                    continue;
142                            }
143    
144                            try {
145                                    Group group = GroupLocalServiceUtil.getGroup(groupId);
146    
147                                    if (!group.isActive()) {
148                                            continue;
149                                    }
150    
151                                    long parentGroupId = groupId;
152    
153                                    if (group.isLayout()) {
154                                            parentGroupId = group.getParentGroupId();
155                                    }
156    
157                                    groupIdsQuery.addTerm(Field.GROUP_ID, parentGroupId);
158    
159                                    groupIds[i] = parentGroupId;
160    
161                                    if (group.isLayout() || searchContext.isScopeStrict()) {
162                                            scopeGroupIdsQuery.addTerm(Field.SCOPE_GROUP_ID, groupId);
163                                    }
164                            }
165                            catch (Exception e) {
166                                    continue;
167                            }
168                    }
169    
170                    searchContext.setGroupIds(groupIds);
171    
172                    if (groupIdsQuery.hasClauses()) {
173                            try {
174                                    facetQuery.add(groupIdsQuery, BooleanClauseOccur.MUST);
175                            }
176                            catch (ParseException pe) {
177                                    _log.error(pe, pe);
178                            }
179                    }
180    
181                    if (scopeGroupIdsQuery.hasClauses()) {
182                            try {
183                                    facetQuery.add(scopeGroupIdsQuery, BooleanClauseOccur.MUST);
184                            }
185                            catch (ParseException pe) {
186                                    _log.error(pe, pe);
187                            }
188                    }
189    
190                    return BooleanClauseFactoryUtil.create(
191                            searchContext, facetQuery, BooleanClauseOccur.MUST.getName());
192            }
193    
194            private static Log _log = LogFactoryUtil.getLog(ScopeFacet.class);
195    
196    }