/
RepositoryColumnBean.java
103 lines (88 loc) · 3.44 KB
/
RepositoryColumnBean.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
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* 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.mapping.semistructured.query;
import jakarta.data.repository.DataRepository;
import jakarta.enterprise.context.spi.CreationalContext;
import org.eclipse.jnosql.mapping.core.Converters;
import org.eclipse.jnosql.mapping.DatabaseQualifier;
import org.eclipse.jnosql.mapping.DatabaseType;
import org.eclipse.jnosql.mapping.semistructured.SemistructuredTemplate;
import org.eclipse.jnosql.mapping.metadata.EntitiesMetadata;
import org.eclipse.jnosql.mapping.core.spi.AbstractBean;
import org.eclipse.jnosql.mapping.core.util.AnnotationLiteralUtil;
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;
/**
* JNoSQL discoveryBean to CDI extension to register Repository
*/
public class RepositoryColumnBean<T extends DataRepository<?, ?>> extends AbstractBean<T> {
private final Class<T> type;
private final Set<Type> types;
private final String provider;
private final Set<Annotation> qualifiers;
/**
* Constructor
*
* @param type the tye
* @param provider the provider name, that must be a
*/
@SuppressWarnings("unchecked")
public RepositoryColumnBean(Class<?> type, String provider) {
this.type = (Class<T>) type;
this.types = Collections.singleton(type);
this.provider = provider;
if (provider.isEmpty()) {
this.qualifiers = new HashSet<>();
qualifiers.add(DatabaseQualifier.ofColumn());
qualifiers.add(AnnotationLiteralUtil.DEFAULT_ANNOTATION);
qualifiers.add(AnnotationLiteralUtil.ANY_ANNOTATION);
} else {
this.qualifiers = Collections.singleton(DatabaseQualifier.ofColumn(provider));
}
}
@Override
public Class<?> getBeanClass() {
return type;
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public T create(CreationalContext<T> creationalContext) {
EntitiesMetadata entities = getInstance(EntitiesMetadata.class);
SemistructuredTemplate template = provider.isEmpty() ? getInstance(SemistructuredTemplate.class) :
getInstance(SemistructuredTemplate.class, DatabaseQualifier.ofColumn(provider));
Converters converters = getInstance(Converters.class);
ColumnRepositoryProxy handler = new ColumnRepositoryProxy<>(template,
entities, type, converters);
return (T) 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.COLUMN + "-" + provider;
}
}