parent
8b2f31ad32
commit
d03eb7e824
@ -0,0 +1,58 @@
|
||||
package space.kklochko.jdbc_hospital_example.cli.commands.factories;
|
||||
|
||||
import lombok.NonNull;
|
||||
import space.kklochko.jdbc_hospital_example.cli.commands.CommandData;
|
||||
import space.kklochko.jdbc_hospital_example.cli.commands.CommandEntity;
|
||||
import space.kklochko.jdbc_hospital_example.db.repositories.AbstractRepository;
|
||||
import space.kklochko.jdbc_hospital_example.models.Department;
|
||||
import space.kklochko.jdbc_hospital_example.models.Indicator;
|
||||
import space.kklochko.jdbc_hospital_example.models.Patient;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CommandFactory {
|
||||
public <T> CommandEntity create(@NonNull CommandData commandData, @NonNull AbstractRepository<T> repository) {
|
||||
try {
|
||||
EntityFactory entityFactory = new EntityFactory();
|
||||
|
||||
if(commandData.getName().equals("insert")) {
|
||||
if(commandData.getDatatype().equals("indicator")) {
|
||||
Method method = repository.getClass().getMethod("create", Indicator.class);
|
||||
Indicator indicator = (Indicator) entityFactory.create(commandData);
|
||||
return new CommandEntity<Indicator, T>(repository, method, indicator);
|
||||
}else if(commandData.getDatatype().equals("patient")) {
|
||||
Method method = repository.getClass().getMethod("create", Patient.class);
|
||||
Patient patient = (Patient) entityFactory.create(commandData);
|
||||
return new CommandEntity<Patient, T>(repository, method, patient);
|
||||
}else {
|
||||
Method method = repository.getClass().getMethod("create", Department.class);
|
||||
Department department = (Department) entityFactory.create(commandData);
|
||||
return new CommandEntity<Department, T>(repository, method, department);
|
||||
}
|
||||
}else if(commandData.getName().equals("update")) {
|
||||
if(commandData.getDatatype().equals("indicator")) {
|
||||
Method method = repository.getClass().getMethod("update", Indicator.class);
|
||||
Indicator indicator = (Indicator) entityFactory.create(commandData);
|
||||
return new CommandEntity<Indicator, T>(repository, method, indicator);
|
||||
}else if(commandData.getDatatype().equals("patient")) {
|
||||
Method method = repository.getClass().getMethod("update", Patient.class);
|
||||
Patient patient = (Patient) entityFactory.create(commandData);
|
||||
return new CommandEntity<Patient, T>(repository, method, patient);
|
||||
}else {
|
||||
Method method = repository.getClass().getMethod("update", Department.class);
|
||||
Department department = (Department) entityFactory.create(commandData);
|
||||
return new CommandEntity<Department, T>(repository, method, department);
|
||||
}
|
||||
}else {
|
||||
String uuidString = commandData.arguments.get("uuid");
|
||||
UUID uuid = UUID.fromString(uuidString);
|
||||
|
||||
Method method = repository.getClass().getMethod("delete");
|
||||
return new CommandEntity<UUID, T>(repository, method, uuid);
|
||||
}
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package space.kklochko.jdbc_hospital_example.cli.commands.factories;
|
||||
|
||||
import lombok.NonNull;
|
||||
import space.kklochko.jdbc_hospital_example.cli.commands.CommandData;
|
||||
import space.kklochko.jdbc_hospital_example.models.Department;
|
||||
import space.kklochko.jdbc_hospital_example.models.Indicator;
|
||||
import space.kklochko.jdbc_hospital_example.models.Patient;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class EntityFactory {
|
||||
public Object create(@NonNull CommandData commandData) {
|
||||
if(commandData.getDatatype().equals("indicator")) {
|
||||
Map<String, String> values = commandData.getArguments();
|
||||
Timestamp timestamp = Timestamp.valueOf(values.get("timestamp"));
|
||||
Indicator indicator = new Indicator(values.get("type"), values.get("values"), timestamp);
|
||||
|
||||
String uuidString = values.getOrDefault("id", null);
|
||||
if(uuidString != null) {
|
||||
UUID uuid = UUID.fromString(uuidString);
|
||||
indicator.setId(uuid);
|
||||
}
|
||||
|
||||
return (Object) indicator;
|
||||
}else if(commandData.getDatatype().equals("patient")) {
|
||||
Map<String, String> values = commandData.getArguments();
|
||||
short age = Short.parseShort(values.get("age"));
|
||||
Patient patient = new Patient(values.get("name"), age, values.get("phone"));
|
||||
|
||||
String uuidString = values.getOrDefault("id", null);
|
||||
if(uuidString != null) {
|
||||
UUID uuid = UUID.fromString(uuidString);
|
||||
patient.setId(uuid);
|
||||
}
|
||||
|
||||
return (Object) patient;
|
||||
}else {
|
||||
Map<String, String> values = commandData.getArguments();
|
||||
Department department = new Department(values.get("name"), values.get("location"), values.get("phone"));
|
||||
|
||||
String uuidString = values.getOrDefault("id", null);
|
||||
if(uuidString != null) {
|
||||
UUID uuid = UUID.fromString(uuidString);
|
||||
department.setId(uuid);
|
||||
}
|
||||
|
||||
return (Object) department;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package space.kklochko.jdbc_hospital_example.cli.commands.factories;
|
||||
|
||||
import lombok.NonNull;
|
||||
import space.kklochko.jdbc_hospital_example.cli.commands.CommandData;
|
||||
import space.kklochko.jdbc_hospital_example.cli.commands.CommandEntities;
|
||||
import space.kklochko.jdbc_hospital_example.db.repositories.AbstractRepository;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class ReadAllFactory {
|
||||
public <T> CommandEntities<T> create(@NonNull CommandData commandData, @NonNull AbstractRepository<T> repository) {
|
||||
try {
|
||||
Method method = repository.getClass().getMethod("readAll");
|
||||
return new CommandEntities<T>(repository, method);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue