Skip to content

Commit fd12b92

Browse files
DATAMONGO-1590 - Add PersistableMongoEntityInformation.
PersistableMongoEntityInformation considers and delegates calls to Persistable implementing types. Related to: DATCMNS-976
1 parent 5a4edf8 commit fd12b92

File tree

4 files changed

+162
-3
lines changed

4 files changed

+162
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<properties>
2929
<project.type>multi</project.type>
3030
<dist.id>spring-data-mongodb</dist.id>
31-
<springdata.commons>1.13.0.DATACMNS-976-SNAPSHOT</springdata.commons>
31+
<springdata.commons>1.13.0.BUILD-SNAPSHOT</springdata.commons>
3232
<mongo>2.14.3</mongo>
3333
<mongo.osgi>2.13.0</mongo.osgi>
3434
</properties>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2017 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.mongodb.repository.support;
17+
18+
import java.io.Serializable;
19+
20+
import org.springframework.data.domain.Persistable;
21+
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
22+
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
23+
import org.springframework.util.Assert;
24+
import org.springframework.util.ClassUtils;
25+
26+
/**
27+
* Support class responsible for creating {@link MongoEntityInformation} instances for a given
28+
* {@link MongoPersistentEntity}.
29+
*
30+
* @author Christoph Strobl
31+
* @since 1.10
32+
*/
33+
final class MongoEntityInformationSupport {
34+
35+
private MongoEntityInformationSupport() {}
36+
37+
/**
38+
* Factory method for creating {@link MongoEntityInformation}.
39+
*
40+
* @param entity must not be {@literal null}.
41+
* @param idType can be {@literal null}.
42+
* @return never {@literal null}.
43+
*/
44+
static <T, ID extends Serializable> MongoEntityInformation<T, ID> entityInformationFor(
45+
MongoPersistentEntity<?> entity, Class<?> idType) {
46+
47+
Assert.notNull(entity, "Entity must not be null!");
48+
49+
MappingMongoEntityInformation entityInformation = new MappingMongoEntityInformation<T, ID>(
50+
(MongoPersistentEntity<T>) entity, (Class<ID>) idType);
51+
return ClassUtils.isAssignable(Persistable.class, entity.getType())
52+
? new PersistableMongoEntityInformation<T, ID>(entityInformation) : entityInformation;
53+
}
54+
55+
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.Serializable;
2121
import java.lang.reflect.Method;
2222

23+
import org.springframework.data.domain.Persistable;
2324
import org.springframework.data.mapping.context.MappingContext;
2425
import org.springframework.data.mapping.model.MappingException;
2526
import org.springframework.data.mongodb.core.MongoOperations;
@@ -42,6 +43,7 @@
4243
import org.springframework.data.repository.query.RepositoryQuery;
4344
import org.springframework.expression.spel.standard.SpelExpressionParser;
4445
import org.springframework.util.Assert;
46+
import org.springframework.util.ClassUtils;
4547

4648
/**
4749
* Factory to create {@link MongoRepository} instances.
@@ -123,8 +125,8 @@ private <T, ID extends Serializable> MongoEntityInformation<T, ID> getEntityInfo
123125
String.format("Could not lookup mapping metadata for domain class %s!", domainClass.getName()));
124126
}
125127

126-
return new MappingMongoEntityInformation<T, ID>((MongoPersistentEntity<T>) entity,
127-
information != null ? (Class<ID>) information.getIdType() : null);
128+
return MongoEntityInformationSupport.<T, ID> entityInformationFor(entity,
129+
information != null ? information.getIdType() : null);
128130
}
129131

130132
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2017 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.mongodb.repository.support;
17+
18+
import java.io.Serializable;
19+
20+
import org.springframework.data.domain.Persistable;
21+
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
22+
23+
/**
24+
* {@link MongoEntityInformation} implementation wrapping an existing {@link MongoEntityInformation} considering
25+
* {@link Persistable} types by delegating {@link #isNew(Object)} and {@link #getId(Object)} to the corresponding
26+
* {@link Persistable#isNew()} and {@link Persistable#getId()} implementations.
27+
*
28+
* @author Christoph Strobl
29+
* @since 1.10
30+
*/
31+
public class PersistableMongoEntityInformation<T, ID extends Serializable> implements MongoEntityInformation<T, ID> {
32+
33+
private final MongoEntityInformation<T, ID> delegate;
34+
35+
public PersistableMongoEntityInformation(MongoEntityInformation<T, ID> delegate) {
36+
this.delegate = delegate;
37+
}
38+
39+
/*
40+
* (non-Javadoc)
41+
* @see org.springframework.data.mongodb.repository.MongoEntityInformation#getCollectionName()
42+
*/
43+
@Override
44+
public String getCollectionName() {
45+
return delegate.getCollectionName();
46+
}
47+
48+
/*
49+
* (non-Javadoc)
50+
* @see org.springframework.data.mongodb.repository.MongoEntityInformation#getIdAttribute()
51+
*/
52+
@Override
53+
public String getIdAttribute() {
54+
return delegate.getIdAttribute();
55+
}
56+
57+
/*
58+
* (non-Javadoc)
59+
* @see org.springframework.data.repository.core.EntityInformation#isNew(java.lang.Object)
60+
*/
61+
@Override
62+
public boolean isNew(T t) {
63+
64+
if (t instanceof Persistable) {
65+
return ((Persistable) t).isNew();
66+
}
67+
68+
return delegate.isNew(t);
69+
}
70+
71+
/*
72+
* (non-Javadoc)
73+
* @see org.springframework.data.repository.core.EntityInformation#getId(java.lang.Object)
74+
*/
75+
@Override
76+
public ID getId(T t) {
77+
78+
if (t instanceof Persistable) {
79+
return (ID) ((Persistable) t).getId();
80+
}
81+
82+
return delegate.getId(t);
83+
}
84+
85+
/*
86+
* (non-Javadoc)
87+
* @see org.springframework.data.repository.core.support.PersistentEntityInformation#getIdType()
88+
*/
89+
@Override
90+
public Class<ID> getIdType() {
91+
return delegate.getIdType();
92+
}
93+
94+
/*
95+
* (non-Javadoc)
96+
* @see org.springframework.data.repository.core.support.EntityMetadata#getJavaType()
97+
*/
98+
@Override
99+
public Class<T> getJavaType() {
100+
return delegate.getJavaType();
101+
}
102+
}

0 commit comments

Comments
 (0)