Commit 1d3cecb9 by Jason Song

Merge pull request #48 from yiming187/converter

Refactor DTOConverter out of entity class
parents 8563b8b9 9d19d637
package com.ctrip.apollo.biz.converter;
import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
public class ConverterUtils {
static String[] getNullPropertyNames(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<String>();
for (java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) emptyNames.add(pd.getName());
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
}
package com.ctrip.apollo.biz.converter;
import org.springframework.beans.BeanUtils;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.ConfigItem;
import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
public class DTOToEntityConverter {
public static Cluster convert(ClusterDTO source) {
Cluster target = new Cluster();
BeanUtils.copyProperties(source, target, ConverterUtils.getNullPropertyNames(source));
return target;
}
public static ConfigItem convert(ConfigItemDTO source) {
ConfigItem target = new ConfigItem();
BeanUtils.copyProperties(source, target, ConverterUtils.getNullPropertyNames(source));
return target;
}
public static ReleaseSnapshot convert(ReleaseSnapshotDTO source) {
ReleaseSnapshot target = new ReleaseSnapshot();
BeanUtils.copyProperties(source, target, ConverterUtils.getNullPropertyNames(source));
return target;
}
public static Version convert(VersionDTO source) {
Version target = new Version();
BeanUtils.copyProperties(source, target, ConverterUtils.getNullPropertyNames(source));
return target;
}
}
package com.ctrip.apollo.biz.converter;
import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.ConfigItem;
import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
public class EntityToDTOConverter {
public static ClusterDTO convert(Cluster source) {
ClusterDTO target = new ClusterDTO();
BeanUtils.copyProperties(source, target, ConverterUtils.getNullPropertyNames(source));
return target;
}
public static ConfigItemDTO convert(ConfigItem source) {
ConfigItemDTO target = new ConfigItemDTO();
BeanUtils.copyProperties(source, target, ConverterUtils.getNullPropertyNames(source));
return target;
}
public static ReleaseSnapshotDTO convert(ReleaseSnapshot source) {
ReleaseSnapshotDTO target = new ReleaseSnapshotDTO();
BeanUtils.copyProperties(source, target, ConverterUtils.getNullPropertyNames(source));
return target;
}
public static VersionDTO convert(Version source) {
VersionDTO target = new VersionDTO();
BeanUtils.copyProperties(source, target, ConverterUtils.getNullPropertyNames(source));
return target;
}
}
package com.ctrip.apollo.biz.entity; package com.ctrip.apollo.biz.entity;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where; import org.hibernate.annotations.Where;
...@@ -150,19 +148,4 @@ public class ConfigItem { ...@@ -150,19 +148,4 @@ public class ConfigItem {
this.dataChangeLastModifiedTime = dataChangeLastModifiedTime; this.dataChangeLastModifiedTime = dataChangeLastModifiedTime;
} }
public ConfigItemDTO toDTO() {
ConfigItemDTO dto = new ConfigItemDTO();
dto.setAppId(appId);
dto.setId(id);
dto.setClusterId(clusterId);
dto.setClusterName(clusterName);
dto.setDataChangeCreatedBy(dataChangeCreatedBy);
dto.setDataChangeLastModifiedBy(dataChangeLastModifiedBy);
dto.setDataChangeCreatedTime(dataChangeCreatedTime);
dto.setDataChangeLastModifiedTime(dataChangeLastModifiedTime);
dto.setKey(key);
dto.setValue(value);
dto.setComment(comment);
return dto;
}
} }
package com.ctrip.apollo.biz.entity; package com.ctrip.apollo.biz.entity;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where; import org.hibernate.annotations.Where;
...@@ -16,7 +14,7 @@ import javax.persistence.Id; ...@@ -16,7 +14,7 @@ import javax.persistence.Id;
@Entity @Entity
@Where(clause = "isDeleted = 0") @Where(clause = "isDeleted = 0")
@SQLDelete(sql = "Update ReleaseSnapShot set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update ReleaseSnapShot set isDeleted = 1 where id = ?")
public class ReleaseSnapShot { public class ReleaseSnapshot {
@Id @Id
@GeneratedValue @GeneratedValue
private long id; private long id;
...@@ -32,7 +30,7 @@ public class ReleaseSnapShot { ...@@ -32,7 +30,7 @@ public class ReleaseSnapShot {
private boolean isDeleted; private boolean isDeleted;
public ReleaseSnapShot() { public ReleaseSnapshot() {
} }
public long getId() { public long getId() {
...@@ -75,12 +73,4 @@ public class ReleaseSnapShot { ...@@ -75,12 +73,4 @@ public class ReleaseSnapShot {
isDeleted = deleted; isDeleted = deleted;
} }
public ReleaseSnapshotDTO toDTO() {
ReleaseSnapshotDTO dto = new ReleaseSnapshotDTO();
dto.setId(id);
dto.setClusterName(clusterName);
dto.setConfigurations(configurations);
dto.setReleaseId(releaseId);
return dto;
}
} }
package com.ctrip.apollo.biz.entity; package com.ctrip.apollo.biz.entity;
import com.ctrip.apollo.core.dto.VersionDTO;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where; import org.hibernate.annotations.Where;
...@@ -84,13 +82,4 @@ public class Version { ...@@ -84,13 +82,4 @@ public class Version {
this.parentVersion = parentVersion; this.parentVersion = parentVersion;
} }
public VersionDTO toDTO() {
VersionDTO dto = new VersionDTO();
dto.setAppId(this.appId);
dto.setId(this.id);
dto.setName(this.name);
dto.setParentVersion(this.parentVersion);
dto.setReleaseId(this.releaseId);
return dto;
}
} }
package com.ctrip.apollo.biz.repository; package com.ctrip.apollo.biz.repository;
import com.ctrip.apollo.biz.entity.ReleaseSnapShot; import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
...@@ -10,8 +10,8 @@ import java.util.List; ...@@ -10,8 +10,8 @@ import java.util.List;
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public interface ReleaseSnapShotRepository public interface ReleaseSnapShotRepository
extends PagingAndSortingRepository<ReleaseSnapShot, Long> { extends PagingAndSortingRepository<ReleaseSnapshot, Long> {
ReleaseSnapShot findByReleaseIdAndClusterName(long releaseId, String clusterName); ReleaseSnapshot findByReleaseIdAndClusterName(long releaseId, String clusterName);
List<ReleaseSnapShot> findByReleaseId(long releaseId); List<ReleaseSnapshot> findByReleaseId(long releaseId);
} }
package com.ctrip.apollo.biz.service.impl; package com.ctrip.apollo.biz.service.impl;
import com.ctrip.apollo.biz.converter.EntityToDTOConverter;
import com.ctrip.apollo.biz.entity.Cluster; import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.ConfigItem; import com.ctrip.apollo.biz.entity.ConfigItem;
import com.ctrip.apollo.biz.entity.ReleaseSnapShot; import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Version; import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ClusterRepository; import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.ConfigItemRepository; import com.ctrip.apollo.biz.repository.ConfigItemRepository;
...@@ -39,15 +40,16 @@ public class AdminConfigServiceImpl implements AdminConfigService { ...@@ -39,15 +40,16 @@ public class AdminConfigServiceImpl implements AdminConfigService {
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
} }
List<ReleaseSnapShot> releaseSnapShots = releaseSnapShotRepository.findByReleaseId(releaseId); List<ReleaseSnapshot> releaseSnapShots = releaseSnapShotRepository.findByReleaseId(releaseId);
if (releaseSnapShots == null || releaseSnapShots.size() == 0) { if (releaseSnapShots == null || releaseSnapShots.size() == 0) {
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
} }
List<ReleaseSnapshotDTO> result = new ArrayList<>(releaseSnapShots.size()); List<ReleaseSnapshotDTO> result = new ArrayList<>(releaseSnapShots.size());
for (ReleaseSnapShot releaseSnapShot : releaseSnapShots) { for (ReleaseSnapshot releaseSnapShot : releaseSnapShots) {
result.add(releaseSnapShot.toDTO()); ReleaseSnapshotDTO dto = EntityToDTOConverter.convert(releaseSnapShot);
result.add(dto);
} }
return result; return result;
} }
...@@ -66,7 +68,8 @@ public class AdminConfigServiceImpl implements AdminConfigService { ...@@ -66,7 +68,8 @@ public class AdminConfigServiceImpl implements AdminConfigService {
List<VersionDTO> result = new ArrayList<>(versions.size()); List<VersionDTO> result = new ArrayList<>(versions.size());
for (Version version : versions) { for (Version version : versions) {
result.add(version.toDTO()); VersionDTO dto = EntityToDTOConverter.convert(version);
result.add(dto);
} }
return result; return result;
} }
...@@ -80,7 +83,8 @@ public class AdminConfigServiceImpl implements AdminConfigService { ...@@ -80,7 +83,8 @@ public class AdminConfigServiceImpl implements AdminConfigService {
if (version == null){ if (version == null){
return null; return null;
} }
return version.toDTO(); VersionDTO dto = EntityToDTOConverter.convert(version);
return dto;
} }
@Override @Override
...@@ -112,7 +116,8 @@ public class AdminConfigServiceImpl implements AdminConfigService { ...@@ -112,7 +116,8 @@ public class AdminConfigServiceImpl implements AdminConfigService {
List<ConfigItemDTO> result = new ArrayList<>(configItems.size()); List<ConfigItemDTO> result = new ArrayList<>(configItems.size());
for (ConfigItem configItem : configItems) { for (ConfigItem configItem : configItems) {
result.add(configItem.toDTO()); ConfigItemDTO dto = EntityToDTOConverter.convert(configItem);
result.add(dto);
} }
return result; return result;
} }
......
...@@ -2,7 +2,7 @@ package com.ctrip.apollo.biz.service.impl; ...@@ -2,7 +2,7 @@ package com.ctrip.apollo.biz.service.impl;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.ctrip.apollo.biz.entity.ReleaseSnapShot; import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Version; import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository; import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository; import com.ctrip.apollo.biz.repository.VersionRepository;
...@@ -49,7 +49,7 @@ public class ConfigServiceImpl implements ConfigService { ...@@ -49,7 +49,7 @@ public class ConfigServiceImpl implements ConfigService {
@Override @Override
public ApolloConfig loadConfigByVersionAndClusterName(Version version, String clusterName) { public ApolloConfig loadConfigByVersionAndClusterName(Version version, String clusterName) {
ReleaseSnapShot releaseSnapShot = ReleaseSnapshot releaseSnapShot =
releaseSnapShotRepository releaseSnapShotRepository
.findByReleaseIdAndClusterName(version.getReleaseId(), clusterName); .findByReleaseIdAndClusterName(version.getReleaseId(), clusterName);
if (releaseSnapShot == null) { if (releaseSnapShot == null) {
...@@ -59,7 +59,7 @@ public class ConfigServiceImpl implements ConfigService { ...@@ -59,7 +59,7 @@ public class ConfigServiceImpl implements ConfigService {
return assembleConfig(version, releaseSnapShot); return assembleConfig(version, releaseSnapShot);
} }
private ApolloConfig assembleConfig(Version version, ReleaseSnapShot releaseSnapShot) { private ApolloConfig assembleConfig(Version version, ReleaseSnapshot releaseSnapShot) {
ApolloConfig config = ApolloConfig config =
new ApolloConfig(version.getAppId(), releaseSnapShot.getClusterName(), version.getName(), new ApolloConfig(version.getAppId(), releaseSnapShot.getClusterName(), version.getName(),
version.getReleaseId()); version.getReleaseId());
......
...@@ -2,7 +2,7 @@ package com.ctrip.apollo.biz.service.impl; ...@@ -2,7 +2,7 @@ package com.ctrip.apollo.biz.service.impl;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.ctrip.apollo.biz.entity.ReleaseSnapShot; import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Version; import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository; import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository; import com.ctrip.apollo.biz.repository.VersionRepository;
...@@ -60,7 +60,7 @@ public class ConfigServiceImplTest { ...@@ -60,7 +60,7 @@ public class ConfigServiceImplTest {
String someValidConfiguration = "{\"apollo.bar\": \"foo\"}"; String someValidConfiguration = "{\"apollo.bar\": \"foo\"}";
Version someVersion = assembleVersion(someAppId, someVersionName, someReleaseId); Version someVersion = assembleVersion(someAppId, someVersionName, someReleaseId);
ReleaseSnapShot ReleaseSnapshot
someReleaseSnapShot = someReleaseSnapShot =
assembleReleaseSnapShot(someReleaseId, someClusterName, someValidConfiguration); assembleReleaseSnapShot(someReleaseId, someClusterName, someValidConfiguration);
Map<String, Object> someMap = Maps.newHashMap(); Map<String, Object> someMap = Maps.newHashMap();
...@@ -122,9 +122,9 @@ public class ConfigServiceImplTest { ...@@ -122,9 +122,9 @@ public class ConfigServiceImplTest {
return version; return version;
} }
private ReleaseSnapShot assembleReleaseSnapShot(long releaseId, String clusterName, private ReleaseSnapshot assembleReleaseSnapShot(long releaseId, String clusterName,
String configurations) { String configurations) {
ReleaseSnapShot releaseSnapShot = new ReleaseSnapShot(); ReleaseSnapshot releaseSnapShot = new ReleaseSnapshot();
releaseSnapShot.setReleaseId(releaseId); releaseSnapShot.setReleaseId(releaseId);
releaseSnapShot.setClusterName(clusterName); releaseSnapShot.setClusterName(clusterName);
releaseSnapShot.setConfigurations(configurations); releaseSnapShot.setConfigurations(configurations);
......
...@@ -40,10 +40,10 @@ ...@@ -40,10 +40,10 @@
<url>https://travis-ci.org/ctripcorp/apollo</url> <url>https://travis-ci.org/ctripcorp/apollo</url>
</ciManagement> </ciManagement>
<issueManagement> <issueManagement>
<system>github</system> <system>github</system>
<url>https://github.com/ctripcorp/apollo/issues</url> <url>https://github.com/ctripcorp/apollo/issues</url>
</issueManagement> </issueManagement>
<developers> <developers>
<developer> <developer>
...@@ -77,14 +77,14 @@ ...@@ -77,14 +77,14 @@
</roles> </roles>
</developer> </developer>
</developers> </developers>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<github.global.server>github</github.global.server> <github.global.server>github</github.global.server>
<github.global.oauth2Token>${env.GITHUB_OAUTH_TOKEN}</github.global.oauth2Token> <github.global.oauth2Token>${env.GITHUB_OAUTH_TOKEN}</github.global.oauth2Token>
</properties> </properties>
<modules> <modules>
<module>apollo-buildtools</module> <module>apollo-buildtools</module>
<module>apollo-core</module> <module>apollo-core</module>
...@@ -95,7 +95,7 @@ ...@@ -95,7 +95,7 @@
<module>apollo-portal</module> <module>apollo-portal</module>
<module>apollo-demo</module> <module>apollo-demo</module>
</modules> </modules>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<dependency> <dependency>
...@@ -154,7 +154,7 @@ ...@@ -154,7 +154,7 @@
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
...@@ -162,7 +162,7 @@ ...@@ -162,7 +162,7 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<pluginManagement> <pluginManagement>
<plugins> <plugins>
...@@ -218,22 +218,6 @@ ...@@ -218,22 +218,6 @@
</pluginManagement> </pluginManagement>
<plugins> <plugins>
<plugin> <plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>0.12</version>
<configuration>
<message>Building site for ${project.version}</message>
</configuration>
<executions>
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>site</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId> <artifactId>maven-checkstyle-plugin</artifactId>
</plugin> </plugin>
...@@ -243,7 +227,7 @@ ...@@ -243,7 +227,7 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<profiles> <profiles>
<profile> <profile>
<id>travis</id> <id>travis</id>
...@@ -256,6 +240,22 @@ ...@@ -256,6 +240,22 @@
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>0.12</version>
<configuration>
<message>Building site for ${project.version}</message>
</configuration>
<executions>
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>site</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId> <groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId> <artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version> <version>2.7</version>
...@@ -285,7 +285,7 @@ ...@@ -285,7 +285,7 @@
</snapshots> </snapshots>
</repository> </repository>
</repositories> </repositories>
<reporting> <reporting>
<plugins> <plugins>
<plugin> <plugin>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment