/
RepositoryGraphBean.java
110 lines (94 loc) · 3.73 KB
/
RepositoryGraphBean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright (c) 2017 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.artemis.graph.query;
import jakarta.nosql.mapping.Converters;
import jakarta.nosql.mapping.DatabaseType;
import jakarta.nosql.mapping.Repository;
import jakarta.nosql.mapping.reflection.ClassMappings;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.eclipse.jnosql.artemis.graph.GraphTemplate;
import org.eclipse.jnosql.artemis.DatabaseQualifier;
import org.eclipse.jnosql.artemis.graph.GraphConverter;
import org.eclipse.jnosql.artemis.spi.AbstractBean;
import org.eclipse.jnosql.artemis.util.AnnotationLiteralUtil;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.BeanManager;
import java.lang.annotation.Annotation;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* Artemis discoveryBean to CDI extension to register {@link Repository}
*/
public class RepositoryGraphBean extends AbstractBean<Repository>{
private final Class type;
private final Set<Type> types;
private final String provider;
private final Set<Annotation> qualifiers;
/**
* Constructor
*
* @param type the tye
* @param beanManager the beanManager
* @param provider the provider name, that must be a
*/
public RepositoryGraphBean(Class type, BeanManager beanManager, String provider) {
super(beanManager);
this.type = type;
this.types = Collections.singleton(type);
this.provider = provider;
if (provider.isEmpty()) {
this.qualifiers = new HashSet<>();
qualifiers.add(DatabaseQualifier.ofGraph());
qualifiers.add(AnnotationLiteralUtil.DEFAULT_ANNOTATION);
qualifiers.add(AnnotationLiteralUtil.ANY_ANNOTATION);
} else {
this.qualifiers = Collections.singleton(DatabaseQualifier.ofGraph(provider));
}
}
@Override
public Class<?> getBeanClass() {
return type;
}
@Override
public Repository create(CreationalContext<Repository> creationalContext) {
ClassMappings classMappings = getInstance(ClassMappings.class);
GraphTemplate repository = provider.isEmpty() ? getInstance(GraphTemplate.class) :
getInstance(GraphTemplate.class, DatabaseQualifier.ofGraph(provider));
GraphConverter converter = getInstance(GraphConverter.class);
Graph graph = provider.isEmpty() ? getInstance(Graph.class) :
getInstance(Graph.class, DatabaseQualifier.ofGraph(provider));
Converters converters = getInstance(Converters.class);
GraphRepositoryProxy handler = new GraphRepositoryProxy(repository,
classMappings, type, graph, converter, converters);
return (Repository) Proxy.newProxyInstance(type.getClassLoader(),
new Class[]{type},
handler);
}
@Override
public Set<Type> getTypes() {
return types;
}
@Override
public Set<Annotation> getQualifiers() {
return qualifiers;
}
@Override
public String getId() {
return type.getName() + '@' + DatabaseType.GRAPH + "-" + provider;
}
}