diff --git a/src/main/java/com/arangodb/springframework/config/AbstractArangoConfiguration.java b/src/main/java/com/arangodb/springframework/config/AbstractArangoConfiguration.java index 6f5261f0d..97311d215 100644 --- a/src/main/java/com/arangodb/springframework/config/AbstractArangoConfiguration.java +++ b/src/main/java/com/arangodb/springframework/config/AbstractArangoConfiguration.java @@ -20,9 +20,7 @@ package com.arangodb.springframework.config; -import java.lang.annotation.Annotation; import java.util.Collections; -import java.util.Optional; import java.util.Set; import org.springframework.context.annotation.Bean; @@ -32,24 +30,14 @@ import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy; import com.arangodb.ArangoDB; -import com.arangodb.ArangoDBException; -import com.arangodb.springframework.annotation.From; -import com.arangodb.springframework.annotation.Ref; -import com.arangodb.springframework.annotation.Relations; -import com.arangodb.springframework.annotation.To; import com.arangodb.springframework.core.ArangoOperations; import com.arangodb.springframework.core.convert.ArangoConverter; import com.arangodb.springframework.core.convert.ArangoCustomConversions; import com.arangodb.springframework.core.convert.ArangoTypeMapper; import com.arangodb.springframework.core.convert.DefaultArangoConverter; import com.arangodb.springframework.core.convert.DefaultArangoTypeMapper; -import com.arangodb.springframework.core.convert.resolver.FromResolver; -import com.arangodb.springframework.core.convert.resolver.RefResolver; -import com.arangodb.springframework.core.convert.resolver.ReferenceResolver; -import com.arangodb.springframework.core.convert.resolver.RelationResolver; -import com.arangodb.springframework.core.convert.resolver.RelationsResolver; +import com.arangodb.springframework.core.convert.resolver.DefaultResolverFactory; import com.arangodb.springframework.core.convert.resolver.ResolverFactory; -import com.arangodb.springframework.core.convert.resolver.ToResolver; import com.arangodb.springframework.core.mapping.ArangoMappingContext; import com.arangodb.springframework.core.template.ArangoTemplate; @@ -109,40 +97,8 @@ protected ArangoTypeMapper arangoTypeMapper() throws Exception { return new DefaultArangoTypeMapper(typeKey(), arangoMappingContext()); } - protected ResolverFactory resolverFactory() { - return new ResolverFactory() { - @SuppressWarnings("unchecked") - @Override - public Optional> getReferenceResolver(final A annotation) { - ReferenceResolver resolver = null; - try { - if (annotation instanceof Ref) { - resolver = (ReferenceResolver) new RefResolver(arangoTemplate()); - } - } catch (final Exception e) { - throw new ArangoDBException(e); - } - return Optional.ofNullable(resolver); - } - - @SuppressWarnings("unchecked") - @Override - public Optional> getRelationResolver(final A annotation) { - RelationResolver resolver = null; - try { - if (annotation instanceof From) { - resolver = (RelationResolver) new FromResolver(arangoTemplate()); - } else if (annotation instanceof To) { - resolver = (RelationResolver) new ToResolver(arangoTemplate()); - } else if (annotation instanceof Relations) { - resolver = (RelationResolver) new RelationsResolver(arangoTemplate()); - } - } catch (final Exception e) { - throw new ArangoDBException(e); - } - return Optional.ofNullable(resolver); - } - }; + protected ResolverFactory resolverFactory() throws Exception { + return new DefaultResolverFactory(arangoTemplate()); } } diff --git a/src/main/java/com/arangodb/springframework/core/convert/resolver/DefaultResolverFactory.java b/src/main/java/com/arangodb/springframework/core/convert/resolver/DefaultResolverFactory.java new file mode 100644 index 000000000..4fc5747a6 --- /dev/null +++ b/src/main/java/com/arangodb/springframework/core/convert/resolver/DefaultResolverFactory.java @@ -0,0 +1,75 @@ +/* + * DISCLAIMER + * + * Copyright 2018 ArangoDB GmbH, Cologne, Germany + * + * 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. + * + * Copyright holder is ArangoDB GmbH, Cologne, Germany + */ + +package com.arangodb.springframework.core.convert.resolver; + +import java.lang.annotation.Annotation; +import java.util.Optional; + +import com.arangodb.springframework.annotation.From; +import com.arangodb.springframework.annotation.Ref; +import com.arangodb.springframework.annotation.Relations; +import com.arangodb.springframework.annotation.To; +import com.arangodb.springframework.core.ArangoOperations; + +/** + * + * @author Mark Vollmary + * @author Christian Lechner + */ +public class DefaultResolverFactory implements ResolverFactory { + + private final RefResolver refResolver; + private final RelationsResolver relationsResolver; + private final FromResolver fromResolver; + private final ToResolver toResolver; + + public DefaultResolverFactory(final ArangoOperations template) { + refResolver = new RefResolver(template); + relationsResolver = new RelationsResolver(template); + fromResolver = new FromResolver(template); + toResolver = new ToResolver(template); + } + + @Override + @SuppressWarnings("unchecked") + public Optional> getReferenceResolver(final A annotation) { + ReferenceResolver resolver = null; + if (Ref.class.equals(annotation)) { + resolver = (ReferenceResolver) refResolver; + } + return Optional.ofNullable(resolver); + } + + @SuppressWarnings("unchecked") + @Override + public Optional> getRelationResolver(final A annotation) { + RelationResolver resolver = null; + if (From.class.equals(annotation)) { + resolver = (RelationResolver) fromResolver; + } else if (To.class.equals(annotation)) { + resolver = (RelationResolver) toResolver; + } else if (Relations.class.equals(annotation)) { + resolver = (RelationResolver) relationsResolver; + } + return Optional.ofNullable(resolver); + } + +} diff --git a/src/main/java/com/arangodb/springframework/core/convert/resolver/ResolverFactory.java b/src/main/java/com/arangodb/springframework/core/convert/resolver/ResolverFactory.java index 2791a2b0d..df36ec9d9 100644 --- a/src/main/java/com/arangodb/springframework/core/convert/resolver/ResolverFactory.java +++ b/src/main/java/com/arangodb/springframework/core/convert/resolver/ResolverFactory.java @@ -1,36 +1,36 @@ -/* - * DISCLAIMER - * - * Copyright 2017 ArangoDB GmbH, Cologne, Germany - * - * 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. - * - * Copyright holder is ArangoDB GmbH, Cologne, Germany - */ - -package com.arangodb.springframework.core.convert.resolver; - -import java.lang.annotation.Annotation; -import java.util.Optional; - -/** - * @author Mark Vollmary - * - */ -public interface ResolverFactory { - - Optional> getReferenceResolver(A annotation); - - Optional> getRelationResolver(A annotation); - -} +/* + * DISCLAIMER + * + * Copyright 2017 ArangoDB GmbH, Cologne, Germany + * + * 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. + * + * Copyright holder is ArangoDB GmbH, Cologne, Germany + */ + +package com.arangodb.springframework.core.convert.resolver; + +import java.lang.annotation.Annotation; +import java.util.Optional; + +/** + * @author Mark Vollmary + * + */ +public interface ResolverFactory { + + Optional> getReferenceResolver(A annotation); + + Optional> getRelationResolver(A annotation); + +}