Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
apollo
Commits
f888f994
Commit
f888f994
authored
Mar 28, 2016
by
lepdou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bean dto transform util
parent
c9fc5b11
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
201 additions
and
112 deletions
+201
-112
DTOToEntityConverter.java
.../com/ctrip/apollo/biz/converter/DTOToEntityConverter.java
+0
-40
EntityToDTOConverter.java
.../com/ctrip/apollo/biz/converter/EntityToDTOConverter.java
+0
-45
AdminConfigServiceImpl.java
...ctrip/apollo/biz/service/impl/AdminConfigServiceImpl.java
+6
-26
ApolloBeanUtils.java
...main/java/com/ctrip/apollo/biz/utils/ApolloBeanUtils.java
+194
-0
ConverterUtils.java
.../main/java/com/ctrip/apollo/biz/utils/ConverterUtils.java
+1
-1
No files found.
apollo-biz/src/main/java/com/ctrip/apollo/biz/converter/DTOToEntityConverter.java
deleted
100644 → 0
View file @
c9fc5b11
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
;
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/converter/EntityToDTOConverter.java
deleted
100644 → 0
View file @
c9fc5b11
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
;
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/impl/AdminConfigServiceImpl.java
View file @
f888f994
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
;
...
...
@@ -10,6 +9,7 @@ import com.ctrip.apollo.biz.repository.ConfigItemRepository;
import
com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository
;
import
com.ctrip.apollo.biz.repository.VersionRepository
;
import
com.ctrip.apollo.biz.service.AdminConfigService
;
import
com.ctrip.apollo.biz.utils.ApolloBeanUtils
;
import
com.ctrip.apollo.core.dto.ClusterDTO
;
import
com.ctrip.apollo.core.dto.ConfigItemDTO
;
import
com.ctrip.apollo.core.dto.ReleaseSnapshotDTO
;
...
...
@@ -18,7 +18,6 @@ import com.ctrip.apollo.core.dto.VersionDTO;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
...
...
@@ -46,12 +45,7 @@ public class AdminConfigServiceImpl implements AdminConfigService {
return
Collections
.
EMPTY_LIST
;
}
List
<
ReleaseSnapshotDTO
>
result
=
new
ArrayList
<>(
releaseSnapShots
.
size
());
for
(
ReleaseSnapshot
releaseSnapShot
:
releaseSnapShots
)
{
ReleaseSnapshotDTO
dto
=
EntityToDTOConverter
.
convert
(
releaseSnapShot
);
result
.
add
(
dto
);
}
return
result
;
return
ApolloBeanUtils
.
batchTransform
(
ReleaseSnapshotDTO
.
class
,
releaseSnapShots
);
}
...
...
@@ -66,12 +60,7 @@ public class AdminConfigServiceImpl implements AdminConfigService {
return
Collections
.
EMPTY_LIST
;
}
List
<
VersionDTO
>
result
=
new
ArrayList
<>(
versions
.
size
());
for
(
Version
version
:
versions
)
{
VersionDTO
dto
=
EntityToDTOConverter
.
convert
(
version
);
result
.
add
(
dto
);
}
return
result
;
return
ApolloBeanUtils
.
batchTransform
(
VersionDTO
.
class
,
versions
);
}
@Override
...
...
@@ -83,7 +72,7 @@ public class AdminConfigServiceImpl implements AdminConfigService {
if
(
version
==
null
){
return
null
;
}
VersionDTO
dto
=
EntityToDTOConverter
.
convert
(
version
);
VersionDTO
dto
=
ApolloBeanUtils
.
transfrom
(
VersionDTO
.
class
,
version
);
return
dto
;
}
...
...
@@ -97,11 +86,7 @@ public class AdminConfigServiceImpl implements AdminConfigService {
return
Collections
.
EMPTY_LIST
;
}
List
<
ClusterDTO
>
result
=
new
ArrayList
<>(
clusters
.
size
());
for
(
Cluster
cluster
:
clusters
)
{
result
.
add
(
cluster
.
toDTO
());
}
return
result
;
return
ApolloBeanUtils
.
batchTransform
(
ClusterDTO
.
class
,
clusters
);
}
@Override
...
...
@@ -114,12 +99,7 @@ public class AdminConfigServiceImpl implements AdminConfigService {
return
Collections
.
EMPTY_LIST
;
}
List
<
ConfigItemDTO
>
result
=
new
ArrayList
<>(
configItems
.
size
());
for
(
ConfigItem
configItem
:
configItems
)
{
ConfigItemDTO
dto
=
EntityToDTOConverter
.
convert
(
configItem
);
result
.
add
(
dto
);
}
return
result
;
return
ApolloBeanUtils
.
batchTransform
(
ConfigItemDTO
.
class
,
configItems
);
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/utils/ApolloBeanUtils.java
0 → 100644
View file @
f888f994
package
com
.
ctrip
.
apollo
.
biz
.
utils
;
import
org.springframework.util.CollectionUtils
;
import
java.lang.reflect.Field
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
public
class
ApolloBeanUtils
{
/**
* <pre>
* List<UserBean> userBeans = userDao.queryUsers();
* List<UserDTO> userDTOs = BeanUtil.batchTransform(UserDTO.class, userBeans);
* </pre>
*/
public
static
<
T
>
List
<
T
>
batchTransform
(
final
Class
<
T
>
clazz
,
List
srcList
)
{
if
(
CollectionUtils
.
isEmpty
(
srcList
))
{
return
Collections
.
EMPTY_LIST
;
}
List
<
T
>
result
=
new
ArrayList
<>(
srcList
.
size
());
for
(
Object
srcObject
:
srcList
)
{
result
.
add
(
transfrom
(
clazz
,
srcObject
));
}
return
result
;
}
/**
* 封装{@link org.springframework.beans.BeanUtils#copyProperties},惯用与直接将转换结果返回
*
* <pre>
* UserBean userBean = new UserBean("username");
* return BeanUtil.transform(UserDTO.class, userBean);
* </pre>
*/
public
static
<
T
>
T
transfrom
(
Class
<
T
>
clazz
,
Object
src
)
{
T
instance
=
null
;
try
{
instance
=
clazz
.
newInstance
();
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
org
.
springframework
.
beans
.
BeanUtils
.
copyProperties
(
src
,
instance
,
ConverterUtils
.
getNullPropertyNames
(
src
));
return
instance
;
}
/**
* 用于将一个列表转换为列表中的对象的某个属性映射到列表中的对象
*
* <pre>
* List<UserDTO> userList = userService.queryUsers();
* Map<Integer, userDTO> userIdToUser = BeanUtil.mapByKey("userId", Integer.class, userList,
* UserDTO.class);
* </pre>
*
* @param key 属性名
*/
public
static
<
K
,
V
>
Map
<
K
,
V
>
mapByKey
(
String
key
,
List
list
)
{
Map
<
K
,
V
>
map
=
new
HashMap
<
K
,
V
>();
if
(
CollectionUtils
.
isEmpty
(
list
))
{
return
map
;
}
try
{
Class
clazz
=
list
.
get
(
0
).
getClass
();
Field
field
=
deepFindField
(
clazz
,
key
);
field
.
setAccessible
(
true
);
for
(
Object
o
:
list
)
{
map
.
put
((
K
)
field
.
get
(
o
),
(
V
)
o
);
}
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
();
}
return
map
;
}
/**
* 根据列表里面的属性聚合
*
* <pre>
* List<ShopDTO> shopList = shopService.queryShops();
* Map<Integer, List<ShopDTO>> city2Shops = BeanUtil.aggByKeyToList("cityId", shopList);
* </pre>
*/
public
static
<
K
,
V
>
Map
<
K
,
List
<
V
>>
aggByKeyToList
(
String
key
,
List
list
)
{
Map
<
K
,
List
<
V
>>
map
=
new
HashMap
<
K
,
List
<
V
>>();
if
(
CollectionUtils
.
isEmpty
(
list
))
{
//防止外面传入空list
return
map
;
}
try
{
Class
clazz
=
list
.
get
(
0
).
getClass
();
Field
field
=
deepFindField
(
clazz
,
key
);
field
.
setAccessible
(
true
);
for
(
Object
o
:
list
)
{
K
k
=
(
K
)
field
.
get
(
o
);
if
(
map
.
get
(
k
)
==
null
)
{
map
.
put
(
k
,
new
ArrayList
<
V
>());
}
map
.
get
(
k
).
add
((
V
)
o
);
}
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
();
}
return
map
;
}
/**
* 用于将一个对象的列表转换为列表中对象的属性集合
*
* <pre>
* List<UserDTO> userList = userService.queryUsers();
* Set<Integer> userIds = BeanUtil.toPropertySet("userId", userList);
* </pre>
*/
public
static
Set
toPropertySet
(
String
key
,
List
list
)
{
Set
set
=
new
HashSet
();
if
(
CollectionUtils
.
isEmpty
(
list
))
{
//防止外面传入空list
return
set
;
}
try
{
Class
clazz
=
list
.
get
(
0
).
getClass
();
Field
field
=
deepFindField
(
clazz
,
key
);
if
(
field
==
null
)
{
return
set
;
}
field
.
setAccessible
(
true
);
for
(
Object
o
:
list
)
{
set
.
add
(
field
.
get
(
o
));
}
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
return
set
;
}
private
static
Field
deepFindField
(
Class
clazz
,
String
key
)
{
Field
field
=
null
;
while
(!
clazz
.
getName
().
equals
(
Object
.
class
.
getName
()))
{
try
{
field
=
clazz
.
getDeclaredField
(
key
);
if
(
field
!=
null
)
{
break
;
}
}
catch
(
Exception
e
)
{
clazz
=
clazz
.
getSuperclass
();
}
}
return
field
;
}
/**
* 获取某个对象的某个属性
*/
public
static
Object
getProperty
(
Object
obj
,
String
fieldName
)
{
try
{
Field
field
=
deepFindField
(
obj
.
getClass
(),
fieldName
);
if
(
field
!=
null
)
{
field
.
setAccessible
(
true
);
return
field
.
get
(
obj
);
}
}
catch
(
Exception
e
)
{
//ig
}
return
null
;
}
/**
* 设置某个对象的某个属性
*/
public
static
void
setProperty
(
Object
obj
,
String
fieldName
,
Object
value
)
{
try
{
Field
field
=
deepFindField
(
obj
.
getClass
(),
fieldName
);
if
(
field
!=
null
)
{
field
.
setAccessible
(
true
);
field
.
set
(
obj
,
value
);
}
}
catch
(
Exception
e
)
{
//ig
}
}
public
static
List
toPropertyList
(
String
key
,
List
list
)
{
return
new
ArrayList
(
toPropertySet
(
key
,
list
));
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/
converter
/ConverterUtils.java
→
apollo-biz/src/main/java/com/ctrip/apollo/biz/
utils
/ConverterUtils.java
View file @
f888f994
package
com
.
ctrip
.
apollo
.
biz
.
converter
;
package
com
.
ctrip
.
apollo
.
biz
.
utils
;
import
java.util.HashSet
;
import
java.util.Set
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment