*/
package org.sonar.server.rule.ws;
-import com.google.common.base.Function;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import java.util.Collection;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
+import org.sonar.core.util.stream.Collectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import static com.google.common.collect.FluentIterable.from;
import static com.google.common.collect.Sets.newHashSet;
import static java.util.Collections.singletonList;
+import static org.sonar.core.util.Protobuf.setNullable;
/**
* Add details about active rules to api/rules/search and api/rules/show
}
private Collection<String> writeActiveRules(DbSession dbSession, SearchResponse.Builder response, RuleQuery query, List<RuleDto> rules) {
- Collection<String> qProfileKeys = newHashSet();
+ Collection<String> qProfileKeys = new HashSet<>();
Rules.Actives.Builder activesBuilder = response.getActivesBuilder();
String profileKey = query.getQProfileKey();
if (profileKey != null) {
// Load details of active rules on the selected profile
List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfileKey(dbSession, profileKey);
- Map<RuleKey, ActiveRuleDto> activeRuleByRuleKey = from(activeRuleDtos).uniqueIndex(ActiveRuleToRuleKey.INSTANCE);
+ Map<RuleKey, ActiveRuleDto> activeRuleByRuleKey = activeRuleDtos.stream().collect(Collectors.uniqueIndex(d -> d.getKey().ruleKey()));
ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = activeRuleDtosToActiveRuleParamDtos(dbSession, activeRuleDtos);
for (RuleDto rule : rules) {
} else {
// Load details of all active rules
List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByRuleIds(dbSession, Lists.transform(rules, RuleDto::getId));
- Multimap<RuleKey, ActiveRuleDto> activeRulesByRuleKey = from(activeRuleDtos).index(ActiveRuleToRuleKey.INSTANCE);
+ Multimap<RuleKey, ActiveRuleDto> activeRulesByRuleKey = from(activeRuleDtos).index(d -> d.getKey().ruleKey());
ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = activeRuleDtosToActiveRuleParamDtos(dbSession, activeRuleDtos);
for (RuleDto rule : rules) {
qProfileKeys = writeActiveRules(rule.getKey(), activeRulesByRuleKey.get(rule.getKey()), activeRuleParamsByActiveRuleKey, activesBuilder);
}
private Rules.QProfiles.Builder buildQProfiles(DbSession dbSession, Collection<String> harvestedProfileKeys) {
- Map<String, QualityProfileDto> qProfilesByKey = Maps.newHashMap();
+ Map<String, QualityProfileDto> qProfilesByKey = new HashMap<>();
for (String qProfileKey : harvestedProfileKeys) {
if (!qProfilesByKey.containsKey(qProfileKey)) {
QualityProfileDto profile = loadProfile(dbSession, qProfileKey);
}
@CheckForNull
- QualityProfileDto loadProfile(DbSession dbSession, String qProfileKey) {
+ private QualityProfileDto loadProfile(DbSession dbSession, String qProfileKey) {
return dbClient.qualityProfileDao().selectByKey(dbSession, qProfileKey);
}
private void writeProfile(Map<String, Rules.QProfile> profilesResponse, QualityProfileDto profile) {
Rules.QProfile.Builder profileResponse = Rules.QProfile.newBuilder();
- if (profile.getName() != null) {
- profileResponse.setName(profile.getName());
- }
+ setNullable(profile.getName(), profileResponse::setName);
+
if (profile.getLanguage() != null) {
profileResponse.setLang(profile.getLanguage());
Language language = languages.get(profile.getLanguage());
String langName = language == null ? profile.getLanguage() : language.getName();
profileResponse.setLangName(langName);
}
- if (profile.getParentKee() != null) {
- profileResponse.setParent(profile.getParentKee());
- }
+ setNullable(profile.getParentKee(), profileResponse::setParent);
profilesResponse.put(profile.getKey(), profileResponse.build());
}
-
- private enum ActiveRuleToRuleKey implements Function<ActiveRuleDto, RuleKey> {
- INSTANCE;
-
- @Override
- public RuleKey apply(@Nonnull ActiveRuleDto input) {
- return input.getKey().ruleKey();
- }
- }
}
*/
package org.sonar.server.rule.ws;
-import com.google.common.base.Function;
-import com.google.common.base.Predicates;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Sets;
import com.google.common.io.Resources;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Set;
import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
+import org.sonar.core.util.stream.Collectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.rule.RuleDefinitionDto;
import org.sonarqube.ws.Rules.SearchResponse;
import org.sonarqube.ws.client.rule.SearchWsRequest;
-import static com.google.common.collect.FluentIterable.from;
+import static java.lang.String.format;
import static org.sonar.api.server.ws.WebService.Param.ASCENDING;
import static org.sonar.api.server.ws.WebService.Param.FACETS;
import static org.sonar.api.server.ws.WebService.Param.FIELDS;
import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_TEMPLATE_KEY;
import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_TYPES;
-/**
- * @since 4.4
- */
public class SearchAction implements RulesWsAction {
public static final String ACTION = "search";
private static final Collection<String> DEFAULT_FACETS = ImmutableSet.of(PARAM_LANGUAGES, PARAM_REPOSITORIES, "tags");
+ private static final String[] POSSIBLE_FACETS = new String[] {
+ FACET_LANGUAGES,
+ FACET_REPOSITORIES,
+ FACET_TAGS,
+ FACET_SEVERITIES,
+ FACET_ACTIVE_SEVERITIES,
+ FACET_STATUSES,
+ FACET_TYPES,
+ FACET_OLD_DEFAULT};
private final RuleQueryFactory ruleQueryFactory;
private final DbClient dbClient;
.addPagingParams(100, MAX_LIMIT)
.setHandler(this);
- Collection<String> possibleFacets = possibleFacets();
- WebService.NewParam paramFacets = action.createParam(FACETS)
+ action.createParam(FACETS)
.setDescription("Comma-separated list of the facets to be computed. No facet is computed by default.")
- .setPossibleValues(possibleFacets);
- if (possibleFacets != null && possibleFacets.size() > 1) {
- Iterator<String> it = possibleFacets.iterator();
- paramFacets.setExampleValue(String.format("%s,%s", it.next(), it.next()));
- }
+ .setPossibleValues(POSSIBLE_FACETS)
+ .setExampleValue(format("%s,%s", POSSIBLE_FACETS[0], POSSIBLE_FACETS[1]));
WebService.NewParam paramFields = action.createParam(FIELDS)
.setDescription("Comma-separated list of the fields to be returned in response. All the fields are returned by default, except actives." +
"</ul>")
.setPossibleValues(OPTIONAL_FIELDS);
Iterator<String> it = OPTIONAL_FIELDS.iterator();
- paramFields.setExampleValue(String.format("%s,%s", it.next(), it.next()));
+ paramFields.setExampleValue(format("%s,%s", it.next(), it.next()));
doDefinition(action);
}
return responseBuilder.build();
}
- protected void writeStatistics(SearchResponse.Builder response, SearchResult searchResult, SearchOptions context) {
+ private static void writeStatistics(SearchResponse.Builder response, SearchResult searchResult, SearchOptions context) {
response.setTotal(searchResult.total);
response.setP(context.getPage());
response.setPs(context.getLimit());
}
- protected void doDefinition(WebService.NewAction action) {
+ private void doDefinition(WebService.NewAction action) {
action.setDescription("Search for a collection of relevant rules matching a specified query.<br/>" +
"Since 5.5, following fields in the response have been deprecated :" +
"<ul>" +
defineRuleSearchParameters(action);
}
- @CheckForNull
- protected Collection<String> possibleFacets() {
- return Arrays.asList(
- FACET_LANGUAGES,
- FACET_REPOSITORIES,
- FACET_TAGS,
- FACET_SEVERITIES,
- FACET_ACTIVE_SEVERITIES,
- FACET_STATUSES,
- FACET_TYPES,
- FACET_OLD_DEFAULT);
- }
-
- /**
- * public visibility because used by {@link org.sonar.server.qualityprofile.ws.BulkRuleActivationActions}
- */
public static void defineRuleSearchParameters(WebService.NewAction action) {
action
.createParam(TEXT_QUERY)
}
}
- protected SearchOptions buildSearchOptions(SearchWsRequest request) {
+ private static SearchOptions buildSearchOptions(SearchWsRequest request) {
SearchOptions context = loadCommonContext(request);
SearchOptions searchOptions = new SearchOptions()
.setLimit(context.getLimit())
return context;
}
- protected SearchResult doSearch(DbSession dbSession, RuleQuery query, SearchOptions context) {
+ private SearchResult doSearch(DbSession dbSession, RuleQuery query, SearchOptions context) {
SearchIdResult<RuleKey> result = ruleIndex.search(query, context);
List<RuleKey> ruleKeys = result.getIds();
// rule order is managed by ES
rules.add(rule);
}
}
- List<Integer> ruleIds = from(rules).transform(RuleDtoToId.INSTANCE).toList();
- List<Integer> templateRuleIds = from(rules)
- .transform(RuleDtoToTemplateId.INSTANCE)
- .filter(Predicates.notNull())
- .toList();
+ List<Integer> ruleIds = rules.stream().map(RuleDto::getId).collect(Collectors.toList());
+ List<Integer> templateRuleIds = rules.stream()
+ .map(RuleDto::getTemplateId)
+ .filter(Objects::nonNull)
+ .collect(Collectors.toList());
List<RuleDefinitionDto> templateRules = dbClient.ruleDao().selectDefinitionByIds(dbSession, templateRuleIds);
List<RuleParamDto> ruleParamDtos = dbClient.ruleDao().selectRuleParamsByRuleIds(dbSession, ruleIds);
return new SearchResult()
.setTotal(result.getTotal());
}
- protected void doContextResponse(DbSession dbSession, SearchWsRequest request, SearchResult result, SearchResponse.Builder response, RuleQuery query) {
+ private void doContextResponse(DbSession dbSession, SearchWsRequest request, SearchResult result, SearchResponse.Builder response, RuleQuery query) {
SearchOptions contextForResponse = loadCommonContext(request);
writeRules(response, result, contextForResponse);
if (contextForResponse.getFields().contains("actives")) {
}
}
- protected void writeFacets(SearchResponse.Builder response, SearchWsRequest request, SearchOptions context, SearchResult results) {
+ private static void writeFacets(SearchResponse.Builder response, SearchWsRequest request, SearchOptions context, SearchResult results) {
addMandatoryFacetValues(results, FACET_LANGUAGES, request.getLanguages());
addMandatoryFacetValues(results, FACET_REPOSITORIES, request.getRepositories());
addMandatoryFacetValues(results, FACET_STATUSES, ALL_STATUSES_EXCEPT_REMOVED);
}
}
- protected void addMandatoryFacetValues(SearchResult results, String facetName, @Nullable Collection<String> mandatoryValues) {
+ private static void addMandatoryFacetValues(SearchResult results, String facetName, @Nullable Collection<String> mandatoryValues) {
Map<String, Long> facetValues = results.facets.get(facetName);
if (facetValues != null) {
Collection<String> valuesToAdd = mandatoryValues == null ? Lists.newArrayList() : mandatoryValues;
}
}
- private enum RuleDtoToId implements Function<RuleDto, Integer> {
- INSTANCE;
-
- @Override
- public Integer apply(@Nonnull RuleDto input) {
- return input.getId();
- }
- }
-
- private enum RuleDtoToTemplateId implements Function<RuleDto, Integer> {
- INSTANCE;
-
- @Override
- public Integer apply(@Nonnull RuleDto input) {
- return input.getTemplateId();
- }
- }
-
}