Adding DescriptionReference entity stack (former Dataset*** entities)

This commit is contained in:
Thomas Georgios Giannos 2023-10-27 11:59:37 +03:00
parent 7508cf8698
commit 60ca51fe00
10 changed files with 735 additions and 4 deletions

View File

@ -72,5 +72,9 @@ public final class Permission {
public static String EditDmpReference = "EditDmpReference";
public static String DeleteDmpReference = "DeleteDmpReference";
public static String BrowseDescriptionReference = "BrowseDescriptionReference";
public static String EditDescriptionReference = "EditDescriptionReference";
public static String DeleteDescriptionReference = "DeleteDescriptionReference";
}

View File

@ -0,0 +1,110 @@
package eu.eudat.data;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.data.converters.DateToUTCConverter;
import eu.eudat.data.converters.enums.IsActiveConverter;
import jakarta.persistence.*;
import java.time.Instant;
import java.util.UUID;
//@Entity
//@Table(name = "\"DescriptionReference\"")
public class DescriptionReferenceEntity {
@Id
@Column(name = "id", updatable = false, nullable = false, columnDefinition = "BINARY(16)")
private UUID id;
public static final String _id = "id";
@Column(name = "data")
private String data;
public static final String _data = "data";
@Column(name = "description_id", columnDefinition = "uuid", nullable = false)
private UUID descriptionId;
public static final String _descriptionId = "descriptionId";
@Column(name = "reference_id", columnDefinition = "uuid", nullable = false)
private UUID referenceId;
public static final String _referenceId = "referenceId";
@Column(name = "created_at")
@Convert(converter = DateToUTCConverter.class)
private Instant createdAt;
public static final String _createdAt = "createdAt";
@Column(name = "updated_at")
@Convert(converter = DateToUTCConverter.class)
private Instant updatedAt;
public static final String _updatedAt = "updatedAt";
@Column(name = "is_active", nullable = false)
@Convert(converter = IsActiveConverter.class)
private IsActive isActive;
public static final String _isActive = "isActive";
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public UUID getDescriptionId() {
return descriptionId;
}
public void setDescriptionId(UUID descriptionId) {
this.descriptionId = descriptionId;
}
public UUID getReferenceId() {
return referenceId;
}
public void setReferenceId(UUID referenceId) {
this.referenceId = referenceId;
}
public Instant getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Instant createdAt) {
this.createdAt = createdAt;
}
public Instant getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Instant updatedAt) {
this.updatedAt = updatedAt;
}
public IsActive getIsActive() {
return isActive;
}
public void setIsActive(IsActive isActive) {
this.isActive = isActive;
}
}

View File

@ -97,10 +97,6 @@ public class DmpEntity {
public static final String _publishedAt = "publishedAt";
//TODO: (thgiannos) Handle using the DMPEntity builder
// @OneToMany(mappedBy = "dmp", fetch = FetchType.LAZY)
// private Set<Dataset> dataset;
// @ManyToOne(fetch = FetchType.LAZY)
// @JoinColumn(name = "\"Grant\"")
//TODO: (thgiannos) Previously 'Grant'

View File

@ -0,0 +1,94 @@
package eu.eudat.model;
import eu.eudat.commons.enums.IsActive;
import java.time.Instant;
import java.util.UUID;
public class DescriptionReference {
private UUID id;
public static final String _id = "id";
private String data;
public static final String _data = "data";
private Description description;
public static final String _description = "description";
private Reference reference;
public static final String _reference = "reference";
private Instant createdAt;
public static final String _createdAt = "createdAt";
private Instant updatedAt;
public static final String _updatedAt = "updatedAt";
private IsActive isActive;
public static final String _isActive = "isActive";
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Description getDescription() {
return description;
}
public void setDescription(Description description) {
this.description = description;
}
public Reference getReference() {
return reference;
}
public void setReference(Reference reference) {
this.reference = reference;
}
public Instant getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Instant createdAt) {
this.createdAt = createdAt;
}
public Instant getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Instant updatedAt) {
this.updatedAt = updatedAt;
}
public IsActive getIsActive() {
return isActive;
}
public void setIsActive(IsActive isActive) {
this.isActive = isActive;
}
}

View File

@ -96,7 +96,9 @@ public class DescriptionBuilder extends BaseBuilder<Description, DescriptionEnti
models.add(m);
}
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
return models;
}

View File

@ -0,0 +1,155 @@
package eu.eudat.model.builder;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.convention.ConventionService;
import eu.eudat.data.DescriptionReferenceEntity;
import eu.eudat.model.Description;
import eu.eudat.model.DescriptionReference;
import eu.eudat.model.Reference;
import eu.eudat.query.DescriptionQuery;
import eu.eudat.query.ReferenceQuery;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.DataLogEntry;
import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.stream.Collectors;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DescriptionReferenceBuilder extends BaseBuilder<DescriptionReference, DescriptionReferenceEntity> {
private final BuilderFactory builderFactory;
private final QueryFactory queryFactory;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
@Autowired
public DescriptionReferenceBuilder(
ConventionService conventionService,
BuilderFactory builderFactory, QueryFactory queryFactory) {
super(conventionService, new LoggerService(LoggerFactory.getLogger(DmpReferenceBuilder.class)));
this.builderFactory = builderFactory;
this.queryFactory = queryFactory;
}
public DescriptionReferenceBuilder authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
}
@Override
public List<DescriptionReference> build(FieldSet fields, List<DescriptionReferenceEntity> data) throws MyApplicationException {
this.logger.debug("building for {} items requesting {} fields", Optional.ofNullable(data).map(List::size).orElse(0), Optional.ofNullable(fields).map(FieldSet::getFields).map(Set::size).orElse(0));
this.logger.trace(new DataLogEntry("requested fields", fields));
if (fields == null || data == null || fields.isEmpty())
return new ArrayList<>();
FieldSet referenceFields = fields.extractPrefixed(this.asPrefix(DescriptionReference._reference));
Map<UUID, Reference> referenceItemsMap = this.collectReferences(referenceFields, data);
FieldSet descriptionFields = fields.extractPrefixed(this.asPrefix(DescriptionReference._description));
Map<UUID, Description> descriptionItemsMap = this.collectDescriptions(descriptionFields, data);
List<DescriptionReference> models = new ArrayList<>();
for (DescriptionReferenceEntity d : data) {
DescriptionReference m = new DescriptionReference();
if (fields.hasField(this.asIndexer(DescriptionReference._id)))
m.setId(d.getId());
if (fields.hasField(this.asIndexer(DescriptionReference._data)))
m.setData(d.getData());
if (fields.hasField(this.asIndexer(DescriptionReference._createdAt)))
m.setCreatedAt(d.getCreatedAt());
if (fields.hasField(this.asIndexer(DescriptionReference._updatedAt)))
m.setUpdatedAt(d.getUpdatedAt());
if (fields.hasField(this.asIndexer(DescriptionReference._isActive)))
m.setIsActive(d.getIsActive());
if (!referenceFields.isEmpty() && referenceItemsMap != null && referenceItemsMap.containsKey(d.getReferenceId())) {
m.setReference(referenceItemsMap.get(d.getReferenceId()));
}
if (!descriptionFields.isEmpty() && descriptionItemsMap != null && descriptionItemsMap.containsKey(d.getDescriptionId())) {
m.setDescription(descriptionItemsMap.get(d.getDescriptionId()));
}
models.add(m);
}
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
return models;
}
private Map<UUID, Description> collectDescriptions(FieldSet fields, List<DescriptionReferenceEntity> data) throws MyApplicationException {
if (fields.isEmpty() || data.isEmpty())
return null;
this.logger.debug("checking related - {}", Description.class.getSimpleName());
Map<UUID, Description> itemMap;
if (!fields.hasOtherField(this.asIndexer(Reference._id))) {
itemMap = this.asEmpty(
data.stream().map(DescriptionReferenceEntity::getDescriptionId).distinct().collect(Collectors.toList()),
x -> {
Description item = new Description();
item.setId(x);
return item;
},
Description::getId);
} else {
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(Reference._id);
DescriptionQuery q = this.queryFactory.query(DescriptionQuery.class).authorize(this.authorize).ids(data.stream().map(DescriptionReferenceEntity::getDescriptionId).distinct().collect(Collectors.toList()));
itemMap = this.builderFactory.builder(DescriptionBuilder.class).authorize(this.authorize).asForeignKey(q, clone, Description::getId);
}
if (!fields.hasField(Description._id)) {
itemMap.forEach((id, item) -> {
if (item != null)
item.setId(null);
});
}
return itemMap;
}
private Map<UUID, Reference> collectReferences(FieldSet fields, List<DescriptionReferenceEntity> data) throws MyApplicationException {
if (fields.isEmpty() || data.isEmpty())
return null;
this.logger.debug("checking related - {}", Reference.class.getSimpleName());
Map<UUID, Reference> itemMap;
if (!fields.hasOtherField(this.asIndexer(Reference._id))) {
itemMap = this.asEmpty(
data.stream().map(DescriptionReferenceEntity::getReferenceId).distinct().collect(Collectors.toList()),
x -> {
Reference item = new Reference();
item.setId(x);
return item;
},
Reference::getId);
} else {
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(Reference._id);
ReferenceQuery q = this.queryFactory.query(ReferenceQuery.class).authorize(this.authorize).ids(data.stream().map(DescriptionReferenceEntity::getReferenceId).distinct().collect(Collectors.toList()));
itemMap = this.builderFactory.builder(ReferenceBuilder.class).authorize(this.authorize).asForeignKey(q, clone, Reference::getId);
}
if (!fields.hasField(Reference._id)) {
itemMap.forEach((id, item) -> {
if (item != null)
item.setId(null);
});
}
return itemMap;
}
}

View File

@ -0,0 +1,50 @@
package eu.eudat.model.censorship;
import eu.eudat.authorization.Permission;
import eu.eudat.convention.ConventionService;
import eu.eudat.model.DescriptionReference;
import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.tools.data.censor.CensorFactory;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.DataLogEntry;
import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.UUID;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DescriptionReferenceCensor extends BaseCensor {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionReferenceCensor.class));
protected final AuthorizationService authService;
protected final CensorFactory censorFactory;
@Autowired
public DescriptionReferenceCensor(ConventionService conventionService,
AuthorizationService authService,
CensorFactory censorFactory) {
super(conventionService);
this.authService = authService;
this.censorFactory = censorFactory;
}
public void censor(FieldSet fields, UUID userId) {
logger.debug(new DataLogEntry("censoring fields", fields));
if (fields == null || fields.isEmpty())
return;
this.authService.authorizeForce(Permission.BrowseDescriptionReference);
FieldSet descriptionFields = fields.extractPrefixed(this.asIndexerPrefix(DescriptionReference._description));
this.censorFactory.censor(DescriptionCensor.class).censor(descriptionFields, userId);
FieldSet referenceFields = fields.extractPrefixed(this.asIndexerPrefix(DescriptionReference._reference));
this.censorFactory.censor(ReferenceCensor.class).censor(referenceFields, userId);
}
}

View File

@ -0,0 +1,79 @@
package eu.eudat.model.deleter;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.data.DescriptionReferenceEntity;
import eu.eudat.query.DescriptionReferenceQuery;
import gr.cite.tools.data.deleter.Deleter;
import gr.cite.tools.data.deleter.DeleterFactory;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import jakarta.persistence.EntityManager;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.management.InvalidApplicationException;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DescriptionReferenceDeleter implements Deleter {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionReferenceDeleter.class));
private final EntityManager entityManager;
protected final QueryFactory queryFactory;
protected final DeleterFactory deleterFactory;
@Autowired
public DescriptionReferenceDeleter(
EntityManager entityManager,
QueryFactory queryFactory,
DeleterFactory deleterFactory
) {
this.entityManager = entityManager;
this.queryFactory = queryFactory;
this.deleterFactory = deleterFactory;
}
public void deleteAndSaveByIds(List<UUID> ids) throws InvalidApplicationException {
logger.debug(new MapLogEntry("collecting to delete").And("count", Optional.ofNullable(ids).map(List::size).orElse(0)).And("ids", ids));
List<DescriptionReferenceEntity> data = this.queryFactory.query(DescriptionReferenceQuery.class).ids(ids).collect();
logger.trace("retrieved {} items", Optional.ofNullable(data).map(List::size).orElse(0));
this.deleteAndSave(data);
}
public void deleteAndSave(List<DescriptionReferenceEntity> data) throws InvalidApplicationException {
logger.debug("will delete {} items", Optional.ofNullable(data).map(List::size).orElse(0));
this.delete(data);
logger.trace("saving changes");
this.entityManager.flush();
logger.trace("changes saved");
}
public void delete(List<DescriptionReferenceEntity> data) throws InvalidApplicationException {
logger.debug("will delete {} items", Optional.ofNullable(data).map(List::size).orElse(0));
if (data == null || data.isEmpty())
return;
Instant now = Instant.now();
for (DescriptionReferenceEntity item : data) {
logger.trace("deleting item {}", item.getId());
item.setIsActive(IsActive.Inactive);
item.setUpdatedAt(now);
logger.trace("updating item");
this.entityManager.merge(item);
logger.trace("updated item");
}
}
}

View File

@ -0,0 +1,220 @@
package eu.eudat.query;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.commons.scope.user.UserScope;
import eu.eudat.data.DescriptionReferenceEntity;
import eu.eudat.model.DescriptionReference;
import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.tools.data.query.FieldResolver;
import gr.cite.tools.data.query.QueryBase;
import gr.cite.tools.data.query.QueryContext;
import jakarta.persistence.Tuple;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Predicate;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.*;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DescriptionReferenceQuery extends QueryBase<DescriptionReferenceEntity> {
private Collection<UUID> ids;
private Collection<UUID> excludedIds;
private Collection<IsActive> isActives;
private Collection<UUID> descriptionIds;
private Collection<UUID> referenceIds;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
private final UserScope userScope;
private final AuthorizationService authService;
public DescriptionReferenceQuery(UserScope userScope, AuthorizationService authService) {
this.userScope = userScope;
this.authService = authService;
}
public DescriptionReferenceQuery ids(UUID value) {
this.ids = List.of(value);
return this;
}
public DescriptionReferenceQuery ids(UUID... value) {
this.ids = Arrays.asList(value);
return this;
}
public DescriptionReferenceQuery ids(Collection<UUID> values) {
this.ids = values;
return this;
}
public DescriptionReferenceQuery excludedIds(Collection<UUID> values) {
this.excludedIds = values;
return this;
}
public DescriptionReferenceQuery excludedIds(UUID value) {
this.excludedIds = List.of(value);
return this;
}
public DescriptionReferenceQuery excludedIds(UUID... value) {
this.excludedIds = Arrays.asList(value);
return this;
}
public DescriptionReferenceQuery isActive(IsActive value) {
this.isActives = List.of(value);
return this;
}
public DescriptionReferenceQuery isActive(IsActive... value) {
this.isActives = Arrays.asList(value);
return this;
}
public DescriptionReferenceQuery isActive(Collection<IsActive> values) {
this.isActives = values;
return this;
}
public DescriptionReferenceQuery descriptionIds(UUID value) {
this.descriptionIds = List.of(value);
return this;
}
public DescriptionReferenceQuery descriptionIds(UUID... value) {
this.descriptionIds = Arrays.asList(value);
return this;
}
public DescriptionReferenceQuery descriptionIds(Collection<UUID> values) {
this.descriptionIds = values;
return this;
}
public DescriptionReferenceQuery referenceIds(UUID value) {
this.referenceIds = List.of(value);
return this;
}
public DescriptionReferenceQuery referenceIds(UUID... value) {
this.referenceIds = Arrays.asList(value);
return this;
}
public DescriptionReferenceQuery referenceIds(Collection<UUID> values) {
this.referenceIds = values;
return this;
}
public DescriptionReferenceQuery authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
}
@Override
protected Boolean isFalseQuery() {
return
this.isEmpty(this.ids) ||
this.isEmpty(this.isActives) ||
this.isEmpty(this.excludedIds) ||
this.isEmpty(this.descriptionIds) ||
this.isEmpty(this.referenceIds);
}
@Override
protected Class<DescriptionReferenceEntity> entityClass() {
return DescriptionReferenceEntity.class;
}
@Override
protected <X, Y> Predicate applyFilters(QueryContext<X, Y> queryContext) {
List<Predicate> predicates = new ArrayList<>();
if (this.ids != null) {
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionReferenceEntity._id));
for (UUID item : this.ids)
inClause.value(item);
predicates.add(inClause);
}
if (this.excludedIds != null) {
CriteriaBuilder.In<UUID> notInClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionReferenceEntity._id));
for (UUID item : this.excludedIds)
notInClause.value(item);
predicates.add(notInClause.not());
}
if (this.isActives != null) {
CriteriaBuilder.In<IsActive> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionReferenceEntity._isActive));
for (IsActive item : this.isActives)
inClause.value(item);
predicates.add(inClause);
}
if (this.descriptionIds != null) {
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionReferenceEntity._descriptionId));
for (UUID item : this.descriptionIds)
inClause.value(item);
predicates.add(inClause);
}
if (this.referenceIds != null) {
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionReferenceEntity._referenceId));
for (UUID item : this.referenceIds)
inClause.value(item);
predicates.add(inClause);
}
if (!predicates.isEmpty()) {
Predicate[] predicatesArray = predicates.toArray(new Predicate[0]);
return queryContext.CriteriaBuilder.and(predicatesArray);
} else {
return null;
}
}
@Override
protected String fieldNameOf(FieldResolver item) {
if (item.match(DescriptionReference._id))
return DescriptionReferenceEntity._id;
if (item.match(DescriptionReference._data))
return DescriptionReferenceEntity._data;
if (item.match(DescriptionReference._description))
return DescriptionReferenceEntity._descriptionId;
else if (item.prefix(DescriptionReference._description))
return DescriptionReferenceEntity._descriptionId;
else if (item.match(DescriptionReference._reference))
return DescriptionReferenceEntity._referenceId;
else if (item.prefix(DescriptionReference._reference))
return DescriptionReferenceEntity._referenceId;
else if (item.match(DescriptionReference._createdAt))
return DescriptionReferenceEntity._createdAt;
else if (item.match(DescriptionReference._updatedAt))
return DescriptionReferenceEntity._updatedAt;
else if (item.match(DescriptionReference._isActive))
return DescriptionReferenceEntity._isActive;
else
return null;
}
@Override
protected DescriptionReferenceEntity convert(Tuple tuple, Set<String> columns) {
DescriptionReferenceEntity item = new DescriptionReferenceEntity();
item.setId(QueryBase.convertSafe(tuple, columns, DescriptionReferenceEntity._id, UUID.class));
item.setData(QueryBase.convertSafe(tuple, columns, DescriptionReferenceEntity._data, String.class));
item.setDescriptionId(QueryBase.convertSafe(tuple, columns, DescriptionReferenceEntity._descriptionId, UUID.class));
item.setReferenceId(QueryBase.convertSafe(tuple, columns, DescriptionReferenceEntity._referenceId, UUID.class));
item.setCreatedAt(QueryBase.convertSafe(tuple, columns, DescriptionReferenceEntity._createdAt, Instant.class));
item.setUpdatedAt(QueryBase.convertSafe(tuple, columns, DescriptionReferenceEntity._updatedAt, Instant.class));
item.setIsActive(QueryBase.convertSafe(tuple, columns, DescriptionReferenceEntity._isActive, IsActive.class));
return item;
}
}

View File

@ -237,3 +237,24 @@ permissions:
clients: [ ]
allowAnonymous: false
allowAuthenticated: false
# DescriptionReference Permissions
BrowseDescriptionReference:
roles:
- Admin
clients: [ ]
allowAnonymous: false
allowAuthenticated: false
EditDescriptionReference:
roles:
- Admin
clients: [ ]
allowAnonymous: false
allowAuthenticated: false
DeleteDescriptionReference:
roles:
- Admin
claims: [ ]
clients: [ ]
allowAnonymous: false
allowAuthenticated: false