Remove the CLI.
This commit is contained in:
@@ -1,136 +0,0 @@
|
||||
package space.kklochko.jpa_hospital_example.cli;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
import space.kklochko.jpa_hospital_example.cli.commands.CommandData;
|
||||
import space.kklochko.jpa_hospital_example.cli.commands.CommandEntities;
|
||||
import space.kklochko.jpa_hospital_example.cli.commands.CommandEntity;
|
||||
import space.kklochko.jpa_hospital_example.cli.commands.factories.CommandFactory;
|
||||
import space.kklochko.jpa_hospital_example.cli.commands.factories.ReadAllFactory;
|
||||
import space.kklochko.jpa_hospital_example.cli.parsers.CommandParser;
|
||||
import space.kklochko.jpa_hospital_example.cli.validators.CommandValidator;
|
||||
import space.kklochko.jpa_hospital_example.cli.validators.InputStringFormatValidator;
|
||||
import space.kklochko.jpa_hospital_example.cli.validators.Validator;
|
||||
import space.kklochko.jpa_hospital_example.db.factories.DataBaseConnection;
|
||||
import space.kklochko.jpa_hospital_example.db.repositories.AbstractRepository;
|
||||
import space.kklochko.jpa_hospital_example.db.repositories.DepartmentRepository;
|
||||
import space.kklochko.jpa_hospital_example.db.repositories.IndicatorRepository;
|
||||
import space.kklochko.jpa_hospital_example.db.repositories.PatientRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.Scanner;
|
||||
@Setter
|
||||
@Getter
|
||||
public class CLI {
|
||||
private final String prompt = "> ";
|
||||
private String state = "prompt";
|
||||
private final String help = "Help (help or h)\n" +
|
||||
"Command format: `command {item}(arg1=value1, ...`\n\n" +
|
||||
|
||||
"Commands: \n" +
|
||||
"insert {item}({args}): \n" +
|
||||
"update {item}({args}): \n" +
|
||||
"readAll {item}: \n" +
|
||||
"delete {item}(uuid='07c6e39e-727a-11ee-8e7b-c0e4349366ab'}).\n" +
|
||||
|
||||
"Examples:\n" +
|
||||
|
||||
"\nDepartment:\n" +
|
||||
"\tinsert department(id='bcbbcdb4-702c-11ee-9113-c0e4349366cb', name='First department', location='Stepan Bandera Street', phone='380123451244')\n" +
|
||||
"\tinsert department(name='First department', location='Stepan Bandera Street', phone='380123451244')\n" +
|
||||
"\tupdate department(id='bcbbcdb4-702c-11ee-9113-c0e4349366cb', name='First department', location='Stepan Bandera Street', phone='380123451244')\n" +
|
||||
"\treadAll department()\n" +
|
||||
"\tremove department(id='bcbbcdb4-702c-11ee-9113-c0e4349366cb')\n" +
|
||||
|
||||
"\nPatient:\n" +
|
||||
"\tinsert patient(id='bcbbcdb4-702c-11ee-9113-c0e4349366bb', name='Oleh', age='21', phone='380123451234')\n" +
|
||||
"\tinsert patient(name='Oleh', age='21', phone='380123451234')\n" +
|
||||
"\tupdate patient(id='bcbbcdb4-702c-11ee-9113-c0e4349366bb', name='Oleh', age='21', phone='380123451234')\n" +
|
||||
"\treadAll patient()\n" +
|
||||
"\tremove patient(id='bcbbcdb4-702c-11ee-9113-c0e4349366bb')\n" +
|
||||
|
||||
"\nIndicators:\n" +
|
||||
"\tinsert indicator(id='07c6e39e-727a-11ee-8e7b-c0e4349366ab', type='blood245', values='well maybe', timestamp='2023-10-17 14:30:00')\n" +
|
||||
"\tinsert indicator(type='blood246', values='well maybe', timestamp='2023-10-17 14:30:00')\n" +
|
||||
"\tupdate indicator(id='07c6e39e-727a-11ee-8e7b-c0e4349366ab', type='blood247', values='well maybe', timestamp='2023-10-17 14:30:00')\n" +
|
||||
"\treadAll indicator()\n" +
|
||||
"\tremove indicator(id='07c6e39e-727a-11ee-8e7b-c0e4349366ab')\n";
|
||||
|
||||
EntityManager manager;
|
||||
|
||||
public CLI(EntityManager manager) {
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Scanner reader = new Scanner(System.in);
|
||||
String line = "";
|
||||
CommandData commandData;
|
||||
|
||||
while(true) {
|
||||
if(getState() == "prompt") {
|
||||
System.out.print(prompt);
|
||||
setState("waitInput");
|
||||
}else if(getState() == "waitInput") {
|
||||
line = reader.nextLine();
|
||||
setState("isHelp?");
|
||||
}else if(getState() == "isHelp?") {
|
||||
if(line.equals("h") || line.equals("help")) {
|
||||
System.out.println(help);
|
||||
setState("prompt");
|
||||
}else {
|
||||
setState("validate");
|
||||
}
|
||||
} else if(getState() == "validate") {
|
||||
ArrayList<Validator> validators = new ArrayList<>();
|
||||
validators.add(new CommandValidator(line));
|
||||
validators.add(new InputStringFormatValidator(line));
|
||||
|
||||
setState("run");
|
||||
|
||||
for (Validator validator : validators) {
|
||||
if(!validator.isValid()) {
|
||||
System.err.println(validator.getMessage());
|
||||
System.out.println();
|
||||
setState("prompt");
|
||||
}
|
||||
}
|
||||
} else if(getState() == "run") {
|
||||
CommandParser commandParser = new CommandParser();
|
||||
commandData = commandParser.parse(line);
|
||||
|
||||
runCommand(commandData);
|
||||
setState("prompt");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void runCommand(CommandData commandData) {
|
||||
if(commandData.getName().equals("readAll")) {
|
||||
ReadAllFactory factory = new ReadAllFactory();
|
||||
CommandEntities command = factory.create(commandData, getRepository(commandData));
|
||||
ArrayList items = command.run();
|
||||
|
||||
for(Iterator iter = command.run().iterator(); iter.hasNext(); ) {
|
||||
System.out.println(iter.next());
|
||||
}
|
||||
} else {
|
||||
CommandFactory factory = new CommandFactory();
|
||||
CommandEntity command = factory.create(commandData, getRepository(commandData));
|
||||
command.run();
|
||||
}
|
||||
}
|
||||
|
||||
public AbstractRepository getRepository(@NonNull CommandData commandData) {
|
||||
if(commandData.getDatatype().equals("indicator")) {
|
||||
return new IndicatorRepository(manager);
|
||||
}else if(commandData.getDatatype().equals("patient")) {
|
||||
return new PatientRepository(manager);
|
||||
}else {
|
||||
return new DepartmentRepository(manager);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package space.kklochko.jpa_hospital_example.cli.commands;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class CommandData {
|
||||
public String name;
|
||||
public String datatype;
|
||||
public Map<String, String> arguments;
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package space.kklochko.jpa_hospital_example.cli.commands;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import space.kklochko.jpa_hospital_example.db.repositories.AbstractRepository;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class CommandEntities<T> {
|
||||
private AbstractRepository<T> repository;
|
||||
private Method method;
|
||||
|
||||
public ArrayList<T> run() {
|
||||
try {
|
||||
return (ArrayList<T>) method.invoke(getRepository());
|
||||
} catch (InvocationTargetException e) {
|
||||
System.err.println(e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
System.err.println(e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package space.kklochko.jpa_hospital_example.cli.commands;
|
||||
|
||||
import lombok.Data;
|
||||
import space.kklochko.jpa_hospital_example.db.repositories.AbstractRepository;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@Data
|
||||
public class CommandEntity <A, T> {
|
||||
private AbstractRepository<T> repository;
|
||||
private Method method;
|
||||
private A argument;
|
||||
|
||||
public CommandEntity(AbstractRepository<T> repository, Method method, A argument) {
|
||||
this.repository = repository;
|
||||
this.method = method;
|
||||
this.argument = argument;
|
||||
}
|
||||
|
||||
public boolean run() {
|
||||
try {
|
||||
return (Boolean) method.invoke(getRepository(), getArgument());
|
||||
} catch (InvocationTargetException e) {
|
||||
System.err.println(e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
System.err.println(e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
-58
@@ -1,58 +0,0 @@
|
||||
package space.kklochko.jpa_hospital_example.cli.commands.factories;
|
||||
|
||||
import lombok.NonNull;
|
||||
import space.kklochko.jpa_hospital_example.cli.commands.CommandData;
|
||||
import space.kklochko.jpa_hospital_example.cli.commands.CommandEntity;
|
||||
import space.kklochko.jpa_hospital_example.db.repositories.AbstractRepository;
|
||||
import space.kklochko.jpa_hospital_example.models.Department;
|
||||
import space.kklochko.jpa_hospital_example.models.Indicator;
|
||||
import space.kklochko.jpa_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("id");
|
||||
UUID uuid = UUID.fromString(uuidString);
|
||||
|
||||
Method method = repository.getClass().getMethod("delete", UUID.class);
|
||||
return new CommandEntity<UUID, T>(repository, method, uuid);
|
||||
}
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
-52
@@ -1,52 +0,0 @@
|
||||
package space.kklochko.jpa_hospital_example.cli.commands.factories;
|
||||
|
||||
import lombok.NonNull;
|
||||
import space.kklochko.jpa_hospital_example.cli.commands.CommandData;
|
||||
import space.kklochko.jpa_hospital_example.models.Department;
|
||||
import space.kklochko.jpa_hospital_example.models.Indicator;
|
||||
import space.kklochko.jpa_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
package space.kklochko.jpa_hospital_example.cli.commands.factories;
|
||||
|
||||
import lombok.NonNull;
|
||||
import space.kklochko.jpa_hospital_example.cli.commands.CommandData;
|
||||
import space.kklochko.jpa_hospital_example.cli.commands.CommandEntities;
|
||||
import space.kklochko.jpa_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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package space.kklochko.jpa_hospital_example.cli.parsers;
|
||||
|
||||
import lombok.NonNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class ArgumentParser {
|
||||
public Map<String, String> parse(@NonNull String args) {
|
||||
Map<String, String> parsed = new LinkedHashMap<String, String>();
|
||||
|
||||
if(args == "") return parsed;
|
||||
|
||||
// remove last '
|
||||
args = args.substring(0, args.length() - 1);
|
||||
|
||||
for(String arg : args.split("',")) {
|
||||
String[] parts = arg.split("=");
|
||||
parsed.put(parts[0].trim(), removeQuotes(parts[1]));
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
public String removeQuotes(@NonNull String withQuotes) {
|
||||
return withQuotes.substring(1, withQuotes.length());
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package space.kklochko.jpa_hospital_example.cli.parsers;
|
||||
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
import space.kklochko.jpa_hospital_example.cli.commands.CommandData;
|
||||
|
||||
@ToString
|
||||
public class CommandParser {
|
||||
public CommandData parse(@NonNull String command) {
|
||||
ArgumentParser parser = new ArgumentParser();
|
||||
String[] command_args = command.split("[()]");
|
||||
String[] command_parts = command_args[0].split(" ");
|
||||
|
||||
String args="";
|
||||
if(command_args.length > 1)
|
||||
args = command_args[1];
|
||||
|
||||
return new CommandData(command_parts[0], command_parts[1], parser.parse(args));
|
||||
}
|
||||
}
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
package space.kklochko.jpa_hospital_example.cli.validators;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class CommandValidator extends Validator {
|
||||
String command;
|
||||
|
||||
String formatRegEx = "(readAll|insert|update|remove)\\s(indicator|patient|department)\\(.*\\)";
|
||||
|
||||
public CommandValidator(String command) {
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
Pattern pattern = Pattern.compile(formatRegEx);
|
||||
return pattern.matcher(command).matches();
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return "Please, check the format: `command item(arg1=value1, ...)`. Or check the `help`";
|
||||
}
|
||||
}
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
package space.kklochko.jpa_hospital_example.cli.validators;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class InputStringFormatValidator extends Validator {
|
||||
String command;
|
||||
String formatRegEx = "\\w+\\s\\w+\\([^)]*\\)";
|
||||
|
||||
public InputStringFormatValidator(String command) {
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
Pattern pattern = Pattern.compile(formatRegEx);
|
||||
return pattern.matcher(command).matches();
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return "Please, check the format: `command item(arg1=value1, ...)`. Or check the `help`";
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package space.kklochko.jpa_hospital_example.cli.validators;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
abstract public class Validator {
|
||||
abstract public boolean isValid();
|
||||
|
||||
abstract public String getMessage();
|
||||
}
|
||||
Reference in New Issue
Block a user