Skip to content

Commit 01697fb

Browse files
Merge pull request #84 from AutomateThePlanet/data-module-fixes
Add Dependency resolver into the data module
2 parents d519682 + 7068c0b commit 01697fb

File tree

18 files changed

+1056
-45
lines changed

18 files changed

+1056
-45
lines changed

bellatrix.core/src/main/java/solutions/bellatrix/core/configuration/ConfigurationService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package solutions.bellatrix.core.configuration;
1515

1616
import com.google.gson.Gson;
17+
import com.google.gson.JsonElement;
1718
import com.google.gson.JsonParser;
1819
import lombok.SneakyThrows;
1920
import lombok.experimental.UtilityClass;
@@ -57,7 +58,11 @@ public static <T> T get(Class<T> configSection) {
5758
String jsonFileContent = getFileAsString(fileName);
5859
String sectionName = getSectionName(configSection);
5960

60-
var jsonObject = JsonParser.parseString(jsonFileContent).getAsJsonObject().get(sectionName).toString();
61+
JsonElement sectionFound = JsonParser.parseString(jsonFileContent).getAsJsonObject().get(sectionName);
62+
if (sectionFound == null) {
63+
return mappedObject;
64+
}
65+
var jsonObject = sectionFound.toString();
6166

6267
var gson = new Gson();
6368

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package solutions.bellatrix.data.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Annotation to mark fields that require automatic dependency creation.
10+
* When a field is marked with this annotation and its value is null,
11+
* the system will automatically create the dependent entity using
12+
* the registered factory and repository.
13+
*/
14+
@Target(ElementType.FIELD)
15+
@Retention(RetentionPolicy.RUNTIME)
16+
public @interface Dependency {
17+
18+
/**
19+
* The entity class that should be created as a dependency.
20+
* This class must have a registered factory and repository.
21+
*/
22+
Class<?> entityType();
23+
24+
/**
25+
* Optional: Custom factory method name to use for creating the dependency.
26+
* If not specified, the default factory method will be used.
27+
*/
28+
String factoryMethod() default "buildDefault";
29+
30+
/**
31+
* Optional: Whether to create the dependency even if the field is not null.
32+
* Default is false (only create if field is null).
33+
*/
34+
boolean forceCreate() default false;
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package solutions.bellatrix.data.configuration;
2+
3+
import solutions.bellatrix.core.utilities.SingletonFactory;
4+
import solutions.bellatrix.data.http.contracts.EntityFactory;
5+
import solutions.bellatrix.data.http.infrastructure.Entity;
6+
7+
import java.util.Map;
8+
import java.util.Objects;
9+
import java.util.concurrent.ConcurrentHashMap;
10+
11+
public enum FactoryProvider {
12+
INSTANCE;
13+
14+
private final Map<Class<? extends Entity>, Class<? extends EntityFactory>> factories = new ConcurrentHashMap<>();
15+
16+
public <T extends Entity> void register(Class<T> entityClass, Class<? extends EntityFactory<T>> factoryClass) {
17+
factories.put(entityClass, factoryClass);
18+
}
19+
20+
public <T extends Entity> EntityFactory<T> get(Class<T> entityClass) {
21+
var factoryClassType = factories.get(entityClass);
22+
23+
if (Objects.isNull(factoryClassType)) {
24+
throw new IllegalArgumentException("No factory registered for entity class: " + entityClass.getName());
25+
}
26+
27+
return (EntityFactory<T>)SingletonFactory.getInstance(factoryClassType);
28+
}
29+
}

bellatrix.data/src/main/java/solutions/bellatrix/data/configuration/RepositoryFactory.java renamed to bellatrix.data/src/main/java/solutions/bellatrix/data/configuration/RepositoryProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
import java.util.Objects;
99
import java.util.concurrent.ConcurrentHashMap;
1010

11-
public enum RepositoryFactory {
11+
public enum RepositoryProvider {
1212
INSTANCE;
1313

1414
private final Map<Class<? extends Entity>, Class<? extends Repository>> repositories = new ConcurrentHashMap<>();
1515

16-
public <T extends Entity> void registerRepository(Class<T> entityClass, Class<? extends Repository<T>> repositoryClass) {
16+
public <T extends Entity> void register(Class<T> entityClass, Class<? extends Repository<T>> repositoryClass) {
1717
repositories.put(entityClass, repositoryClass);
1818
}
1919

20-
public <T extends Entity> Repository<T> getRepository(Class<T> entityClass) {
20+
public <T extends Entity> Repository<T> get(Class<T> entityClass) {
2121
var repositoryClassType = repositories.get(entityClass);
2222

2323
if (Objects.isNull(repositoryClassType)) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solutions.bellatrix.data.http.contracts;
2+
3+
import solutions.bellatrix.data.http.infrastructure.DependencyResolver;
4+
import solutions.bellatrix.data.http.infrastructure.Entity;
5+
6+
public interface EntityFactory<T extends Entity> {
7+
8+
T buildDefault();
9+
10+
default T buildDefaultWithDependencies() {
11+
T entity = buildDefault();
12+
return DependencyResolver.buildDependencies(entity);
13+
}
14+
}

0 commit comments

Comments
 (0)