use mapstruct for dto mapping and fix missing fields

This commit is contained in:
Sebastian Sdorra
2019-08-15 17:01:15 +02:00
parent 924efc6187
commit 55e4568ee5
20 changed files with 181 additions and 136 deletions

View File

@@ -54,5 +54,7 @@ public class MapperModule extends AbstractModule {
bind(UIPluginDtoCollectionMapper.class);
bind(ScmPathInfoStore.class).in(ServletScopes.REQUEST);
bind(PluginDtoMapper.class).to(Mappers.getMapper(PluginDtoMapper.class).getClass());
}
}

View File

@@ -1,91 +0,0 @@
package sonia.scm.api.v2.resources;
import com.google.common.collect.ImmutableList;
import lombok.AllArgsConstructor;
import lombok.Getter;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public final class PluginCenterDto implements Serializable {
@XmlElement(name = "_embedded")
private Embedded embedded;
public Embedded getEmbedded() {
return embedded;
}
@XmlRootElement(name = "_embedded")
@XmlAccessorType(XmlAccessType.FIELD)
public static class Embedded {
@XmlElement(name = "plugins")
private List<Plugin> plugins;
public List<Plugin> getPlugins() {
if (plugins == null) {
plugins = ImmutableList.of();
}
return plugins;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "plugins")
@Getter
@AllArgsConstructor
public static class Plugin {
private String name;
private String displayName;
private String description;
private String category;
private String version;
private String author;
private String avatarUrl;
private String sha256;
@XmlElement(name = "conditions")
private Condition conditions;
@XmlElement(name = "dependecies")
private Dependency dependencies;
@XmlElement(name = "_links")
private Map<String, Link> links;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "conditions")
@Getter
@AllArgsConstructor
public static class Condition {
private List<String> os;
private String arch;
private String minVersion;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "dependencies")
@Getter
@AllArgsConstructor
static class Dependency {
private String name;
}
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
static class Link {
private String href;
private boolean templated;
}
}

View File

@@ -1,33 +0,0 @@
package sonia.scm.api.v2.resources;
import sonia.scm.plugin.PluginCondition;
import sonia.scm.plugin.PluginInformation;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class PluginCenterDtoMapper {
public static Set<PluginInformation> map(List<PluginCenterDto.Plugin> plugins) {
HashSet<PluginInformation> pluginInformationSet = new HashSet<>();
for (PluginCenterDto.Plugin plugin : plugins) {
PluginInformation pluginInformation = new PluginInformation();
pluginInformation.setName(plugin.getName());
pluginInformation.setAuthor(plugin.getAuthor());
pluginInformation.setCategory(plugin.getCategory());
pluginInformation.setVersion(plugin.getVersion());
pluginInformation.setDescription(plugin.getDescription());
if (plugin.getConditions() != null) {
PluginCenterDto.Condition condition = plugin.getConditions();
pluginInformation.setCondition(new PluginCondition(condition.getMinVersion(), condition.getOs(), condition.getArch()));
}
pluginInformationSet.add(pluginInformation);
}
return pluginInformationSet;
}
}

View File

@@ -12,11 +12,12 @@ import lombok.Setter;
public class PluginDto extends HalRepresentation {
private String name;
private String category;
private String version;
private String author;
private String avatarUrl;
private String displayName;
private String description;
private String author;
private String category;
private String avatarUrl;
public PluginDto(Links links) {
add(links);

View File

@@ -1,6 +1,10 @@
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.Links;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.ObjectFactory;
import sonia.scm.plugin.PluginInformation;
import sonia.scm.plugin.PluginState;
import sonia.scm.plugin.PluginWrapper;
@@ -10,20 +14,27 @@ import javax.inject.Inject;
import static de.otto.edison.hal.Link.link;
import static de.otto.edison.hal.Links.linkingTo;
public class PluginDtoMapper {
private final ResourceLinks resourceLinks;
@Mapper
public abstract class PluginDtoMapper {
@Inject
public PluginDtoMapper(ResourceLinks resourceLinks) {
this.resourceLinks = resourceLinks;
}
private ResourceLinks resourceLinks;
public PluginDto map(PluginWrapper plugin) {
return map(plugin.getPlugin().getInformation());
}
public PluginDto map(PluginInformation pluginInformation) {
public abstract PluginDto map(PluginInformation plugin);
@AfterMapping
protected void appendCategory(@MappingTarget PluginDto dto) {
if (dto.getCategory() == null) {
dto.setCategory("Miscellaneous");
}
}
@ObjectFactory
public PluginDto createDto(PluginInformation pluginInformation) {
Links.Builder linksBuilder;
if (pluginInformation.getState() != null && pluginInformation.getState().equals(PluginState.AVAILABLE)) {
linksBuilder = linkingTo()
@@ -38,14 +49,6 @@ public class PluginDtoMapper {
.self(pluginInformation.getName()));
}
PluginDto pluginDto = new PluginDto(linksBuilder.build());
pluginDto.setName(pluginInformation.getName());
pluginDto.setCategory(pluginInformation.getCategory() != null ? pluginInformation.getCategory() : "Miscellaneous");
pluginDto.setVersion(pluginInformation.getVersion());
pluginDto.setAuthor(pluginInformation.getAuthor());
pluginDto.setDescription(pluginInformation.getDescription());
pluginDto.setAvatarUrl(pluginInformation.getAvatarUrl());
return pluginDto;
return new PluginDto(linksBuilder.build());
}
}