Skip to content

Commit 34ce82b

Browse files
committed
Introduce standard DtoInstantiatingConverter.
Move simple Spring Converter implementation that allow to create DTOs from a source object previously copied amongst a variety of store modules. Issue #2476.
1 parent 1e38b4c commit 34ce82b

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.convert;
17+
18+
import org.springframework.core.convert.converter.Converter;
19+
import org.springframework.data.mapping.InstanceCreatorMetadata;
20+
import org.springframework.data.mapping.Parameter;
21+
import org.springframework.data.mapping.PersistentEntity;
22+
import org.springframework.data.mapping.PersistentProperty;
23+
import org.springframework.data.mapping.PersistentPropertyAccessor;
24+
import org.springframework.data.mapping.SimplePropertyHandler;
25+
import org.springframework.data.mapping.context.MappingContext;
26+
import org.springframework.data.mapping.model.EntityInstantiator;
27+
import org.springframework.data.mapping.model.EntityInstantiators;
28+
import org.springframework.data.mapping.model.ParameterValueProvider;
29+
import org.springframework.lang.NonNull;
30+
import org.springframework.lang.Nullable;
31+
import org.springframework.util.Assert;
32+
33+
/**
34+
* Spring {@link Converter} to create instances of the given DTO type from the source value handed into the conversion.
35+
*
36+
* @author Mark Paluch
37+
* @author Oliver Drotbohm
38+
* @since 2.7
39+
*/
40+
public class DtoInstantiatingConverter implements Converter<Object, Object> {
41+
42+
private final Class<?> targetType;
43+
private final MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> context;
44+
private final EntityInstantiator instantiator;
45+
46+
/**
47+
* Create a new {@link Converter} to instantiate DTOs.
48+
*
49+
* @param dtoType must not be {@literal null}.
50+
* @param context must not be {@literal null}.
51+
* @param instantiators must not be {@literal null}.
52+
*/
53+
public DtoInstantiatingConverter(Class<?> dtoType,
54+
MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> context,
55+
EntityInstantiators instantiator) {
56+
57+
Assert.notNull(dtoType, "DTO type must not be null!");
58+
Assert.notNull(context, "MappingContext must not be null!");
59+
Assert.notNull(instantiator, "EntityInstantiators must not be null!");
60+
61+
this.targetType = dtoType;
62+
this.context = context;
63+
this.instantiator = instantiator.getInstantiatorFor(context.getRequiredPersistentEntity(dtoType));
64+
}
65+
66+
/*
67+
* (non-Javadoc)
68+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
69+
*/
70+
@NonNull
71+
@Override
72+
public Object convert(Object source) {
73+
74+
if (targetType.isInterface()) {
75+
return source;
76+
}
77+
78+
PersistentEntity<?, ?> sourceEntity = context.getRequiredPersistentEntity(source.getClass());
79+
PersistentPropertyAccessor<?> sourceAccessor = sourceEntity.getPropertyAccessor(source);
80+
PersistentEntity<?, ?> targetEntity = context.getRequiredPersistentEntity(targetType);
81+
82+
@SuppressWarnings({ "rawtypes", "unchecked" })
83+
Object dto = instantiator.createInstance(targetEntity, new ParameterValueProvider() {
84+
85+
@Override
86+
@Nullable
87+
public Object getParameterValue(Parameter parameter) {
88+
89+
String name = parameter.getName();
90+
91+
if (name == null) {
92+
throw new IllegalArgumentException(String.format("Parameter %s does not have a name", parameter));
93+
}
94+
95+
return sourceAccessor.getProperty(sourceEntity.getRequiredPersistentProperty(name));
96+
}
97+
});
98+
99+
final PersistentPropertyAccessor<?> targetAccessor = targetEntity.getPropertyAccessor(dto);
100+
101+
InstanceCreatorMetadata<? extends PersistentProperty<?>> constructor = targetEntity.getInstanceCreatorMetadata();
102+
103+
targetEntity.doWithProperties((SimplePropertyHandler) property -> {
104+
105+
if (constructor != null && constructor.isCreatorParameter(property)) {
106+
return;
107+
}
108+
109+
targetAccessor.setProperty(property,
110+
sourceAccessor.getProperty(sourceEntity.getRequiredPersistentProperty(property.getName())));
111+
});
112+
113+
return dto;
114+
}
115+
}

0 commit comments

Comments
 (0)