Adding new version dmp persist model, moving associated users and references to new dmp with new version

This commit is contained in:
Thomas Georgios Giannos 2023-11-08 17:13:07 +02:00
parent 0bbad595f5
commit dab5986688
5 changed files with 162 additions and 14 deletions

View File

@ -0,0 +1,71 @@
package eu.eudat.model.persist;
import eu.eudat.commons.validation.FieldNotNullIfOtherSet;
import eu.eudat.commons.validation.ValidId;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.apache.commons.compress.utils.Lists;
import java.util.List;
import java.util.UUID;
@FieldNotNullIfOtherSet(message = "{validation.hashempty}")
public class NewVersionDmpPersist {
@NotNull(message = "{validation.empty}")
@ValidId(message = "{validation.invalidid}")
private UUID id = null;
@NotNull(message = "{validation.empty}")
@NotEmpty(message = "{validation.empty}")
private String label = null;
@NotNull(message = "{validation.empty}")
@NotEmpty(message = "{validation.empty}")
private String description = null;
@NotNull(message = "{validation.empty}")
private List<UUID> descriptionTemplates = Lists.newArrayList();
private String hash;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<UUID> getDescriptionTemplates() {
return descriptionTemplates;
}
public void setDescriptionTemplates(List<UUID> descriptionTemplates) {
this.descriptionTemplates = descriptionTemplates;
}
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
}

View File

@ -1,6 +1,7 @@
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.DmpReferenceEntity;
import eu.eudat.model.DmpReference;
@ -24,6 +25,8 @@ public class DmpReferenceQuery extends QueryBase<DmpReferenceEntity> {
private Collection<UUID> ids;
private Collection<IsActive> isActives;
private Collection<UUID> dmpIds;
private Collection<UUID> referenceIds;
@ -46,6 +49,21 @@ public class DmpReferenceQuery extends QueryBase<DmpReferenceEntity> {
return this;
}
public DmpReferenceQuery isActives(IsActive value) {
this.isActives = List.of(value);
return this;
}
public DmpReferenceQuery isActives(IsActive... value) {
this.isActives = Arrays.asList(value);
return this;
}
public DmpReferenceQuery isActives(Collection<IsActive> values) {
this.isActives = values;
return this;
}
public DmpReferenceQuery dmpIds(UUID value) {
this.dmpIds = List.of(value);
return this;
@ -112,6 +130,12 @@ public class DmpReferenceQuery extends QueryBase<DmpReferenceEntity> {
inClause.value(item);
predicates.add(inClause);
}
if (this.isActives != null) {
CriteriaBuilder.In<IsActive> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpReferenceEntity._isActive));
for (IsActive item : this.isActives)
inClause.value(item);
predicates.add(inClause);
}
if (this.dmpIds != null) {
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpReferenceEntity._dmpId));
for (UUID item : this.dmpIds)

View File

@ -2,11 +2,9 @@ package eu.eudat.query;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.authorization.Permission;
import eu.eudat.commons.enums.DmpAccessType;
import eu.eudat.commons.enums.DmpUserRole;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.commons.scope.user.UserScope;
import eu.eudat.data.DmpDescriptionTemplateEntity;
import eu.eudat.data.DmpEntity;
import eu.eudat.data.DmpUserEntity;
import eu.eudat.model.DmpUser;
import gr.cite.commons.web.authz.service.AuthorizationService;
@ -16,7 +14,6 @@ import gr.cite.tools.data.query.QueryContext;
import jakarta.persistence.Tuple;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Subquery;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@ -30,6 +27,8 @@ public class DmpUserQuery extends QueryBase<DmpUserEntity> {
private Collection<UUID> ids;
private Collection<IsActive> isActives;
private Collection<UUID> dmpIds;
private Collection<UUID> userIds;
@ -54,6 +53,21 @@ public class DmpUserQuery extends QueryBase<DmpUserEntity> {
return this;
}
public DmpUserQuery isActives(IsActive value) {
this.isActives = List.of(value);
return this;
}
public DmpUserQuery isActives(IsActive... value) {
this.isActives = Arrays.asList(value);
return this;
}
public DmpUserQuery isActives(Collection<IsActive> values) {
this.isActives = values;
return this;
}
public DmpUserQuery dmpIds(UUID value) {
this.dmpIds = List.of(value);
return this;
@ -138,7 +152,7 @@ public class DmpUserQuery extends QueryBase<DmpUserEntity> {
predicates.add(queryContext.CriteriaBuilder.equal(queryContext.Root.get(DmpUserEntity._user), ownerId));
}
if (predicates.size() > 0) {
if (!predicates.isEmpty()) {
Predicate[] predicatesArray = predicates.toArray(new Predicate[0]);
return queryContext.CriteriaBuilder.and(predicatesArray);
} else {
@ -155,6 +169,12 @@ public class DmpUserQuery extends QueryBase<DmpUserEntity> {
inClause.value(item);
predicates.add(inClause);
}
if (this.isActives != null) {
CriteriaBuilder.In<IsActive> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpUserEntity._isActive));
for (IsActive item : this.isActives)
inClause.value(item);
predicates.add(inClause);
}
if (this.dmpIds != null) {
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpUserEntity._dmp));
for (UUID item : this.dmpIds)

View File

@ -5,6 +5,7 @@ import eu.eudat.model.DescriptionTemplate;
import eu.eudat.model.Dmp;
import eu.eudat.model.persist.DmpPersist;
import eu.eudat.model.persist.NewVersionDescriptionTemplatePersist;
import eu.eudat.model.persist.NewVersionDmpPersist;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.exception.MyForbiddenException;
import gr.cite.tools.exception.MyNotFoundException;
@ -23,6 +24,6 @@ public interface DmpService {
void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException;
Dmp createNewVersion(DmpPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException;
Dmp createNewVersion(NewVersionDmpPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException;
}

View File

@ -21,10 +21,7 @@ import eu.eudat.model.builder.DmpBuilder;
import eu.eudat.model.deleter.DmpDeleter;
import eu.eudat.model.deleter.DmpDescriptionTemplateDeleter;
import eu.eudat.model.deleter.DmpReferenceDeleter;
import eu.eudat.model.persist.DmpDescriptionTemplatePersist;
import eu.eudat.model.persist.DmpPersist;
import eu.eudat.model.persist.DmpReferencePersist;
import eu.eudat.model.persist.ReferencePersist;
import eu.eudat.model.persist.*;
import eu.eudat.model.persist.referencedefinition.DefinitionPersist;
import eu.eudat.model.persist.referencedefinition.FieldPersist;
import eu.eudat.query.*;
@ -141,7 +138,7 @@ public class DmpServiceImpl implements DmpService {
}
@Override
public Dmp createNewVersion(DmpPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException {
public Dmp createNewVersion(NewVersionDmpPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException {
logger.debug(new MapLogEntry("persisting data dmp (new version)").And("model", model).And("fields", fields));
this.authorizationService.authorizeForce(Permission.CreateNewVersionDmp);
@ -165,9 +162,44 @@ public class DmpServiceImpl implements DmpService {
data.setVersion((short)(oldDmpEntity.getVersion() + 1));
data.setDescription(model.getDescription());
data.setLabel(model.getLabel());
data.setLanguage(model.getLanguage());
data.setStatus(model.getStatus());
data.setProperties(this.jsonHandlingService.toJsonSafe(model.getProperties()));
data.setLanguage(oldDmpEntity.getLanguage());
data.setStatus(oldDmpEntity.getStatus());
data.setProperties(oldDmpEntity.getProperties());
List<DmpUserEntity> dmpUsers = this.queryFactory.query(DmpUserQuery.class)
.dmpIds(model.getId())
.isActives(IsActive.Active)
.collect();
List<DmpReferenceEntity> dmpReferences = this.queryFactory.query(DmpReferenceQuery.class)
.dmpIds(model.getId())
.isActives(IsActive.Active)
.collect();
for (DmpUserEntity dmpUser : dmpUsers) {
DmpUserEntity newUser = new DmpUserEntity();
newUser.setId(UUID.randomUUID());
newUser.setDmp(data.getId());
newUser.setUser(dmpUser.getUser());
newUser.setRole(dmpUser.getRole());
newUser.setCreatedAt(Instant.now());
newUser.setUpdatedAt(Instant.now());
newUser.setIsActive(IsActive.Active);
this.entityManager.persist(newUser);
}
for (DmpReferenceEntity dmpReference : dmpReferences) {
DmpReferenceEntity newReference = new DmpReferenceEntity();
newReference.setId(UUID.randomUUID());
newReference.setDmpId(data.getId());
newReference.setReferenceId(dmpReference.getReferenceId());
newReference.setData(dmpReference.getData());
newReference.setCreatedAt(Instant.now());
newReference.setUpdatedAt(Instant.now());
newReference.setIsActive(IsActive.Active);
this.entityManager.persist(newReference);
}
this.entityManager.persist(data);