Skip to content

Commit 7c56af6

Browse files
committed
DATAJDBC-272 - Polishing.
Add missing relational.repository package that got lost during module split.
1 parent baa6e7e commit 7c56af6

11 files changed

+545
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.springframework.data</groupId>
88
<artifactId>spring-data-relational-parent</artifactId>
9-
<version>1.1.0.BUILD-SNAPSHOT</version>
9+
<version>1.1.0.BUILD-SNAPSHOT</version>
1010
<packaging>pom</packaging>
1111

1212
<name>Spring Data Relational Parent</name>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2018 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+
* http://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.relational.repository.query;
17+
18+
import org.springframework.core.convert.converter.Converter;
19+
import org.springframework.data.convert.EntityInstantiator;
20+
import org.springframework.data.convert.EntityInstantiators;
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.PreferredConstructor;
25+
import org.springframework.data.mapping.PreferredConstructor.Parameter;
26+
import org.springframework.data.mapping.SimplePropertyHandler;
27+
import org.springframework.data.mapping.context.MappingContext;
28+
import org.springframework.data.mapping.model.ParameterValueProvider;
29+
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
30+
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
31+
import org.springframework.util.Assert;
32+
33+
/**
34+
* {@link Converter} to instantiate DTOs from fully equipped domain objects.
35+
*
36+
* @author Mark Paluch
37+
*/
38+
public class DtoInstantiatingConverter implements Converter<Object, Object> {
39+
40+
private final Class<?> targetType;
41+
private final MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> context;
42+
private final EntityInstantiator instantiator;
43+
44+
/**
45+
* Creates a new {@link Converter} to instantiate DTOs.
46+
*
47+
* @param dtoType must not be {@literal null}.
48+
* @param context must not be {@literal null}.
49+
* @param instantiators must not be {@literal null}.
50+
*/
51+
public DtoInstantiatingConverter(Class<?> dtoType,
52+
MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context,
53+
EntityInstantiators instantiator) {
54+
55+
Assert.notNull(dtoType, "DTO type must not be null!");
56+
Assert.notNull(context, "MappingContext must not be null!");
57+
Assert.notNull(instantiator, "EntityInstantiators must not be null!");
58+
59+
this.targetType = dtoType;
60+
this.context = context;
61+
this.instantiator = instantiator.getInstantiatorFor(context.getRequiredPersistentEntity(dtoType));
62+
}
63+
64+
/*
65+
* (non-Javadoc)
66+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
67+
*/
68+
@Override
69+
public Object convert(Object source) {
70+
71+
if (targetType.isInterface()) {
72+
return source;
73+
}
74+
75+
final PersistentEntity<?, ?> sourceEntity = context.getRequiredPersistentEntity(source.getClass());
76+
final PersistentPropertyAccessor sourceAccessor = sourceEntity.getPropertyAccessor(source);
77+
final PersistentEntity<?, ?> targetEntity = context.getRequiredPersistentEntity(targetType);
78+
final PreferredConstructor<?, ? extends PersistentProperty<?>> constructor = targetEntity
79+
.getPersistenceConstructor();
80+
81+
@SuppressWarnings({"rawtypes", "unchecked"})
82+
Object dto = instantiator.createInstance(targetEntity, new ParameterValueProvider() {
83+
84+
@Override
85+
public Object getParameterValue(Parameter parameter) {
86+
return sourceAccessor.getProperty(sourceEntity.getPersistentProperty(parameter.getName()));
87+
}
88+
});
89+
90+
final PersistentPropertyAccessor dtoAccessor = targetEntity.getPropertyAccessor(dto);
91+
92+
targetEntity.doWithProperties((SimplePropertyHandler) property -> {
93+
94+
if (constructor.isConstructorParameter(property)) {
95+
return;
96+
}
97+
98+
dtoAccessor.setProperty(property,
99+
sourceAccessor.getProperty(sourceEntity.getPersistentProperty(property.getName())));
100+
});
101+
102+
return dto;
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2018 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+
* http://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.relational.repository.query;
17+
18+
import org.springframework.data.repository.core.EntityInformation;
19+
20+
/**
21+
* Relational database-specific {@link EntityInformation}.
22+
*
23+
* @author Mark Paluch
24+
*/
25+
public interface RelationalEntityInformation<T, ID> extends EntityInformation<T, ID> {
26+
27+
/**
28+
* Returns the name of the table the entity shall be persisted to.
29+
*
30+
* @return
31+
*/
32+
String getTableName();
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2018 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+
* http://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.relational.repository.query;
17+
18+
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
19+
import org.springframework.data.repository.core.EntityMetadata;
20+
21+
/**
22+
* Extension of {@link EntityMetadata} to additionally expose the collection name an entity shall be persisted to.
23+
*
24+
* @author Mark Paluch
25+
*/
26+
public interface RelationalEntityMetadata<T> extends EntityMetadata<T> {
27+
28+
/**
29+
* Returns the name of the table the entity shall be persisted to.
30+
*
31+
* @return
32+
*/
33+
String getTableName();
34+
35+
/**
36+
* Returns the {@link RelationalPersistentEntity} that supposed to determine the table to be queried.
37+
*
38+
* @return
39+
*/
40+
RelationalPersistentEntity<?> getTableEntity();
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2018 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+
* http://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.relational.repository.query;
17+
18+
import org.springframework.data.repository.query.ParameterAccessor;
19+
import org.springframework.data.repository.query.Parameters;
20+
21+
/**
22+
* Relational-specific {@link ParameterAccessor}.
23+
*
24+
* @author Mark Paluch
25+
*/
26+
public interface RelationalParameterAccessor extends ParameterAccessor {
27+
28+
/**
29+
* Returns the raw parameter values of the underlying query method.
30+
*/
31+
Object[] getValues();
32+
33+
/**
34+
* @return the bindable parameters.
35+
*/
36+
Parameters<?, ?> getBindableParameters();
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2018 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+
* http://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.relational.repository.query;
17+
18+
import java.lang.reflect.Method;
19+
import java.util.List;
20+
21+
import org.springframework.core.MethodParameter;
22+
import org.springframework.data.relational.repository.query.RelationalParameters.RelationalParameter;
23+
import org.springframework.data.repository.query.Parameter;
24+
import org.springframework.data.repository.query.Parameters;
25+
26+
/**
27+
* Custom extension of {@link Parameters}.
28+
*
29+
* @author Mark Paluch
30+
*/
31+
public class RelationalParameters extends Parameters<RelationalParameters, RelationalParameter> {
32+
33+
/**
34+
* Creates a new {@link RelationalParameters} instance from the given {@link Method}.
35+
*
36+
* @param method must not be {@literal null}.
37+
*/
38+
public RelationalParameters(Method method) {
39+
super(method);
40+
}
41+
42+
private RelationalParameters(List<RelationalParameter> parameters) {
43+
super(parameters);
44+
}
45+
46+
/*
47+
* (non-Javadoc)
48+
* @see org.springframework.data.repository.query.Parameters#createParameter(org.springframework.core.MethodParameter)
49+
*/
50+
@Override
51+
protected RelationalParameter createParameter(MethodParameter parameter) {
52+
return new RelationalParameter(parameter);
53+
}
54+
55+
/*
56+
* (non-Javadoc)
57+
* @see org.springframework.data.repository.query.Parameters#createFrom(java.util.List)
58+
*/
59+
@Override
60+
protected RelationalParameters createFrom(List<RelationalParameter> parameters) {
61+
return new RelationalParameters(parameters);
62+
}
63+
64+
/**
65+
* Custom {@link Parameter} implementation.
66+
*
67+
* @author Mark Paluch
68+
*/
69+
public static class RelationalParameter extends Parameter {
70+
71+
/**
72+
* Creates a new {@link RelationalParameter}.
73+
*
74+
* @param parameter must not be {@literal null}.
75+
*/
76+
RelationalParameter(MethodParameter parameter) {
77+
super(parameter);
78+
}
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2018 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+
* http://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.relational.repository.query;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
21+
import org.springframework.data.repository.query.Parameters;
22+
import org.springframework.data.repository.query.ParametersParameterAccessor;
23+
import org.springframework.data.repository.query.QueryMethod;
24+
25+
/**
26+
* Relational-specific {@link ParametersParameterAccessor}.
27+
*
28+
* @author Mark Paluch
29+
*/
30+
public class RelationalParametersParameterAccessor extends ParametersParameterAccessor
31+
implements RelationalParameterAccessor {
32+
33+
private final List<Object> values;
34+
35+
/**
36+
* Creates a new {@link RelationalParametersParameterAccessor}.
37+
*
38+
* @param method must not be {@literal null}.
39+
* @param values must not be {@literal null}.
40+
*/
41+
public RelationalParametersParameterAccessor(QueryMethod method, Object[] values) {
42+
43+
super(method.getParameters(), values);
44+
this.values = Arrays.asList(values);
45+
}
46+
47+
/* (non-Javadoc)
48+
* @see org.springframework.data.relational.repository.query.RelationalParameterAccessor#getValues()
49+
*/
50+
@Override
51+
public Object[] getValues() {
52+
return values.toArray();
53+
}
54+
55+
/* (non-Javadoc)
56+
* @see org.springframework.data.relational.repository.query.RelationalParameterAccessor#getBindableParameters()
57+
*/
58+
@Override
59+
public Parameters<?, ?> getBindableParameters() {
60+
return getParameters().getBindableParameters();
61+
}
62+
}

0 commit comments

Comments
 (0)