Adding description templates on dmp persist

This commit is contained in:
Thomas Georgios Giannos 2023-11-06 17:42:12 +02:00
parent 49e34f9abc
commit a5f46e9328
3 changed files with 109 additions and 8 deletions

View File

@ -0,0 +1,57 @@
package eu.eudat.model.persist;
import eu.eudat.commons.validation.FieldNotNullIfOtherSet;
import eu.eudat.commons.validation.ValidId;
import jakarta.validation.constraints.NotNull;
import java.util.UUID;
@FieldNotNullIfOtherSet(message = "{validation.hashempty}")
public class DmpDescriptionTemplatePersist {
@ValidId(message = "{validation.invalidid}")
private UUID id;
@ValidId(message = "{validation.invalidid}")
@NotNull(message = "{validation.empty}")
private UUID descriptionTemplate;
@ValidId(message = "{validation.invalidid}")
@NotNull(message = "{validation.empty}")
private UUID sectionId;
private String hash;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public UUID getDescriptionTemplate() {
return descriptionTemplate;
}
public void setDescriptionTemplate(UUID descriptionTemplate) {
this.descriptionTemplate = descriptionTemplate;
}
public UUID getSectionId() {
return sectionId;
}
public void setSectionId(UUID sectionId) {
this.sectionId = sectionId;
}
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
}

View File

@ -24,6 +24,8 @@ public class DmpPersist {
private List<DmpReferencePersist> references;
private List<DmpDescriptionTemplatePersist> descriptionTemplates;
private String hash;
public UUID getId() {
@ -82,6 +84,14 @@ public class DmpPersist {
this.references = references;
}
public List<DmpDescriptionTemplatePersist> getDescriptionTemplates() {
return descriptionTemplates;
}
public void setDescriptionTemplates(List<DmpDescriptionTemplatePersist> descriptionTemplates) {
this.descriptionTemplates = descriptionTemplates;
}
public String getHash() {
return hash;
}

View File

@ -9,9 +9,7 @@ import eu.eudat.commons.enums.IsActive;
import eu.eudat.commons.types.reference.DefinitionEntity;
import eu.eudat.commons.types.reference.FieldEntity;
import eu.eudat.convention.ConventionService;
import eu.eudat.data.DmpEntity;
import eu.eudat.data.DmpReferenceEntity;
import eu.eudat.data.ReferenceEntity;
import eu.eudat.data.*;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import eu.eudat.event.DmpTouchedEvent;
import eu.eudat.event.EventBroker;
@ -19,16 +17,16 @@ import eu.eudat.model.Dmp;
import eu.eudat.model.Reference;
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.deleter.ReferenceDeleter;
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.referencedefinition.DefinitionPersist;
import eu.eudat.model.persist.referencedefinition.FieldPersist;
import eu.eudat.query.DmpQuery;
import eu.eudat.query.DmpReferenceQuery;
import eu.eudat.query.ReferenceQuery;
import eu.eudat.query.*;
import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.deleter.DeleterFactory;
@ -119,7 +117,9 @@ public class DmpServiceImpl implements DmpService {
DmpEntity data = this.patchAndSave(model);
this.patchAndSave(model.getReferences(), data.getId());
this.patchAndSaveReferences(model.getReferences(), data.getId());
this.patchAndSaveTemplates(model.getDescriptionTemplates(), data.getId());
this.eventBroker.emit(new DmpTouchedEvent(data.getId()));
@ -202,7 +202,7 @@ public class DmpServiceImpl implements DmpService {
return data;
}
private void patchAndSave(List<DmpReferencePersist> models, UUID dmpId) throws InvalidApplicationException {
private void patchAndSaveReferences(List<DmpReferencePersist> models, UUID dmpId) throws InvalidApplicationException {
if (models == null || models.isEmpty())
return;
@ -269,6 +269,40 @@ public class DmpServiceImpl implements DmpService {
}
private void patchAndSaveTemplates(List<DmpDescriptionTemplatePersist> models, UUID dmpId) throws InvalidApplicationException {
if (models == null || models.isEmpty())
return;
List<DmpDescriptionTemplateEntity> templates = this.queryFactory.query(DmpDescriptionTemplateQuery.class).dmpIds(dmpId).collect();
Map<UUID, List<DmpDescriptionTemplateEntity>> templatesLookup = this.conventionService.toDictionaryOfList(templates, DmpDescriptionTemplateEntity::getDmp);
List<DescriptionTemplateEntity> existingTemplates;
if (templatesLookup.containsKey(dmpId))
existingTemplates = this.queryFactory.query(DescriptionTemplateQuery.class).ids(templatesLookup.get(dmpId).stream().map(DmpDescriptionTemplateEntity::getId).toList()).collect();
else existingTemplates = new ArrayList<>();
List<UUID> updatedTemplatesIds = models.stream().map(DmpDescriptionTemplatePersist::getDescriptionTemplate).filter(this.conventionService::isValidGuid).distinct().toList();
List<DescriptionTemplateEntity> toDelete = existingTemplates.stream().filter(x -> !updatedTemplatesIds.contains(x.getId())).toList();
List<DmpDescriptionTemplateEntity> dmpDescriptionTemplateRecordsToDelete = this.queryFactory.query(DmpDescriptionTemplateQuery.class).descriptionTemplateIds(toDelete.stream().map(DescriptionTemplateEntity::getId).toList()).collect();
this.deleterFactory.deleter(DmpDescriptionTemplateDeleter.class).delete(dmpDescriptionTemplateRecordsToDelete);
for (DmpDescriptionTemplatePersist model : models) {
boolean shouldAdd = existingTemplates.stream().noneMatch(x -> x.getId().equals(model.getDescriptionTemplate()));
if (shouldAdd) {
DmpDescriptionTemplateEntity dmpTemplate = new DmpDescriptionTemplateEntity();
dmpTemplate.setDescriptionTemplate(model.getDescriptionTemplate());
dmpTemplate.setDmp(dmpId);
dmpTemplate.setCreatedAt(Instant.now());
dmpTemplate.setUpdatedAt(Instant.now());
dmpTemplate.setIsActive(IsActive.Active);
this.entityManager.persist(dmpTemplate);
}
}
this.entityManager.flush();
}
private @NotNull DefinitionEntity buildDefinitionEntity(DefinitionPersist persist){
DefinitionEntity data = new DefinitionEntity();
if (persist == null) return data;