Skip to content

Commit cab16ed

Browse files
committed
#80 extract default resolver factory to allow bean dependency, pass query bridge if available
1 parent df59392 commit cab16ed

File tree

2 files changed

+68
-59
lines changed

2 files changed

+68
-59
lines changed

src/main/java/com/arangodb/springframework/config/ArangoConfiguration.java

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
*/
44
package com.arangodb.springframework.config;
55

6-
import java.lang.annotation.Annotation;
76
import java.util.Collection;
87
import java.util.Collections;
9-
import java.util.Optional;
108
import java.util.Set;
119

1210
import org.springframework.context.annotation.Bean;
@@ -16,27 +14,13 @@
1614
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
1715

1816
import com.arangodb.ArangoDB;
19-
import com.arangodb.ArangoDBException;
20-
import com.arangodb.springframework.annotation.Document;
21-
import com.arangodb.springframework.annotation.Edge;
22-
import com.arangodb.springframework.annotation.From;
23-
import com.arangodb.springframework.annotation.Ref;
24-
import com.arangodb.springframework.annotation.Relations;
25-
import com.arangodb.springframework.annotation.To;
2617
import com.arangodb.springframework.core.ArangoOperations;
2718
import com.arangodb.springframework.core.convert.ArangoConverter;
2819
import com.arangodb.springframework.core.convert.ArangoCustomConversions;
2920
import com.arangodb.springframework.core.convert.ArangoTypeMapper;
3021
import com.arangodb.springframework.core.convert.DefaultArangoConverter;
3122
import com.arangodb.springframework.core.convert.DefaultArangoTypeMapper;
32-
import com.arangodb.springframework.core.convert.resolver.DocumentFromResolver;
33-
import com.arangodb.springframework.core.convert.resolver.DocumentToResolver;
34-
import com.arangodb.springframework.core.convert.resolver.EdgeFromResolver;
35-
import com.arangodb.springframework.core.convert.resolver.EdgeToResolver;
36-
import com.arangodb.springframework.core.convert.resolver.RefResolver;
37-
import com.arangodb.springframework.core.convert.resolver.ReferenceResolver;
38-
import com.arangodb.springframework.core.convert.resolver.RelationResolver;
39-
import com.arangodb.springframework.core.convert.resolver.RelationsResolver;
23+
import com.arangodb.springframework.core.convert.resolver.DefaultResolverFactory;
4024
import com.arangodb.springframework.core.convert.resolver.ResolverFactory;
4125
import com.arangodb.springframework.core.mapping.ArangoMappingContext;
4226
import com.arangodb.springframework.core.template.ArangoTemplate;
@@ -102,49 +86,11 @@ default ArangoTypeMapper arangoTypeMapper() throws Exception {
10286
return new DefaultArangoTypeMapper(typeKey(), arangoMappingContext());
10387
}
10488

89+
@Bean
10590
default ResolverFactory resolverFactory() {
106-
return new ResolverFactory() {
107-
@SuppressWarnings("unchecked")
108-
@Override
109-
public <A extends Annotation> Optional<ReferenceResolver<A>> getReferenceResolver(final A annotation) {
110-
ReferenceResolver<A> resolver = null;
111-
try {
112-
if (annotation instanceof Ref) {
113-
resolver = (ReferenceResolver<A>) new RefResolver(arangoTemplate(), null);
114-
}
115-
} catch (final Exception e) {
116-
throw new ArangoDBException(e);
117-
}
118-
return Optional.ofNullable(resolver);
119-
}
120-
121-
@SuppressWarnings("unchecked")
122-
@Override
123-
public <A extends Annotation> Optional<RelationResolver<A>> getRelationResolver(final A annotation,
124-
final Class<? extends Annotation> collectionType) {
125-
RelationResolver<A> resolver = null;
126-
try {
127-
if (annotation instanceof From) {
128-
if (collectionType == Edge.class) {
129-
resolver = (RelationResolver<A>) new EdgeFromResolver(arangoTemplate(), null);
130-
} else if (collectionType == Document.class) {
131-
resolver = (RelationResolver<A>) new DocumentFromResolver(arangoTemplate(), null);
132-
}
133-
} else if (annotation instanceof To) {
134-
if (collectionType == Edge.class) {
135-
resolver = (RelationResolver<A>) new EdgeToResolver(arangoTemplate(), null);
136-
} else if (collectionType == Document.class) {
137-
resolver = (RelationResolver<A>) new DocumentToResolver(arangoTemplate(), null);
138-
}
139-
} else if (annotation instanceof Relations) {
140-
resolver = (RelationResolver<A>) new RelationsResolver(arangoTemplate(), null);
141-
}
142-
} catch (final Exception e) {
143-
throw new ArangoDBException(e);
144-
}
145-
return Optional.ofNullable(resolver);
146-
}
147-
};
91+
return RESOLVER_FACTORY_INSTANCE;
14892
}
14993

94+
ResolverFactory RESOLVER_FACTORY_INSTANCE = new DefaultResolverFactory();
95+
15096
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.arangodb.springframework.core.convert.resolver;
2+
3+
import java.lang.annotation.Annotation;
4+
import java.util.Optional;
5+
6+
import org.springframework.beans.BeansException;
7+
import org.springframework.beans.factory.ObjectProvider;
8+
import org.springframework.context.ApplicationContext;
9+
import org.springframework.context.ApplicationContextAware;
10+
11+
import com.arangodb.springframework.annotation.Document;
12+
import com.arangodb.springframework.annotation.Edge;
13+
import com.arangodb.springframework.annotation.From;
14+
import com.arangodb.springframework.annotation.Ref;
15+
import com.arangodb.springframework.annotation.Relations;
16+
import com.arangodb.springframework.annotation.To;
17+
import com.arangodb.springframework.core.ArangoOperations;
18+
import com.arangodb.springframework.repository.query.QueryTransactionBridge;
19+
20+
public class DefaultResolverFactory implements ResolverFactory, ApplicationContextAware {
21+
22+
private ObjectProvider<ArangoOperations> template;
23+
private ObjectProvider<QueryTransactionBridge> transactionBridge;
24+
25+
@SuppressWarnings("unchecked")
26+
@Override
27+
public <A extends Annotation> Optional<ReferenceResolver<A>> getReferenceResolver(final A annotation) {
28+
ReferenceResolver<A> resolver = null;
29+
if (annotation instanceof Ref) {
30+
resolver = (ReferenceResolver<A>) new RefResolver(template.getObject(), transactionBridge.getIfUnique());
31+
}
32+
return Optional.ofNullable(resolver);
33+
}
34+
35+
@SuppressWarnings("unchecked")
36+
@Override
37+
public <A extends Annotation> Optional<RelationResolver<A>> getRelationResolver(final A annotation,
38+
final Class<? extends Annotation> collectionType) {
39+
RelationResolver<A> resolver = null;
40+
if (annotation instanceof From) {
41+
if (collectionType == Edge.class) {
42+
resolver = (RelationResolver<A>) new EdgeFromResolver(template.getObject(), transactionBridge.getIfUnique());
43+
} else if (collectionType == Document.class) {
44+
resolver = (RelationResolver<A>) new DocumentFromResolver(template.getObject(), transactionBridge.getIfUnique());
45+
}
46+
} else if (annotation instanceof To) {
47+
if (collectionType == Edge.class) {
48+
resolver = (RelationResolver<A>) new EdgeToResolver(template.getObject(), transactionBridge.getIfUnique());
49+
} else if (collectionType == Document.class) {
50+
resolver = (RelationResolver<A>) new DocumentToResolver(template.getObject(), transactionBridge.getIfUnique());
51+
}
52+
} else if (annotation instanceof Relations) {
53+
resolver = (RelationResolver<A>) new RelationsResolver(template.getObject(), transactionBridge.getIfUnique());
54+
}
55+
return Optional.ofNullable(resolver);
56+
}
57+
58+
@Override
59+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
60+
template = applicationContext.getBeanProvider(ArangoOperations.class);
61+
transactionBridge = applicationContext.getBeanProvider(QueryTransactionBridge.class);
62+
}
63+
}

0 commit comments

Comments
 (0)