*/
package org.sonar.server.qualityprofile;
+import com.google.common.base.Optional;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import org.elasticsearch.search.SearchHit;
import org.sonar.api.server.ServerSide;
import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.ActiveRuleKey;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.activity.index.ActivityIndex;
-import org.sonar.server.db.DbClient;
import org.sonar.server.es.SearchOptions;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.search.Result;
import org.sonar.server.user.UserSession;
private final DbClient db;
private final ActivityIndex activityIndex;
+ private final ActiveRuleIndexer activeRuleIndexer;
private final RuleActivator ruleActivator;
private final QProfileFactory factory;
private final QProfileBackuper backuper;
private final QProfileExporters exporters;
private final UserSession userSession;
- public QProfileService(DbClient db, ActivityIndex activityIndex, RuleActivator ruleActivator, QProfileFactory factory,
+ public QProfileService(DbClient db, ActivityIndex activityIndex, ActiveRuleIndexer activeRuleIndexer, RuleActivator ruleActivator, QProfileFactory factory,
QProfileBackuper backuper, QProfileCopier copier, QProfileReset reset, QProfileExporters exporters,
UserSession userSession) {
this.db = db;
this.activityIndex = activityIndex;
+ this.activeRuleIndexer = activeRuleIndexer;
this.ruleActivator = ruleActivator;
this.factory = factory;
this.backuper = backuper;
}
}
dbSession.commit();
+ activeRuleIndexer.index(result.getChanges());
return result;
} finally {
dbSession.close();
try {
List<ActiveRuleChange> changes = ruleActivator.activate(dbSession, activation, profileKey);
dbSession.commit();
+ activeRuleIndexer.index(changes);
return changes;
} finally {
dbSession.close();
public void delete(String key) {
verifyAdminPermission();
- factory.delete(key);
+ DbSession session = db.openSession(false);
+ try {
+ List<ActiveRuleChange> changes = factory.delete(session, key, false);
+ session.commit();
+ activeRuleIndexer.index(changes);
+ } finally {
+ db.closeSession(session);
+ }
}
public void rename(String key, String newName) {
Result<QProfileActivity> result = new Result<>(response);
for (SearchHit hit : response.getHits().getHits()) {
QProfileActivity profileActivity = new QProfileActivity(hit.getSource());
- RuleDto ruleDto = db.deprecatedRuleDao().getNullableByKey(session, profileActivity.ruleKey());
- profileActivity.ruleName(ruleDto != null ? ruleDto.getName() : null);
+ Optional<RuleDto> ruleDto = db.ruleDao().selectByKey(session, profileActivity.ruleKey());
+ profileActivity.ruleName(ruleDto.isPresent() ? ruleDto.get().getName() : null);
String login = profileActivity.getLogin();
if (login != null) {
import org.sonar.api.rules.RulePriority;
import org.sonar.api.utils.ValidationMessages;
import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.ActiveRuleKey;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.activity.Activity;
import org.sonar.server.activity.ActivityService;
-import org.sonar.server.db.DbClient;
import org.sonar.server.es.SearchOptions;
import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer;
+import org.sonar.server.rule.index.RuleIndexer;
import org.sonar.server.search.FacetValue;
import org.sonar.server.search.Result;
import org.sonar.server.tester.ServerTester;
@ClassRule
public static ServerTester tester = new ServerTester().withStartupTasks().withEsIndexes().addComponents(XooProfileImporter.class, XooExporter.class);
+
@org.junit.Rule
public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
QProfileService service;
QProfileLoader loader;
RuleActivator activator;
+ RuleIndexer ruleIndexer;
+ ActiveRuleIndexer activeRuleIndexer;
@Before
public void before() {
service = tester.get(QProfileService.class);
loader = tester.get(QProfileLoader.class);
activator = tester.get(RuleActivator.class);
+ ruleIndexer = tester.get(RuleIndexer.class);
+ activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
// create pre-defined rules
RuleDto xooRule1 = RuleTesting.newXooX1().setSeverity("MINOR");
- db.deprecatedRuleDao().insert(dbSession, xooRule1);
+ db.ruleDao().insert(dbSession, xooRule1);
// create pre-defined profiles P1 and P2
db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1(), QProfileTesting.newXooP2());
+
dbSession.commit();
dbSession.clearCache();
+ ruleIndexer.index();
}
@After
public void create_profile_with_xml() {
userSessionRule.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
- db.deprecatedRuleDao().insert(dbSession, RuleTesting.newDto(RuleKey.of("xoo", "R1")).setLanguage("xoo").setSeverity("MINOR"));
+ db.ruleDao().insert(dbSession, RuleTesting.newDto(RuleKey.of("xoo", "R1")).setLanguage("xoo").setSeverity("MINOR"));
dbSession.commit();
+ ruleIndexer.index();
QProfileResult result = service.create(QProfileName.createFor("xoo", "New Profile"), ImmutableMap.of("XooProfileImporter", "<xml/>"));
QualityProfileDto profile = result.profile();
service.activate(XOO_P1_KEY, new RuleActivation(RuleTesting.XOO_X1).setSeverity("BLOCKER"));
service.activate(XOO_P2_KEY, new RuleActivation(RuleTesting.XOO_X1).setSeverity("BLOCKER"));
-
dbSession.clearCache();
+ activeRuleIndexer.index();
Map<String, Long> counts = loader.countAllActiveRules();
assertThat(counts).hasSize(2);
service.activate(XOO_P1_KEY, new RuleActivation(RuleTesting.XOO_X1).setSeverity("MINOR"));
service.activate(XOO_P2_KEY, new RuleActivation(RuleTesting.XOO_X1).setSeverity("BLOCKER"));
dbSession.clearCache();
+ activeRuleIndexer.index();
Map<String, Multimap<String, FacetValue>> stats = loader.getAllProfileStats();
// create deprecated rule
RuleDto deprecatedXooRule = RuleTesting.newDto(RuleKey.of("xoo", "deprecated1"))
.setSeverity("MINOR").setLanguage("xoo").setStatus(RuleStatus.DEPRECATED);
- db.deprecatedRuleDao().insert(dbSession, deprecatedXooRule);
+ db.ruleDao().insert(dbSession, deprecatedXooRule);
dbSession.commit();
+ ruleIndexer.index();
// active some rules
service.activate(XOO_P1_KEY, new RuleActivation(deprecatedXooRule.getKey()).setSeverity("BLOCKER"));
UserDto user = new UserDto().setLogin("david").setName("David").setEmail("dav@id.com").setCreatedAt(System.currentTimeMillis()).setUpdatedAt(System.currentTimeMillis());
db.userDao().insert(dbSession, user);
+ dbSession.commit();
// We need an actual rule in DB to test RuleName in Activity
- RuleDto rule = db.deprecatedRuleDao().getByKey(dbSession, RuleTesting.XOO_X1);
-
- dbSession.commit();
+ RuleDto rule = db.ruleDao().selectOrFailByKey(dbSession, RuleTesting.XOO_X1);
tester.get(ActivityService.class).save(ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1))
.setSeverity(Severity.MAJOR)
userSessionRule.login("david").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
// We need an actual rule in DB to test RuleName in Activity
- db.deprecatedRuleDao().getByKey(dbSession, RuleTesting.XOO_X1);
- dbSession.commit();
+ // TODO ???
+ //db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1);
+ //dbSession.commit();
tester.get(ActivityService.class).save(
ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1))