2023-10-23 10:31:07 +02:00
|
|
|
package eu.eudat.service.dmp;
|
|
|
|
|
2023-11-02 14:55:17 +01:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2023-10-23 11:51:11 +02:00
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
|
|
import eu.eudat.authorization.Permission;
|
|
|
|
import eu.eudat.commons.JsonHandlingService;
|
2023-11-03 15:02:47 +01:00
|
|
|
import eu.eudat.commons.XmlHandlingService;
|
2023-11-14 17:18:20 +01:00
|
|
|
import eu.eudat.commons.enums.*;
|
2023-11-08 10:45:33 +01:00
|
|
|
import eu.eudat.commons.scope.user.UserScope;
|
2023-11-21 13:26:14 +01:00
|
|
|
import eu.eudat.commons.types.dmp.DmpBlueprintValueEntity;
|
|
|
|
import eu.eudat.commons.types.dmp.DmpContactEntity;
|
|
|
|
import eu.eudat.commons.types.dmp.DmpPropertiesEntity;
|
2023-11-03 15:02:47 +01:00
|
|
|
import eu.eudat.commons.types.reference.DefinitionEntity;
|
|
|
|
import eu.eudat.commons.types.reference.FieldEntity;
|
2023-10-23 11:51:11 +02:00
|
|
|
import eu.eudat.convention.ConventionService;
|
2023-11-06 16:42:12 +01:00
|
|
|
import eu.eudat.data.*;
|
2023-10-23 11:51:11 +02:00
|
|
|
import eu.eudat.errorcode.ErrorThesaurusProperties;
|
|
|
|
import eu.eudat.event.DmpTouchedEvent;
|
|
|
|
import eu.eudat.event.EventBroker;
|
|
|
|
import eu.eudat.model.Dmp;
|
2023-12-04 17:16:57 +01:00
|
|
|
import eu.eudat.model.DmpUser;
|
2023-11-03 15:02:47 +01:00
|
|
|
import eu.eudat.model.Reference;
|
2023-10-23 11:51:11 +02:00
|
|
|
import eu.eudat.model.builder.DmpBuilder;
|
2023-12-04 17:16:57 +01:00
|
|
|
import eu.eudat.model.builder.DmpUserBuilder;
|
2023-11-24 11:39:26 +01:00
|
|
|
import eu.eudat.model.deleter.*;
|
2023-11-08 16:13:07 +01:00
|
|
|
import eu.eudat.model.persist.*;
|
2023-11-21 13:26:14 +01:00
|
|
|
import eu.eudat.model.persist.dmpproperties.DmpBlueprintValuePersist;
|
|
|
|
import eu.eudat.model.persist.dmpproperties.DmpContactPersist;
|
|
|
|
import eu.eudat.model.persist.dmpproperties.DmpPropertiesPersist;
|
2023-11-03 15:02:47 +01:00
|
|
|
import eu.eudat.model.persist.referencedefinition.DefinitionPersist;
|
|
|
|
import eu.eudat.model.persist.referencedefinition.FieldPersist;
|
2023-11-06 16:42:12 +01:00
|
|
|
import eu.eudat.query.*;
|
2023-11-10 15:13:55 +01:00
|
|
|
import eu.eudat.service.description.DescriptionService;
|
2023-10-23 11:51:11 +02:00
|
|
|
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.QueryFactory;
|
|
|
|
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.BaseFieldSet;
|
|
|
|
import gr.cite.tools.fieldset.FieldSet;
|
|
|
|
import gr.cite.tools.logging.LoggerService;
|
|
|
|
import jakarta.persistence.EntityManager;
|
2023-11-02 17:12:36 +01:00
|
|
|
import jakarta.xml.bind.JAXBException;
|
2023-11-03 15:02:47 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2023-10-23 11:51:11 +02:00
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.context.MessageSource;
|
|
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
2023-11-14 10:44:01 +01:00
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
2023-10-23 11:51:11 +02:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import javax.management.InvalidApplicationException;
|
2023-11-02 17:12:36 +01:00
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import javax.xml.transform.TransformerException;
|
2023-11-14 12:41:57 +01:00
|
|
|
import java.io.IOException;
|
2023-10-23 11:51:11 +02:00
|
|
|
import java.time.Instant;
|
2023-11-14 10:44:01 +01:00
|
|
|
import java.util.*;
|
2023-11-03 15:02:47 +01:00
|
|
|
import java.util.stream.Collectors;
|
2023-10-23 11:51:11 +02:00
|
|
|
|
|
|
|
@Service
|
2023-10-23 10:31:07 +02:00
|
|
|
public class DmpServiceImpl implements DmpService {
|
|
|
|
|
2023-10-23 11:51:11 +02:00
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpServiceImpl.class));
|
|
|
|
|
|
|
|
private final EntityManager entityManager;
|
|
|
|
|
|
|
|
private final AuthorizationService authorizationService;
|
|
|
|
|
|
|
|
private final DeleterFactory deleterFactory;
|
|
|
|
|
|
|
|
private final BuilderFactory builderFactory;
|
|
|
|
|
2023-11-02 17:12:36 +01:00
|
|
|
private final QueryFactory queryFactory;
|
|
|
|
|
2023-10-23 11:51:11 +02:00
|
|
|
private final ConventionService conventionService;
|
|
|
|
|
|
|
|
private final ErrorThesaurusProperties errors;
|
|
|
|
|
|
|
|
private final MessageSource messageSource;
|
|
|
|
|
2023-11-03 15:02:47 +01:00
|
|
|
private final XmlHandlingService xmlHandlingService;
|
|
|
|
|
2023-11-02 14:55:17 +01:00
|
|
|
private final JsonHandlingService jsonHandlingService;
|
|
|
|
|
2023-11-08 10:45:33 +01:00
|
|
|
private final UserScope userScope;
|
|
|
|
|
2023-10-23 11:51:11 +02:00
|
|
|
private final EventBroker eventBroker;
|
|
|
|
|
2023-11-10 15:13:55 +01:00
|
|
|
private final DescriptionService descriptionService;
|
|
|
|
|
2023-10-23 11:51:11 +02:00
|
|
|
@Autowired
|
|
|
|
public DmpServiceImpl(
|
|
|
|
EntityManager entityManager,
|
|
|
|
AuthorizationService authorizationService,
|
|
|
|
DeleterFactory deleterFactory,
|
|
|
|
BuilderFactory builderFactory,
|
2023-11-08 10:45:33 +01:00
|
|
|
QueryFactory queryFactory,
|
|
|
|
ConventionService conventionService,
|
2023-10-23 11:51:11 +02:00
|
|
|
ErrorThesaurusProperties errors,
|
|
|
|
MessageSource messageSource,
|
2023-11-08 10:45:33 +01:00
|
|
|
XmlHandlingService xmlHandlingService,
|
|
|
|
JsonHandlingService jsonHandlingService,
|
2023-11-10 15:13:55 +01:00
|
|
|
UserScope userScope, EventBroker eventBroker, DescriptionService descriptionService) {
|
2023-10-23 11:51:11 +02:00
|
|
|
this.entityManager = entityManager;
|
|
|
|
this.authorizationService = authorizationService;
|
|
|
|
this.deleterFactory = deleterFactory;
|
|
|
|
this.builderFactory = builderFactory;
|
2023-11-02 17:12:36 +01:00
|
|
|
this.queryFactory = queryFactory;
|
2023-10-23 11:51:11 +02:00
|
|
|
this.conventionService = conventionService;
|
|
|
|
this.errors = errors;
|
|
|
|
this.messageSource = messageSource;
|
2023-11-03 15:02:47 +01:00
|
|
|
this.xmlHandlingService = xmlHandlingService;
|
2023-11-02 14:55:17 +01:00
|
|
|
this.jsonHandlingService = jsonHandlingService;
|
2023-11-08 10:45:33 +01:00
|
|
|
this.userScope = userScope;
|
2023-10-23 11:51:11 +02:00
|
|
|
this.eventBroker = eventBroker;
|
2023-11-10 15:13:55 +01:00
|
|
|
this.descriptionService = descriptionService;
|
2023-10-23 11:51:11 +02:00
|
|
|
}
|
|
|
|
|
2023-11-03 15:02:47 +01:00
|
|
|
public Dmp persist(DmpPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JsonProcessingException {
|
2023-10-23 11:51:11 +02:00
|
|
|
this.authorizationService.authorizeForce(Permission.EditDmp);
|
|
|
|
|
2023-11-03 15:02:47 +01:00
|
|
|
DmpEntity data = this.patchAndSave(model);
|
2023-10-23 11:51:11 +02:00
|
|
|
|
2023-11-06 16:42:12 +01:00
|
|
|
this.patchAndSaveReferences(model.getReferences(), data.getId());
|
|
|
|
|
2023-11-24 11:39:26 +01:00
|
|
|
this.patchAndSaveTemplates(data.getId(), model.getDescriptionTemplates());
|
2023-10-23 11:51:11 +02:00
|
|
|
|
|
|
|
this.eventBroker.emit(new DmpTouchedEvent(data.getId()));
|
2023-11-03 15:02:47 +01:00
|
|
|
|
2023-11-17 18:01:44 +01:00
|
|
|
return this.builderFactory.builder(DmpBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(BaseFieldSet.build(fields, Dmp._id, Dmp._hash), data);
|
2023-10-23 11:51:11 +02:00
|
|
|
}
|
|
|
|
|
2023-11-14 12:41:57 +01:00
|
|
|
public void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException, IOException {
|
2023-10-23 11:51:11 +02:00
|
|
|
logger.debug("deleting dmp: {}", id);
|
|
|
|
|
|
|
|
this.authorizationService.authorizeForce(Permission.DeleteDmp);
|
|
|
|
|
2023-11-14 12:41:57 +01:00
|
|
|
this.deleterFactory.deleter(DmpDeleter.class).deleteAndSaveByIds(List.of(id), false);
|
2023-10-23 11:51:11 +02:00
|
|
|
}
|
|
|
|
|
2023-11-02 17:12:36 +01:00
|
|
|
@Override
|
2023-11-08 16:13:07 +01:00
|
|
|
public Dmp createNewVersion(NewVersionDmpPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException {
|
2023-11-02 17:12:36 +01:00
|
|
|
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());
|
2023-11-14 17:18:20 +01:00
|
|
|
DmpQuery latestVersionDmpEntityQuery = this.queryFactory.query(DmpQuery.class).groupIds(oldDmpEntity.getGroupId()).versionStatuses(DmpVersionStatus.Current);
|
|
|
|
List<DmpEntity> latestVersionDmps = latestVersionDmpEntityQuery.collect();
|
|
|
|
if (latestVersionDmps.isEmpty()) throw new MyValidationException("Previous dmp not found");
|
|
|
|
if (latestVersionDmps.size() > 1) throw new MyValidationException("Multiple previous dmps found");
|
|
|
|
if (!latestVersionDmps.get(0).getVersion().equals(oldDmpEntity.getVersion())){
|
2023-11-03 15:02:47 +01:00
|
|
|
throw new MyValidationException(this.errors.getDmpNewVersionConflict().getCode(), this.errors.getDmpNewVersionConflict().getMessage());
|
2023-11-02 17:12:36 +01:00
|
|
|
}
|
|
|
|
|
2023-11-10 15:13:55 +01:00
|
|
|
DmpEntity newDmp = new DmpEntity();
|
|
|
|
newDmp.setId(UUID.randomUUID());
|
|
|
|
newDmp.setIsActive(IsActive.Active);
|
|
|
|
newDmp.setCreatedAt(Instant.now());
|
|
|
|
newDmp.setUpdatedAt(Instant.now());
|
|
|
|
newDmp.setGroupId(oldDmpEntity.getGroupId());
|
2023-11-14 17:18:20 +01:00
|
|
|
newDmp.setVersionStatus(DmpVersionStatus.Current);
|
2023-11-10 15:13:55 +01:00
|
|
|
newDmp.setVersion((short)(oldDmpEntity.getVersion() + 1));
|
|
|
|
newDmp.setDescription(model.getDescription());
|
|
|
|
newDmp.setLabel(model.getLabel());
|
|
|
|
newDmp.setLanguage(oldDmpEntity.getLanguage());
|
|
|
|
newDmp.setStatus(DmpStatus.Draft);
|
|
|
|
newDmp.setProperties(oldDmpEntity.getProperties());
|
2023-11-20 16:09:24 +01:00
|
|
|
newDmp.setBlueprintId(model.getBlueprintId());
|
2023-11-08 16:13:07 +01:00
|
|
|
|
|
|
|
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();
|
2023-11-10 15:13:55 +01:00
|
|
|
List<DmpDescriptionTemplateEntity> dmpDescriptionTemplates = this.queryFactory.query(DmpDescriptionTemplateQuery.class)
|
|
|
|
.dmpIds(model.getId())
|
|
|
|
.isActive(IsActive.Active)
|
|
|
|
.collect();
|
2023-11-08 16:13:07 +01:00
|
|
|
|
|
|
|
for (DmpUserEntity dmpUser : dmpUsers) {
|
|
|
|
DmpUserEntity newUser = new DmpUserEntity();
|
|
|
|
newUser.setId(UUID.randomUUID());
|
2023-11-10 15:13:55 +01:00
|
|
|
newUser.setDmp(newDmp.getId());
|
2023-11-20 16:09:24 +01:00
|
|
|
newUser.setUserId(dmpUser.getUserId());
|
2023-11-08 16:13:07 +01:00
|
|
|
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());
|
2023-11-10 15:13:55 +01:00
|
|
|
newReference.setDmpId(newDmp.getId());
|
2023-11-08 16:13:07 +01:00
|
|
|
newReference.setReferenceId(dmpReference.getReferenceId());
|
|
|
|
newReference.setData(dmpReference.getData());
|
|
|
|
newReference.setCreatedAt(Instant.now());
|
|
|
|
newReference.setUpdatedAt(Instant.now());
|
|
|
|
newReference.setIsActive(IsActive.Active);
|
|
|
|
|
|
|
|
this.entityManager.persist(newReference);
|
|
|
|
}
|
2023-11-02 17:12:36 +01:00
|
|
|
|
2023-11-10 15:13:55 +01:00
|
|
|
for (DmpDescriptionTemplateEntity dmpDescriptionTemplate : dmpDescriptionTemplates) {
|
|
|
|
DmpDescriptionTemplateEntity newTemplate = new DmpDescriptionTemplateEntity();
|
|
|
|
newTemplate.setId(UUID.randomUUID());
|
|
|
|
newTemplate.setDmpId(newDmp.getId());
|
2023-11-24 11:39:26 +01:00
|
|
|
newTemplate.setDescriptionTemplateGroupId(dmpDescriptionTemplate.getDescriptionTemplateGroupId());
|
2023-11-10 15:13:55 +01:00
|
|
|
newTemplate.setSectionId(dmpDescriptionTemplate.getSectionId());
|
|
|
|
newTemplate.setCreatedAt(Instant.now());
|
|
|
|
newTemplate.setUpdatedAt(Instant.now());
|
|
|
|
newTemplate.setIsActive(IsActive.Active);
|
|
|
|
|
|
|
|
this.entityManager.persist(newTemplate);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (UUID descriptionId : model.getDescriptions()) {
|
|
|
|
this.descriptionService.clone(newDmp.getId(), descriptionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.entityManager.persist(newDmp);
|
2023-11-02 17:12:36 +01:00
|
|
|
|
2023-11-14 17:18:20 +01:00
|
|
|
oldDmpEntity.setVersionStatus(DmpVersionStatus.Previous);
|
|
|
|
this.entityManager.merge(oldDmpEntity);
|
|
|
|
|
2023-11-02 17:12:36 +01:00
|
|
|
this.entityManager.flush();
|
|
|
|
|
2023-11-10 15:13:55 +01:00
|
|
|
return this.builderFactory.builder(DmpBuilder.class).build(BaseFieldSet.build(fields, Dmp._id), newDmp);
|
2023-11-02 17:12:36 +01:00
|
|
|
}
|
|
|
|
|
2023-11-09 13:10:32 +01:00
|
|
|
@Override
|
|
|
|
public Dmp buildClone(CloneDmpPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException {
|
|
|
|
this.authorizationService.authorizeForce(Permission.CloneDmp);
|
|
|
|
|
2023-11-17 18:01:44 +01:00
|
|
|
DmpEntity existingDmpEntity = this.queryFactory.query(DmpQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).ids(model.getId()).firstAs(fields);
|
2023-11-10 15:13:55 +01:00
|
|
|
if (!this.conventionService.isValidGuid(model.getId()) || existingDmpEntity == null)
|
|
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
DmpEntity newDmp = new DmpEntity();
|
|
|
|
newDmp.setId(UUID.randomUUID());
|
|
|
|
newDmp.setIsActive(IsActive.Active);
|
|
|
|
newDmp.setCreatedAt(Instant.now());
|
|
|
|
newDmp.setUpdatedAt(Instant.now());
|
|
|
|
newDmp.setGroupId(UUID.randomUUID());
|
|
|
|
newDmp.setVersion((short) 1);
|
|
|
|
newDmp.setDescription(model.getDescription());
|
|
|
|
newDmp.setLabel(model.getLabel());
|
|
|
|
newDmp.setLanguage(existingDmpEntity.getLanguage());
|
|
|
|
newDmp.setStatus(DmpStatus.Draft);
|
|
|
|
newDmp.setProperties(existingDmpEntity.getProperties());
|
2023-11-20 16:09:24 +01:00
|
|
|
newDmp.setBlueprintId(existingDmpEntity.getBlueprintId());
|
2023-11-10 15:13:55 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
List<DmpDescriptionTemplateEntity> dmpDescriptionTemplates = this.queryFactory.query(DmpDescriptionTemplateQuery.class)
|
|
|
|
.dmpIds(model.getId())
|
|
|
|
.isActive(IsActive.Active)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
for (DmpUserEntity dmpUser : dmpUsers) {
|
|
|
|
DmpUserEntity newUser = new DmpUserEntity();
|
|
|
|
newUser.setId(UUID.randomUUID());
|
|
|
|
newUser.setDmp(newDmp.getId());
|
2023-11-20 16:09:24 +01:00
|
|
|
newUser.setUserId(dmpUser.getUserId());
|
2023-11-10 15:13:55 +01:00
|
|
|
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(newDmp.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);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (DmpDescriptionTemplateEntity dmpDescriptionTemplate : dmpDescriptionTemplates) {
|
|
|
|
DmpDescriptionTemplateEntity newTemplate = new DmpDescriptionTemplateEntity();
|
|
|
|
newTemplate.setId(UUID.randomUUID());
|
|
|
|
newTemplate.setDmpId(newDmp.getId());
|
2023-11-24 11:39:26 +01:00
|
|
|
newTemplate.setDescriptionTemplateGroupId(dmpDescriptionTemplate.getDescriptionTemplateGroupId());
|
2023-11-10 15:13:55 +01:00
|
|
|
newTemplate.setSectionId(dmpDescriptionTemplate.getSectionId());
|
|
|
|
newTemplate.setCreatedAt(Instant.now());
|
|
|
|
newTemplate.setUpdatedAt(Instant.now());
|
|
|
|
newTemplate.setIsActive(IsActive.Active);
|
|
|
|
|
|
|
|
this.entityManager.persist(newTemplate);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
DmpEntity resultingDmpEntity = this.queryFactory.query(DmpQuery.class).ids(newDmp.getId()).firstAs(fields);
|
|
|
|
return this.builderFactory.builder(DmpBuilder.class).build(fields, resultingDmpEntity);
|
2023-11-09 13:10:32 +01:00
|
|
|
}
|
|
|
|
|
2023-11-13 15:46:35 +01:00
|
|
|
@Override
|
2023-12-04 17:16:57 +01:00
|
|
|
public List<DmpUser> assignUsers(UUID dmp, List<DmpUserPersist> model, FieldSet fieldSet) throws InvalidApplicationException {
|
2023-11-13 15:46:35 +01:00
|
|
|
this.authorizationService.authorizeForce(Permission.AssignDmpUsers);
|
|
|
|
|
2023-11-13 16:05:25 +01:00
|
|
|
List<DmpUserEntity> existingUsers = this.queryFactory.query(DmpUserQuery.class)
|
|
|
|
.dmpIds(dmp)
|
|
|
|
.isActives(IsActive.Active)
|
|
|
|
.collect();
|
2023-11-13 15:46:35 +01:00
|
|
|
|
2023-12-04 17:16:57 +01:00
|
|
|
List<UUID> updatedCreatedIds = new ArrayList<>();
|
2023-11-13 15:46:35 +01:00
|
|
|
for (DmpUserPersist dmpUser : model) {
|
2023-12-04 17:16:57 +01:00
|
|
|
DmpUserEntity dmpUserEntity = existingUsers.stream().filter(x-> x.getDmp().equals(dmp) && x.getUserId().equals(dmpUser.getUser()) && x.getRole().equals(dmpUser.getRole())).findFirst().orElse(null);
|
|
|
|
if (dmpUserEntity == null){
|
|
|
|
dmpUserEntity = new DmpUserEntity();
|
|
|
|
dmpUserEntity.setId(UUID.randomUUID());
|
|
|
|
dmpUserEntity.setDmp(dmp);
|
|
|
|
dmpUserEntity.setUserId(dmpUser.getUser());
|
|
|
|
dmpUserEntity.setRole(dmpUser.getRole());
|
|
|
|
dmpUserEntity.setCreatedAt(Instant.now());
|
|
|
|
dmpUserEntity.setUpdatedAt(Instant.now());
|
|
|
|
dmpUserEntity.setIsActive(IsActive.Active);
|
|
|
|
this.entityManager.persist(dmpUserEntity);
|
|
|
|
}
|
|
|
|
updatedCreatedIds.add(dmpUserEntity.getUserId());
|
2023-11-13 15:46:35 +01:00
|
|
|
}
|
|
|
|
|
2023-12-04 17:16:57 +01:00
|
|
|
List<DmpUserEntity> toDelete = existingUsers.stream().filter(x-> updatedCreatedIds.stream().noneMatch(y-> y.equals(x.getId()))).collect(Collectors.toList());
|
|
|
|
if (!toDelete.isEmpty()) this.deleterFactory.deleter(DmpUserDeleter.class).delete(toDelete);
|
2023-11-13 15:46:35 +01:00
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
2023-12-04 17:16:57 +01:00
|
|
|
List<DmpUserEntity> persisted = this.queryFactory.query(DmpUserQuery.class)
|
2023-11-13 16:05:25 +01:00
|
|
|
.dmpIds(dmp)
|
|
|
|
.isActives(IsActive.Active)
|
|
|
|
.collect();
|
2023-12-04 17:16:57 +01:00
|
|
|
return this.builderFactory.builder(DmpUserBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(BaseFieldSet.build(fieldSet, DmpUser._id, DmpUser._hash), persisted);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Dmp removeUser(DmpUserRemovePersist model, FieldSet fields) throws InvalidApplicationException {
|
|
|
|
this.authorizationService.authorizeForce(Permission.AssignDmpUsers);
|
|
|
|
DmpEntity data = this.entityManager.find(DmpEntity.class, model.getId());
|
|
|
|
if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
List<DmpUserEntity> existingUsers = this.queryFactory.query(DmpUserQuery.class)
|
|
|
|
.dmpIds(model.getDmpId()).ids(model.getId()).userRoles(model.getRole())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
if (!existingUsers.isEmpty()) this.deleterFactory.deleter(DmpUserDeleter.class).delete(existingUsers);
|
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
return this.builderFactory.builder(DmpBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(BaseFieldSet.build(fields, Dmp._id, Dmp._hash), data);
|
2023-11-13 15:46:35 +01:00
|
|
|
}
|
|
|
|
|
2023-11-14 10:44:01 +01:00
|
|
|
@Override
|
|
|
|
public ResponseEntity<byte[]> export(UUID id, DmpExportType exportType) {
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
switch (exportType){
|
|
|
|
case Xml -> {
|
|
|
|
headers.setContentType(MediaType.APPLICATION_XML);
|
|
|
|
return new ResponseEntity<>(new byte[]{}, headers, HttpStatus.OK);
|
|
|
|
}
|
|
|
|
case Json -> {
|
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
return new ResponseEntity<>(new byte[]{}, headers, HttpStatus.OK);
|
|
|
|
}
|
|
|
|
case Word -> {
|
|
|
|
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
|
|
|
return new ResponseEntity<>(new byte[]{}, headers, HttpStatus.OK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ResponseEntity.badRequest().body(new byte[]{});
|
|
|
|
}
|
|
|
|
|
2023-11-10 15:13:55 +01:00
|
|
|
private DmpEntity patchAndSave(DmpPersist model) throws JsonProcessingException, InvalidApplicationException {
|
2023-11-03 15:02:47 +01:00
|
|
|
Boolean isUpdate = this.conventionService.isValidGuid(model.getId());
|
|
|
|
|
|
|
|
DmpEntity data;
|
2023-11-08 10:45:33 +01:00
|
|
|
DmpUserEntity dmpUserEntity = new DmpUserEntity();
|
2023-11-03 15:02:47 +01:00
|
|
|
if (isUpdate) {
|
|
|
|
data = this.entityManager.find(DmpEntity.class, model.getId());
|
|
|
|
if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
2023-11-13 15:46:35 +01:00
|
|
|
if (model.getStatus() != null && model.getStatus() == DmpStatus.Finalized && data.getStatus() != DmpStatus.Finalized) {
|
|
|
|
this.authorizationService.authorizeForce(Permission.FinalizeDmp);
|
|
|
|
data.setStatus(model.getStatus());
|
|
|
|
data.setFinalizedAt(Instant.now());
|
|
|
|
}
|
2023-11-03 15:02:47 +01:00
|
|
|
} else {
|
|
|
|
data = new DmpEntity();
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
data.setGroupId(UUID.randomUUID());
|
|
|
|
data.setVersion((short) 1);
|
2023-11-10 15:13:55 +01:00
|
|
|
data.setStatus(DmpStatus.Draft);
|
2023-11-14 17:18:20 +01:00
|
|
|
data.setVersionStatus(DmpVersionStatus.Current);
|
2023-11-20 16:09:24 +01:00
|
|
|
data.setCreatorId(userScope.getUserId());
|
|
|
|
data.setBlueprintId(model.getBlueprint());
|
2023-11-03 15:02:47 +01:00
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
data.setCreatedAt(Instant.now());
|
2023-11-08 10:45:33 +01:00
|
|
|
|
|
|
|
dmpUserEntity.setId(UUID.randomUUID());
|
|
|
|
dmpUserEntity.setDmp(data.getId());
|
2023-11-20 16:09:24 +01:00
|
|
|
dmpUserEntity.setUserId(userScope.getUserId());
|
2023-11-08 10:45:33 +01:00
|
|
|
dmpUserEntity.setRole(DmpUserRole.Owner);
|
|
|
|
dmpUserEntity.setCreatedAt(Instant.now());
|
|
|
|
dmpUserEntity.setUpdatedAt(Instant.now());
|
|
|
|
dmpUserEntity.setIsActive(IsActive.Active);
|
2023-11-03 15:02:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data.setLabel(model.getLabel());
|
2023-11-21 13:26:14 +01:00
|
|
|
data.setProperties(this.jsonHandlingService.toJson(this.buildDmpPropertiesEntity(model.getProperties())));
|
2023-11-03 15:02:47 +01:00
|
|
|
data.setDescription(model.getDescription());
|
2023-11-10 18:11:15 +01:00
|
|
|
data.setAccessType(model.getAccessType());
|
2023-11-03 15:02:47 +01:00
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
if (isUpdate)
|
|
|
|
this.entityManager.merge(data);
|
2023-11-08 10:45:33 +01:00
|
|
|
else {
|
2023-11-03 15:02:47 +01:00
|
|
|
this.entityManager.persist(data);
|
2023-11-08 10:45:33 +01:00
|
|
|
this.entityManager.persist(dmpUserEntity);
|
|
|
|
}
|
2023-11-03 15:02:47 +01:00
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2023-11-21 13:26:14 +01:00
|
|
|
private @NotNull DmpPropertiesEntity buildDmpPropertiesEntity(DmpPropertiesPersist persist){
|
|
|
|
DmpPropertiesEntity data = new DmpPropertiesEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getContacts())){
|
|
|
|
data.setContacts(new ArrayList<>());
|
|
|
|
for (DmpContactPersist contactPersist: persist.getContacts()) {
|
|
|
|
data.getContacts().add(this.buildDmpContactEntity(contactPersist));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getDmpBlueprintValues())){
|
|
|
|
data.setDmpBlueprintValues(new ArrayList<>());
|
|
|
|
for (DmpBlueprintValuePersist fieldValuePersist: persist.getDmpBlueprintValues()) {
|
|
|
|
data.getDmpBlueprintValues().add(this.buildDmpBlueprintValueEntity(fieldValuePersist));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull DmpContactEntity buildDmpContactEntity(DmpContactPersist persist){
|
|
|
|
DmpContactEntity data = new DmpContactEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
|
|
|
|
data.setEmail(persist.getEmail());
|
|
|
|
data.setLastName(persist.getLastName());
|
|
|
|
data.setFirstName(persist.getFirstName());
|
|
|
|
data.setUserId(persist.getUserId());
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private @NotNull DmpBlueprintValueEntity buildDmpBlueprintValueEntity(DmpBlueprintValuePersist persist){
|
|
|
|
DmpBlueprintValueEntity data = new DmpBlueprintValueEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
|
|
|
|
data.setValue(persist.getFieldValue());
|
|
|
|
data.setFieldId(persist.getFieldId());
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2023-11-06 16:42:12 +01:00
|
|
|
private void patchAndSaveReferences(List<DmpReferencePersist> models, UUID dmpId) throws InvalidApplicationException {
|
2023-11-03 15:02:47 +01:00
|
|
|
if (models == null || models.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
List<DmpReferenceEntity> references = this.queryFactory.query(DmpReferenceQuery.class).dmpIds(dmpId).collect();
|
|
|
|
Map<UUID, List<DmpReferenceEntity>> referencesLookup = this.conventionService.toDictionaryOfList(references, DmpReferenceEntity::getDmpId);
|
|
|
|
|
|
|
|
List<ReferenceEntity> existingReferences;
|
|
|
|
if (referencesLookup.containsKey(dmpId))
|
|
|
|
existingReferences = this.queryFactory.query(ReferenceQuery.class).ids(referencesLookup.get(dmpId).stream().map(DmpReferenceEntity::getId).toList()).collect();
|
|
|
|
else
|
|
|
|
existingReferences = new ArrayList<>();
|
|
|
|
|
2023-11-06 12:31:01 +01:00
|
|
|
List<UUID> updatedReferencesIds = models.stream().map(x -> x.getReference().getId()).filter(this.conventionService::isValidGuid).distinct().toList();
|
2023-11-03 15:02:47 +01:00
|
|
|
List<ReferenceEntity> toDelete = existingReferences.stream().filter(x -> !updatedReferencesIds.contains(x.getId())).toList();
|
2023-11-03 15:47:54 +01:00
|
|
|
List<DmpReferenceEntity> dmpReferenceRecordsToDelete = this.queryFactory.query(DmpReferenceQuery.class).referenceIds(toDelete.stream().map(ReferenceEntity::getId).toList()).collect();
|
|
|
|
this.deleterFactory.deleter(DmpReferenceDeleter.class).delete(dmpReferenceRecordsToDelete);
|
2023-11-03 15:02:47 +01:00
|
|
|
|
|
|
|
Map<UUID, ReferenceEntity> existingReferencesLookup = existingReferences.stream().collect(Collectors.toMap(ReferenceEntity::getId, x -> x));
|
|
|
|
|
2023-11-06 12:31:01 +01:00
|
|
|
for (DmpReferencePersist model : models) {
|
|
|
|
ReferencePersist referenceModel = model.getReference();
|
|
|
|
|
|
|
|
Boolean isUpdate = this.conventionService.isValidGuid(referenceModel.getId());
|
|
|
|
|
2023-11-03 15:02:47 +01:00
|
|
|
ReferenceEntity data;
|
|
|
|
if (isUpdate) {
|
2023-11-06 12:31:01 +01:00
|
|
|
if (!existingReferencesLookup.containsKey(referenceModel.getId()))
|
|
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{referenceModel.getId(), Reference.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
data = existingReferencesLookup.get(referenceModel.getId());
|
2023-11-03 15:02:47 +01:00
|
|
|
if (data == null)
|
2023-11-06 12:31:01 +01:00
|
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{referenceModel.getId(), Reference.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(referenceModel.getHash()))
|
2023-11-03 15:02:47 +01:00
|
|
|
throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
|
|
|
} else {
|
|
|
|
data = new ReferenceEntity();
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
data.setCreatedAt(Instant.now());
|
2023-11-03 15:47:54 +01:00
|
|
|
|
|
|
|
DmpReferenceEntity dmpReference = new DmpReferenceEntity();
|
|
|
|
dmpReference.setReferenceId(data.getId());
|
|
|
|
dmpReference.setDmpId(dmpId);
|
|
|
|
dmpReference.setCreatedAt(Instant.now());
|
|
|
|
dmpReference.setUpdatedAt(Instant.now());
|
|
|
|
dmpReference.setIsActive(IsActive.Active);
|
2023-11-06 12:31:01 +01:00
|
|
|
dmpReference.setData(model.getData());
|
2023-11-03 15:47:54 +01:00
|
|
|
this.entityManager.persist(dmpReference);
|
2023-11-03 15:02:47 +01:00
|
|
|
}
|
|
|
|
|
2023-11-06 12:31:01 +01:00
|
|
|
data.setDefinition(this.xmlHandlingService.toXmlSafe(this.buildDefinitionEntity(referenceModel.getDefinition())));
|
2023-11-03 15:02:47 +01:00
|
|
|
data.setUpdatedAt(Instant.now());
|
2023-11-06 12:31:01 +01:00
|
|
|
data.setReference(referenceModel.getReference());
|
|
|
|
data.setAbbreviation(referenceModel.getAbbreviation());
|
|
|
|
data.setSource(referenceModel.getSource());
|
|
|
|
data.setSourceType(referenceModel.getSourceType());
|
2023-11-03 15:02:47 +01:00
|
|
|
|
|
|
|
if (isUpdate)
|
|
|
|
this.entityManager.merge(data);
|
|
|
|
else
|
|
|
|
this.entityManager.persist(data);
|
|
|
|
|
|
|
|
}
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-06 16:42:12 +01:00
|
|
|
|
2023-11-24 11:39:26 +01:00
|
|
|
private void patchAndSaveTemplates(UUID id, List<DmpDescriptionTemplatePersist> models) throws InvalidApplicationException {
|
|
|
|
if (models == null) models = new ArrayList<>();
|
|
|
|
List<DmpDescriptionTemplateEntity> items = this.queryFactory.query(DmpDescriptionTemplateQuery.class).isActive(IsActive.Active).dmpIds(id).collect();
|
|
|
|
List<UUID> updatedCreatedIds = new ArrayList<>();
|
2023-11-06 16:42:12 +01:00
|
|
|
for (DmpDescriptionTemplatePersist model : models) {
|
2023-11-24 11:39:26 +01:00
|
|
|
DmpDescriptionTemplateEntity data = items.stream().filter(x -> x.getDescriptionTemplateGroupId().equals(model.getDescriptionTemplateGroupId()) && x.getSectionId().equals(model.getSectionId())).findFirst().orElse(null);
|
|
|
|
if (data == null){
|
|
|
|
data = new DmpDescriptionTemplateEntity();
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
data.setCreatedAt(Instant.now());
|
|
|
|
data.setDmpId(id);
|
|
|
|
data.setSectionId(model.getSectionId());
|
|
|
|
data.setDescriptionTemplateGroupId(model.getDescriptionTemplateGroupId());
|
|
|
|
this.entityManager.persist(data);
|
2023-11-06 16:42:12 +01:00
|
|
|
}
|
2023-11-24 11:39:26 +01:00
|
|
|
updatedCreatedIds.add(data.getId());
|
2023-11-06 16:42:12 +01:00
|
|
|
}
|
2023-11-24 11:39:26 +01:00
|
|
|
List<DmpDescriptionTemplateEntity> toDelete = items.stream().filter(x-> updatedCreatedIds.stream().noneMatch(y-> y.equals(x.getId()))).collect(Collectors.toList());
|
|
|
|
|
|
|
|
this.deleterFactory.deleter(DmpDescriptionTemplateDeleter.class).delete(toDelete);
|
2023-11-06 16:42:12 +01:00
|
|
|
}
|
|
|
|
|
2023-11-03 15:02:47 +01:00
|
|
|
private @NotNull DefinitionEntity buildDefinitionEntity(DefinitionPersist persist){
|
|
|
|
DefinitionEntity data = new DefinitionEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getFields())){
|
|
|
|
data.setFields(new ArrayList<>());
|
|
|
|
for (FieldPersist fieldPersist: persist.getFields()) {
|
2023-11-21 13:26:14 +01:00
|
|
|
data.getFields().add(this.buildDmpContactEntity(fieldPersist));
|
2023-11-03 15:02:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2023-11-21 13:26:14 +01:00
|
|
|
private @NotNull FieldEntity buildDmpContactEntity(FieldPersist persist){
|
2023-11-03 15:02:47 +01:00
|
|
|
FieldEntity data = new FieldEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
|
|
|
|
data.setCode(persist.getCode());
|
|
|
|
data.setDataType(persist.getDataType());
|
|
|
|
data.setCode(persist.getCode());
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2023-12-04 17:16:57 +01:00
|
|
|
private DmpUserEntity checkUserRoleIfExists(List<DmpUserEntity> dmpUserEntities, UUID dmp, UUID user, DmpUserRole role) {
|
2023-11-13 15:46:35 +01:00
|
|
|
for (DmpUserEntity dmpUser : dmpUserEntities) {
|
2023-11-20 16:09:24 +01:00
|
|
|
if (dmpUser.getDmp().equals(dmp) && dmpUser.getUserId().equals(user) && dmpUser.getRole() == role) {
|
2023-12-04 17:16:57 +01:00
|
|
|
return dmpUser;
|
2023-11-13 15:46:35 +01:00
|
|
|
};
|
|
|
|
}
|
2023-12-04 17:16:57 +01:00
|
|
|
return null;
|
2023-11-13 15:46:35 +01:00
|
|
|
}
|
|
|
|
|
2023-10-23 10:31:07 +02:00
|
|
|
}
|