Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,104 @@
package net.onelitefeather.vulpes.api.model;

import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MappedSuperclass;
import jakarta.validation.constraints.NotNull;
import net.onelitefeather.vulpes.api.model.project.ProjectEntity;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

/**
* Base class for every entity that belongs to a {@link ProjectEntity}. Adds the project
* reference and the entity's local key, which combine into a namespaced key
* (e.g. {@code cygnus:whatever}) via {@link #getNamespacedKey()}.
*
* @author theEvilReaper
* @version 1.0.0
* @since 2.2.0
*/
@MappedSuperclass
public abstract class AbstractEntity extends IdentifiableEntity {

@ManyToOne
@JoinColumn(name = "project_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private ProjectEntity project;

@NotNull
private String key;

/**
* Default constructor for JPA and Micronaut Data.
*/
protected AbstractEntity() {
// No-argument constructor for JPA
}

/**
* Constructs a new {@link AbstractEntity} with the specified local key and project.
*
* @param key the local key of the entity within the project's namespace
* @param project the project this entity belongs to
*/
protected AbstractEntity(String key, ProjectEntity project) {
this.key = key;
this.project = project;
}

/**
* Returns the project this entity belongs to.
*
* @return the project of the entity
*/
public ProjectEntity getProject() {
return project;
}

/**
* Sets the project this entity belongs to.
*
* @param project the project to set
*/
public void setProject(ProjectEntity project) {
this.project = project;
}

/**
* Returns the local key of the entity within its project's namespace (e.g. {@code whatever}).
*
* @return the local key of the entity
*/
public String getKey() {
return key;
}

/**
* Sets the local key of the entity within its project's namespace (e.g. {@code whatever}).
*
* @param key the local key to set
*/
public void setKey(String key) {
this.key = key;
}

/**
* Returns the full namespaced key, combining the project's key with this entity's local key
* (e.g. project key {@code cygnus} and local key {@code whatever} become {@code cygnus:whatever}).
*
* @return the namespaced key of the entity
*/
public String getNamespacedKey() {
return project.getKey() + ":" + key;
}

/**
* Derives the variable name of the entity from its local key, e.g. local key {@code whatever}
* becomes {@code WHATEVER}.
*
* @return the derived variable name of the entity
*/
public String getVariableName() {
return key.toUpperCase();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package net.onelitefeather.vulpes.api.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import net.onelitefeather.vulpes.api.generator.VulpesGenerator;
import jakarta.persistence.UniqueConstraint;
import net.onelitefeather.vulpes.api.model.project.ProjectEntity;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import java.util.UUID;

Expand All @@ -27,21 +20,14 @@
@Entity(name = "attributes")
@Table(name = "attributes", indexes = {
@Index(name = "idx_attributes_project_id", columnList = "project_id")
}, uniqueConstraints = {
@UniqueConstraint(name = "uq_attributes_project_key", columnNames = {"project_id", "key"})
})
public class AttributeEntity implements VulpesModel {
public class AttributeEntity extends AbstractEntity {

@Id
@GeneratedValue(strategy = GenerationType.UUID)
@VulpesGenerator
private UUID id;
private String uiName;
private String variableName;
private double defaultValue;
private double maximumValue;
@ManyToOne
@JoinColumn(name = "project_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private ProjectEntity project;

/**
* Default constructor for JPA and Micronaut Data.
Expand All @@ -58,40 +44,23 @@ public AttributeEntity() {
*
* @param id the unique identifier of the attribute
* @param uiName the model name associated with the attribute
* @param variableName the name of the attribute
* @param key the local key of the attribute within the project's namespace
* (e.g. {@code generic.max_health}, becomes {@code <project-key>:generic.max_health}
* via {@link #getNamespacedKey()})
* @param defaultValue the default value of the attribute
* @param maximumValue the maximum value of the attribute
* @param project the project this attribute belongs to
*/
public AttributeEntity(UUID id, String uiName, String variableName, double defaultValue, double maximumValue, ProjectEntity project) {
this.id = id;
public AttributeEntity(UUID id, String uiName, String key, double defaultValue, double maximumValue, ProjectEntity project) {
super(key, project);
this.setId(id);
this.uiName = uiName;
this.variableName = variableName;
this.defaultValue = defaultValue;
this.maximumValue = maximumValue;
this.project = project;
}

// Getters and setters for each field

/**
* Returns the unique identifier of the attribute
*
* @return the unique identifier of the attribute
*/
public UUID getId() {
return id;
}

/**
* Sets the unique identifier of the attribute
*
* @param id the unique identifier to set
*/
public void setId(UUID id) {
this.id = id;
}

/**
* Returns the model name associated with the attribute
*
Expand All @@ -110,24 +79,6 @@ public void setUiName(String modelName) {
this.uiName = modelName;
}

/**
* Returns the name of the attribute
*
* @return the name of the attribute
*/
public String getVariableName() {
return variableName;
}

/**
* Sets the name of the attribute
*
* @param name the name to set
*/
public void setVariableName(String name) {
this.variableName = name;
}

/**
* Returns the default value of the attribute
*
Expand Down Expand Up @@ -164,24 +115,6 @@ public void setMaximumValue(double maximumValue) {
this.maximumValue = maximumValue;
}

/**
* Returns the project this attribute belongs to.
*
* @return the project of the attribute
*/
public ProjectEntity getProject() {
return project;
}

/**
* Sets the project this attribute belongs to.
*
* @param project the project to set
*/
public void setProject(ProjectEntity project) {
this.project = project;
}

/**
* Provides a string representation of the AttributeModel
*
Expand All @@ -190,12 +123,12 @@ public void setProject(ProjectEntity project) {
@Override
public String toString() {
return "AttributeModel{" +
"id='" + id + '\'' +
"id='" + getId() + '\'' +
", modelName='" + uiName + '\'' +
", name='" + variableName + '\'' +
", key='" + getKey() + '\'' +
", defaultValue=" + defaultValue +
", maximumValue=" + maximumValue +
", project=" + project +
", project=" + getProject() +
'}';
}
}
Loading
Loading