Skip to content

Commit 979aa01

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

File tree

6 files changed

+238
-36
lines changed

6 files changed

+238
-36
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+
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MappingMongoEntityInformationUnitTests.java

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.junit.runner.RunWith;
2525
import org.mockito.Mock;
2626
import org.mockito.runners.MockitoJUnitRunner;
27-
import org.springframework.data.domain.Persistable;
2827
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
2928
import org.springframework.data.mongodb.repository.Person;
3029
import org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation;
@@ -39,14 +38,12 @@
3938
public class MappingMongoEntityInformationUnitTests {
4039

4140
@Mock MongoPersistentEntity<Person> info;
42-
@Mock MongoPersistentEntity<TypeImplementingPersistable> persistableImplementingEntityTypeInfo;
4341

4442
@Before
4543
public void setUp() {
4644

4745
when(info.getType()).thenReturn(Person.class);
4846
when(info.getCollection()).thenReturn("Person");
49-
when(persistableImplementingEntityTypeInfo.getType()).thenReturn(TypeImplementingPersistable.class);
5047
}
5148

5249
@Test // DATAMONGO-248
@@ -62,34 +59,4 @@ public void usesCustomCollectionIfGiven() {
6259
MongoEntityInformation<Person, Long> information = new MappingMongoEntityInformation<Person, Long>(info, "foobar");
6360
assertThat(information.getCollectionName(), is("foobar"));
6461
}
65-
66-
@Test // DATAMONGO-1590
67-
public void considersPersistableIsNew() {
68-
69-
MongoEntityInformation<TypeImplementingPersistable, Long> information = new MappingMongoEntityInformation<TypeImplementingPersistable, Long>(
70-
persistableImplementingEntityTypeInfo);
71-
assertThat(information.isNew(new TypeImplementingPersistable(100L, true)), is(true));
72-
assertThat(information.isNew(new TypeImplementingPersistable(100L, false)), is(false));
73-
}
74-
75-
static class TypeImplementingPersistable implements Persistable<Long> {
76-
77-
final Long id;
78-
final boolean isNew;
79-
80-
public TypeImplementingPersistable(Long id, boolean isNew) {
81-
this.id = id;
82-
this.isNew = isNew;
83-
}
84-
85-
@Override
86-
public Long getId() {
87-
return id;
88-
}
89-
90-
@Override
91-
public boolean isNew() {
92-
return isNew;
93-
}
94-
}
9562
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.query;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
import static org.mockito.Mockito.*;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.mockito.Mock;
26+
import org.mockito.runners.MockitoJUnitRunner;
27+
import org.springframework.data.domain.Persistable;
28+
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
29+
import org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation;
30+
import org.springframework.data.mongodb.repository.support.PersistableMongoEntityInformation;
31+
32+
/**
33+
* Tests for {@link PersistableMongoEntityInformation}.
34+
*
35+
* @author Christoph Strobl
36+
*/
37+
@RunWith(MockitoJUnitRunner.class)
38+
public class PersistableMappingMongoEntityInformationUnitTests {
39+
40+
@Mock MongoPersistentEntity<TypeImplementingPersistable> persistableImplementingEntityTypeInfo;
41+
42+
@Before
43+
public void setUp() {
44+
when(persistableImplementingEntityTypeInfo.getType()).thenReturn(TypeImplementingPersistable.class);
45+
}
46+
47+
@Test // DATAMONGO-1590
48+
public void considersPersistableIsNew() {
49+
50+
PersistableMongoEntityInformation<TypeImplementingPersistable, Long> information = new PersistableMongoEntityInformation<TypeImplementingPersistable, Long>(
51+
new MappingMongoEntityInformation<TypeImplementingPersistable, Long>(persistableImplementingEntityTypeInfo));
52+
53+
assertThat(information.isNew(new TypeImplementingPersistable(100L, false)), is(false));
54+
}
55+
56+
static class TypeImplementingPersistable implements Persistable<Long> {
57+
58+
final Long id;
59+
final boolean isNew;
60+
61+
public TypeImplementingPersistable(Long id, boolean isNew) {
62+
this.id = id;
63+
this.isNew = isNew;
64+
}
65+
66+
@Override
67+
public Long getId() {
68+
return id;
69+
}
70+
71+
@Override
72+
public boolean isNew() {
73+
return isNew;
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)