-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #518 from eclipse/NOSQL-516
Enable Custom Repository at Eclipse JNoSQL
- Loading branch information
Showing
33 changed files
with
1,778 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...umn/src/main/java/org/eclipse/jnosql/mapping/column/query/CustomRepositoryColumnBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* 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.column.query; | ||
|
||
import jakarta.enterprise.context.spi.CreationalContext; | ||
import org.eclipse.jnosql.mapping.DatabaseQualifier; | ||
import org.eclipse.jnosql.mapping.DatabaseType; | ||
import org.eclipse.jnosql.mapping.column.ColumnTemplate; | ||
import org.eclipse.jnosql.mapping.core.Converters; | ||
import org.eclipse.jnosql.mapping.core.spi.AbstractBean; | ||
import org.eclipse.jnosql.mapping.core.util.AnnotationLiteralUtil; | ||
import org.eclipse.jnosql.mapping.metadata.EntitiesMetadata; | ||
import org.eclipse.jnosql.mapping.semistructured.query.CustomRepositoryHandler; | ||
|
||
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; | ||
|
||
|
||
/** | ||
* This class serves as a JNoSQL discovery bean for CDI extension, responsible for registering Custom Repository instances. | ||
* It extends {@link AbstractBean} and is parameterized with type {@code T} representing the repository type. | ||
* <p> | ||
* Upon instantiation, it initializes with the provided repository type, provider name, and qualifiers. | ||
* The provider name specifies the database provider for the repository. | ||
* </p> | ||
* | ||
* @param <T> the type of the repository | ||
* @see AbstractBean | ||
*/ | ||
public class CustomRepositoryColumnBean<T> 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 CustomRepositoryColumnBean(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("unchecked") | ||
@Override | ||
public T create(CreationalContext<T> context) { | ||
var entities = getInstance(EntitiesMetadata.class); | ||
var template = provider.isEmpty() ? getInstance(ColumnTemplate.class) : | ||
getInstance(ColumnTemplate.class, DatabaseQualifier.ofColumn(provider)); | ||
|
||
var converters = getInstance(Converters.class); | ||
|
||
var handler = CustomRepositoryHandler.builder() | ||
.entitiesMetadata(entities) | ||
.template(template) | ||
.customRepositoryType(type) | ||
.converters(converters) | ||
.build(); | ||
|
||
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...nosql-mapping-column/src/test/java/org/eclipse/jnosql/mapping/column/entities/People.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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.column.entities; | ||
|
||
import jakarta.data.repository.Insert; | ||
import jakarta.data.repository.Repository; | ||
|
||
|
||
@Repository | ||
public interface People { | ||
|
||
@Insert | ||
Person insert(Person person); | ||
} |
87 changes: 87 additions & 0 deletions
87
...column/src/test/java/org/eclipse/jnosql/mapping/column/spi/ColumnCustomExtensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 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.column.spi; | ||
|
||
import jakarta.inject.Inject; | ||
import org.assertj.core.api.SoftAssertions; | ||
import org.eclipse.jnosql.mapping.Database; | ||
import org.eclipse.jnosql.mapping.DatabaseType; | ||
import org.eclipse.jnosql.mapping.column.ColumnTemplate; | ||
import org.eclipse.jnosql.mapping.column.MockProducer; | ||
import org.eclipse.jnosql.mapping.column.entities.People; | ||
import org.eclipse.jnosql.mapping.column.entities.Person; | ||
import org.eclipse.jnosql.mapping.core.Converters; | ||
import org.eclipse.jnosql.mapping.core.spi.EntityMetadataExtension; | ||
import org.eclipse.jnosql.mapping.reflection.Reflections; | ||
import org.eclipse.jnosql.mapping.semistructured.EntityConverter; | ||
import org.jboss.weld.junit5.auto.AddExtensions; | ||
import org.jboss.weld.junit5.auto.AddPackages; | ||
import org.jboss.weld.junit5.auto.EnableAutoWeld; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
|
||
@EnableAutoWeld | ||
@AddPackages(value = {Converters.class, EntityConverter.class, ColumnTemplate.class}) | ||
@AddPackages(MockProducer.class) | ||
@AddPackages(Reflections.class) | ||
@AddExtensions({EntityMetadataExtension.class, ColumnExtension.class}) | ||
class ColumnCustomExtensionTest { | ||
|
||
@Inject | ||
@Database(value = DatabaseType.COLUMN) | ||
private People people; | ||
|
||
@Inject | ||
@Database(value = DatabaseType.COLUMN, provider = "columnRepositoryMock") | ||
private People pepoleMock; | ||
|
||
@Inject | ||
private People repository; | ||
|
||
|
||
@Test | ||
void shouldInitiate() { | ||
assertNotNull(people); | ||
Person person = people.insert(Person.builder().build()); | ||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(person).isNotNull(); | ||
soft.assertThat(person.getName()).isEqualTo("Default"); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldUseMock(){ | ||
assertNotNull(pepoleMock); | ||
|
||
Person person = pepoleMock.insert(Person.builder().build()); | ||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(person).isNotNull(); | ||
soft.assertThat(person.getName()).isEqualTo("columnRepositoryMock"); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldUseDefault(){ | ||
assertNotNull(repository); | ||
|
||
Person person = repository.insert(Person.builder().build()); | ||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(person).isNotNull(); | ||
soft.assertThat(person.getName()).isEqualTo("Default"); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.