mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 17:05:43 +01:00
use mapstruct for dto mapping and fix missing fields
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,6 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.api.v2.resources.PluginCenterDto;
|
||||
import sonia.scm.cache.Cache;
|
||||
import sonia.scm.cache.CacheManager;
|
||||
import sonia.scm.config.ScmConfiguration;
|
||||
@@ -79,7 +78,7 @@ import javax.xml.bind.JAXB;
|
||||
|
||||
import sonia.scm.net.ahc.AdvancedHttpClient;
|
||||
|
||||
import static sonia.scm.api.v2.resources.PluginCenterDtoMapper.*;
|
||||
import static sonia.scm.plugin.PluginCenterDtoMapper.*;
|
||||
|
||||
/**
|
||||
* TODO replace aether stuff.
|
||||
@@ -595,14 +594,8 @@ public class DefaultPluginManager implements PluginManager
|
||||
{
|
||||
synchronized (DefaultPluginManager.class)
|
||||
{
|
||||
String pluginUrl = configuration.getPluginUrl();
|
||||
|
||||
pluginUrl = buildPluginUrl(pluginUrl);
|
||||
|
||||
if (logger.isInfoEnabled())
|
||||
{
|
||||
logger.info("fetch plugin informations from {}", pluginUrl);
|
||||
}
|
||||
String pluginUrl = buildPluginUrl(configuration.getPluginUrl());
|
||||
logger.info("fetch plugin information from {}", pluginUrl);
|
||||
|
||||
if (REMOTE_PLUGINS_ENABLED && Util.isNotEmpty(pluginUrl))
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -45,10 +45,10 @@ public final class PluginCenterDto implements Serializable {
|
||||
public static class Plugin {
|
||||
|
||||
private String name;
|
||||
private String version;
|
||||
private String displayName;
|
||||
private String description;
|
||||
private String category;
|
||||
private String version;
|
||||
private String author;
|
||||
private String avatarUrl;
|
||||
private String sha256;
|
||||
@@ -86,6 +86,5 @@ public final class PluginCenterDto implements Serializable {
|
||||
@Getter
|
||||
static class Link {
|
||||
private String href;
|
||||
private boolean templated;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Mapper
|
||||
public interface PluginCenterDtoMapper {
|
||||
|
||||
@Mapping(source = "conditions", target = "condition")
|
||||
PluginInformation map(PluginCenterDto.Plugin plugin);
|
||||
|
||||
PluginCondition map(PluginCenterDto.Condition condition);
|
||||
|
||||
static Set<PluginInformation> map(List<PluginCenterDto.Plugin> dtos) {
|
||||
PluginCenterDtoMapper mapper = Mappers.getMapper(PluginCenterDtoMapper.class);
|
||||
Set<PluginInformation> plugins = new HashSet<>();
|
||||
for (PluginCenterDto.Plugin plugin : dtos) {
|
||||
plugins.add(mapper.map(plugin));
|
||||
}
|
||||
return plugins;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user