Add CObject, CObjectEntity, Repository

Add ObjectTypeRepository
Refactor ObjectType attributes to List
This commit is contained in:
Max W. 2024-06-18 00:22:22 +02:00
parent 38c7658854
commit fdb5f426be
6 changed files with 171 additions and 7 deletions

View File

@ -0,0 +1,20 @@
package de.w665.fluidcms.db.entity;
import de.w665.fluidcms.model.ObjectAttribute;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.Map;
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class CObjectEntity {
private String id;
private String name;
private String objectTypeId;
private Map<String, ObjectAttribute> attributes;
}

View File

@ -0,0 +1,65 @@
package de.w665.fluidcms.db.repo;
import com.rethinkdb.RethinkDB;
import com.rethinkdb.net.Connection;
import de.w665.fluidcms.db.RethinkDBConfig;
import de.w665.fluidcms.db.RethinkDBConnector;
import de.w665.fluidcms.db.entity.CObjectEntity;
import de.w665.fluidcms.model.CObject;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@ApplicationScoped
public class CObjectRepository {
@Inject
RethinkDBConnector connector;
@Inject
RethinkDBConfig config;
@Inject
ObjectTypeRepository objectTypeRepository;
private final String TABLE_NAME = "c_objects";
private RethinkDB r;
private Connection connection;
@PostConstruct
public void init() {
this.r = this.connector.getR();
this.connection = this.connector.getConnection();
// Check if table exists
try {
r.db(config.getDatabase()).tableCreate(TABLE_NAME).run(connection);
log.debug("Table " + TABLE_NAME + " created");
} catch (Exception e) {
log.debug("Table " + TABLE_NAME + " already exists");
if (config.isAutoresetonstartup()) {
r.db(config.getDatabase()).tableDrop(TABLE_NAME).run(connection);
r.db(config.getDatabase()).tableCreate(TABLE_NAME).run(connection);
log.debug("Table " + TABLE_NAME + " reset");
}
}
}
public void insert(CObject cObject) {
// Create entity CObject and replace ObjectType with a reference to the object type (e.g. ObjectType ID)
CObjectEntity entity = new CObjectEntity(cObject.getId(), cObject.getName(), cObject.getType().getId(), cObject.getAttributes());
r.db(config.getDatabase()).table(TABLE_NAME).insert(entity).run(connection);
}
public CObject get(String id) {
CObjectEntity entity = r.db(config.getDatabase()).table(TABLE_NAME).filter(r.hashMap("id", id)).run(connection, CObjectEntity.class).first();
if(entity == null) {
log.warn("Entity CObject with ID {} not found", id);
return null;
}
// Construct CObject from entity
return new CObject(entity.getId(), entity.getName(), objectTypeRepository.get(entity.getId()), entity.getAttributes());
}
}

View File

@ -0,0 +1,54 @@
package de.w665.fluidcms.db.repo;
import com.rethinkdb.RethinkDB;
import com.rethinkdb.net.Connection;
import de.w665.fluidcms.db.RethinkDBConfig;
import de.w665.fluidcms.db.RethinkDBConnector;
import de.w665.fluidcms.model.ObjectType;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@ApplicationScoped
public class ObjectTypeRepository {
@Inject
RethinkDBConnector connector;
@Inject
RethinkDBConfig config;
private final String TABLE_NAME = "object_types";
private RethinkDB r;
private Connection connection;
@PostConstruct
public void init() {
this.r = this.connector.getR();
this.connection = this.connector.getConnection();
// Check if table exists
try {
r.db(config.getDatabase()).tableCreate(TABLE_NAME).run(connection);
log.debug("Table " + TABLE_NAME + " created");
} catch (Exception e) {
log.debug("Table " + TABLE_NAME + " already exists");
if(config.isAutoresetonstartup()) {
r.db(config.getDatabase()).tableDrop(TABLE_NAME).run(connection);
r.db(config.getDatabase()).tableCreate(TABLE_NAME).run(connection);
log.debug("Table " + TABLE_NAME + " reset");
}
}
}
public void insert(ObjectType objectType) {
objectType.setId(r.uuid().run(connection, String.class).first());
r.db(config.getDatabase()).table(TABLE_NAME).insert(objectType).run(connection);
}
public ObjectType get(String id) {
return r.db(config.getDatabase()).table(TABLE_NAME).filter(r.hashMap("id", id)).run(connection, ObjectType.class).first();
}
}

View File

@ -0,0 +1,19 @@
package de.w665.fluidcms.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.Map;
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class CObject {
private String id;
private String name;
private ObjectType type; // TODO: When developing the object repository, replace this with a reference to the object type (e.g. ObjectType ID)
private Map<String, ObjectAttribute> attributes;
}

View File

@ -4,7 +4,7 @@ import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.Map;
import java.util.List;
@Getter
@Setter
@ -15,5 +15,5 @@ public class ObjectType {
private String description;
private String icon;
private String color;
private Map<String, ObjectAttribute> attributes;
private List<ObjectAttribute> attributes;
}

View File

@ -1,24 +1,30 @@
package de.w665.fluidcms.rest.mapping;
import de.w665.fluidcms.db.repo.ObjectTypeRepository;
import de.w665.fluidcms.model.ObjectAttribute;
import de.w665.fluidcms.model.ObjectType;
import de.w665.fluidcms.model.objectattributes.StringObjectAttribute;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import lombok.extern.slf4j.Slf4j;
import org.jboss.resteasy.reactive.RestResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Path("/objectType")
public class ObjectTypeResource {
@Inject
ObjectTypeRepository objectTypeRepository;
@POST
@Consumes("application/json")
@Produces("application/json")
public RestResponse<ObjectType> createObjectType(ObjectType objectType) {
log.debug(objectType.toString());
objectTypeRepository.insert(objectType);
return RestResponse.ok(objectType);
}
@ -42,9 +48,9 @@ public class ObjectTypeResource {
attr2.setValue("Attribute " + number + " Value");
// Add the StringObjectAttribute objects to a Map
Map<String, ObjectAttribute> attributes = new HashMap<>();
attributes.put("attr1", attr1);
attributes.put("attr2", attr2);
List<ObjectAttribute> attributes = new ArrayList<>();
attributes.add(attr1);
attributes.add(attr2);
// Create an ObjectType object and set the attributes
ObjectType objectType = new ObjectType();