return doActivate(dbSession, activation, context);
}
- public List<ActiveRuleChange> activateAndCommit(DbSession dbSession, RuleActivation activation, QProfileDto profile) {
+ public void activateAndCommit(DbSession dbSession, RuleActivation activation, QProfileDto profile) {
List<ActiveRuleChange> changes = activate(dbSession, activation, profile);
activeRuleIndexer.commitAndIndex(dbSession, changes);
- return changes;
}
public List<ActiveRuleChange> activate(DbSession dbSession, RuleActivation activation, QProfileDto profile) {
ActiveRuleChange change;
boolean stopPropagation = false;
- ActiveRuleDto activeRule = context.activeRule();
+ ActiveRuleDto activeRule = context.getActiveRule();
if (activeRule == null) {
if (activation.isReset()) {
// ignore reset when rule is not activated
return changes;
}
// new activation
- change = new ActiveRuleChange(ActiveRuleChange.Type.ACTIVATED, context.activeRuleKey());
+ change = new ActiveRuleChange(ActiveRuleChange.Type.ACTIVATED, context.getActiveRuleKey());
applySeverityAndParamToChange(activation, context, change);
if (context.isCascade() || context.isSameAsParent(change)) {
change.setInheritance(ActiveRule.Inheritance.INHERITED);
// propagating to descendants, but child profile already overrides rule -> stop propagation
return changes;
}
- change = new ActiveRuleChange(ActiveRuleChange.Type.UPDATED, context.activeRuleKey());
+ change = new ActiveRuleChange(ActiveRuleChange.Type.UPDATED, context.getActiveRuleKey());
if (context.isCascade() && activeRule.getInheritance() == null) {
// activate on child, then on parent -> mark child as overriding parent
change.setInheritance(ActiveRule.Inheritance.OVERRIDES);
- change.setSeverity(context.currentSeverity());
+ change.setSeverity(context.getActiveSeverityBeforeChange());
change.setParameters(context.activeRuleParamsAsStringMap());
stopPropagation = true;
} else {
applySeverityAndParamToChange(activation, context, change);
- if (!context.isCascade() && context.parentActiveRule() != null) {
+ if (!context.isCascade() && context.getParentActiveRule() != null) {
// override rule which is already declared on parents
change.setInheritance(context.isSameAsParent(change) ? ActiveRule.Inheritance.INHERITED : ActiveRule.Inheritance.OVERRIDES);
}
}
private void updateProfileDates(DbSession dbSession, RuleActivatorContext context) {
- QProfileDto profile = context.getProfile();
+ QProfileDto profile = context.getOrganizationProfile();
if (profile != null) {
- profile.setRulesUpdatedAtAsDate(context.getInitDate());
+ profile.setRulesUpdatedAtAsDate(context.getDate());
if (userSession.isLoggedIn()) {
- profile.setUserUpdatedAt(context.getInitDate().getTime());
+ profile.setUserUpdatedAt(context.getDate().getTime());
}
db.qualityProfileDao().update(dbSession, profile);
} else {
// built-in profile, change rules_profiles.rules_updated_at
RulesProfileDto rulesProfile = context.getRulesProfile();
- rulesProfile.setRulesUpdatedAtAsDate(context.getInitDate());
+ rulesProfile.setRulesUpdatedAtAsDate(context.getDate());
db.qualityProfileDao().update(dbSession, rulesProfile);
}
}
if (request.isReset()) {
// load severity and params from parent profile, else from default values
change.setSeverity(firstNonNull(
- context.parentSeverity(), context.defaultSeverity()));
- for (RuleParamDto ruleParamDto : context.ruleParams()) {
+ context.getActiveParentSeverity(), context.getRule().getSeverityString()));
+ for (RuleParamDto ruleParamDto : context.getRuleParams()) {
String paramKey = ruleParamDto.getName();
change.setParameter(paramKey, validateParam(ruleParamDto, firstNonNull(
- context.parentParamValue(paramKey), context.defaultParamValue(paramKey))));
+ context.getActiveParentParamValue(paramKey), context.getDefaultParamValue(paramKey))));
}
- } else if (context.activeRule() != null) {
+ } else if (context.getActiveRule() != null) {
// already activated -> load severity and parameters from request, else keep existing ones, else from parent,
// else from default
change.setSeverity(firstNonNull(
request.getSeverity(),
- context.currentSeverity(),
- context.parentSeverity(),
- context.defaultSeverity()));
- for (RuleParamDto ruleParamDto : context.ruleParams()) {
+ context.getActiveSeverityBeforeChange(),
+ context.getActiveParentSeverity(),
+ context.getRule().getSeverityString()));
+ for (RuleParamDto ruleParamDto : context.getRuleParams()) {
String paramKey = ruleParamDto.getName();
- String paramValue = context.hasRequestParamValue(request, paramKey) ?
+ String paramValue = context.hasRequestedParamValue(request, paramKey) ?
// If the request contains the parameter then we're using either value from request, or parent value, or default value
firstNonNull(
- context.requestParamValue(request, paramKey),
- context.parentParamValue(paramKey),
- context.defaultParamValue(paramKey))
+ context.getRequestedParamValue(request, paramKey),
+ context.getActiveParentParamValue(paramKey),
+ context.getDefaultParamValue(paramKey))
// If the request doesn't contain the parameter, then we're using either value in DB, or parent value, or default value
: firstNonNull(
- context.currentParamValue(paramKey),
- context.parentParamValue(paramKey),
- context.defaultParamValue(paramKey));
+ context.getActiveParamValue(paramKey),
+ context.getActiveParentParamValue(paramKey),
+ context.getDefaultParamValue(paramKey));
change.setParameter(paramKey, validateParam(ruleParamDto, paramValue));
}
- } else if (context.activeRule() == null) {
+ } else if (context.getActiveRule() == null) {
// not activated -> load severity and parameters from request, else from parent, else from defaults
change.setSeverity(firstNonNull(
request.getSeverity(),
- context.parentSeverity(),
- context.defaultSeverity()));
- for (RuleParamDto ruleParamDto : context.ruleParams()) {
+ context.getActiveParentSeverity(),
+ context.getRule().getSeverityString()));
+ for (RuleParamDto ruleParamDto : context.getRuleParams()) {
String paramKey = ruleParamDto.getName();
change.setParameter(paramKey, validateParam(ruleParamDto,
firstNonNull(
- context.requestParamValue(request, paramKey),
- context.parentParamValue(paramKey),
- context.defaultParamValue(paramKey))));
+ context.getRequestedParamValue(request, paramKey),
+ context.getActiveParentParamValue(paramKey),
+ context.getDefaultParamValue(paramKey))));
}
}
}
}
protected List<QProfileDto> getChildren(DbSession session, RuleActivatorContext context) {
- if (context.getProfile() != null) {
- return db.qualityProfileDao().selectChildren(session, context.getProfile());
+ QProfileDto orgProfile = context.getOrganizationProfile();
+ if (orgProfile != null) {
+ return db.qualityProfileDao().selectChildren(session, orgProfile);
}
return db.qualityProfileDao().selectChildrenOfBuiltInRulesProfile(session, context.getRulesProfile());
}
dao.insert(dbSession, activeRule);
for (Map.Entry<String, String> param : change.getParameters().entrySet()) {
if (param.getValue() != null) {
- ActiveRuleParamDto paramDto = ActiveRuleParamDto.createFor(context.ruleParamsByKeys().get(param.getKey()));
+ ActiveRuleParamDto paramDto = ActiveRuleParamDto.createFor(context.getRuleParam(param.getKey()));
paramDto.setValue(param.getValue());
dao.insertParam(dbSession, activeRule, paramDto);
}
private ActiveRuleDto doUpdate(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
ActiveRuleDao dao = db.activeRuleDao();
- ActiveRuleDto activeRule = context.activeRule();
+ ActiveRuleDto activeRule = context.getActiveRule();
if (activeRule != null) {
String severity = change.getSeverity();
if (severity != null) {
dao.update(dbSession, activeRule);
for (Map.Entry<String, String> param : change.getParameters().entrySet()) {
- ActiveRuleParamDto activeRuleParamDto = context.activeRuleParamsAsMap().get(param.getKey());
+ ActiveRuleParamDto activeRuleParamDto = context.getActiveParamBeforeChange(param.getKey());
if (activeRuleParamDto == null) {
// did not exist
if (param.getValue() != null) {
- activeRuleParamDto = ActiveRuleParamDto.createFor(context.ruleParamsByKeys().get(param.getKey()));
+ activeRuleParamDto = ActiveRuleParamDto.createFor(context.getRuleParam(param.getKey()));
activeRuleParamDto.setValue(param.getValue());
dao.insertParam(dbSession, activeRule, activeRuleParamDto);
}
private List<ActiveRuleChange> cascadeDeactivation(DbSession dbSession, RuleActivatorContext context, RuleKey ruleKey, boolean force) {
List<ActiveRuleChange> changes = new ArrayList<>();
ActiveRuleChange change;
- ActiveRuleDto activeRuleDto = context.activeRule();
+ ActiveRuleDto activeRuleDto = context.getActiveRule();
if (activeRuleDto == null) {
return changes;
}
private final QProfileDto profile;
private final RulesProfileDto rulesProfile;
- private final Date initDate = new Date();
+ private final Date now = new Date();
private RuleDefinitionDto rule;
- private final Map<String, RuleParamDto> ruleParams = new HashMap<>();
+ private final Map<String, RuleParamDto> ruleParamsByKey = new HashMap<>();
private ActiveRuleDto activeRule;
private ActiveRuleDto parentActiveRule;
private final Map<String, ActiveRuleParamDto> activeRuleParams = new HashMap<>();
this.isCascade = false;
}
+ /**
+ * The organization profile that is being updated. Null if updating built-in profile.
+ */
@CheckForNull
- QProfileDto getProfile() {
+ QProfileDto getOrganizationProfile() {
return profile;
}
return rulesProfile;
}
- boolean isBuiltIn() {
- return profile == null;
- }
-
+ /**
+ * Whether activation is being applied on a descendant profile ({@code true}) or
+ * on the target profile ({@code false}).
+ */
boolean isCascade() {
return isCascade;
}
- ActiveRuleKey activeRuleKey() {
+ ActiveRuleKey getActiveRuleKey() {
return ActiveRuleKey.of(rulesProfile, rule.getKey());
}
return this;
}
-
-
- Date getInitDate() {
- return initDate;
+ /**
+ * Date of request for activation.
+ */
+ Date getDate() {
+ return now;
}
- Map<String, RuleParamDto> ruleParamsByKeys() {
- return ruleParams;
+ @CheckForNull
+ RuleParamDto getRuleParam(String key) {
+ return ruleParamsByKey.get(key);
}
- Collection<RuleParamDto> ruleParams() {
- return ruleParams.values();
+ Collection<RuleParamDto> getRuleParams() {
+ return ruleParamsByKey.values();
}
- RuleActivatorContext setRuleParams(Collection<RuleParamDto> ruleParams) {
- this.ruleParams.clear();
+ void setRuleParams(Collection<RuleParamDto> ruleParams) {
+ this.ruleParamsByKey.clear();
for (RuleParamDto ruleParam : ruleParams) {
- this.ruleParams.put(ruleParam.getName(), ruleParam);
+ this.ruleParamsByKey.put(ruleParam.getName(), ruleParam);
}
- return this;
}
+ /**
+ * The active rule as it was before applying the current activation.
+ * Return null if the rule was not activated.
+ */
@CheckForNull
- ActiveRuleDto activeRule() {
+ ActiveRuleDto getActiveRule() {
return activeRule;
}
}
@CheckForNull
- ActiveRuleDto parentActiveRule() {
+ ActiveRuleDto getParentActiveRule() {
return parentActiveRule;
}
- RuleActivatorContext setParentActiveRule(@Nullable ActiveRuleDto a) {
+ void setParentActiveRule(@Nullable ActiveRuleDto a) {
this.parentActiveRule = a;
- return this;
}
@CheckForNull
- String requestParamValue(RuleActivation request, String key) {
+ String getRequestedParamValue(RuleActivation request, String key) {
if (rule.isCustomRule()) {
return null;
}
return request.getParameter(key);
}
- boolean hasRequestParamValue(RuleActivation request, String key) {
+ boolean hasRequestedParamValue(RuleActivation request, String key) {
return request.hasParameter(key);
}
@CheckForNull
- String currentParamValue(String key) {
+ String getActiveParamValue(String key) {
ActiveRuleParamDto param = activeRuleParams.get(key);
return param != null ? param.getValue() : null;
}
@CheckForNull
- String parentParamValue(String key) {
+ ActiveRuleParamDto getActiveParamBeforeChange(String key) {
+ return activeRuleParams.get(key);
+ }
+
+ @CheckForNull
+ String getActiveParentParamValue(String key) {
ActiveRuleParamDto param = parentActiveRuleParams.get(key);
return param != null ? param.getValue() : null;
}
@CheckForNull
- String defaultParamValue(String key) {
- RuleParamDto param = ruleParams.get(key);
+ String getDefaultParamValue(String key) {
+ RuleParamDto param = ruleParamsByKey.get(key);
return param != null ? param.getDefaultValue() : null;
}
@CheckForNull
- String currentSeverity() {
+ String getActiveSeverityBeforeChange() {
return activeRule != null ? activeRule.getSeverityString() : null;
}
@CheckForNull
- String parentSeverity() {
+ String getActiveParentSeverity() {
return parentActiveRule != null ? parentActiveRule.getSeverityString() : null;
}
- @CheckForNull
- String defaultSeverity() {
- return rule.getSeverityString();
- }
-
- Map<String, ActiveRuleParamDto> activeRuleParamsAsMap() {
- return activeRuleParams;
- }
Map<String, String> activeRuleParamsAsStringMap() {
Map<String, String> params = Maps.newHashMap();
return params;
}
- RuleActivatorContext setActiveRuleParams(@Nullable Collection<ActiveRuleParamDto> a) {
+ void setActiveRuleParams(@Nullable Collection<ActiveRuleParamDto> a) {
activeRuleParams.clear();
if (a != null) {
for (ActiveRuleParamDto ar : a) {
this.activeRuleParams.put(ar.getKey(), ar);
}
}
- return this;
}
- RuleActivatorContext setParentActiveRuleParams(@Nullable Collection<ActiveRuleParamDto> a) {
+ void setParentActiveRuleParams(@Nullable Collection<ActiveRuleParamDto> a) {
parentActiveRuleParams.clear();
if (a != null) {
for (ActiveRuleParamDto ar : a) {
this.parentActiveRuleParams.put(ar.getKey(), ar);
}
}
- return this;
}
/**
return false;
}
for (Map.Entry<String, String> entry : change.getParameters().entrySet()) {
- if (entry.getValue() != null && !entry.getValue().equals(parentParamValue(entry.getKey()))) {
+ if (entry.getValue() != null && !entry.getValue().equals(getActiveParentParamValue(entry.getKey()))) {
return false;
}
}