Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce uuid field for all entities #515

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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,24 @@
ALTER TABLE IF EXISTS shogun.applications ADD COLUMN IF NOT EXISTS uuid uuid;
ALTER TABLE IF EXISTS shogun.groups ADD COLUMN IF NOT EXISTS uuid uuid;
ALTER TABLE IF EXISTS shogun.layers ADD COLUMN IF NOT EXISTS uuid uuid;
ALTER TABLE IF EXISTS shogun.users ADD COLUMN IF NOT EXISTS uuid uuid;

ALTER TABLE IF EXISTS shogun.permissions ADD COLUMN IF NOT EXISTS uuid uuid;
ALTER TABLE IF EXISTS shogun.groupclasspermissions ADD COLUMN IF NOT EXISTS uuid uuid;
ALTER TABLE IF EXISTS shogun.groupinstancepermissions ADD COLUMN IF NOT EXISTS uuid uuid;
ALTER TABLE IF EXISTS shogun.userclasspermissions ADD COLUMN IF NOT EXISTS uuid uuid;
ALTER TABLE IF EXISTS shogun.userinstancepermissions ADD COLUMN IF NOT EXISTS uuid uuid;

ALTER TABLE IF EXISTS shogun.files RENAME COLUMN file_uuid TO uuid;
ALTER TABLE IF EXISTS shogun.imagefiles RENAME COLUMN file_uuid TO uuid;

UPDATE shogun.applications SET uuid = gen_random_uuid() WHERE uuid IS NULL;
UPDATE shogun.groups SET uuid = gen_random_uuid() WHERE uuid IS NULL;
UPDATE shogun.layers SET uuid = gen_random_uuid() WHERE uuid IS NULL;
UPDATE shogun.users SET uuid = gen_random_uuid() WHERE uuid IS NULL;

UPDATE shogun.permissions SET uuid = gen_random_uuid() WHERE uuid IS NULL;
UPDATE shogun.groupclasspermissions SET uuid = gen_random_uuid() WHERE uuid IS NULL;
UPDATE shogun.groupinstancepermissions SET uuid = gen_random_uuid() WHERE uuid IS NULL;
UPDATE shogun.userclasspermissions SET uuid = gen_random_uuid() WHERE uuid IS NULL;
UPDATE shogun.userinstancepermissions SET uuid = gen_random_uuid() WHERE uuid IS NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package de.terrestris.shogun.lib.controller;

import com.github.fge.jsonpatch.mergepatch.JsonMergePatch;
import de.terrestris.shogun.lib.model.BaseEntity;
import de.terrestris.shogun.lib.service.BaseService;
import lombok.extern.log4j.Log4j2;
Expand All @@ -33,8 +34,8 @@

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

// TODO Specify and type extension of BaseService
@Log4j2
Expand Down Expand Up @@ -89,6 +90,67 @@ public List<S> findAll() {
}
}

@GetMapping("/uuid/{uuid}")
@ResponseStatus(HttpStatus.OK)
public S findOne(@PathVariable("uuid") UUID uuid) {
log.trace("Requested to return entity of type {} with UUID {}",
getGenericClassName(), uuid);

try {
Optional<S> entity = service.findOne(uuid);

if (entity.isPresent()) {
S persistedEntity = entity.get();

log.trace("Successfully got entity of type {} with UUID {}",
getGenericClassName(), uuid);

return persistedEntity;
} else {
log.error("Could not find entity of type {} with UUID {}",
getGenericClassName(), uuid);

throw new ResponseStatusException(
HttpStatus.NOT_FOUND,
messageSource.getMessage(
"BaseController.NOT_FOUND",
null,
LocaleContextHolder.getLocale()
)
);
}
} catch (AccessDeniedException ade) {
log.warn("Access to entity of type {} with UUID {} is denied",
getGenericClassName(), uuid);

throw new ResponseStatusException(
HttpStatus.NOT_FOUND,
messageSource.getMessage(
"BaseController.NOT_FOUND",
null,
LocaleContextHolder.getLocale()
),
ade
);
} catch (ResponseStatusException rse) {
throw rse;
} catch (Exception e) {
log.error("Error while requesting entity of type {} with UUID {}: \n {}",
getGenericClassName(), uuid, e.getMessage());
log.trace("Full stack trace: ", e);

throw new ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR,
messageSource.getMessage(
"BaseController.INTERNAL_SERVER_ERROR",
null,
LocaleContextHolder.getLocale()
),
e
);
}
}

@GetMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public S findOne(@PathVariable("id") Long entityId) {
Expand Down Expand Up @@ -535,26 +597,15 @@ public S update(@RequestBody S entity, @PathVariable("id") Long entityId) {
}
}

@PatchMapping("/{id}")
@PatchMapping(value = "/{id}", consumes = "application/json-patch+json")
@ResponseStatus(HttpStatus.OK)
public S updatePartial(@RequestBody Map<String, Object> values, @PathVariable("id") Long entityId) {
log.trace("Requested to partially update entity of type {} with ID {} ({})", getGenericClassName(), entityId, values);
public S updatePartial(@RequestBody JsonMergePatch patch, @PathVariable("id") Long entityId) {
log.trace("Requested to partially update entity of type {} with ID {} ({})", getGenericClassName(), entityId, patch);

try {
Object idFromValues = values.get("id");
if (idFromValues == null) {
log.error("Field 'id' (entity {})is missing in the passed values: {}.", entityId, values);
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}
Long id = Long.valueOf((Integer) idFromValues);
if (!entityId.equals(id)) {
log.error("IDs of update candidate (ID: {}) and update data ({}) don't match.", entityId, values);
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}

S persistedEntity = service.findOne(entityId).orElseThrow();
if (persistedEntity != null) {
S updatedEntity = service.updatePartial(entityId, persistedEntity, values);
S updatedEntity = service.updatePartial(persistedEntity, patch);

log.trace("Successfully updated values for entity of type {} with ID {}",
getGenericClassName(), entityId);
Expand Down Expand Up @@ -588,7 +639,7 @@ public S updatePartial(@RequestBody Map<String, Object> values, @PathVariable("i
);
} catch (NumberFormatException nfe) {
log.error("Can't parse 'id' field ({}) from values ({}). It has to be an Integer.: {}",
values, entityId, nfe.getMessage());
patch, entityId, nfe.getMessage());
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
} catch (ResponseStatusException rse) {
throw rse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import lombok.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;
import org.hibernate.envers.AuditTable;
import org.hibernate.envers.Audited;
Expand All @@ -33,6 +34,7 @@

@Entity(name = "applications")
@Table(schema = "shogun")
@DynamicUpdate
@Audited
@AuditTable(value = "applications_rev", schema = "shogun_rev")
@Cacheable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import org.hibernate.annotations.UpdateTimestamp;
import org.hibernate.annotations.*;
import org.hibernate.envers.Audited;

import javax.persistence.*;
import java.io.Serializable;
import java.time.OffsetDateTime;
import java.util.UUID;

@MappedSuperclass
@Audited
Expand All @@ -56,6 +54,14 @@ public abstract class BaseEntity implements Serializable {
)
private Long id;

@Column(columnDefinition = "uuid")
@Type(type="pg-uuid")
@Getter @Setter
@Schema(
description = "The UUID of the entity."
)
private UUID uuid = UUID.randomUUID();

@CreationTimestamp
@Column(updatable = false)
@Getter @Setter
Expand Down
13 changes: 2 additions & 11 deletions shogun-lib/src/main/java/de/terrestris/shogun/lib/model/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import lombok.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.util.UUID;

@Entity(name = "files")
@Table(schema = "shogun")
@DynamicUpdate
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "files")
@AllArgsConstructor
Expand All @@ -37,15 +37,6 @@
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class File extends BaseEntity {

@Column(columnDefinition = "uuid", updatable = false, nullable = false)
@Type(type="pg-uuid")
@Getter
@Schema(
description = "The (auto assigned) UUID of the file.",
accessMode = Schema.AccessMode.READ_ONLY
)
private UUID fileUuid = UUID.randomUUID();

@Column
@Getter @Setter
@Schema(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import lombok.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.envers.AuditTable;
import org.hibernate.envers.Audited;

import javax.persistence.*;

@Entity(name = "groups")
@Table(schema = "shogun")
@DynamicUpdate
@Audited
@AuditTable(value = "groups_rev", schema = "shogun_rev")
@Cacheable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@
package de.terrestris.shogun.lib.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity(name = "imagefiles")
@Table(schema = "shogun")
@DynamicUpdate
@Cacheable
@Data
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.geojson.GeoJsonObject;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;
import org.hibernate.envers.AuditTable;
import org.hibernate.envers.Audited;
Expand All @@ -32,6 +33,7 @@

@Entity(name = "layers")
@Table(schema = "shogun")
@DynamicUpdate
@Audited
@AuditTable(value = "layers_rev", schema = "shogun_rev")
@Cacheable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import lombok.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;
import org.hibernate.envers.AuditTable;
import org.hibernate.envers.Audited;
Expand All @@ -30,6 +31,7 @@

@Entity(name = "users")
@Table(schema = "shogun")
@DynamicUpdate
@Audited
@AuditTable(value = "users_rev", schema = "shogun_rev")
@Cacheable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@
package de.terrestris.shogun.lib.model.security.permission;

import de.terrestris.shogun.lib.model.Group;
import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.envers.AuditTable;
import org.hibernate.envers.Audited;

import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity(name = "groupclasspermissions")
@Table(schema = "shogun")
@DynamicUpdate
@Audited
@AuditTable(value = "groupclasspermissions_rev", schema = "shogun_rev")
@Cacheable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@
package de.terrestris.shogun.lib.model.security.permission;

import de.terrestris.shogun.lib.model.Group;
import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.envers.AuditTable;
import org.hibernate.envers.Audited;

import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity(name = "groupinstancepermissions")
@Table(schema = "shogun")
@DynamicUpdate
@Audited
@AuditTable(value = "groupinstancepermissions_rev", schema = "shogun_rev")
@Cacheable
Expand Down
Loading