Browse Source
We now wrap the MappingMongoEntityInformation into one that delegates the methods implemented by Persistable to the actual entity in case it implements said interface. Original pull request: #436.pull/410/merge
4 changed files with 237 additions and 2 deletions
@ -0,0 +1,55 @@
@@ -0,0 +1,55 @@
|
||||
/* |
||||
* Copyright 2017 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.mongodb.repository.support; |
||||
|
||||
import java.io.Serializable; |
||||
|
||||
import org.springframework.data.domain.Persistable; |
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; |
||||
import org.springframework.data.mongodb.repository.query.MongoEntityInformation; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.ClassUtils; |
||||
|
||||
/** |
||||
* Support class responsible for creating {@link MongoEntityInformation} instances for a given |
||||
* {@link MongoPersistentEntity}. |
||||
* |
||||
* @author Christoph Strobl |
||||
* @since 1.10 |
||||
*/ |
||||
final class MongoEntityInformationSupport { |
||||
|
||||
private MongoEntityInformationSupport() {} |
||||
|
||||
/** |
||||
* Factory method for creating {@link MongoEntityInformation}. |
||||
* |
||||
* @param entity must not be {@literal null}. |
||||
* @param idType can be {@literal null}. |
||||
* @return never {@literal null}. |
||||
*/ |
||||
static <T, ID extends Serializable> MongoEntityInformation<T, ID> entityInformationFor( |
||||
MongoPersistentEntity<?> entity, Class<?> idType) { |
||||
|
||||
Assert.notNull(entity, "Entity must not be null!"); |
||||
|
||||
MappingMongoEntityInformation entityInformation = new MappingMongoEntityInformation<T, ID>( |
||||
(MongoPersistentEntity<T>) entity, (Class<ID>) idType); |
||||
return ClassUtils.isAssignable(Persistable.class, entity.getType()) |
||||
? new PersistableMongoEntityInformation<T, ID>(entityInformation) : entityInformation; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,102 @@
@@ -0,0 +1,102 @@
|
||||
/* |
||||
* Copyright 2017 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.mongodb.repository.support; |
||||
|
||||
import java.io.Serializable; |
||||
|
||||
import org.springframework.data.domain.Persistable; |
||||
import org.springframework.data.mongodb.repository.query.MongoEntityInformation; |
||||
|
||||
/** |
||||
* {@link MongoEntityInformation} implementation wrapping an existing {@link MongoEntityInformation} considering |
||||
* {@link Persistable} types by delegating {@link #isNew(Object)} and {@link #getId(Object)} to the corresponding |
||||
* {@link Persistable#isNew()} and {@link Persistable#getId()} implementations. |
||||
* |
||||
* @author Christoph Strobl |
||||
* @since 1.10 |
||||
*/ |
||||
public class PersistableMongoEntityInformation<T, ID extends Serializable> implements MongoEntityInformation<T, ID> { |
||||
|
||||
private final MongoEntityInformation<T, ID> delegate; |
||||
|
||||
public PersistableMongoEntityInformation(MongoEntityInformation<T, ID> delegate) { |
||||
this.delegate = delegate; |
||||
} |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see org.springframework.data.mongodb.repository.MongoEntityInformation#getCollectionName() |
||||
*/ |
||||
@Override |
||||
public String getCollectionName() { |
||||
return delegate.getCollectionName(); |
||||
} |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see org.springframework.data.mongodb.repository.MongoEntityInformation#getIdAttribute() |
||||
*/ |
||||
@Override |
||||
public String getIdAttribute() { |
||||
return delegate.getIdAttribute(); |
||||
} |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see org.springframework.data.repository.core.EntityInformation#isNew(java.lang.Object) |
||||
*/ |
||||
@Override |
||||
public boolean isNew(T t) { |
||||
|
||||
if (t instanceof Persistable) { |
||||
return ((Persistable) t).isNew(); |
||||
} |
||||
|
||||
return delegate.isNew(t); |
||||
} |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see org.springframework.data.repository.core.EntityInformation#getId(java.lang.Object) |
||||
*/ |
||||
@Override |
||||
public ID getId(T t) { |
||||
|
||||
if (t instanceof Persistable) { |
||||
return (ID) ((Persistable) t).getId(); |
||||
} |
||||
|
||||
return delegate.getId(t); |
||||
} |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see org.springframework.data.repository.core.support.PersistentEntityInformation#getIdType() |
||||
*/ |
||||
@Override |
||||
public Class<ID> getIdType() { |
||||
return delegate.getIdType(); |
||||
} |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see org.springframework.data.repository.core.support.EntityMetadata#getJavaType() |
||||
*/ |
||||
@Override |
||||
public Class<T> getJavaType() { |
||||
return delegate.getJavaType(); |
||||
} |
||||
} |
||||
@ -0,0 +1,76 @@
@@ -0,0 +1,76 @@
|
||||
/* |
||||
* Copyright 2017 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.mongodb.repository.query; |
||||
|
||||
import static org.hamcrest.CoreMatchers.*; |
||||
import static org.junit.Assert.*; |
||||
import static org.mockito.Mockito.*; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.Mock; |
||||
import org.mockito.runners.MockitoJUnitRunner; |
||||
import org.springframework.data.domain.Persistable; |
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; |
||||
import org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation; |
||||
import org.springframework.data.mongodb.repository.support.PersistableMongoEntityInformation; |
||||
|
||||
/** |
||||
* Tests for {@link PersistableMongoEntityInformation}. |
||||
* |
||||
* @author Christoph Strobl |
||||
*/ |
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class PersistableMappingMongoEntityInformationUnitTests { |
||||
|
||||
@Mock MongoPersistentEntity<TypeImplementingPersistable> persistableImplementingEntityTypeInfo; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
when(persistableImplementingEntityTypeInfo.getType()).thenReturn(TypeImplementingPersistable.class); |
||||
} |
||||
|
||||
@Test // DATAMONGO-1590
|
||||
public void considersPersistableIsNew() { |
||||
|
||||
PersistableMongoEntityInformation<TypeImplementingPersistable, Long> information = new PersistableMongoEntityInformation<TypeImplementingPersistable, Long>( |
||||
new MappingMongoEntityInformation<TypeImplementingPersistable, Long>(persistableImplementingEntityTypeInfo)); |
||||
|
||||
assertThat(information.isNew(new TypeImplementingPersistable(100L, false)), is(false)); |
||||
} |
||||
|
||||
static class TypeImplementingPersistable implements Persistable<Long> { |
||||
|
||||
final Long id; |
||||
final boolean isNew; |
||||
|
||||
public TypeImplementingPersistable(Long id, boolean isNew) { |
||||
this.id = id; |
||||
this.isNew = isNew; |
||||
} |
||||
|
||||
@Override |
||||
public Long getId() { |
||||
return id; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isNew() { |
||||
return isNew; |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue