import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+import javax.persistence.Transient;
import java.util.Date;
private String noteUserLogin;
private String noteData;
+ // This field do not exists in db, it's only retrieve by joins
+ @Transient
private Integer parentId;
public Integer getId() {
<select id="selectById" parameterType="Integer" resultType="ActiveRule">
SELECT <include refid="activeRuleColumns"/>
- FROM active_rules a
+ FROM active_rules a1
<include refid="activeRuleJoin"/>
<where>
AND a.id=#{id}
<select id="selectAll" parameterType="map" resultType="ActiveRule">
select
- <include refid="activeRuleColumns"/>,active_rule_parent.id as parentId
+ <include refid="activeRuleColumns"/>
from active_rules a
<include refid="activeRuleJoin"/>
</select>
throw new BadRequestException("Please do not select a child profile as parent.");
}
String parentName = parentProfile != null ? parentProfile.getName() : null;
-
- ProfilesManager.RuleInheritanceActions actions = profilesManager.profileParentChanged(profile.getId(), parentName, userSession.name());
- ruleRegistry.deleteActiveRules(actions.idsToDelete());
- ruleRegistry.bulkIndexActiveRuleIds(actions.idsToIndex(), session);
-
profile.setParent(parentName);
dao.update(profile, session);
session.commit();
+ ProfilesManager.RuleInheritanceActions actions = profilesManager.profileParentChanged(profile.getId(), parentName, userSession.name());
+ ruleRegistry.deleteActiveRules(actions.idsToDelete());
+ ruleRegistry.bulkIndexActiveRuleIds(actions.idsToIndex(), session);
} finally {
MyBatis.closeQuietly(session);
}
public static final String OVERRIDES = "OVERRIDES";
private final Integer id;
- private final Integer parentId;
private final String key;
private final String repositoryKey;
private final String name;
private final String description;
private final String status;
private final String cardinality;
- private final Integer ruleId;
+ private final Integer templateId;
private final Date createdAt;
private final Date updatedAt;
private final QProfileRuleNote ruleNote;
private final Integer activeRuleId;
+ private final Integer activeRuleParentId;
private final String severity;
private final String inheritance;
private final QProfileRuleNote activeRuleNote;
description = (String) ruleSource.get(RuleDocument.FIELD_DESCRIPTION);
status = (String) ruleSource.get(RuleDocument.FIELD_STATUS);
cardinality = (String) ruleSource.get("cardinality");
- ruleId = (Integer) ruleSource.get(RuleDocument.FIELD_PARENT_KEY);
+ templateId = (Integer) ruleSource.get(RuleDocument.FIELD_TEMPLATE_ID);
createdAt = parseOptionalDate(RuleDocument.FIELD_CREATED_AT, ruleSource);
updatedAt = parseOptionalDate(RuleDocument.FIELD_UPDATED_AT, ruleSource);
severity = (String) ruleSource.get(ActiveRuleDocument.FIELD_SEVERITY);
inheritance = null;
activeRuleNote = null;
- parentId = null;
+ activeRuleParentId = null;
} else {
activeRuleId = (Integer) activeRuleSource.get(ActiveRuleDocument.FIELD_ID);
severity = (String) activeRuleSource.get(ActiveRuleDocument.FIELD_SEVERITY);
inheritance = (String) activeRuleSource.get(ActiveRuleDocument.FIELD_INHERITANCE);
- parentId = (Integer) activeRuleSource.get(ActiveRuleDocument.FIELD_PARENT_ID);
+ activeRuleParentId = (Integer) activeRuleSource.get(ActiveRuleDocument.FIELD_ACTIVE_RULE_PARENT_ID);
if (activeRuleSource.containsKey(ActiveRuleDocument.FIELD_NOTE)) {
Map<String, Object> ruleNoteDocument = (Map<String, Object>) activeRuleSource.get(ActiveRuleDocument.FIELD_NOTE);
}
@CheckForNull
- public Integer ruleId() {
- return ruleId;
+ public Integer templateId() {
+ return templateId;
}
@CheckForNull
- public Integer parentId() {
- return parentId;
+ public Integer activeRuleParentId() {
+ return activeRuleParentId;
}
public boolean isInherited() {
}
public boolean isEditable() {
- return ruleId != null;
+ return templateId != null;
}
public List<QProfileRuleParam> params() {
return activeRuleNote;
}
- @Override
- public String toString() {
- return new ReflectionToStringBuilder(this).toString();
- }
-
public List<String> systemTags() {
return systemTags;
}
return adminTags;
}
+ @Override
+ public String toString() {
+ return new ReflectionToStringBuilder(this).toString();
+ }
+
}
*/
package org.sonar.server.qualityprofile;
+import org.apache.commons.lang.builder.ReflectionToStringBuilder;
+
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
public String type() {
return type;
}
+
+ @Override
+ public String toString() {
+ return new ReflectionToStringBuilder(this).toString();
+ }
}
@CheckForNull
public QProfileRule parentProfileRule(QProfileRule rule) {
- Integer parentId = rule.parentId();
+ Integer parentId = rule.activeRuleParentId();
if (parentId != null) {
return rules.findByActiveRuleId(parentId);
}
public class ActiveRuleDocument {
public static final String FIELD_ID = "id";
- public static final String FIELD_PARENT_ID = "parentId";
public static final String FIELD_SEVERITY = "severity";
public static final String FIELD_PROFILE_ID = "profileId";
public static final String FIELD_INHERITANCE = "inheritance";
+ public static final String FIELD_ACTIVE_RULE_PARENT_ID = "activeRuleParentId";
public static final String FIELD_PARAMS = "params";
public static final String FIELD_NOTE = "note";
public static final String FIELD_STATUS = "status";
public static final String FIELD_SEVERITY = "severity";
public static final String FIELD_REPOSITORY_KEY = "repositoryKey";
- public static final String FIELD_PARENT_KEY = "parentKey";
+ public static final String FIELD_TEMPLATE_ID = "templateId";
public static final String FIELD_DESCRIPTION = "description";
public static final String FIELD_NAME = "name";
public static final String FIELD_LANGUAGE = "language";
.field(RuleDocument.FIELD_LANGUAGE, rule.getLanguage())
.field(RuleDocument.FIELD_NAME, rule.getName())
.field(RuleDocument.FIELD_DESCRIPTION, rule.getDescription())
- .field(RuleDocument.FIELD_PARENT_KEY, rule.getParentId() == null ? null : rule.getParentId())
+ .field(RuleDocument.FIELD_TEMPLATE_ID, rule.getParentId() == null ? null : rule.getParentId())
.field(RuleDocument.FIELD_REPOSITORY_KEY, rule.getRepositoryKey())
.field(RuleDocument.FIELD_SEVERITY, getSeverityFromOrdinal(rule.getSeverity()))
.field(RuleDocument.FIELD_STATUS, rule.getStatus())
XContentBuilder document = XContentFactory.jsonBuilder()
.startObject()
.field(ActiveRuleDocument.FIELD_ID, activeRule.getId())
- .field(ActiveRuleDocument.FIELD_PARENT_ID, activeRule.getParentId())
+ .field(ActiveRuleDocument.FIELD_ACTIVE_RULE_PARENT_ID, activeRule.getParentId())
.field(ActiveRuleDocument.FIELD_SEVERITY, getSeverityFromOrdinal(activeRule.getSeverity()))
.field(ActiveRuleDocument.FIELD_PROFILE_ID, activeRule.getProfileId())
.field(ActiveRuleDocument.FIELD_INHERITANCE, activeRule.getInheritance());
"type": "integer",
"index": "not_analyzed"
},
- "parentId": {
+ "activeRuleParentId": {
"type": "integer",
"index": "not_analyzed"
},
"type": "string",
"index": "no"
},
- "parentKey": {
+ "templateId": {
"type": "string",
"index": "not_analyzed"
},
call_backend do
@profile = Internal.quality_profiles.profile(params[:id].to_i)
@rule = Internal.quality_profiles.rule(params[:rule_id].to_i)
- if @rule.ruleId().nil?
+ if @rule.templateId().nil?
redirect_to :action => 'index', :id => params[:id]
else
- @parent_rule = Internal.quality_profiles.rule(@rule.ruleId())
+ @parent_rule = Internal.quality_profiles.rule(@rule.templateId())
@active_rules = Internal.quality_profiles.countActiveRules(@rule)
end
end
%>
<table width="100%" class="table spacer-bottom bordered background-gray">
<%
+ puts "#### " + rule.to_s
if parent_active_rule
parent_active_rule_link = link_to parent_profile.name, :controller => 'rules_configuration', :action => 'index',
:id => parent_profile.id, :rule_id => rule.id, :anchor => 'rule' + rule.id.to_s
%>
<tr>
<td colspan="2">
- <%= message(rule.inherited? ? 'rules_configuration.rule_inherited_from_profile_x' : 'rules_configuration.rule_overriding_from_profile_x',
- :params => parent_active_rule_link) -%>
+ <%= message(rule.inherited? ? 'rules_configuration.rule_inherited_from_profile_x' : 'rules_configuration.rule_overriding_from_profile_x', :params => parent_active_rule_link) -%>
<% if parent_active_rule.severity != rule.severity %>
<img src="<%= ApplicationController.root_context -%>/images/overrides.png" alt="Overrides parent definition" title="<%= message('rules_configuration.overrides_parent_definition') -%>" style="vertical-align: middle"/>
<span class="form-val-note" style="font-weight: bold"> <%= message('rules_configuration.original_severity') -%>
public void parent_active_rule() throws Exception {
QProfileRule rule = mock(QProfileRule.class);
when(rule.id()).thenReturn(10);
- when(rule.parentId()).thenReturn(6);
+ when(rule.activeRuleParentId()).thenReturn(6);
qProfiles.parentProfileRule(rule);
verify(rules).findByActiveRuleId(6);
public void parent_active_rule_return_null_when_no_parent() throws Exception {
QProfileRule rule = mock(QProfileRule.class);
when(rule.id()).thenReturn(10);
- when(rule.parentId()).thenReturn(null);
+ when(rule.activeRuleParentId()).thenReturn(null);
assertThat(qProfiles.parentProfileRule(rule)).isNull();
verify(rules, never()).findByActiveRuleId(anyInt());
assertThat(profileRules.findByActiveRuleId(9999)).isNull();
}
+ @Test
+ public void find_by_active_rule_id_with_inheritance() throws Exception {
+ esSetup.client().prepareBulk()
+ // Parent active rule
+ .add(Requests.indexRequest().index("rules").type("active_rule").parent("25").source(testFileAsString("find_active_rules_with_inheritance/active_rule25.json")))
+ // Child active rule
+ .add(Requests.indexRequest().index("rules").type("active_rule").parent("759").source(testFileAsString("find_active_rules_with_inheritance/active_rule391.json")))
+ .setRefresh(true)
+ .execute().actionGet();
+
+ QProfileRule child = profileRules.findByActiveRuleId(391);
+ assertThat(child.inheritance()).isEqualTo("INHERITED");
+ assertThat(child.activeRuleParentId()).isEqualTo(25);
+ }
+
@Test
public void find_by_profile_id_and_rule_id() {
// Rules on profiles
--- /dev/null
+{
+ "id": 25,
+ "severity": "MINOR",
+ "profileId": 1,
+ "inheritance": null
+}
--- /dev/null
+{
+ "id": 391,
+ "severity": "MINOR",
+ "profileId": 2,
+ "inheritance": "INHERITED",
+ "activeRuleParentId": 25
+}