Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package solutions.bellatrix.core.configuration;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
Expand Down Expand Up @@ -57,7 +58,11 @@ public static <T> T get(Class<T> configSection) {
String jsonFileContent = getFileAsString(fileName);
String sectionName = getSectionName(configSection);

var jsonObject = JsonParser.parseString(jsonFileContent).getAsJsonObject().get(sectionName).toString();
JsonElement sectionFound = JsonParser.parseString(jsonFileContent).getAsJsonObject().get(sectionName);
if (sectionFound == null) {
return mappedObject;
}
var jsonObject = sectionFound.toString();

var gson = new Gson();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package solutions.bellatrix.data.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation to mark fields that require automatic dependency creation.
* When a field is marked with this annotation and its value is null,
* the system will automatically create the dependent entity using
* the registered factory and repository.
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Dependency {

/**
* The entity class that should be created as a dependency.
* This class must have a registered factory and repository.
*/
Class<?> entityType();

/**
* Optional: Custom factory method name to use for creating the dependency.
* If not specified, the default factory method will be used.
*/
String factoryMethod() default "buildDefault";

/**
* Optional: Whether to create the dependency even if the field is not null.
* Default is false (only create if field is null).
*/
boolean forceCreate() default false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package solutions.bellatrix.data.configuration;

import solutions.bellatrix.core.utilities.SingletonFactory;
import solutions.bellatrix.data.http.contracts.EntityFactory;
import solutions.bellatrix.data.http.infrastructure.Entity;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public enum FactoryProvider {
INSTANCE;

private final Map<Class<? extends Entity>, Class<? extends EntityFactory>> factories = new ConcurrentHashMap<>();

public <T extends Entity> void register(Class<T> entityClass, Class<? extends EntityFactory<T>> factoryClass) {
factories.put(entityClass, factoryClass);
}

public <T extends Entity> EntityFactory<T> get(Class<T> entityClass) {
var factoryClassType = factories.get(entityClass);

if (Objects.isNull(factoryClassType)) {
throw new IllegalArgumentException("No factory registered for entity class: " + entityClass.getName());
}

return (EntityFactory<T>)SingletonFactory.getInstance(factoryClassType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public enum RepositoryFactory {
public enum RepositoryProvider {
INSTANCE;

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

public <T extends Entity> void registerRepository(Class<T> entityClass, Class<? extends Repository<T>> repositoryClass) {
public <T extends Entity> void register(Class<T> entityClass, Class<? extends Repository<T>> repositoryClass) {
repositories.put(entityClass, repositoryClass);
}

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

if (Objects.isNull(repositoryClassType)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package solutions.bellatrix.data.http.contracts;

import solutions.bellatrix.data.http.infrastructure.DependencyResolver;
import solutions.bellatrix.data.http.infrastructure.Entity;

public interface EntityFactory<T extends Entity> {

T buildDefault();

default T buildDefaultWithDependencies() {
T entity = buildDefault();
return DependencyResolver.buildDependencies(entity);
}
}
Loading