Adding new version service method for Dmps

This commit is contained in:
Thomas Georgios Giannos 2023-11-02 18:12:36 +02:00
parent 02d8132f3d
commit e02237e599
6 changed files with 100 additions and 2 deletions

View File

@ -40,6 +40,7 @@ public final class Permission {
public static String BrowseDmp = "BrowseDmp";
public static String EditDmp = "EditDmp";
public static String DeleteDmp = "DeleteDmp";
public static String CreateNewVersionDmp = "CreateNewVersionDmp";
//DmpBlueprint
public static String BrowseDmpBlueprint = "BrowseDmpBlueprint";

View File

@ -20,6 +20,8 @@ public class DmpPersist {
private String description;
private String language;
private String hash;
public UUID getId() {
@ -62,6 +64,14 @@ public class DmpPersist {
this.description = description;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getHash() {
return hash;
}

View File

@ -37,6 +37,8 @@ public class DmpQuery extends QueryBase<DmpEntity> {
private Collection<Integer> versions;
private Collection<UUID> groupIds;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
private final UserScope userScope;
@ -128,6 +130,21 @@ public class DmpQuery extends QueryBase<DmpEntity> {
return this;
}
public DmpQuery groupIds(UUID value) {
this.groupIds = List.of(value);
return this;
}
public DmpQuery groupIds(UUID... value) {
this.groupIds = Arrays.asList(value);
return this;
}
public DmpQuery groupIds(Collection<UUID> values) {
this.groupIds = values;
return this;
}
public DmpQuery authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
@ -154,7 +171,8 @@ public class DmpQuery extends QueryBase<DmpEntity> {
for (UUID item : this.ids)
inClause.value(item);
predicates.add(inClause);
}if (this.excludedIds != null) {
}
if (this.excludedIds != null) {
CriteriaBuilder.In<UUID> notInClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpEntity._id));
for (UUID item : this.excludedIds)
notInClause.value(item);
@ -178,6 +196,12 @@ public class DmpQuery extends QueryBase<DmpEntity> {
inClause.value(item);
predicates.add(inClause);
}
if (this.groupIds != null) {
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpEntity._groupId));
for (UUID item : this.groupIds)
inClause.value(item);
predicates.add(inClause);
}
if (!predicates.isEmpty()) {
Predicate[] predicatesArray = predicates.toArray(new Predicate[0]);
return queryContext.CriteriaBuilder.and(predicatesArray);

View File

@ -1,14 +1,20 @@
package eu.eudat.service.dmp;
import com.fasterxml.jackson.core.JsonProcessingException;
import eu.eudat.model.DescriptionTemplate;
import eu.eudat.model.Dmp;
import eu.eudat.model.persist.DmpPersist;
import eu.eudat.model.persist.NewVersionDescriptionTemplatePersist;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.exception.MyForbiddenException;
import gr.cite.tools.exception.MyNotFoundException;
import gr.cite.tools.exception.MyValidationException;
import gr.cite.tools.fieldset.FieldSet;
import jakarta.xml.bind.JAXBException;
import javax.management.InvalidApplicationException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.util.UUID;
public interface DmpService {
@ -17,4 +23,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;
}

View File

@ -6,17 +6,22 @@ import eu.eudat.authorization.Permission;
import eu.eudat.commons.JsonHandlingService;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.convention.ConventionService;
import eu.eudat.data.DescriptionTemplateEntity;
import eu.eudat.data.DmpEntity;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import eu.eudat.event.DmpTouchedEvent;
import eu.eudat.event.EventBroker;
import eu.eudat.model.DescriptionTemplate;
import eu.eudat.model.Dmp;
import eu.eudat.model.builder.DescriptionTemplateBuilder;
import eu.eudat.model.builder.DmpBuilder;
import eu.eudat.model.deleter.DmpDeleter;
import eu.eudat.model.persist.DmpPersist;
import eu.eudat.query.DmpQuery;
import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.deleter.DeleterFactory;
import gr.cite.tools.data.query.Ordering;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.exception.MyForbiddenException;
@ -27,6 +32,7 @@ import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import jakarta.persistence.EntityManager;
import jakarta.xml.bind.JAXBException;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
@ -34,6 +40,8 @@ import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import javax.management.InvalidApplicationException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.time.Instant;
import java.util.List;
import java.util.UUID;
@ -51,6 +59,8 @@ public class DmpServiceImpl implements DmpService {
private final BuilderFactory builderFactory;
private final QueryFactory queryFactory;
private final ConventionService conventionService;
private final ErrorThesaurusProperties errors;
@ -67,7 +77,7 @@ public class DmpServiceImpl implements DmpService {
AuthorizationService authorizationService,
DeleterFactory deleterFactory,
BuilderFactory builderFactory,
ConventionService conventionService,
QueryFactory queryFactory, ConventionService conventionService,
ErrorThesaurusProperties errors,
MessageSource messageSource,
JsonHandlingService jsonHandlingService,
@ -76,6 +86,7 @@ public class DmpServiceImpl implements DmpService {
this.authorizationService = authorizationService;
this.deleterFactory = deleterFactory;
this.builderFactory = builderFactory;
this.queryFactory = queryFactory;
this.conventionService = conventionService;
this.errors = errors;
this.messageSource = messageSource;
@ -132,4 +143,41 @@ public class DmpServiceImpl implements DmpService {
this.deleterFactory.deleter(DmpDeleter.class).deleteAndSaveByIds(List.of(id));
}
@Override
public Dmp createNewVersion(DmpPersist 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);
DmpEntity oldDmpEntity = this.entityManager.find(DmpEntity.class, model.getId());
if (oldDmpEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
if (!this.conventionService.hashValue(oldDmpEntity.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
DmpQuery latestVersionDmpEntityQuery = this.queryFactory.query(DmpQuery.class).groupIds(oldDmpEntity.getGroupId());
latestVersionDmpEntityQuery.setOrder(new Ordering().addDescending(Dmp._version));
DmpEntity latestVersionDmpEntity = latestVersionDmpEntityQuery.first();
if (!latestVersionDmpEntity.getVersion().equals(oldDmpEntity.getVersion())){
//TODO: (THGIANNOS) Create event for version conflict
// throw new MyValidationException(this.errors.getDescriptionTemplateNewVersionConflict().getCode(), this.errors.getDescriptionTemplateNewVersionConflict().getMessage());
}
DmpEntity data = new DmpEntity();
data.setId(UUID.randomUUID());
data.setIsActive(IsActive.Active);
data.setCreatedAt(Instant.now());
data.setUpdatedAt(Instant.now());
data.setGroupId(oldDmpEntity.getGroupId());
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()));
this.entityManager.persist(data);
this.entityManager.flush();
return this.builderFactory.builder(DmpBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(BaseFieldSet.build(fields, Dmp._id), data);
}
}

View File

@ -175,6 +175,13 @@ permissions:
clients: [ ]
allowAnonymous: false
allowAuthenticated: false
CreateNewVersionDmp:
roles:
- Admin
claims: [ ]
clients: [ ]
allowAnonymous: false
allowAuthenticated: false
# DmpBlueprint
BrowseDmpBlueprint:
roles: