|
40 | 40 | import org.springframework.context.annotation.Scope; |
41 | 41 | import org.springframework.stereotype.Service; |
42 | 42 |
|
| 43 | +import java.util.Arrays; |
| 44 | +import java.util.Collections; |
43 | 45 | import java.util.List; |
| 46 | +import java.util.Objects; |
| 47 | +import java.util.Set; |
| 48 | +import java.util.stream.Collectors; |
44 | 49 |
|
45 | 50 | @Service |
46 | 51 | @Scope("singleton") |
@@ -71,6 +76,7 @@ public RangerGdsDatasetService() { |
71 | 76 | searchFields.add(new SearchField(SearchFilter.CREATED_BY, "obj.addedByUserId", SearchField.DATA_TYPE.INTEGER, SearchField.SEARCH_TYPE.FULL)); |
72 | 77 | searchFields.add(new SearchField(SearchFilter.DATASET_LABEL, "obj.labels", SearchField.DATA_TYPE.STRING, SearchField.SEARCH_TYPE.PARTIAL)); |
73 | 78 | searchFields.add(new SearchField(SearchFilter.DATASET_KEYWORD, "obj.keywords", SearchField.DATA_TYPE.STRING, SearchField.SEARCH_TYPE.PARTIAL)); |
| 79 | + searchFields.add(new SearchField(SearchFilter.IS_ENABLED, "obj.isEnabled", SearchField.DATA_TYPE.BOOLEAN, SearchField.SEARCH_TYPE.FULL)); |
74 | 80 |
|
75 | 81 | sortFields.add(new SortField(SearchFilter.CREATE_TIME, "obj.createTime")); |
76 | 82 | sortFields.add(new SortField(SearchFilter.UPDATE_TIME, "obj.updateTime")); |
@@ -214,13 +220,45 @@ public RangerDatasetList searchDatasets(SearchFilter filter) { |
214 | 220 | List<XXGdsDataset> datasets = super.searchResources(filter, searchFields, sortFields, ret); |
215 | 221 |
|
216 | 222 | if (datasets != null) { |
| 223 | + Set<String> searchLabels = extractFilterValues(SearchFilter.DATASET_LABEL, filter); |
| 224 | + Set<String> searchKeywords = extractFilterValues(SearchFilter.DATASET_KEYWORD, filter); |
| 225 | + String labelMatchType = filter.getParam(SearchFilter.DATASET_LABEL_MATCH_TYPE); |
| 226 | + String keywordMatchType = filter.getParam(SearchFilter.DATASET_KEYWORD_MATCH_TYPE); |
217 | 227 | for (XXGdsDataset dataset : datasets) { |
218 | | - ret.getList().add(getPopulatedViewObject(dataset)); |
| 228 | + boolean isLabelOrKeywordMatch = CollectionUtils.isEmpty(searchLabels) && CollectionUtils.isEmpty(searchKeywords); |
| 229 | + if (!isLabelOrKeywordMatch) { |
| 230 | + isLabelOrKeywordMatch = isAnyMatch(labelMatchType, JsonUtils.jsonToSetString(dataset.getLabels()), searchLabels); |
| 231 | + } |
| 232 | + |
| 233 | + if (!isLabelOrKeywordMatch) { |
| 234 | + isLabelOrKeywordMatch = isAnyMatch(keywordMatchType, JsonUtils.jsonToSetString(dataset.getKeywords()), searchKeywords); |
| 235 | + } |
| 236 | + |
| 237 | + if (isLabelOrKeywordMatch) { |
| 238 | + ret.getList().add(getPopulatedViewObject(dataset)); |
| 239 | + } |
219 | 240 | } |
220 | 241 | } |
221 | 242 |
|
222 | 243 | LOG.debug("<== searchDatasets({}): ret={}", filter, ret); |
223 | 244 |
|
224 | 245 | return ret; |
225 | 246 | } |
| 247 | + |
| 248 | + private Set<String> extractFilterValues(String key, SearchFilter filter) { |
| 249 | + Object[] multiVal = filter.getMultiValueParam(key); |
| 250 | + |
| 251 | + return multiVal != null ? Arrays.stream(multiVal).filter(Objects::nonNull).map(Object::toString).collect(Collectors.toSet()) : Collections.emptySet(); |
| 252 | + } |
| 253 | + |
| 254 | + private boolean isAnyMatch(String matchType, Set<String> values, Set<String> searchValues) { |
| 255 | + if (CollectionUtils.isNotEmpty(searchValues) && CollectionUtils.isNotEmpty(values)) { |
| 256 | + if (SearchField.SEARCH_TYPE.FULL.name().equalsIgnoreCase(matchType)) { |
| 257 | + return searchValues.stream().anyMatch(searchValue -> values.stream().anyMatch(value -> value.equalsIgnoreCase(searchValue))); |
| 258 | + } else { |
| 259 | + return searchValues.stream().anyMatch(searchValue -> values.stream().anyMatch(value -> StringUtils.containsIgnoreCase(value, searchValue))); |
| 260 | + } |
| 261 | + } |
| 262 | + return false; |
| 263 | + } |
226 | 264 | } |
0 commit comments