Skip to content

Commit

Permalink
SONAR-8888 use DefinedQProfileRepository in QProfileResetImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb committed Mar 23, 2017
1 parent ced85d3 commit c733f4b
Showing 1 changed file with 19 additions and 41 deletions.
Expand Up @@ -19,22 +19,17 @@
*/ */
package org.sonar.server.qualityprofile; package org.sonar.server.qualityprofile;


import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.sonar.api.profiles.ProfileDefinition;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.ActiveRule; import org.sonar.api.rules.ActiveRule;
import org.sonar.api.rules.ActiveRuleParam; import org.sonar.api.rules.ActiveRuleParam;
import org.sonar.api.server.ServerSide; import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.ValidationMessages;
import org.sonar.db.DbClient; import org.sonar.db.DbClient;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto; import org.sonar.db.organization.OrganizationDto;
Expand All @@ -46,7 +41,6 @@
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;


import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import static org.sonar.server.ws.WsUtils.checkRequest;


@ServerSide @ServerSide
public class QProfileResetImpl implements QProfileReset { public class QProfileResetImpl implements QProfileReset {
Expand All @@ -55,34 +49,34 @@ public class QProfileResetImpl implements QProfileReset {
private final QProfileFactory factory; private final QProfileFactory factory;
private final RuleActivator activator; private final RuleActivator activator;
private final ActiveRuleIndexer activeRuleIndexer; private final ActiveRuleIndexer activeRuleIndexer;
private final ProfileDefinition[] definitions; private final DefinedQProfileRepository definedQProfileRepositories;


public QProfileResetImpl(DbClient db, RuleActivator activator, ActiveRuleIndexer activeRuleIndexer, QProfileFactory factory, ProfileDefinition... definitions) { public QProfileResetImpl(DbClient db, RuleActivator activator, ActiveRuleIndexer activeRuleIndexer, QProfileFactory factory,
DefinedQProfileRepository definedQProfileRepository) {
this.db = db; this.db = db;
this.activator = activator; this.activator = activator;
this.activeRuleIndexer = activeRuleIndexer; this.activeRuleIndexer = activeRuleIndexer;
this.factory = factory; this.factory = factory;
this.definitions = definitions; this.definedQProfileRepositories = definedQProfileRepository;
}

public QProfileResetImpl(DbClient db, RuleActivator activator, ActiveRuleIndexer activeRuleIndexer, QProfileFactory factory) {
this(db, activator, activeRuleIndexer, factory, new ProfileDefinition[0]);
} }


@Override @Override
public void resetLanguage(DbSession dbSession, OrganizationDto organization, String language) { public void resetLanguage(DbSession dbSession, OrganizationDto organization, String language) {
ListMultimap<QProfileName, RulesProfile> profilesByName = loadDefinitionsGroupedByName(language); definedQProfileRepositories.getQProfilesByLanguage()
for (Map.Entry<QProfileName, Collection<RulesProfile>> entry : profilesByName.asMap().entrySet()) { .entrySet()
QProfileName profileName = entry.getKey(); .stream()
QualityProfileDto profile = factory.getOrCreate(dbSession, organization, profileName); .filter(entry -> entry.getKey().equals(language))
List<RuleActivation> activations = Lists.newArrayList(); .map(Map.Entry::getValue)
for (RulesProfile def : entry.getValue()) { .flatMap(List::stream)
for (ActiveRule activeRule : def.getActiveRules()) { .forEach(definedQProfile -> resetProfile(dbSession, organization, definedQProfile));
activations.add(getRuleActivation(dbSession, activeRule)); }
}
} private void resetProfile(DbSession dbSession, OrganizationDto organization, DefinedQProfile definedQProfile) {
reset(dbSession, profile, activations); QualityProfileDto profile = factory.getOrCreate(dbSession, organization, definedQProfile.getQProfileName());
}
List<RuleActivation> activations = Lists.newArrayList();
definedQProfile.getActiveRules().forEach(activeRule -> activations.add(getRuleActivation(dbSession, activeRule)));
reset(dbSession, profile, activations);
} }


private RuleActivation getRuleActivation(DbSession dbSession, ActiveRule activeRule) { private RuleActivation getRuleActivation(DbSession dbSession, ActiveRule activeRule) {
Expand Down Expand Up @@ -139,20 +133,4 @@ public BulkChangeResult reset(DbSession dbSession, QualityProfileDto profile, Co
return result; return result;
} }


private ListMultimap<QProfileName, RulesProfile> loadDefinitionsGroupedByName(String language) {
ListMultimap<QProfileName, RulesProfile> profilesByName = ArrayListMultimap.create();
for (ProfileDefinition definition : definitions) {
ValidationMessages validation = ValidationMessages.create();
RulesProfile profile = definition.createProfile(validation);
if (language.equals(profile.getLanguage())) {
processValidationMessages(validation);
profilesByName.put(new QProfileName(profile.getLanguage(), profile.getName()), profile);
}
}
return profilesByName;
}

private static void processValidationMessages(ValidationMessages messages) {
checkRequest(messages.getErrors().isEmpty(), messages.getErrors());
}
} }

0 comments on commit c733f4b

Please sign in to comment.