diff --git a/src/main/java/org/springframework/data/repository/support/DomainClassPropertyEditor.java b/src/main/java/org/springframework/data/repository/support/DomainClassPropertyEditor.java deleted file mode 100644 index cb871711d..000000000 --- a/src/main/java/org/springframework/data/repository/support/DomainClassPropertyEditor.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright 2008-2013 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.repository.support; - -import java.beans.PropertyEditor; -import java.beans.PropertyEditorSupport; -import java.io.Serializable; - -import org.springframework.beans.PropertyEditorRegistry; -import org.springframework.beans.SimpleTypeConverter; -import org.springframework.data.repository.CrudRepository; -import org.springframework.data.repository.core.EntityInformation; -import org.springframework.util.Assert; -import org.springframework.util.StringUtils; - -/** - * Generic {@link PropertyEditor} to map entities handled by a {@link CrudRepository} to their id's and vice versa. - * - * @author Oliver Gierke - */ -public class DomainClassPropertyEditor extends PropertyEditorSupport { - - private final RepositoryInvoker invoker; - private final EntityInformation information; - private final PropertyEditorRegistry registry; - - /** - * Creates a new {@link DomainClassPropertyEditor} for the given repository, {@link EntityInformation} and - * {@link PropertyEditorRegistry}. - * - * @param invoker must not be {@literal null}. - * @param information must not be {@literal null}. - * @param registry must not be {@literal null}. - */ - public DomainClassPropertyEditor(RepositoryInvoker invoker, EntityInformation information, - PropertyEditorRegistry registry) { - - Assert.notNull(invoker); - Assert.notNull(information); - Assert.notNull(registry); - - this.invoker = invoker; - this.information = information; - this.registry = registry; - } - - /* - * (non-Javadoc) - * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String) - */ - @Override - public void setAsText(String idAsString) { - - if (!StringUtils.hasText(idAsString)) { - setValue(null); - return; - } - - setValue(invoker.invokeFindOne(getId(idAsString))); - } - - /* - * (non-Javadoc) - * @see java.beans.PropertyEditorSupport#getAsText() - */ - @Override - @SuppressWarnings("unchecked") - public String getAsText() { - - T entity = (T) getValue(); - - if (null == entity) { - return null; - } - - Object id = getId(entity); - return id == null ? null : id.toString(); - } - - /** - * Looks up the id of the given entity using one of the {@link org.synyx.hades.dao.orm.GenericDaoSupport.IdAware} - * implementations of Hades. - * - * @param entity - * @return - */ - private ID getId(T entity) { - - return information.getId(entity); - } - - /** - * Returns the actual typed id. Looks up an available customly registered {@link PropertyEditor} from the - * {@link PropertyEditorRegistry} before falling back on a {@link SimpleTypeConverter} to translate the {@link String} - * id into the type one. - * - * @param idAsString - * @return - */ - @SuppressWarnings("unchecked") - private ID getId(String idAsString) { - - Class idClass = information.getIdType(); - - PropertyEditor idEditor = registry.findCustomEditor(idClass, null); - - if (idEditor != null) { - idEditor.setAsText(idAsString); - return (ID) idEditor.getValue(); - } - - return new SimpleTypeConverter().convertIfNecessary(idAsString, idClass); - } - - /* - * (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - - if (this == obj) { - return true; - } - - if (obj == null || this.getClass() != obj.getClass()) { - return false; - } - - DomainClassPropertyEditor that = (DomainClassPropertyEditor) obj; - - return this.invoker.equals(that.invoker) && this.registry.equals(that.registry) - && this.information.equals(that.information); - } - - /* - * (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - - int hashCode = 17; - hashCode += invoker.hashCode() * 32; - hashCode += information.hashCode() * 32; - hashCode += registry.hashCode() * 32; - return hashCode; - } -} diff --git a/src/main/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrar.java b/src/main/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrar.java deleted file mode 100644 index ba1d806b5..000000000 --- a/src/main/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrar.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2008-2013 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.repository.support; - -import java.io.Serializable; - -import org.springframework.beans.PropertyEditorRegistrar; -import org.springframework.beans.PropertyEditorRegistry; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.data.repository.core.RepositoryInformation; -import org.springframework.web.servlet.DispatcherServlet; - -/** - * Simple helper class to use Hades DAOs to provide {@link java.beans.PropertyEditor}s for domain classes. To get this - * working configure a {@link org.springframework.web.bind.support.ConfigurableWebBindingInitializer} for your - * {@link org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter} and register the - * {@link DomainClassPropertyEditorRegistrar} there: - * <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> - * <property name="webBindingInitializer"> - * <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> - * <property name="propertyEditorRegistrars"> - * <bean class="org.springframework.data.extensions.beans.DomainClassPropertyEditorRegistrar" /> - * </property> - * </bean> - * </property> - * </bean> - * Make sure this bean declaration is in the {@link ApplicationContext} created by the {@link DispatcherServlet} - * whereas the repositories need to be declared in the root - * {@link org.springframework.web.context.WebApplicationContext}. - * - * @author Oliver Gierke - * @deprecated use {@link DomainClassConverter} instead, will be removed in 1.10 - */ -@Deprecated -public class DomainClassPropertyEditorRegistrar implements PropertyEditorRegistrar, ApplicationContextAware { - - private Repositories repositories = Repositories.NONE; - private RepositoryInvokerFactory repositoryInvokerFactory; - - /* - * (non-Javadoc) - * @see org.springframework.beans.PropertyEditorRegistrar#registerCustomEditors(org.springframework.beans.PropertyEditorRegistry) - */ - public void registerCustomEditors(PropertyEditorRegistry registry) { - - for (Class domainClass : repositories) { - - RepositoryInformation repositoryInformation = repositories.getRepositoryInformationFor(domainClass); - RepositoryInvoker invoker = repositoryInvokerFactory.getInvokerFor(domainClass); - - DomainClassPropertyEditor editor = new DomainClassPropertyEditor( - invoker, repositories.getEntityInformationFor(repositoryInformation.getDomainType()), registry); - - registry.registerCustomEditor(repositoryInformation.getDomainType(), editor); - } - } - - /* - * (non-Javadoc) - * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) - */ - public void setApplicationContext(ApplicationContext context) { - - this.repositories = new Repositories(context); - this.repositoryInvokerFactory = new DefaultRepositoryInvokerFactory(repositories); - } -} diff --git a/src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrarUnitTests.java b/src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrarUnitTests.java deleted file mode 100644 index 3522a9035..000000000 --- a/src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrarUnitTests.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2008-2013 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.repository.support; - -import static org.mockito.Matchers.*; -import static org.mockito.Mockito.*; - -import java.io.Serializable; - -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.beans.PropertyEditorRegistry; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import org.springframework.context.support.GenericApplicationContext; -import org.springframework.data.repository.CrudRepository; -import org.springframework.data.repository.core.support.DummyRepositoryFactoryBean; - -/** - * Unit test for {@link DomainClassPropertyEditorRegistrar}. - * - * @author Oliver Gierke - */ -@RunWith(MockitoJUnitRunner.class) -@SuppressWarnings("deprecation") -public class DomainClassPropertyEditorRegistrarUnitTests { - - @Mock PropertyEditorRegistry registry; - - DomainClassPropertyEditorRegistrar registrar; - GenericApplicationContext context; - DomainClassPropertyEditor reference; - - @Before - public void setup() { - - BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DummyRepositoryFactoryBean.class); - builder.addPropertyValue("repositoryInterface", EntityRepository.class); - - DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); - factory.registerBeanDefinition("provider", builder.getBeanDefinition()); - - context = new GenericApplicationContext(factory); - context.refresh(); - registrar = new DomainClassPropertyEditorRegistrar(); - } - - @Test - public void addsRepositoryForEntityIfAvailableInAppContext() throws Exception { - - registrar.setApplicationContext(context); - registrar.registerCustomEditors(registry); - - verify(registry).registerCustomEditor(eq(Entity.class), any(DomainClassPropertyEditor.class)); - } - - @Test - public void doesNotAddDaoAtAllIfNoDaosFound() throws Exception { - - registrar.registerCustomEditors(registry); - - verify(registry, never()).registerCustomEditor(eq(Entity.class), any(DomainClassPropertyEditor.class)); - } - - static class Entity { - - } - - static interface EntityRepository extends CrudRepository { - - } -} diff --git a/src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorUnitTests.java b/src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorUnitTests.java deleted file mode 100644 index 62ff9ef2a..000000000 --- a/src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorUnitTests.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright 2008-2013 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.repository.support; - -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - -import java.beans.PropertyEditor; - -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.beans.PropertyEditorRegistry; -import org.springframework.data.domain.Persistable; -import org.springframework.data.repository.core.EntityInformation; - -/** - * Unit test for {@link DomainClassPropertyEditor}. - * - * @author Oliver Gierke - */ -@RunWith(MockitoJUnitRunner.class) -public class DomainClassPropertyEditorUnitTests { - - DomainClassPropertyEditor editor; - - @Mock PropertyEditorRegistry registry; - @Mock RepositoryInvoker invoker; - @Mock EntityInformation information; - - @Before - public void setUp() { - - when(information.getIdType()).thenReturn(Integer.class); - editor = new DomainClassPropertyEditor(invoker, information, registry); - } - - @Test - public void convertsPlainIdTypeCorrectly() throws Exception { - - User user = new User(1); - when(information.getId(user)).thenReturn(user.getId()); - when(invoker.invokeFindOne(1)).thenReturn(user); - - editor.setAsText("1"); - - verify(invoker, times(1)).invokeFindOne(1); - } - - @Test - public void convertsEntityToIdCorrectly() throws Exception { - - User user = new User(1); - editor.setValue(user); - when(information.getId(user)).thenReturn(user.getId()); - assertThat(editor.getAsText(), is("1")); - } - - @Test - public void usesCustomEditorIfConfigured() throws Exception { - - PropertyEditor customEditor = mock(PropertyEditor.class); - when(customEditor.getValue()).thenReturn(1); - - when(registry.findCustomEditor(Integer.class, null)).thenReturn(customEditor); - - convertsPlainIdTypeCorrectly(); - - verify(customEditor, times(1)).setAsText("1"); - } - - @Test - public void returnsNullIdIfNoEntitySet() throws Exception { - - editor.setValue(null); - assertThat(editor.getAsText(), is(nullValue())); - } - - @Test - public void resetsValueToNullAfterEmptyStringConversion() throws Exception { - - assertValueResetToNullAfterConverting(""); - } - - @Test - public void resetsValueToNullAfterNullStringConversion() throws Exception { - - assertValueResetToNullAfterConverting(null); - } - - private void assertValueResetToNullAfterConverting(String source) throws Exception { - - convertsPlainIdTypeCorrectly(); - assertThat(editor.getValue(), is(notNullValue())); - - editor.setAsText(source); - assertThat(editor.getValue(), is(nullValue())); - } - - /** - * Sample entity. - * - * @author Oliver Gierke - */ - @SuppressWarnings("serial") - private static class User implements Persistable { - - private Integer id; - - public User(Integer id) { - - this.id = id; - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Persistable#getId() - */ - public Integer getId() { - - return id; - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Persistable#isNew() - */ - public boolean isNew() { - - return getId() != null; - } - } -}