From 950dde7f836feb20bcbe298718879eb16991f0b6 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 11 Sep 2023 19:47:41 +0400 Subject: [PATCH] #3 -- Add rule models --- .../java/ru/ulstu/fc/core/BaseEntity.java | 86 +++++++++++++++++++ .../java/ru/ulstu/fc/rule/model/Rule.java | 18 ++++ .../java/ru/ulstu/fc/rule/model/Term.java | 36 ++++++++ .../java/ru/ulstu/fc/rule/model/Variable.java | 30 +++++++ src/main/resources/application.properties | 1 + 5 files changed, 171 insertions(+) create mode 100644 src/main/java/ru/ulstu/fc/core/BaseEntity.java create mode 100644 src/main/java/ru/ulstu/fc/rule/model/Rule.java create mode 100644 src/main/java/ru/ulstu/fc/rule/model/Term.java create mode 100644 src/main/java/ru/ulstu/fc/rule/model/Variable.java diff --git a/src/main/java/ru/ulstu/fc/core/BaseEntity.java b/src/main/java/ru/ulstu/fc/core/BaseEntity.java new file mode 100644 index 0000000..55542dc --- /dev/null +++ b/src/main/java/ru/ulstu/fc/core/BaseEntity.java @@ -0,0 +1,86 @@ +package ru.ulstu.fc.core; + +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; +import javax.persistence.Version; +import java.io.Serializable; + +@MappedSuperclass +public abstract class BaseEntity implements Serializable, Comparable { + @Id + @GeneratedValue(strategy = GenerationType.TABLE) + private Integer id; + + @Version + private Integer version; + + public BaseEntity() { + } + + public BaseEntity(Integer id, Integer version) { + this.id = id; + this.version = version; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getVersion() { + return version; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!getClass().isAssignableFrom(obj.getClass())) { + return false; + } + BaseEntity other = (BaseEntity) obj; + if (id == null) { + if (other.getId() != null) { + return false; + } + } else if (!id.equals(other.getId())) { + return false; + } + return true; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (id == null ? 0 : id.hashCode()); + return result; + } + + @Override + public String toString() { + return getClass().getSimpleName() + "{" + + "id=" + id + + ", version=" + version + + '}'; + } + + @Override + public int compareTo(BaseEntity o) { + return id != null ? id.compareTo(o.getId()) : -1; + } + + public void reset() { + this.id = null; + this.version = null; + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/model/Rule.java b/src/main/java/ru/ulstu/fc/rule/model/Rule.java new file mode 100644 index 0000000..22c3b3a --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/model/Rule.java @@ -0,0 +1,18 @@ +package ru.ulstu.fc.rule.model; + +import ru.ulstu.fc.core.BaseEntity; + +import javax.persistence.Entity; + +@Entity +public class Rule extends BaseEntity { + private String value; + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/model/Term.java b/src/main/java/ru/ulstu/fc/rule/model/Term.java new file mode 100644 index 0000000..a0b5580 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/model/Term.java @@ -0,0 +1,36 @@ +package ru.ulstu.fc.rule.model; + +import ru.ulstu.fc.core.BaseEntity; + +import javax.persistence.Entity; + +@Entity +public class Term extends BaseEntity { + private String name; + private double min; + private double max; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getMin() { + return min; + } + + public void setMin(double min) { + this.min = min; + } + + public double getMax() { + return max; + } + + public void setMax(double max) { + this.max = max; + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/model/Variable.java b/src/main/java/ru/ulstu/fc/rule/model/Variable.java new file mode 100644 index 0000000..66753db --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/model/Variable.java @@ -0,0 +1,30 @@ +package ru.ulstu.fc.rule.model; + +import ru.ulstu.fc.core.BaseEntity; + +import javax.persistence.Entity; +import javax.persistence.OneToMany; +import java.util.List; + +@Entity +public class Variable extends BaseEntity { + private String name; + @OneToMany + private List terms; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getTerms() { + return terms; + } + + public void setTerms(List terms) { + this.terms = terms; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index bb03f7e..8764e4c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -10,6 +10,7 @@ extractor.custom-projects-dir= server.error.include-stacktrace=always server.error.include-exception=true server.error.include-message=always +# go to http://localhost:8080/h2-console spring.datasource.url=jdbc:h2:file:./data/db spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa