package org.sonar.server.qualityprofile;
import com.google.common.collect.ImmutableList;
-import java.util.Date;
import java.util.List;
-import java.util.Map;
import javax.annotation.CheckForNull;
import org.sonar.db.qualityprofile.ActiveRuleKey;
public static final List<Inheritance> ALL = ImmutableList.of(NONE, OVERRIDES, INHERITED);
}
- Date createdAt();
+ long createdAt();
- Date updatedAt();
+ long updatedAt();
ActiveRuleKey key();
@CheckForNull
ActiveRuleKey parentKey();
- Map<String, String> params();
-
}
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
-import java.util.Iterator;
import java.util.List;
-import java.util.Map;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.profiles.ProfileExporter;
import org.sonar.api.rules.RulePriority;
import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.ValidationMessages;
+import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.qualityprofile.ActiveRuleDto;
+import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
@ServerSide
public class QProfileExporters {
+ private final DbClient dbClient;
private final QProfileLoader loader;
private final RuleFinder ruleFinder;
private final RuleActivator ruleActivator;
private final ProfileExporter[] exporters;
private final ProfileImporter[] importers;
- public QProfileExporters(QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator, ProfileExporter[] exporters, ProfileImporter[] importers) {
+ public QProfileExporters(DbClient dbClient, QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator, ProfileExporter[] exporters, ProfileImporter[] importers) {
+ this.dbClient = dbClient;
this.loader = loader;
this.ruleFinder = ruleFinder;
this.ruleActivator = ruleActivator;
/**
* Used by Pico if no {@link ProfileImporter} is found
*/
- public QProfileExporters(QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator, ProfileExporter[] exporters) {
- this(loader, ruleFinder, ruleActivator, exporters, new ProfileImporter[0]);
+ public QProfileExporters(QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator, ProfileExporter[] exporters, DbClient dbClient) {
+ this(dbClient, loader, ruleFinder, ruleActivator, exporters, new ProfileImporter[0]);
}
/**
* Used by Pico if no {@link ProfileExporter} is found
*/
- public QProfileExporters(QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator, ProfileImporter[] importers) {
- this(loader, ruleFinder, ruleActivator, new ProfileExporter[0], importers);
+ public QProfileExporters(QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator, ProfileImporter[] importers, DbClient dbClient) {
+ this(dbClient, loader, ruleFinder, ruleActivator, new ProfileExporter[0], importers);
}
/**
* Used by Pico if no {@link ProfileImporter} nor {@link ProfileExporter} is found
*/
- public QProfileExporters(QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator) {
- this(loader, ruleFinder, ruleActivator, new ProfileExporter[0], new ProfileImporter[0]);
+ public QProfileExporters(QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator, DbClient dbClient) {
+ this(dbClient, loader, ruleFinder, ruleActivator, new ProfileExporter[0], new ProfileImporter[0]);
}
public List<ProfileExporter> exportersForLanguage(String language) {
}
private RulesProfile wrap(QualityProfileDto profile) {
+ DbSession dbSession = dbClient.openSession(false);
RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage());
- for (Iterator<ActiveRuleDoc> activeRuleIterator = loader.findActiveRulesByProfile(profile.getKey()); activeRuleIterator.hasNext();) {
- ActiveRule activeRule = activeRuleIterator.next();
- Rule rule = ruleFinder.findByKey(activeRule.key().ruleKey());
- org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.severity()));
- for (Map.Entry<String, String> entry : activeRule.params().entrySet()) {
- wrappedActiveRule.setParameter(entry.getKey(), entry.getValue());
+ try {
+ for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByProfileKey(dbSession, profile.getKey())) {
+ Rule rule = ruleFinder.findByKey(activeRule.getKey().ruleKey());
+ org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.getSeverityString()));
+ List<ActiveRuleParamDto> paramDtos = dbClient.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRule.getId());
+ for (ActiveRuleParamDto activeRuleParamDto : paramDtos) {
+ wrappedActiveRule.setParameter(activeRuleParamDto.getKey(), activeRuleParamDto.getValue());
+ }
}
+ } finally {
+ dbClient.closeSession(dbSession);
}
return target;
}
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
-import java.util.Collections;
-import java.util.Date;
import java.util.Map;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.db.qualityprofile.ActiveRuleKey;
import org.sonar.server.qualityprofile.ActiveRule;
import org.sonar.server.search.BaseDoc;
-import org.sonar.server.search.IndexUtils;
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_CREATED_AT;
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE;
}
@Override
- @Deprecated
- public Map<String, String> params() {
- return Collections.emptyMap();
- }
-
- @Override
- @Deprecated
- public Date createdAt() {
- return IndexUtils.parseDateTime((String) getNullableField(FIELD_ACTIVE_RULE_CREATED_AT));
- }
-
- @CheckForNull
- public Long createdAtAsLong() {
+ public long createdAt() {
return (Long) getField(FIELD_ACTIVE_RULE_CREATED_AT);
}
}
@Override
- @Deprecated
- public Date updatedAt() {
- return IndexUtils.parseDateTime((String) getNullableField(FIELD_ACTIVE_RULE_UPDATED_AT));
- }
-
- @CheckForNull
- public Long updatedAtAsLong() {
+ public long updatedAt() {
return (Long) getField(FIELD_ACTIVE_RULE_UPDATED_AT);
}
bulk.add(newIndexRequest(activeRule));
// it's more efficient to sort programmatically than in SQL on some databases (MySQL for instance)
- maxDate = Math.max(maxDate, activeRule.updatedAtAsLong());
+ maxDate = Math.max(maxDate, activeRule.updatedAt());
}
bulk.stop();
return maxDate;
return this;
}
- @CheckForNull
- public Long createdAtAsLong() {
+ public long createdAt() {
return (Long) getField(RuleIndexDefinition.FIELD_RULE_CREATED_AT);
}
return this;
}
- @CheckForNull
- public Long updatedAtAtAsLong() {
+ public long updatedAt() {
return (Long) getField(RuleIndexDefinition.FIELD_RULE_UPDATED_AT);
}
bulk.add(newIndexRequest(rule));
// it's more efficient to sort programmatically than in SQL on some databases (MySQL for instance)
- maxDate = Math.max(maxDate, rule.updatedAtAtAsLong());
+ maxDate = Math.max(maxDate, rule.updatedAt());
}
bulk.stop();
return maxDate;
ActiveRule.Inheritance.valueOf(expectedInheritance));
// Dates should be set
- assertThat(activeRule.createdAtAsLong()).isNotNull();
- assertThat(activeRule.updatedAtAsLong()).isNotNull();
+ assertThat(activeRule.createdAt()).isNotNull();
+ assertThat(activeRule.updatedAt()).isNotNull();
}
}
assertThat(found).as("Rule is not activated in index").isTrue();
assertThat(activeRule.severity()).isEqualTo(CRITICAL);
assertThat(activeRule.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
assertThat(activeRule.parentKey()).isNull();
- assertThat(activeRule.createdAtAsLong()).isEqualTo(1500000000000L);
- assertThat(activeRule.updatedAtAsLong()).isEqualTo(1600000000000L);
+ assertThat(activeRule.createdAt()).isEqualTo(1500000000000L);
+ assertThat(activeRule.updatedAt()).isEqualTo(1600000000000L);
}
@Test
assertThat(activeRule.severity()).isEqualTo(CRITICAL);
assertThat(activeRule.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
assertThat(activeRule.parentKey()).isNull();
- assertThat(activeRule.createdAtAsLong()).isEqualTo(2000000000000L);
- assertThat(activeRule.updatedAtAsLong()).isEqualTo(2100000000000L);
+ assertThat(activeRule.createdAt()).isEqualTo(2000000000000L);
+ assertThat(activeRule.updatedAt()).isEqualTo(2100000000000L);
key = ActiveRuleKey.of("parent", RuleKey.of("xoo", "S001"));
activeRule = activeRulesByKey.get(key);
assertThat(activeRule.severity()).isEqualTo(INFO);
assertThat(activeRule.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
assertThat(activeRule.parentKey()).isNull();
- assertThat(activeRule.createdAtAsLong()).isEqualTo(1700000000000L);
- assertThat(activeRule.updatedAtAsLong()).isEqualTo(1800000000000L);
+ assertThat(activeRule.createdAt()).isEqualTo(1700000000000L);
+ assertThat(activeRule.updatedAt()).isEqualTo(1800000000000L);
key = ActiveRuleKey.of("child", RuleKey.of("xoo", "S001"));
activeRule = activeRulesByKey.get(key);
assertThat(activeRule.severity()).isEqualTo(BLOCKER);
assertThat(activeRule.inheritance()).isEqualTo(INHERITED);
assertThat(activeRule.parentKey()).isEqualTo(ActiveRuleKey.of("parent", RuleKey.of("xoo", "S001")));
- assertThat(activeRule.createdAtAsLong()).isEqualTo(1500000000000L);
- assertThat(activeRule.updatedAtAsLong()).isEqualTo(1600000000000L);
+ assertThat(activeRule.createdAt()).isEqualTo(1500000000000L);
+ assertThat(activeRule.updatedAt()).isEqualTo(1600000000000L);
}
@Test
ActiveRuleIndex activeRuleIndex = mock(ActiveRuleIndex.class);
when(activeRuleIndex.findByProfile(Matchers.anyString())).thenReturn(Sets.<ActiveRuleDoc>newHashSet().iterator());
- exporters = new QProfileExporters(new QProfileLoader(dbClient, activeRuleIndex, mock(RuleIndex.class)), null, null, new ProfileExporter[] {exporter1, exporter2}, null);
+ exporters = new QProfileExporters(dbClient, new QProfileLoader(dbClient, activeRuleIndex, mock(RuleIndex.class)), null, null, new ProfileExporter[] {exporter1, exporter2}, null);
wsTester = new WsTester(new QProfilesWs(mock(RuleActivationActions.class),
mock(BulkRuleActivationActions.class),
mock(ProjectAssociationActions.class),
@Test
public void do_not_fail_when_no_exporters() throws Exception {
- QProfileExporters myExporters = new QProfileExporters(null, null, null, new ProfileExporter[0], null);
+ QProfileExporters myExporters = new QProfileExporters(dbClient, null, null, null, new ProfileExporter[0], null);
WsTester myWsTester = new WsTester(new QProfilesWs(mock(RuleActivationActions.class),
mock(BulkRuleActivationActions.class),
mock(ProjectAssociationActions.class),
assertThat(rule.status()).isEqualTo(RuleStatus.READY);
assertThat(rule.isTemplate()).isFalse();
assertThat(rule.allTags()).containsOnly("bug", "performance", "cwe");
- assertThat(rule.createdAtAsLong()).isEqualTo(1500000000000L);
- assertThat(rule.updatedAtAtAsLong()).isEqualTo(1600000000000L);
+ assertThat(rule.createdAt()).isEqualTo(1500000000000L);
+ assertThat(rule.updatedAt()).isEqualTo(1600000000000L);
}
@Test
assertThat(rule.status()).isEqualTo(RuleStatus.READY);
assertThat(rule.isTemplate()).isFalse();
assertThat(rule.allTags()).containsOnly("bug", "performance", "cwe");
- assertThat(rule.createdAtAsLong()).isEqualTo(1500000000000L);
- assertThat(rule.updatedAtAtAsLong()).isEqualTo(1600000000000L);
+ assertThat(rule.createdAt()).isEqualTo(1500000000000L);
+ assertThat(rule.updatedAt()).isEqualTo(1600000000000L);
rule = rulesByKey.get("S002");
assertThat(rule.key()).isEqualTo(RuleKey.of("xoo", "S002"));
assertThat(rule.status()).isEqualTo(RuleStatus.BETA);
assertThat(rule.isTemplate()).isTrue();
assertThat(rule.allTags()).isEmpty();
- assertThat(rule.createdAtAsLong()).isEqualTo(2000000000000L);
- assertThat(rule.updatedAtAtAsLong()).isEqualTo(2100000000000L);
+ assertThat(rule.createdAt()).isEqualTo(2000000000000L);
+ assertThat(rule.updatedAt()).isEqualTo(2100000000000L);
}
@Test