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;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
......@@ -150,19 +148,4 @@ public class ConfigItem {
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;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
......@@ -16,7 +14,7 @@ import javax.persistence.Id;
@Entity
@Where(clause = "isDeleted = 0")
@SQLDelete(sql = "Update ReleaseSnapShot set isDeleted = 1 where id = ?")
public class ReleaseSnapShot {
public class ReleaseSnapshot {
@Id
@GeneratedValue
private long id;
......@@ -32,7 +30,7 @@ public class ReleaseSnapShot {
private boolean isDeleted;
public ReleaseSnapShot() {
public ReleaseSnapshot() {
}
public long getId() {
......@@ -75,12 +73,4 @@ public class ReleaseSnapShot {
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;
import com.ctrip.apollo.core.dto.VersionDTO;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
......@@ -84,13 +82,4 @@ public class Version {
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;
import com.ctrip.apollo.biz.entity.ReleaseSnapShot;
import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import org.springframework.data.repository.PagingAndSortingRepository;
......@@ -10,8 +10,8 @@ import java.util.List;
* @author Jason Song(song_s@ctrip.com)
*/
public interface ReleaseSnapShotRepository
extends PagingAndSortingRepository<ReleaseSnapShot, Long> {
ReleaseSnapShot findByReleaseIdAndClusterName(long releaseId, String clusterName);
extends PagingAndSortingRepository<ReleaseSnapshot, Long> {
ReleaseSnapshot findByReleaseIdAndClusterName(long releaseId, String clusterName);
List<ReleaseSnapShot> findByReleaseId(long releaseId);
List<ReleaseSnapshot> findByReleaseId(long releaseId);
}
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.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.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.ConfigItemRepository;
......@@ -39,15 +40,16 @@ public class AdminConfigServiceImpl implements AdminConfigService {
return Collections.EMPTY_LIST;
}
List<ReleaseSnapShot> releaseSnapShots = releaseSnapShotRepository.findByReleaseId(releaseId);
List<ReleaseSnapshot> releaseSnapShots = releaseSnapShotRepository.findByReleaseId(releaseId);
if (releaseSnapShots == null || releaseSnapShots.size() == 0) {
return Collections.EMPTY_LIST;
}
List<ReleaseSnapshotDTO> result = new ArrayList<>(releaseSnapShots.size());
for (ReleaseSnapShot releaseSnapShot : releaseSnapShots) {
result.add(releaseSnapShot.toDTO());
for (ReleaseSnapshot releaseSnapShot : releaseSnapShots) {
ReleaseSnapshotDTO dto = EntityToDTOConverter.convert(releaseSnapShot);
result.add(dto);
}
return result;
}
......@@ -66,7 +68,8 @@ public class AdminConfigServiceImpl implements AdminConfigService {
List<VersionDTO> result = new ArrayList<>(versions.size());
for (Version version : versions) {
result.add(version.toDTO());
VersionDTO dto = EntityToDTOConverter.convert(version);
result.add(dto);
}
return result;
}
......@@ -80,7 +83,8 @@ public class AdminConfigServiceImpl implements AdminConfigService {
if (version == null){
return null;
}
return version.toDTO();
VersionDTO dto = EntityToDTOConverter.convert(version);
return dto;
}
@Override
......@@ -112,7 +116,8 @@ public class AdminConfigServiceImpl implements AdminConfigService {
List<ConfigItemDTO> result = new ArrayList<>(configItems.size());
for (ConfigItem configItem : configItems) {
result.add(configItem.toDTO());
ConfigItemDTO dto = EntityToDTOConverter.convert(configItem);
result.add(dto);
}
return result;
}
......
......@@ -2,7 +2,7 @@ package com.ctrip.apollo.biz.service.impl;
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.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
......@@ -49,7 +49,7 @@ public class ConfigServiceImpl implements ConfigService {
@Override
public ApolloConfig loadConfigByVersionAndClusterName(Version version, String clusterName) {
ReleaseSnapShot releaseSnapShot =
ReleaseSnapshot releaseSnapShot =
releaseSnapShotRepository
.findByReleaseIdAndClusterName(version.getReleaseId(), clusterName);
if (releaseSnapShot == null) {
......@@ -59,7 +59,7 @@ public class ConfigServiceImpl implements ConfigService {
return assembleConfig(version, releaseSnapShot);
}
private ApolloConfig assembleConfig(Version version, ReleaseSnapShot releaseSnapShot) {
private ApolloConfig assembleConfig(Version version, ReleaseSnapshot releaseSnapShot) {
ApolloConfig config =
new ApolloConfig(version.getAppId(), releaseSnapShot.getClusterName(), version.getName(),
version.getReleaseId());
......
......@@ -2,7 +2,7 @@ package com.ctrip.apollo.biz.service.impl;
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.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
......@@ -60,7 +60,7 @@ public class ConfigServiceImplTest {
String someValidConfiguration = "{\"apollo.bar\": \"foo\"}";
Version someVersion = assembleVersion(someAppId, someVersionName, someReleaseId);
ReleaseSnapShot
ReleaseSnapshot
someReleaseSnapShot =
assembleReleaseSnapShot(someReleaseId, someClusterName, someValidConfiguration);
Map<String, Object> someMap = Maps.newHashMap();
......@@ -122,9 +122,9 @@ public class ConfigServiceImplTest {
return version;
}
private ReleaseSnapShot assembleReleaseSnapShot(long releaseId, String clusterName,
private ReleaseSnapshot assembleReleaseSnapShot(long releaseId, String clusterName,
String configurations) {
ReleaseSnapShot releaseSnapShot = new ReleaseSnapShot();
ReleaseSnapshot releaseSnapShot = new ReleaseSnapshot();
releaseSnapShot.setReleaseId(releaseId);
releaseSnapShot.setClusterName(clusterName);
releaseSnapShot.setConfigurations(configurations);
......
......@@ -40,10 +40,10 @@
<url>https://travis-ci.org/ctripcorp/apollo</url>
</ciManagement>
<issueManagement>
<system>github</system>
<url>https://github.com/ctripcorp/apollo/issues</url>
</issueManagement>
<issueManagement>
<system>github</system>
<url>https://github.com/ctripcorp/apollo/issues</url>
</issueManagement>
<developers>
<developer>
......@@ -77,14 +77,14 @@
</roles>
</developer>
</developers>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<github.global.server>github</github.global.server>
<github.global.oauth2Token>${env.GITHUB_OAUTH_TOKEN}</github.global.oauth2Token>
</properties>
<modules>
<module>apollo-buildtools</module>
<module>apollo-core</module>
......@@ -95,7 +95,7 @@
<module>apollo-portal</module>
<module>apollo-demo</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
......@@ -154,7 +154,7 @@
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
......@@ -162,7 +162,7 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
......@@ -218,22 +218,6 @@
</pluginManagement>
<plugins>
<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>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
......@@ -243,7 +227,7 @@
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>travis</id>
......@@ -256,6 +240,22 @@
<build>
<plugins>
<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>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
......@@ -285,7 +285,7 @@
</snapshots>
</repository>
</repositories>
<reporting>
<plugins>
<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