Skip to content

Commit

Permalink
SONAR-6195 Adjust UI to request fields as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
jblievremont committed Jun 25, 2015
1 parent 9273b7f commit 5a9eaac
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 30 deletions.
Expand Up @@ -81,6 +81,7 @@
import org.sonar.server.util.Validation;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;

/**
* Used through ruby code <pre>Internal.issues</pre>
Expand All @@ -100,6 +101,8 @@ public class InternalRubyIssueService {

private static final String ACTION_PLANS_ERRORS_ACTION_PLAN_DOES_NOT_EXIST_MESSAGE = "action_plans.errors.action_plan_does_not_exist";

private static final List<String> ISSUE_FIELDS = ImmutableList.copyOf(IssueJsonWriter.SELECTABLE_FIELDS);

private final IssueService issueService;
private final IssueQueryService issueQueryService;
private final IssueCommentService commentService;
Expand Down Expand Up @@ -742,14 +745,20 @@ public String writeIssueJson(@Nullable Issue original) {

projectsByComponentUuid = issueComponentHelper.prepareComponentsAndProjects(projectUuids, componentUuids, componentsByUuid, componentDtos, subProjectDtos, dbSession);

Map<String, ActionPlan> actionPlans = newHashMap();
String actionPlanKey = issue.actionPlanKey();
if (actionPlanKey != null) {
actionPlans.put(actionPlanKey, actionPlanService.findByKey(actionPlanKey, userSession));
}

json.beginObject().name("issue");
issueWriter.write(json, issue,
usersByLogin,
componentsByUuid,
projectsByComponentUuid,
ImmutableMultimap.<String, DefaultIssueComment>of(),
ImmutableMap.<String, ActionPlan>of(),
ImmutableList.copyOf(IssueJsonWriter.SELECTABLE_FIELDS));
actionPlans,
ISSUE_FIELDS);

json.name("users").beginArray();
String assignee = issue.assignee();
Expand Down
Expand Up @@ -32,7 +32,6 @@
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.i18n.I18n;
import org.sonar.api.issue.ActionPlan;
import org.sonar.api.issue.Issue;
import org.sonar.api.issue.IssueComment;
Expand Down Expand Up @@ -86,13 +85,11 @@ public class IssueJsonWriter {

private static final List<String> SELECTABLE_MINUS_EXTRAS = ImmutableList.copyOf(Sets.difference(SELECTABLE_FIELDS, EXTRA_FIELDS));

private final I18n i18n;
private final Durations durations;
private final UserSession userSession;
private final IssueActionsWriter actionsWriter;

public IssueJsonWriter(I18n i18n, Durations durations, UserSession userSession, IssueActionsWriter actionsWriter) {
this.i18n = i18n;
public IssueJsonWriter(Durations durations, UserSession userSession, IssueActionsWriter actionsWriter) {
this.durations = durations;
this.userSession = userSession;
this.actionsWriter = actionsWriter;
Expand All @@ -102,12 +99,7 @@ public void write(JsonWriter json, Issue issue, Map<String, User> usersByLogin,
Map<String, ComponentDto> projectsByComponentUuid, Multimap<String, DefaultIssueComment> commentsByIssues, Map<String, ActionPlan> actionPlanByKeys,
@Nullable List<String> selectedFields) {

List<String> fields = Lists.newArrayList();
if (selectedFields == null || selectedFields.isEmpty()) {
fields.addAll(SELECTABLE_MINUS_EXTRAS);
} else {
fields.addAll(selectedFields);
}
List<String> fields = useDefaultFieldsIfNotSpecified(selectedFields);

json.beginObject();

Expand Down Expand Up @@ -145,19 +137,29 @@ public void write(JsonWriter json, Issue issue, Map<String, User> usersByLogin,
writeIfNeeded(json, isoDate(updateDate), FIELD_UPDATE_DATE, fields);
writeIfNeeded(json, isoDate(issue.closeDate()), FIELD_CLOSE_DATE, fields);

if (JsonWriterUtils.isFieldWanted(FIELD_TAGS, fields)) {
if (JsonWriterUtils.isFieldNeeded(FIELD_TAGS, fields)) {
writeTags(issue, json);
}
if (JsonWriterUtils.isFieldWanted(FIELD_COMMENTS, fields)) {
if (JsonWriterUtils.isFieldNeeded(FIELD_COMMENTS, fields)) {
writeIssueComments(commentsByIssues.get(issue.key()), usersByLogin, json);
}
if (JsonWriterUtils.isFieldWanted(FIELD_ATTRIBUTES, fields)) {
if (JsonWriterUtils.isFieldNeeded(FIELD_ATTRIBUTES, fields)) {
writeIssueAttributes(issue, json);
}
writeIssueExtraFields(issue, actionPlanByKeys, fields, json);
json.endObject();
}

private List<String> useDefaultFieldsIfNotSpecified(List<String> selectedFields) {
List<String> fields = Lists.newArrayList();
if (selectedFields == null || selectedFields.isEmpty()) {
fields.addAll(SELECTABLE_MINUS_EXTRAS);
} else {
fields.addAll(selectedFields);
}
return fields;
}

@CheckForNull
private static String isoDate(@Nullable Date date) {
if (date != null) {
Expand Down Expand Up @@ -211,15 +213,15 @@ private static void writeIssueAttributes(Issue issue, JsonWriter json) {

private void writeIssueExtraFields(Issue issue, Map<String, ActionPlan> actionPlanByKeys,
@Nullable List<String> fields, JsonWriter json) {
if (JsonWriterUtils.isFieldWanted(FIELD_ACTIONS, fields)) {
if (JsonWriterUtils.isFieldNeeded(FIELD_ACTIONS, fields)) {
actionsWriter.writeActions(issue, json);
}

if (JsonWriterUtils.isFieldWanted(FIELD_TRANSITIONS, fields)) {
if (JsonWriterUtils.isFieldNeeded(FIELD_TRANSITIONS, fields)) {
actionsWriter.writeTransitions(issue, json);
}

if (JsonWriterUtils.isFieldWanted(FIELD_ACTION_PLAN_NAME, fields)) {
if (JsonWriterUtils.isFieldNeeded(FIELD_ACTION_PLAN_NAME, fields)) {
writeActionPlanName(issue, actionPlanByKeys, json);
}
}
Expand Down
Expand Up @@ -29,7 +29,7 @@
import org.sonar.server.user.UserSession;
import org.sonar.server.user.index.UserDoc;

import static org.sonar.server.ws.JsonWriterUtils.isFieldWanted;
import static org.sonar.server.ws.JsonWriterUtils.isFieldNeeded;
import static org.sonar.server.ws.JsonWriterUtils.writeIfNeeded;

public class UserJsonWriter {
Expand Down Expand Up @@ -76,7 +76,7 @@ public void write(JsonWriter json, @Nullable User user) {
}

private void writeGroupsIfNeeded(JsonWriter json, Collection<String> groups, @Nullable Collection<String> fields) {
if (isFieldWanted(FIELD_GROUPS, fields) && userSession.hasGlobalPermission(GlobalPermissions.SYSTEM_ADMIN)) {
if (isFieldNeeded(FIELD_GROUPS, fields) && userSession.hasGlobalPermission(GlobalPermissions.SYSTEM_ADMIN)) {
json.name(FIELD_GROUPS).beginArray();
for (String groupName : groups) {
json.value(groupName);
Expand All @@ -86,7 +86,7 @@ private void writeGroupsIfNeeded(JsonWriter json, Collection<String> groups, @Nu
}

private static void writeScmAccountsIfNeeded(JsonWriter json, Collection<String> fields, User user) {
if (isFieldWanted(FIELD_SCM_ACCOUNTS, fields)) {
if (isFieldNeeded(FIELD_SCM_ACCOUNTS, fields)) {
json.name(FIELD_SCM_ACCOUNTS)
.beginArray()
.values(((UserDoc) user).scmAccounts())
Expand Down
Expand Up @@ -25,31 +25,35 @@

public class JsonWriterUtils {

private JsonWriterUtils() {
// Utility class
}

public static void writeIfNeeded(JsonWriter json, @Nullable String value, String field, Collection<String> fields) {
if (isFieldWanted(field, fields)) {
if (isFieldNeeded(field, fields)) {
json.prop(field, value);
}
}

public static void writeIfNeeded(JsonWriter json, @Nullable Boolean value, String field, Collection<String> fields) {
if (isFieldWanted(field, fields)) {
if (isFieldNeeded(field, fields)) {
json.prop(field, value);
}
}

public static void writeIfNeeded(JsonWriter json, @Nullable Integer value, String field, Collection<String> fields) {
if (isFieldWanted(field, fields)) {
if (isFieldNeeded(field, fields)) {
json.prop(field, value);
}
}

public static void writeIfNeeded(JsonWriter json, @Nullable Long value, String field, Collection<String> fields) {
if (isFieldWanted(field, fields)) {
if (isFieldNeeded(field, fields)) {
json.prop(field, value);
}
}

public static boolean isFieldWanted(String field, @Nullable Collection<String> fields) {
public static boolean isFieldNeeded(String field, @Nullable Collection<String> fields) {
return fields == null || fields.isEmpty() || fields.contains(field);
}
}
6 changes: 4 additions & 2 deletions server/sonar-web/src/main/js/apps/issues/controller.js
Expand Up @@ -5,7 +5,9 @@ define([
], function (Controller, ComponentViewer, HomeView) {

var $ = jQuery,
EXTRA_FIELDS = 'actions,transitions,actionPlanName',
FIELDS = 'component,componentId,project,subProject,rule,status,resolution,author,reporter,assignee,debt,line,' +
'message,severity,actionPlan,creationDate,updateDate,closeDate,tags,comments,attr,actions,transitions,' +
'actionPlanName',
FACET_DATA_FIELDS = ['components', 'projects', 'users', 'rules', 'actionPlans', 'languages'];

return Controller.extend({
Expand All @@ -21,7 +23,7 @@ define([
ps: this.pageSize,
s: 'FILE_LINE',
asc: true,
extra_fields: EXTRA_FIELDS,
f: FIELDS,
facets: this._facetsFromServer().join()
};
},
Expand Down
5 changes: 3 additions & 2 deletions server/sonar-web/src/main/js/components/source-viewer/main.js
Expand Up @@ -274,7 +274,9 @@ define([
options = {
data: {
componentUuids: this.model.id,
extra_fields: 'actions,transitions,actionPlanName',
f: 'component,componentId,project,subProject,rule,status,resolution,author,reporter,assignee,debt,' +
'line,message,severity,actionPlan,creationDate,updateDate,closeDate,tags,comments,attr,actions,' +
'transitions,actionPlanName',
resolved: false,
s: 'FILE_LINE',
asc: true,
Expand Down Expand Up @@ -712,4 +714,3 @@ define([
});

});

0 comments on commit 5a9eaac

Please sign in to comment.