Skip to content

Commit

Permalink
SONAR-6582 Extract issue serialization class
Browse files Browse the repository at this point in the history
Use common issue JSON representation for most actions on issues.
  • Loading branch information
jblievremont committed Jun 18, 2015
1 parent e183ec7 commit 5df7990
Show file tree
Hide file tree
Showing 9 changed files with 448 additions and 225 deletions.
Expand Up @@ -19,19 +19,30 @@
*/ */
package org.sonar.server.issue; package org.sonar.server.issue;


import org.sonar.server.issue.ws.IssueComponentHelper;
import org.sonar.server.issue.ws.IssueJsonWriter;

import org.elasticsearch.common.collect.Lists;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.io.StringWriter;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.sonar.api.issue.ActionPlan; import org.sonar.api.issue.ActionPlan;
import org.sonar.api.issue.Issue; import org.sonar.api.issue.Issue;
Expand All @@ -42,26 +53,33 @@
import org.sonar.api.issue.internal.FieldDiffs; import org.sonar.api.issue.internal.FieldDiffs;
import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.ServerSide; import org.sonar.api.server.ServerSide;
import org.sonar.api.user.User;
import org.sonar.api.utils.SonarException; import org.sonar.api.utils.SonarException;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.api.web.UserRole; import org.sonar.api.web.UserRole;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.issue.ActionPlanStats; import org.sonar.core.issue.ActionPlanStats;
import org.sonar.core.issue.DefaultActionPlan; import org.sonar.core.issue.DefaultActionPlan;
import org.sonar.core.issue.db.IssueFilterDto; import org.sonar.core.issue.db.IssueFilterDto;
import org.sonar.core.issue.workflow.Transition; import org.sonar.core.issue.workflow.Transition;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.resource.ResourceDao; import org.sonar.core.resource.ResourceDao;
import org.sonar.core.resource.ResourceDto; import org.sonar.core.resource.ResourceDto;
import org.sonar.core.resource.ResourceQuery; import org.sonar.core.resource.ResourceQuery;
import org.sonar.server.db.DbClient;
import org.sonar.server.es.SearchOptions; import org.sonar.server.es.SearchOptions;
import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.issue.actionplan.ActionPlanService; import org.sonar.server.issue.actionplan.ActionPlanService;
import org.sonar.server.issue.filter.IssueFilterParameters; import org.sonar.server.issue.filter.IssueFilterParameters;
import org.sonar.server.issue.filter.IssueFilterService; import org.sonar.server.issue.filter.IssueFilterService;
import org.sonar.server.search.QueryContext; import org.sonar.server.search.QueryContext;
import org.sonar.server.user.UserSession; import org.sonar.server.user.UserSession;
import org.sonar.server.user.index.UserIndex;
import org.sonar.server.util.RubyUtils; import org.sonar.server.util.RubyUtils;
import org.sonar.server.util.Validation; import org.sonar.server.util.Validation;

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


/** /**
* Used through ruby code <pre>Internal.issues</pre> * Used through ruby code <pre>Internal.issues</pre>
Expand Down Expand Up @@ -90,6 +108,10 @@ public class InternalRubyIssueService {
private final ActionService actionService; private final ActionService actionService;
private final IssueFilterService issueFilterService; private final IssueFilterService issueFilterService;
private final IssueBulkChangeService issueBulkChangeService; private final IssueBulkChangeService issueBulkChangeService;
private final IssueJsonWriter issueWriter;
private final IssueComponentHelper issueComponentHelper;
private final UserIndex userIndex;
private final DbClient dbClient;
private final UserSession userSession; private final UserSession userSession;


public InternalRubyIssueService( public InternalRubyIssueService(
Expand All @@ -99,6 +121,7 @@ public InternalRubyIssueService(
IssueChangelogService changelogService, ActionPlanService actionPlanService, IssueChangelogService changelogService, ActionPlanService actionPlanService,
ResourceDao resourceDao, ActionService actionService, ResourceDao resourceDao, ActionService actionService,
IssueFilterService issueFilterService, IssueBulkChangeService issueBulkChangeService, IssueFilterService issueFilterService, IssueBulkChangeService issueBulkChangeService,
IssueJsonWriter issueWriter, IssueComponentHelper issueComponentHelper, UserIndex userIndex, DbClient dbClient,
UserSession userSession) { UserSession userSession) {
this.issueService = issueService; this.issueService = issueService;
this.issueQueryService = issueQueryService; this.issueQueryService = issueQueryService;
Expand All @@ -109,6 +132,10 @@ public InternalRubyIssueService(
this.actionService = actionService; this.actionService = actionService;
this.issueFilterService = issueFilterService; this.issueFilterService = issueFilterService;
this.issueBulkChangeService = issueBulkChangeService; this.issueBulkChangeService = issueBulkChangeService;
this.issueWriter = issueWriter;
this.issueComponentHelper = issueComponentHelper;
this.userIndex = userIndex;
this.dbClient = dbClient;
this.userSession = userSession; this.userSession = userSession;
} }


Expand Down Expand Up @@ -667,4 +694,67 @@ public Map<String, Long> listTagsForComponent(String componentUuid, int pageSize
public boolean isUserIssueAdmin(String projectUuid) { public boolean isUserIssueAdmin(String projectUuid) {
return userSession.hasProjectPermissionByUuid(UserRole.ISSUE_ADMIN, projectUuid); return userSession.hasProjectPermissionByUuid(UserRole.ISSUE_ADMIN, projectUuid);
} }

/**
* Used by issue modification actions currently implemented in Rails
* @param issue
* @return the JSON representation of the modified issue, as a ready to use string
*/
public String writeIssueJson(Issue issue) {
StringWriter writer = new StringWriter();
JsonWriter json = JsonWriter.of(writer);
DbSession dbSession = dbClient.openSession(false);
try {
Map<String, User> usersByLogin = getIssueUsersByLogin(issue);

Set<String> componentUuids = ImmutableSet.of(issue.componentUuid());
Set<String> projectUuids = Sets.newHashSet();
Set<ComponentDto> componentDtos = Sets.newHashSet();
List<ComponentDto> projectDtos = Lists.newArrayList();

Map<String, ComponentDto> componentsByUuid = Maps.newHashMap();
Map<String, ComponentDto> projectsByComponentUuid = Maps.newHashMap();

List<ComponentDto> fileDtos = dbClient.componentDao().selectByUuids(dbSession, componentUuids);
List<ComponentDto> subProjectDtos = dbClient.componentDao().selectSubProjectsByComponentUuids(dbSession, componentUuids);
componentDtos.addAll(fileDtos);
componentDtos.addAll(subProjectDtos);
for (ComponentDto component : componentDtos) {
projectUuids.add(component.projectUuid());
}
projectDtos.addAll(dbClient.componentDao().selectByUuids(dbSession, projectUuids));
componentDtos.addAll(projectDtos);

for (ComponentDto componentDto : componentDtos) {
componentsByUuid.put(componentDto.uuid(), componentDto);
}

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

json.beginObject().name("issue");
issueWriter.write(json, issue,
usersByLogin,
componentsByUuid,
projectsByComponentUuid,
ImmutableMultimap.<String, DefaultIssueComment>of(),
ImmutableMap.<String, ActionPlan>of(),
ImmutableList.of(IssueJsonWriter.ACTIONS_EXTRA_FIELD, IssueJsonWriter.TRANSITIONS_EXTRA_FIELD));
json.endObject().close();
} finally {
MyBatis.closeQuietly(dbSession);
IOUtils.closeQuietly(writer);
}
return writer.toString();
}

private Map<String, User> getIssueUsersByLogin(Issue issue) {
Map<String, User> usersByLogin = Maps.newHashMap();
if (issue.assignee() != null) {
usersByLogin.put(issue.assignee(), userIndex.getByLogin(issue.assignee()));
}
if (issue.reporter() != null) {
usersByLogin.put(issue.reporter(), userIndex.getByLogin(issue.reporter()));
}
return usersByLogin;
}
} }
@@ -0,0 +1,96 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.issue.ws;

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

import org.sonar.server.db.DbClient;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.persistence.DbSession;

/**
* This class computes some collections of {@link ComponentDto}s used to serialize issues.
*/
public class IssueComponentHelper {

private final DbClient dbClient;

public IssueComponentHelper(DbClient dbClient) {
this.dbClient = dbClient;
}

public Map<String, ComponentDto> prepareComponentsAndProjects(Set<String> projectUuids, Set<String> componentUuids, Map<String, ComponentDto> componentsByUuid,
Collection<ComponentDto> componentDtos, List<ComponentDto> projectDtos, DbSession session) {
Map<String, ComponentDto> projectsByComponentUuid;
List<ComponentDto> fileDtos = dbClient.componentDao().selectByUuids(session, componentUuids);
List<ComponentDto> subProjectDtos = dbClient.componentDao().selectSubProjectsByComponentUuids(session, componentUuids);
componentDtos.addAll(fileDtos);
componentDtos.addAll(subProjectDtos);
for (ComponentDto component : componentDtos) {
projectUuids.add(component.projectUuid());
}
projectDtos.addAll(dbClient.componentDao().selectByUuids(session, projectUuids));
componentDtos.addAll(projectDtos);

for (ComponentDto componentDto : componentDtos) {
componentsByUuid.put(componentDto.uuid(), componentDto);
}

projectsByComponentUuid = getProjectsByComponentUuid(componentDtos, projectDtos);
return projectsByComponentUuid;
}

private Map<String, ComponentDto> getProjectsByComponentUuid(Collection<ComponentDto> components, Collection<ComponentDto> projects) {
Map<String, ComponentDto> projectsByUuid = buildProjectsByUuid(projects);
return buildProjectsByComponentUuid(components, projectsByUuid);
}

private static Map<String, ComponentDto> buildProjectsByUuid(Collection<ComponentDto> projects) {
Map<String, ComponentDto> projectsByUuid = newHashMap();
for (ComponentDto project : projects) {
if (project == null) {
throw new IllegalStateException("Found a null project in issues");
}
if (project.uuid() == null) {
throw new IllegalStateException("Project has no UUID: " + project.getKey());
}
projectsByUuid.put(project.uuid(), project);
}
return projectsByUuid;
}

private static Map<String, ComponentDto> buildProjectsByComponentUuid(Collection<ComponentDto> components, Map<String, ComponentDto> projectsByUuid) {
Map<String, ComponentDto> projectsByComponentUuid = newHashMap();
for (ComponentDto component : components) {
if (component.uuid() == null) {
throw new IllegalStateException("Component has no UUID: " + component.getKey());
}
if (!projectsByUuid.containsKey(component.projectUuid())) {
throw new IllegalStateException("Project cannot be found for component: " + component.getKey() + " / " + component.uuid());
}
projectsByComponentUuid.put(component.uuid(), projectsByUuid.get(component.projectUuid()));
}
return projectsByComponentUuid;
}
}

0 comments on commit 5df7990

Please sign in to comment.