2023-10-24 14:38:15 +02:00
|
|
|
package eu.eudat.service.description;
|
|
|
|
|
2024-03-19 16:21:50 +01:00
|
|
|
import eu.eudat.authorization.AffiliatedResource;
|
2023-10-24 14:38:15 +02:00
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
|
|
import eu.eudat.authorization.Permission;
|
2024-03-15 13:13:55 +01:00
|
|
|
import eu.eudat.authorization.authorizationcontentresolver.AuthorizationContentResolver;
|
2023-10-24 14:38:15 +02:00
|
|
|
import eu.eudat.commons.JsonHandlingService;
|
2023-11-15 16:06:49 +01:00
|
|
|
import eu.eudat.commons.XmlHandlingService;
|
2024-02-06 11:27:47 +01:00
|
|
|
import eu.eudat.commons.enums.*;
|
2023-12-14 17:19:43 +01:00
|
|
|
import eu.eudat.commons.enums.notification.NotificationContactType;
|
2024-04-09 13:48:04 +02:00
|
|
|
import eu.eudat.commons.scope.tenant.TenantScope;
|
2023-11-10 15:13:55 +01:00
|
|
|
import eu.eudat.commons.scope.user.UserScope;
|
2024-02-06 11:27:47 +01:00
|
|
|
import eu.eudat.commons.types.description.*;
|
|
|
|
import eu.eudat.commons.types.descriptionreference.DescriptionReferenceDataEntity;
|
|
|
|
import eu.eudat.commons.types.descriptiontemplate.FieldSetEntity;
|
2024-02-08 17:12:47 +01:00
|
|
|
import eu.eudat.commons.types.descriptiontemplate.fielddata.ReferenceTypeDataEntity;
|
2024-02-06 14:03:49 +01:00
|
|
|
import eu.eudat.commons.types.descriptiontemplate.fielddata.UploadDataEntity;
|
2023-12-14 17:19:43 +01:00
|
|
|
import eu.eudat.commons.types.notification.*;
|
2023-11-15 16:06:49 +01:00
|
|
|
import eu.eudat.commons.types.reference.DefinitionEntity;
|
2024-04-01 10:16:19 +02:00
|
|
|
import eu.eudat.commons.notification.NotificationProperties;
|
2023-10-24 14:38:15 +02:00
|
|
|
import eu.eudat.convention.ConventionService;
|
2023-11-15 16:06:49 +01:00
|
|
|
import eu.eudat.data.*;
|
2023-10-24 14:38:15 +02:00
|
|
|
import eu.eudat.errorcode.ErrorThesaurusProperties;
|
|
|
|
import eu.eudat.event.DescriptionTouchedEvent;
|
|
|
|
import eu.eudat.event.EventBroker;
|
2024-03-29 10:47:27 +01:00
|
|
|
import eu.eudat.integrationevent.outbox.annotationentityremoval.AnnotationEntityRemovalIntegrationEventHandler;
|
|
|
|
import eu.eudat.integrationevent.outbox.annotationentitytouch.AnnotationEntityTouchedIntegrationEventHandler;
|
2024-01-19 14:12:33 +01:00
|
|
|
import eu.eudat.integrationevent.outbox.notification.NotifyIntegrationEvent;
|
|
|
|
import eu.eudat.integrationevent.outbox.notification.NotifyIntegrationEventHandler;
|
2023-12-06 17:33:01 +01:00
|
|
|
import eu.eudat.model.*;
|
2023-10-24 14:38:15 +02:00
|
|
|
import eu.eudat.model.builder.DescriptionBuilder;
|
|
|
|
import eu.eudat.model.deleter.DescriptionDeleter;
|
2023-11-15 16:06:49 +01:00
|
|
|
import eu.eudat.model.deleter.DescriptionReferenceDeleter;
|
|
|
|
import eu.eudat.model.deleter.DescriptionTagDeleter;
|
2023-12-18 11:55:19 +01:00
|
|
|
import eu.eudat.model.file.FileEnvelope;
|
2024-02-06 14:03:49 +01:00
|
|
|
import eu.eudat.model.persist.*;
|
2024-02-06 11:27:47 +01:00
|
|
|
import eu.eudat.model.persist.descriptionproperties.*;
|
|
|
|
import eu.eudat.model.persist.descriptionreference.DescriptionReferenceDataPersist;
|
2023-11-15 16:06:49 +01:00
|
|
|
import eu.eudat.model.persist.referencedefinition.DefinitionPersist;
|
|
|
|
import eu.eudat.query.*;
|
2024-01-09 15:02:12 +01:00
|
|
|
import eu.eudat.service.elastic.ElasticService;
|
2024-02-06 14:03:49 +01:00
|
|
|
import eu.eudat.service.storage.StorageFileProperties;
|
|
|
|
import eu.eudat.service.storage.StorageFileService;
|
2023-12-18 11:55:19 +01:00
|
|
|
import eu.eudat.service.transformer.FileTransformerService;
|
2023-10-24 14:38:15 +02:00
|
|
|
import gr.cite.commons.web.authz.service.AuthorizationService;
|
|
|
|
import gr.cite.tools.data.builder.BuilderFactory;
|
|
|
|
import gr.cite.tools.data.deleter.DeleterFactory;
|
2023-12-14 17:19:43 +01:00
|
|
|
import gr.cite.tools.data.query.Ordering;
|
2023-10-24 14:38:15 +02:00
|
|
|
import gr.cite.tools.data.query.QueryFactory;
|
2024-02-06 14:03:49 +01:00
|
|
|
import gr.cite.tools.exception.*;
|
2023-10-24 14:38:15 +02:00
|
|
|
import gr.cite.tools.fieldset.BaseFieldSet;
|
|
|
|
import gr.cite.tools.fieldset.FieldSet;
|
|
|
|
import gr.cite.tools.logging.LoggerService;
|
|
|
|
import gr.cite.tools.logging.MapLogEntry;
|
2024-02-06 14:03:49 +01:00
|
|
|
import gr.cite.tools.validation.ValidatorFactory;
|
2024-04-08 17:07:47 +02:00
|
|
|
import jakarta.xml.bind.JAXBException;
|
2024-02-06 14:03:49 +01:00
|
|
|
import org.apache.commons.io.FilenameUtils;
|
2023-11-15 16:06:49 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2023-10-24 14:38:15 +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-12-18 11:55:19 +01:00
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
2023-10-24 14:38:15 +02:00
|
|
|
import org.springframework.stereotype.Service;
|
2024-02-06 14:03:49 +01:00
|
|
|
import org.springframework.util.unit.DataSize;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
2023-10-24 14:38:15 +02:00
|
|
|
|
|
|
|
import javax.management.InvalidApplicationException;
|
2023-11-14 12:41:57 +01:00
|
|
|
import java.io.IOException;
|
2024-02-06 14:03:49 +01:00
|
|
|
import java.net.URLConnection;
|
|
|
|
import java.time.Duration;
|
2023-10-24 14:38:15 +02:00
|
|
|
import java.time.Instant;
|
2023-12-06 17:33:01 +01:00
|
|
|
import java.util.*;
|
2023-11-15 16:06:49 +01:00
|
|
|
import java.util.stream.Collectors;
|
2023-10-24 14:38:15 +02:00
|
|
|
|
|
|
|
@Service
|
|
|
|
public class DescriptionServiceImpl implements DescriptionService {
|
|
|
|
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionServiceImpl.class));
|
2024-04-01 17:36:03 +02:00
|
|
|
private final TenantEntityManager entityManager;
|
2023-10-24 14:38:15 +02:00
|
|
|
private final AuthorizationService authorizationService;
|
|
|
|
private final DeleterFactory deleterFactory;
|
|
|
|
private final BuilderFactory builderFactory;
|
|
|
|
private final ConventionService conventionService;
|
|
|
|
private final ErrorThesaurusProperties errors;
|
|
|
|
private final MessageSource messageSource;
|
|
|
|
private final EventBroker eventBroker;
|
|
|
|
private final QueryFactory queryFactory;
|
|
|
|
private final JsonHandlingService jsonHandlingService;
|
2023-11-10 15:13:55 +01:00
|
|
|
private final UserScope userScope;
|
2023-11-15 16:06:49 +01:00
|
|
|
private final XmlHandlingService xmlHandlingService;
|
2023-12-18 11:55:19 +01:00
|
|
|
private final FileTransformerService fileTransformerService;
|
2024-01-19 14:12:33 +01:00
|
|
|
private final NotifyIntegrationEventHandler eventHandler;
|
2023-12-14 17:19:43 +01:00
|
|
|
private final NotificationProperties notificationProperties;
|
2024-01-09 15:02:12 +01:00
|
|
|
private final ElasticService elasticService;
|
2024-02-06 14:03:49 +01:00
|
|
|
private final ValidatorFactory validatorFactory;
|
|
|
|
private final StorageFileProperties storageFileConfig;
|
|
|
|
private final StorageFileService storageFileService;
|
2024-03-15 13:13:55 +01:00
|
|
|
private final AuthorizationContentResolver authorizationContentResolver;
|
2024-03-29 10:47:27 +01:00
|
|
|
private final AnnotationEntityTouchedIntegrationEventHandler annotationEntityTouchedIntegrationEventHandler;
|
|
|
|
private final AnnotationEntityRemovalIntegrationEventHandler annotationEntityRemovalIntegrationEventHandler;
|
2024-04-09 13:48:04 +02:00
|
|
|
private final TenantScope tenantScope;
|
|
|
|
|
2023-10-24 14:38:15 +02:00
|
|
|
@Autowired
|
|
|
|
public DescriptionServiceImpl(
|
2024-04-09 13:48:04 +02:00
|
|
|
TenantEntityManager entityManager,
|
2024-03-15 13:13:55 +01:00
|
|
|
AuthorizationService authorizationService,
|
|
|
|
DeleterFactory deleterFactory,
|
|
|
|
BuilderFactory builderFactory,
|
|
|
|
ConventionService conventionService,
|
|
|
|
ErrorThesaurusProperties errors,
|
|
|
|
MessageSource messageSource,
|
|
|
|
EventBroker eventBroker,
|
|
|
|
QueryFactory queryFactory,
|
|
|
|
JsonHandlingService jsonHandlingService,
|
|
|
|
UserScope userScope,
|
2024-04-09 13:48:04 +02:00
|
|
|
XmlHandlingService xmlHandlingService, NotifyIntegrationEventHandler eventHandler, NotificationProperties notificationProperties, FileTransformerService fileTransformerService, ElasticService elasticService, ValidatorFactory validatorFactory, StorageFileProperties storageFileConfig, StorageFileService storageFileService, AuthorizationContentResolver authorizationContentResolver, AnnotationEntityTouchedIntegrationEventHandler annotationEntityTouchedIntegrationEventHandler, AnnotationEntityRemovalIntegrationEventHandler annotationEntityRemovalIntegrationEventHandler, TenantScope tenantScope) {
|
2023-10-24 14:38:15 +02:00
|
|
|
this.entityManager = entityManager;
|
|
|
|
this.authorizationService = authorizationService;
|
|
|
|
this.deleterFactory = deleterFactory;
|
|
|
|
this.builderFactory = builderFactory;
|
|
|
|
this.conventionService = conventionService;
|
|
|
|
this.errors = errors;
|
|
|
|
this.messageSource = messageSource;
|
|
|
|
this.eventBroker = eventBroker;
|
|
|
|
this.queryFactory = queryFactory;
|
|
|
|
this.jsonHandlingService = jsonHandlingService;
|
2023-11-10 15:13:55 +01:00
|
|
|
this.userScope = userScope;
|
2023-11-15 16:06:49 +01:00
|
|
|
this.xmlHandlingService = xmlHandlingService;
|
2023-12-14 17:19:43 +01:00
|
|
|
this.eventHandler = eventHandler;
|
|
|
|
this.notificationProperties = notificationProperties;
|
2023-12-18 11:55:19 +01:00
|
|
|
this.fileTransformerService = fileTransformerService;
|
2024-01-09 15:02:12 +01:00
|
|
|
this.elasticService = elasticService;
|
2024-02-06 14:03:49 +01:00
|
|
|
this.validatorFactory = validatorFactory;
|
|
|
|
this.storageFileConfig = storageFileConfig;
|
|
|
|
this.storageFileService = storageFileService;
|
2024-03-15 13:13:55 +01:00
|
|
|
this.authorizationContentResolver = authorizationContentResolver;
|
2024-03-29 10:47:27 +01:00
|
|
|
this.annotationEntityTouchedIntegrationEventHandler = annotationEntityTouchedIntegrationEventHandler;
|
|
|
|
this.annotationEntityRemovalIntegrationEventHandler = annotationEntityRemovalIntegrationEventHandler;
|
2024-04-09 13:48:04 +02:00
|
|
|
this.tenantScope = tenantScope;
|
2023-10-24 14:38:15 +02:00
|
|
|
}
|
|
|
|
|
2024-03-19 16:21:50 +01:00
|
|
|
@Override
|
|
|
|
public Map<UUID, List<String>> getDescriptionSectionPermissions(DescriptionSectionPermissionResolver model) {
|
|
|
|
logger.debug(new MapLogEntry("get description section permissions").And("model", model));
|
|
|
|
|
|
|
|
Map<UUID, List<String>> response = new HashMap<>();
|
|
|
|
|
|
|
|
Map<UUID, AffiliatedResource> affiliatedResourceMap = this.authorizationContentResolver.descriptionsAffiliationBySections(model.getDmpId(), model.getSectionIds());
|
|
|
|
for (UUID sectionId : model.getSectionIds().stream().distinct().toList()){
|
|
|
|
AffiliatedResource affiliatedResource = affiliatedResourceMap.getOrDefault(sectionId, null);
|
|
|
|
response.put(sectionId, new ArrayList<>());
|
|
|
|
for (String permission : model.getPermissions()){
|
|
|
|
if (this.authorizationService.authorizeAtLeastOne(List.of(affiliatedResource), permission)) response.get(sectionId).add(permission);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2023-11-15 16:06:49 +01:00
|
|
|
//region Persist
|
|
|
|
|
2023-10-24 14:38:15 +02:00
|
|
|
@Override
|
2024-01-09 15:02:12 +01:00
|
|
|
public Description persist(DescriptionPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, IOException {
|
2024-02-06 11:27:47 +01:00
|
|
|
logger.debug(new MapLogEntry("persisting data description").And("model", model).And("fields", fields));
|
2023-10-24 14:38:15 +02:00
|
|
|
|
|
|
|
Boolean isUpdate = this.conventionService.isValidGuid(model.getId());
|
2024-03-15 13:13:55 +01:00
|
|
|
|
2024-03-19 16:21:50 +01:00
|
|
|
DmpDescriptionTemplateEntity dmpDescriptionTemplate = this.entityManager.find(DmpDescriptionTemplateEntity.class, model.getDmpDescriptionTemplateId());
|
|
|
|
if (dmpDescriptionTemplate == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getDmpDescriptionTemplateId(), DmpDescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
if (isUpdate) this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(model.getId())), Permission.EditDescription);
|
|
|
|
else this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionsAffiliationBySection(model.getDmpId(), dmpDescriptionTemplate.getSectionId())), Permission.EditDescription);
|
2023-10-24 14:38:15 +02:00
|
|
|
|
|
|
|
DescriptionEntity data;
|
|
|
|
if (isUpdate) {
|
|
|
|
data = this.entityManager.find(DescriptionEntity.class, model.getId());
|
2023-11-15 16:06:49 +01:00
|
|
|
if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), Description.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-12-06 17:33:01 +01:00
|
|
|
if (data.getStatus().equals(DescriptionStatus.Finalized)) throw new MyValidationException(this.errors.getDescriptionIsFinalized().getCode(), this.errors.getDescriptionIsFinalized().getMessage());
|
|
|
|
if (!data.getDmpId().equals(model.getDmpId())) throw new MyValidationException(this.errors.getDmpCanNotChange().getCode(), this.errors.getDmpCanNotChange().getMessage());
|
|
|
|
if (!data.getDmpDescriptionTemplateId().equals(model.getDmpDescriptionTemplateId())) throw new MyValidationException(this.errors.getDmpDescriptionTemplateCanNotChange().getCode(), this.errors.getDmpDescriptionTemplateCanNotChange().getMessage());
|
2023-10-24 14:38:15 +02:00
|
|
|
} else {
|
|
|
|
data = new DescriptionEntity();
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
data.setCreatedAt(Instant.now());
|
2024-02-08 08:40:16 +01:00
|
|
|
data.setCreatedById(this.userScope.getUserId());
|
2023-12-06 17:33:01 +01:00
|
|
|
data.setDmpId(model.getDmpId());
|
|
|
|
data.setDmpDescriptionTemplateId(model.getDmpDescriptionTemplateId());
|
2023-10-24 14:38:15 +02:00
|
|
|
}
|
|
|
|
|
2023-12-06 17:33:01 +01:00
|
|
|
DescriptionTemplateEntity descriptionTemplateEntity = this.entityManager.find(DescriptionTemplateEntity.class, model.getDescriptionTemplateId());
|
|
|
|
if (descriptionTemplateEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getDescriptionTemplateId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
if (!dmpDescriptionTemplate.getDescriptionTemplateGroupId().equals(descriptionTemplateEntity.getGroupId())) throw new MyValidationException(this.errors.getInvalidDescriptionTemplate().getCode(), this.errors.getInvalidDescriptionTemplate().getMessage());
|
|
|
|
|
|
|
|
DmpEntity dmp = this.entityManager.find(DmpEntity.class, data.getDmpId());
|
|
|
|
if (dmp == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{data.getDmpId(), Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
if (dmp.getStatus().equals(DmpStatus.Finalized) && isUpdate) throw new MyValidationException(this.errors.getDmpIsFinalized().getCode(), this.errors.getDmpIsFinalized().getMessage());
|
|
|
|
|
2023-10-24 14:38:15 +02:00
|
|
|
data.setLabel(model.getLabel());
|
|
|
|
data.setStatus(model.getStatus());
|
2024-03-15 14:48:32 +01:00
|
|
|
if (model.getStatus() == DescriptionStatus.Finalized) data.setFinalizedAt(Instant.now());
|
2023-10-24 14:38:15 +02:00
|
|
|
data.setDescription(model.getDescription());
|
2023-11-24 11:39:26 +01:00
|
|
|
data.setDescriptionTemplateId(model.getDescriptionTemplateId());
|
2023-10-24 14:38:15 +02:00
|
|
|
data.setUpdatedAt(Instant.now());
|
2023-11-15 16:06:49 +01:00
|
|
|
if (isUpdate) this.entityManager.merge(data);
|
|
|
|
else this.entityManager.persist(data);
|
2023-10-24 14:38:15 +02:00
|
|
|
|
2024-02-06 11:27:47 +01:00
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
eu.eudat.commons.types.descriptiontemplate.DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(eu.eudat.commons.types.descriptiontemplate.DefinitionEntity.class, descriptionTemplateEntity.getDefinition());
|
2024-02-08 17:12:47 +01:00
|
|
|
Map<String, List<UUID>> fieldToReferenceMap = this.patchAndSaveReferences(this.buildDescriptionReferencePersists(model.getProperties()), data.getId(), definition);
|
2024-02-06 11:27:47 +01:00
|
|
|
|
|
|
|
this.entityManager.flush();
|
2023-11-15 16:06:49 +01:00
|
|
|
|
2024-02-06 11:27:47 +01:00
|
|
|
data.setProperties(this.jsonHandlingService.toJson(this.buildPropertyDefinitionEntity(model.getProperties(), definition, fieldToReferenceMap)));
|
|
|
|
|
|
|
|
this.entityManager.merge(data);
|
|
|
|
|
2023-10-24 14:38:15 +02:00
|
|
|
this.entityManager.flush();
|
2023-12-06 17:33:01 +01:00
|
|
|
|
2024-02-06 11:27:47 +01:00
|
|
|
this.persistTags(data.getId(), model.getTags());
|
|
|
|
|
2023-12-06 17:33:01 +01:00
|
|
|
if (isUpdate){
|
2023-12-14 17:19:43 +01:00
|
|
|
this.sendNotification(data);
|
2023-12-06 17:33:01 +01:00
|
|
|
}
|
2023-10-24 14:38:15 +02:00
|
|
|
|
2023-12-06 17:33:01 +01:00
|
|
|
//this.deleteOldFilesAndAddNew(datasetWizardModel, userInfo); //TODO
|
2023-10-24 14:38:15 +02:00
|
|
|
this.eventBroker.emit(new DescriptionTouchedEvent(data.getId()));
|
2024-01-09 15:02:12 +01:00
|
|
|
|
2024-03-29 10:47:27 +01:00
|
|
|
this.annotationEntityTouchedIntegrationEventHandler.handleDescription(data.getId());
|
|
|
|
|
2024-01-09 15:02:12 +01:00
|
|
|
this.elasticService.persistDescription(data);
|
2024-03-12 17:27:16 +01:00
|
|
|
return this.builderFactory.builder(DescriptionBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(BaseFieldSet.build(fields, Description._id), data);
|
2023-12-04 17:38:23 +01:00
|
|
|
}
|
2024-04-05 17:32:49 +02:00
|
|
|
@Override
|
2024-04-08 17:07:47 +02:00
|
|
|
public void updateDescriptionTemplate(UpdateDescriptionTemplatePersist model) throws InvalidApplicationException, IOException, JAXBException {
|
2024-04-05 17:32:49 +02:00
|
|
|
logger.debug(new MapLogEntry("update description template").And("model", model));
|
|
|
|
|
|
|
|
this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(model.getId())), Permission.EditDescription);
|
|
|
|
|
|
|
|
DescriptionEntity data = this.entityManager.find(DescriptionEntity.class, model.getId());
|
|
|
|
if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), Description.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
2024-04-09 12:46:15 +02:00
|
|
|
if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
2024-04-05 17:32:49 +02:00
|
|
|
|
|
|
|
DescriptionTemplateEntity oldDescriptionTemplateEntity = this.entityManager.find(DescriptionTemplateEntity.class, data.getDescriptionTemplateId());
|
|
|
|
if (oldDescriptionTemplateEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{data.getDescriptionTemplateId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
DmpDescriptionTemplateEntity dmpDescriptionTemplateEntity = this.entityManager.find(DmpDescriptionTemplateEntity.class, data.getDmpDescriptionTemplateId());
|
|
|
|
if (dmpDescriptionTemplateEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{data.getDmpDescriptionTemplateId(), DmpDescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
|
|
|
|
List<DescriptionTemplateEntity> latestVersionDescriptionTemplates = this.queryFactory.query(DescriptionTemplateQuery.class)
|
|
|
|
.versionStatuses(DescriptionTemplateVersionStatus.Current)
|
|
|
|
.isActive(IsActive.Active)
|
|
|
|
.groupIds(oldDescriptionTemplateEntity.getGroupId())
|
|
|
|
.collect();
|
|
|
|
if (latestVersionDescriptionTemplates.isEmpty())
|
|
|
|
throw new MyValidationException("New version not found");
|
|
|
|
if (latestVersionDescriptionTemplates.size() > 1)
|
|
|
|
throw new MyValidationException("Multiple template found");
|
2024-04-09 12:19:32 +02:00
|
|
|
if (latestVersionDescriptionTemplates.getFirst().getVersion().equals(oldDescriptionTemplateEntity.getVersion()))
|
|
|
|
throw new MyValidationException("Description already upgraded");
|
2024-04-05 17:32:49 +02:00
|
|
|
|
|
|
|
data.setDescriptionTemplateId(latestVersionDescriptionTemplates.getFirst().getId());
|
|
|
|
|
|
|
|
eu.eudat.commons.types.descriptiontemplate.DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(eu.eudat.commons.types.descriptiontemplate.DefinitionEntity.class, latestVersionDescriptionTemplates.getFirst().getDefinition());
|
2024-04-09 12:46:15 +02:00
|
|
|
PropertyDefinitionEntity propertyDefinition = this.jsonHandlingService.fromJson(PropertyDefinitionEntity.class, data.getProperties());
|
2024-04-05 17:32:49 +02:00
|
|
|
|
2024-04-09 12:46:15 +02:00
|
|
|
data.setProperties(this.jsonHandlingService.toJson(this.cleanPropertyDefinitionEntity(propertyDefinition, definition)));
|
2024-04-05 17:32:49 +02:00
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
this.entityManager.merge(data);
|
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
|
|
|
|
this.sendNotification(data);
|
|
|
|
|
|
|
|
this.eventBroker.emit(new DescriptionTouchedEvent(data.getId()));
|
|
|
|
|
|
|
|
this.annotationEntityTouchedIntegrationEventHandler.handleDescription(data.getId());
|
|
|
|
|
|
|
|
this.elasticService.persistDescription(data);
|
|
|
|
}
|
2023-12-04 17:38:23 +01:00
|
|
|
|
2023-12-14 17:19:43 +01:00
|
|
|
private void sendNotification(DescriptionEntity description) throws InvalidApplicationException {
|
|
|
|
List<DmpUserEntity> existingUsers = this.queryFactory.query(DmpUserQuery.class)
|
|
|
|
.dmpIds(description.getDmpId())
|
|
|
|
.isActives(IsActive.Active)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
if (existingUsers == null || existingUsers.size() <= 1){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (DmpUserEntity dmpUser : existingUsers) {
|
|
|
|
if (!dmpUser.getUserId().equals(this.userScope.getUserIdSafe())){
|
|
|
|
UserEntity user = this.queryFactory.query(UserQuery.class).ids(dmpUser.getUserId()).first();
|
|
|
|
if (user != null){
|
2024-01-10 16:46:45 +01:00
|
|
|
this.createDescriptionNotificationEvent(description, user, NotificationContactType.EMAIL);
|
|
|
|
this.createDescriptionNotificationEvent(description, user, NotificationContactType.IN_APP);
|
2023-12-14 17:19:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-08 17:07:47 +02:00
|
|
|
private @NotNull PropertyDefinitionEntity cleanPropertyDefinitionEntity(PropertyDefinitionEntity data, eu.eudat.commons.types.descriptiontemplate.DefinitionEntity definition) {
|
|
|
|
PropertyDefinitionEntity cleanData = new PropertyDefinitionEntity();
|
|
|
|
if (data == null) return cleanData;
|
|
|
|
if (data.getFieldSets() != null && !data.getFieldSets().isEmpty()){
|
|
|
|
cleanData.setFieldSets(new HashMap<>());
|
|
|
|
for (String key: data.getFieldSets().keySet()) {
|
|
|
|
FieldSetEntity fieldSetEntity = definition != null ? definition.getFieldSetById(key).stream().findFirst().orElse(null) : null;
|
|
|
|
if (fieldSetEntity != null){
|
|
|
|
cleanData.getFieldSets().put(key, this.cleanPropertyDefinitionFieldSetEntity(data.getFieldSets().get(key), fieldSetEntity));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cleanData;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull PropertyDefinitionFieldSetEntity cleanPropertyDefinitionFieldSetEntity(PropertyDefinitionFieldSetEntity persist, FieldSetEntity fieldSetEntity) {
|
|
|
|
PropertyDefinitionFieldSetEntity cleanData = new PropertyDefinitionFieldSetEntity();
|
|
|
|
if (persist == null) return cleanData;
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getItems())){
|
|
|
|
cleanData.setItems(new ArrayList<>());
|
|
|
|
for (PropertyDefinitionFieldSetItemEntity itemsPersist: persist.getItems()) {
|
|
|
|
cleanData.getItems().add(this.cleanPropertyDefinitionFieldSetItemEntity(itemsPersist, fieldSetEntity));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cleanData;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull PropertyDefinitionFieldSetItemEntity cleanPropertyDefinitionFieldSetItemEntity(PropertyDefinitionFieldSetItemEntity data, FieldSetEntity fieldSetEntity) {
|
|
|
|
PropertyDefinitionFieldSetItemEntity cleanData = new PropertyDefinitionFieldSetItemEntity();
|
|
|
|
if (data == null) return cleanData;
|
|
|
|
if (data.getFields() != null && !data.getFields().isEmpty()){
|
|
|
|
cleanData.setOrdinal(data.getOrdinal());
|
|
|
|
cleanData.setComment(data.getComment());
|
|
|
|
cleanData.setFields(new HashMap<>());
|
|
|
|
for (String key: data.getFields().keySet()) {
|
|
|
|
eu.eudat.commons.types.descriptiontemplate.FieldEntity fieldEntity = fieldSetEntity != null ? fieldSetEntity.getFieldById(key).stream().findFirst().orElse(null) : null;
|
|
|
|
if (fieldEntity != null){
|
|
|
|
cleanData.getFields().put(key,data.getFields().get(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cleanData;
|
|
|
|
}
|
|
|
|
|
2024-01-10 16:46:45 +01:00
|
|
|
private void createDescriptionNotificationEvent(DescriptionEntity description, UserEntity user, NotificationContactType type) throws InvalidApplicationException {
|
2024-01-19 14:12:33 +01:00
|
|
|
NotifyIntegrationEvent event = new NotifyIntegrationEvent();
|
2024-01-10 16:46:45 +01:00
|
|
|
event.setUserId(this.userScope.getUserId());
|
|
|
|
|
|
|
|
UserContactInfoQuery query = this.queryFactory.query(UserContactInfoQuery.class).userIds(user.getId());
|
|
|
|
query.setOrder(new Ordering().addAscending(UserContactInfo._ordinal));
|
|
|
|
|
|
|
|
List<ContactPair> contactPairs = new ArrayList<>();
|
|
|
|
contactPairs.add(new ContactPair(ContactInfoType.Email, query.first().getValue()));
|
|
|
|
NotificationContactData contactData = new NotificationContactData(contactPairs, null, null);
|
|
|
|
event.setContactHint(jsonHandlingService.toJsonSafe(contactData));
|
|
|
|
event.setContactTypeHint(type);
|
|
|
|
|
|
|
|
event = this.applyNotificationType(description.getStatus(), event);
|
|
|
|
NotificationFieldData data = new NotificationFieldData();
|
|
|
|
List<FieldInfo> fieldInfoList = new ArrayList<>();
|
|
|
|
fieldInfoList.add(new FieldInfo("{recipient}", DataType.String, user.getName()));
|
|
|
|
fieldInfoList.add(new FieldInfo("{reasonName}", DataType.String, this.queryFactory.query(UserQuery.class).ids(this.userScope.getUserId()).first().getName()));
|
|
|
|
fieldInfoList.add(new FieldInfo("{name}", DataType.String, description.getLabel()));
|
|
|
|
fieldInfoList.add(new FieldInfo("{id}", DataType.String, description.getId().toString()));
|
|
|
|
data.setFields(fieldInfoList);
|
|
|
|
event.setData(jsonHandlingService.toJsonSafe(data));
|
|
|
|
|
|
|
|
eventHandler.handle(event);
|
|
|
|
}
|
|
|
|
|
2024-01-19 14:12:33 +01:00
|
|
|
private NotifyIntegrationEvent applyNotificationType(DescriptionStatus status, NotifyIntegrationEvent event) {
|
2023-12-14 17:19:43 +01:00
|
|
|
switch (status) {
|
|
|
|
case Draft:
|
2024-01-04 15:25:07 +01:00
|
|
|
event.setNotificationType(notificationProperties.getDescriptionModifiedType());
|
2024-03-19 16:21:50 +01:00
|
|
|
break;
|
2023-12-14 17:19:43 +01:00
|
|
|
case Finalized:
|
2024-01-04 15:25:07 +01:00
|
|
|
event.setNotificationType(notificationProperties.getDescriptionFinalisedType());
|
2024-03-19 16:21:50 +01:00
|
|
|
break;
|
2023-12-14 17:19:43 +01:00
|
|
|
default:
|
2023-12-15 09:15:00 +01:00
|
|
|
throw new MyApplicationException("Unsupported Description Status.");
|
2023-12-14 17:19:43 +01:00
|
|
|
}
|
2024-03-19 16:21:50 +01:00
|
|
|
return event;
|
2023-12-14 17:19:43 +01:00
|
|
|
}
|
|
|
|
|
2023-12-04 17:38:23 +01:00
|
|
|
@Override
|
2024-04-01 17:36:03 +02:00
|
|
|
public Description persistStatus(DescriptionStatusPersist model, FieldSet fields) throws IOException, InvalidApplicationException {
|
2023-12-04 17:38:23 +01:00
|
|
|
logger.debug(new MapLogEntry("persisting data dmp").And("model", model).And("fields", fields));
|
|
|
|
|
2024-03-15 13:13:55 +01:00
|
|
|
this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(model.getId())), Permission.EditDescription);
|
2023-12-04 17:38:23 +01:00
|
|
|
|
|
|
|
DescriptionEntity data = this.entityManager.find(DescriptionEntity.class, model.getId());
|
|
|
|
if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), Description.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
|
|
|
if (!data.getStatus().equals(model.getStatus())){
|
2023-12-05 10:32:17 +01:00
|
|
|
if (data.getStatus().equals(DescriptionStatus.Finalized)){
|
2024-03-15 13:13:55 +01:00
|
|
|
this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(model.getId())), Permission.FinalizeDescription);
|
2023-12-04 17:38:23 +01:00
|
|
|
DmpEntity dmpEntity = this.entityManager.find(DmpEntity.class, data.getDmpId());
|
|
|
|
if (dmpEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{data.getDmpId(), DmpEntity.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
2023-12-05 10:32:17 +01:00
|
|
|
if(!dmpEntity.getStatus().equals(DmpStatus.Draft)) throw new MyValidationException(this.errors.getDmpIsFinalized().getCode(), this.errors.getDmpIsFinalized().getMessage());
|
2023-12-04 17:38:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data.setStatus(model.getStatus());
|
2024-03-15 14:48:32 +01:00
|
|
|
if (model.getStatus() == DescriptionStatus.Finalized) data.setFinalizedAt(Instant.now());
|
2023-12-04 17:38:23 +01:00
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
this.entityManager.merge(data);
|
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
2024-01-09 15:02:12 +01:00
|
|
|
this.elasticService.persistDescription(data);
|
2023-12-04 17:38:23 +01:00
|
|
|
this.eventBroker.emit(new DescriptionTouchedEvent(data.getId()));
|
2024-03-29 10:47:27 +01:00
|
|
|
|
|
|
|
this.annotationEntityTouchedIntegrationEventHandler.handleDescription(data.getId());
|
2023-12-04 17:38:23 +01:00
|
|
|
}
|
2024-03-12 17:27:16 +01:00
|
|
|
return this.builderFactory.builder(DescriptionBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(BaseFieldSet.build(fields, Description._id), data);
|
2023-10-24 14:38:15 +02:00
|
|
|
}
|
|
|
|
|
2024-04-01 17:36:03 +02:00
|
|
|
public List<DescriptionValidationResult> validate(List<UUID> descriptionIds) throws InvalidApplicationException {
|
2024-03-15 08:39:36 +01:00
|
|
|
List<DescriptionValidationResult> descriptionValidationResults = new ArrayList<>();
|
|
|
|
|
|
|
|
List<DescriptionEntity> descriptions = this.queryFactory.query(DescriptionQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(descriptionIds).isActive(IsActive.Active).collect();
|
|
|
|
if (descriptions == null){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (DescriptionEntity description: descriptions) {
|
|
|
|
DescriptionValidationResult descriptionValidationResult = new DescriptionValidationResult(description.getId(), DescriptionValidationOutput.Invalid);
|
|
|
|
|
2024-03-19 08:50:29 +01:00
|
|
|
DescriptionPersist.DescriptionPersistValidator validator = this.validatorFactory.validator(DescriptionPersist.DescriptionPersistValidator.class);
|
2024-03-19 12:54:23 +01:00
|
|
|
validator.validate(this.buildDescriptionPersist(description));
|
2024-03-19 08:50:29 +01:00
|
|
|
if (validator.result().isValid()) descriptionValidationResult.setResult(DescriptionValidationOutput.Valid);;
|
2024-03-15 08:39:36 +01:00
|
|
|
descriptionValidationResults.add(descriptionValidationResult);
|
|
|
|
|
|
|
|
}
|
|
|
|
return descriptionValidationResults;
|
|
|
|
}
|
|
|
|
|
2024-04-01 17:36:03 +02:00
|
|
|
private @NotNull PropertyDefinitionEntity buildPropertyDefinitionEntity(PropertyDefinitionPersist persist, eu.eudat.commons.types.descriptiontemplate.DefinitionEntity definition, Map<String, List<UUID>> fieldToReferenceMap) throws InvalidApplicationException {
|
2023-11-15 16:06:49 +01:00
|
|
|
PropertyDefinitionEntity data = new PropertyDefinitionEntity();
|
|
|
|
if (persist == null) return data;
|
2024-02-01 15:40:11 +01:00
|
|
|
if (persist.getFieldSets() != null && !persist.getFieldSets().isEmpty()){
|
|
|
|
data.setFieldSets(new HashMap<>());
|
|
|
|
for (String key: persist.getFieldSets().keySet()) {
|
2024-02-06 11:27:47 +01:00
|
|
|
FieldSetEntity fieldSetEntity = definition != null ? definition.getFieldSetById(key).stream().findFirst().orElse(null) : null;
|
|
|
|
data.getFieldSets().put(key, this.buildPropertyDefinitionFieldSetEntity(persist.getFieldSets().get(key), fieldSetEntity, fieldToReferenceMap));
|
2023-11-15 16:06:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2024-04-01 17:36:03 +02:00
|
|
|
private @NotNull PropertyDefinitionFieldSetEntity buildPropertyDefinitionFieldSetEntity(PropertyDefinitionFieldSetPersist persist, FieldSetEntity fieldSetEntity, Map<String, List<UUID>> fieldToReferenceMap) throws InvalidApplicationException {
|
2024-02-01 15:40:11 +01:00
|
|
|
PropertyDefinitionFieldSetEntity data = new PropertyDefinitionFieldSetEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getItems())){
|
|
|
|
data.setItems(new ArrayList<>());
|
2024-02-06 11:27:47 +01:00
|
|
|
for (PropertyDefinitionFieldSetItemPersist itemsPersist: persist.getItems()) {
|
|
|
|
data.getItems().add(this.buildPropertyDefinitionFieldSetItemEntity(itemsPersist, fieldSetEntity, fieldToReferenceMap));
|
2024-02-01 15:40:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2024-04-01 17:36:03 +02:00
|
|
|
private @NotNull PropertyDefinitionFieldSetItemEntity buildPropertyDefinitionFieldSetItemEntity(PropertyDefinitionFieldSetItemPersist persist, FieldSetEntity fieldSetEntity, Map<String, List<UUID>> fieldToReferenceMap) throws InvalidApplicationException {
|
2024-02-01 15:40:11 +01:00
|
|
|
PropertyDefinitionFieldSetItemEntity data = new PropertyDefinitionFieldSetItemEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
if (persist.getFields() != null && !persist.getFields().isEmpty()){
|
2024-02-08 08:40:16 +01:00
|
|
|
data.setOrdinal(persist.getOrdinal());
|
|
|
|
data.setComment(persist.getComment());
|
2024-02-01 15:40:11 +01:00
|
|
|
data.setFields(new HashMap<>());
|
|
|
|
for (String key: persist.getFields().keySet()) {
|
2024-04-05 16:25:25 +02:00
|
|
|
eu.eudat.commons.types.descriptiontemplate.FieldEntity fieldEntity = fieldSetEntity != null ? fieldSetEntity.getFieldById(key).stream().findFirst().orElse(null) : null;
|
2024-02-06 11:27:47 +01:00
|
|
|
data.getFields().put(key, this.buildFieldEntity(persist.getFields().get(key), fieldEntity, fieldToReferenceMap));
|
2024-02-01 15:40:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2024-04-01 17:36:03 +02:00
|
|
|
private @NotNull FieldEntity buildFieldEntity(FieldPersist persist, eu.eudat.commons.types.descriptiontemplate.FieldEntity fieldEntity, Map<String, List<UUID>> fieldToReferenceMap) throws InvalidApplicationException {
|
2024-02-06 11:27:47 +01:00
|
|
|
FieldType fieldType = fieldEntity != null && fieldEntity.getData() != null ? fieldEntity.getData().getFieldType() : FieldType.FREE_TEXT;
|
2023-11-15 16:06:49 +01:00
|
|
|
FieldEntity data = new FieldEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
|
2024-02-06 14:03:49 +01:00
|
|
|
if (FieldType.isTextType(fieldType)) {
|
2024-02-08 08:40:16 +01:00
|
|
|
if (FieldType.UPLOAD.equals(fieldType) && !this.conventionService.isNullOrEmpty(persist.getTextValue())){
|
2024-04-10 09:47:05 +02:00
|
|
|
UUID fileId = this.conventionService.isValidUUID(persist.getTextValue()) ? UUID.fromString(persist.getTextValue()) : null;
|
|
|
|
if (fileId != null && !this.storageFileService.fileRefExists(fileId.toString(), StorageType.Main)){
|
|
|
|
StorageFile storageFile = this.storageFileService.copyToStorage(fileId, StorageType.Main, true, new BaseFieldSet().ensure(StorageFile._id));
|
|
|
|
this.storageFileService.updatePurgeAt(storageFile.getId(), null);
|
|
|
|
}
|
2024-04-10 16:38:39 +02:00
|
|
|
if (this.conventionService.isValidUUID(data.getTextValue()) && !UUID.fromString(data.getTextValue()).equals(fileId)) {
|
|
|
|
this.storageFileService.updatePurgeAt(UUID.fromString(data.getTextValue()), Instant.now().minusSeconds(60));
|
|
|
|
}
|
2024-04-10 09:47:05 +02:00
|
|
|
data.setTextValue(fileId == null ? null : fileId.toString());
|
2024-02-06 14:03:49 +01:00
|
|
|
} else {
|
|
|
|
data.setTextValue(persist.getTextValue());
|
|
|
|
}
|
|
|
|
}
|
2024-02-07 12:53:01 +01:00
|
|
|
else if (FieldType.isTextListType(fieldType)) {
|
2024-02-21 08:55:29 +01:00
|
|
|
if (FieldType.INTERNAL_ENTRIES_DMPS.equals(fieldType) && !this.conventionService.isListNullOrEmpty(persist.getTextListValue())){
|
2024-02-07 12:53:01 +01:00
|
|
|
List<UUID> ids = persist.getTextListValue().stream().map(UUID::fromString).toList();
|
|
|
|
Set<UUID> existingIds = this.queryFactory.query(DmpQuery.class).ids(ids).isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(Dmp._id)).stream().map(DmpEntity::getId).collect(Collectors.toSet());
|
|
|
|
for (UUID id : ids){
|
|
|
|
if (!existingIds.contains(id)) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
}
|
2024-02-21 08:55:29 +01:00
|
|
|
} if (FieldType.INTERNAL_ENTRIES_DESCRIPTIONS.equals(fieldType) && !this.conventionService.isListNullOrEmpty(persist.getTextListValue())){
|
2024-02-07 12:53:01 +01:00
|
|
|
List<UUID> ids = persist.getTextListValue().stream().map(UUID::fromString).toList();
|
|
|
|
Set<UUID> existingIds = this.queryFactory.query(DescriptionQuery.class).ids(ids).isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(Description._id)).stream().map(DescriptionEntity::getId).collect(Collectors.toSet());
|
|
|
|
for (UUID id : ids){
|
|
|
|
if (!existingIds.contains(id)) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Description.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
}
|
|
|
|
}
|
2024-04-05 16:25:25 +02:00
|
|
|
if (fieldType.equals(FieldType.SELECT) && this.conventionService.isListNullOrEmpty(persist.getTextListValue()) && !this.conventionService.isNullOrEmpty(persist.getTextValue())){
|
|
|
|
data.setTextListValue(List.of(persist.getTextValue()));
|
|
|
|
} else{
|
|
|
|
data.setTextListValue(persist.getTextListValue());
|
|
|
|
}
|
2024-02-07 12:53:01 +01:00
|
|
|
}
|
2024-02-06 11:27:47 +01:00
|
|
|
else if (FieldType.isReferenceType(fieldType) && fieldEntity != null ) {
|
|
|
|
List<UUID> referenceIds = fieldToReferenceMap.getOrDefault(fieldEntity.getId(), null);
|
|
|
|
if (referenceIds != null) data.setTextListValue(referenceIds.stream().map(UUID::toString).toList());
|
|
|
|
}
|
2024-02-07 12:53:01 +01:00
|
|
|
else if (FieldType.isDateType(fieldType)) data.setDateValue(persist.getDateValue());
|
|
|
|
else if (FieldType.isExternalIdentifierType(fieldType) && persist.getExternalIdentifier() != null) data.setExternalIdentifier(this.buildExternalIdentifierEntity(persist.getExternalIdentifier()));
|
|
|
|
|
2023-11-15 16:06:49 +01:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2024-02-06 11:27:47 +01:00
|
|
|
private @NotNull ExternalIdentifierEntity buildExternalIdentifierEntity(ExternalIdentifierPersist persist){
|
|
|
|
ExternalIdentifierEntity data = new ExternalIdentifierEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
|
|
|
|
data.setIdentifier(persist.getIdentifier());
|
|
|
|
data.setType(persist.getType());
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull List<DescriptionReferencePersist> buildDescriptionReferencePersists(PropertyDefinitionPersist persist){
|
|
|
|
List<DescriptionReferencePersist> descriptionReferencePersists = new ArrayList<>();
|
|
|
|
if (persist.getFieldSets() != null && !persist.getFieldSets().isEmpty()){
|
|
|
|
for (PropertyDefinitionFieldSetPersist propertyDefinitionFieldSetPersist: persist.getFieldSets().values()) {
|
2024-03-26 17:44:59 +01:00
|
|
|
if (!this.conventionService.isListNullOrEmpty( propertyDefinitionFieldSetPersist.getItems())) {
|
2024-02-06 11:27:47 +01:00
|
|
|
for (PropertyDefinitionFieldSetItemPersist definitionFieldSetItemPersist : propertyDefinitionFieldSetPersist.getItems()) {
|
|
|
|
if (definitionFieldSetItemPersist.getFields() != null && !definitionFieldSetItemPersist.getFields().isEmpty()) {
|
|
|
|
for (String key : definitionFieldSetItemPersist.getFields().keySet()) {
|
|
|
|
FieldPersist fieldPersist = definitionFieldSetItemPersist.getFields().get(key);
|
|
|
|
BuildDescriptionReferencePersist(key, fieldPersist, descriptionReferencePersists);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return descriptionReferencePersists;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void BuildDescriptionReferencePersist(String fieldId, FieldPersist fieldPersist, List<DescriptionReferencePersist> descriptionReferencePersists) {
|
2024-03-27 10:39:41 +01:00
|
|
|
if (fieldPersist.getReference() != null) {
|
|
|
|
if (fieldPersist.getReferences() == null) fieldPersist.setReferences(new ArrayList<>());
|
|
|
|
fieldPersist.getReferences().add(fieldPersist.getReference());
|
|
|
|
}
|
2024-03-26 17:44:59 +01:00
|
|
|
if (!this.conventionService.isListNullOrEmpty(fieldPersist.getReferences())) {
|
2024-02-06 11:27:47 +01:00
|
|
|
for (ReferencePersist referencePersist : fieldPersist.getReferences()) {
|
|
|
|
DescriptionReferencePersist descriptionReferencePersist = new DescriptionReferencePersist();
|
|
|
|
descriptionReferencePersist.setData(new DescriptionReferenceDataPersist());
|
|
|
|
descriptionReferencePersist.getData().setFieldId(fieldId);
|
|
|
|
descriptionReferencePersist.setReference(referencePersist);
|
|
|
|
descriptionReferencePersists.add(descriptionReferencePersist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 17:12:47 +01:00
|
|
|
private Map<String, List<UUID>> patchAndSaveReferences(List<DescriptionReferencePersist> models, UUID descriptionId, eu.eudat.commons.types.descriptiontemplate.DefinitionEntity definition) throws InvalidApplicationException {
|
2024-02-06 11:27:47 +01:00
|
|
|
if (models == null) models = new ArrayList<>();
|
|
|
|
|
|
|
|
Map<String, List<UUID>> fieldToReferenceMap = new HashMap<>();
|
|
|
|
|
|
|
|
List<DescriptionReferenceEntity> descriptionReferences = this.queryFactory.query(DescriptionReferenceQuery.class).descriptionIds(descriptionId).collect();
|
|
|
|
Map<UUID, List<DescriptionReferenceEntity>> descriptionReferenceEntityByReferenceId = new HashMap<>();
|
|
|
|
for (DescriptionReferenceEntity descriptionReferenceEntity : descriptionReferences){
|
|
|
|
List<DescriptionReferenceEntity> descriptionReferenceEntities = descriptionReferenceEntityByReferenceId.getOrDefault(descriptionReferenceEntity.getReferenceId(), null);
|
|
|
|
if (descriptionReferenceEntities == null) {
|
|
|
|
descriptionReferenceEntities = new ArrayList<>();
|
|
|
|
descriptionReferenceEntityByReferenceId.put(descriptionReferenceEntity.getReferenceId(), descriptionReferenceEntities);
|
|
|
|
}
|
|
|
|
descriptionReferenceEntities.add(descriptionReferenceEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<UUID, DescriptionReferenceDataEntity> descriptionReferenceDataEntityMap = new HashMap<>();
|
|
|
|
for (DescriptionReferenceEntity descriptionReferenceEntity : descriptionReferences){
|
|
|
|
descriptionReferenceDataEntityMap.put(descriptionReferenceEntity.getId(), this.jsonHandlingService.fromJsonSafe(DescriptionReferenceDataEntity.class, descriptionReferenceEntity.getData()));
|
|
|
|
}
|
|
|
|
|
|
|
|
List<UUID> updatedCreatedIds = new ArrayList<>();
|
|
|
|
for (DescriptionReferencePersist model : models) {
|
|
|
|
ReferencePersist referencePersist = model.getReference();
|
|
|
|
ReferenceEntity referenceEntity = null;
|
|
|
|
if (this.conventionService.isValidGuid(referencePersist.getId())){
|
|
|
|
referenceEntity = this.entityManager.find(ReferenceEntity.class, referencePersist.getId());
|
|
|
|
if (referenceEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{referencePersist.getId(), Reference.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
} else {
|
2024-02-08 17:12:47 +01:00
|
|
|
ReferenceTypeDataEntity fieldEntity = definition.getFieldById(model.getData().getFieldId()).stream().filter(x-> x.getData() != null && x.getData().getFieldType().equals(FieldType.REFERENCE_TYPES)).map(x-> (ReferenceTypeDataEntity)x.getData()).findFirst().orElse(null);
|
|
|
|
if (fieldEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getData().getFieldId(), ReferenceTypeDataEntity.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
referenceEntity = this.queryFactory.query(ReferenceQuery.class).sourceTypes(referencePersist.getSourceType()).typeIds(fieldEntity.getReferenceTypeId()).sources(referencePersist.getSource()).isActive(IsActive.Active).references(referencePersist.getReference()).first();
|
2024-02-06 11:27:47 +01:00
|
|
|
if (referenceEntity == null){
|
|
|
|
referenceEntity = new ReferenceEntity();
|
|
|
|
referenceEntity.setId(UUID.randomUUID());
|
2024-04-08 09:45:32 +02:00
|
|
|
referenceEntity.setLabel(referencePersist.getLabel());
|
2024-02-06 11:27:47 +01:00
|
|
|
referenceEntity.setIsActive(IsActive.Active);
|
|
|
|
referenceEntity.setCreatedAt(Instant.now());
|
2024-02-08 17:12:47 +01:00
|
|
|
referenceEntity.setTypeId(fieldEntity.getReferenceTypeId());
|
2024-02-06 11:27:47 +01:00
|
|
|
|
|
|
|
referenceEntity.setDefinition(this.xmlHandlingService.toXmlSafe(this.buildDefinitionEntity(referencePersist.getDefinition())));
|
|
|
|
referenceEntity.setUpdatedAt(Instant.now());
|
|
|
|
referenceEntity.setReference(referencePersist.getReference());
|
|
|
|
referenceEntity.setAbbreviation(referencePersist.getAbbreviation());
|
|
|
|
referenceEntity.setSource(referencePersist.getSource());
|
|
|
|
referenceEntity.setSourceType(referencePersist.getSourceType());
|
2024-04-09 13:48:04 +02:00
|
|
|
try {
|
|
|
|
ReferenceTypeEntity referenceType = this.queryFactory.query(ReferenceTypeQuery.class).ids(fieldEntity.getReferenceTypeId()).firstAs(new BaseFieldSet().ensure(ReferenceType._id).ensure(ReferenceTypeEntity._tenantId));
|
|
|
|
if (referenceType == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{fieldEntity.getReferenceTypeId(), ReferenceType.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
if (referenceEntity.getSourceType().equals(ReferenceSourceType.External) && !this.tenantScope.isDefaultTenant() && referenceType.getTenantId() == null){
|
|
|
|
this.tenantScope.setTempTenant(this.entityManager.getEntityManager(), null, this.tenantScope.getDefaultTenantCode());
|
|
|
|
}
|
|
|
|
this.entityManager.persist(referenceEntity);
|
|
|
|
} finally {
|
|
|
|
tenantScope.removeTempTenant(this.entityManager.getEntityManager());
|
|
|
|
}
|
2024-02-06 11:27:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DescriptionReferenceEntity data = null;
|
|
|
|
List<DescriptionReferenceEntity> descriptionReferenceEntities = descriptionReferenceEntityByReferenceId.getOrDefault(referenceEntity.getId(), new ArrayList<>());
|
|
|
|
for (DescriptionReferenceEntity descriptionReferenceEntity : descriptionReferenceEntities){
|
|
|
|
DescriptionReferenceDataEntity descriptionReferenceDataEntity = descriptionReferenceDataEntityMap.getOrDefault(descriptionReferenceEntity.getId(), new DescriptionReferenceDataEntity());
|
|
|
|
if (Objects.equals(descriptionReferenceDataEntity.getFieldId(), model.getData().getFieldId())){
|
|
|
|
data = descriptionReferenceEntity;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
boolean isUpdate = data != null;
|
|
|
|
|
|
|
|
if (!isUpdate) {
|
|
|
|
data = new DescriptionReferenceEntity();
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
data.setReferenceId(referenceEntity.getId());
|
|
|
|
data.setDescriptionId(descriptionId);
|
|
|
|
data.setCreatedAt(Instant.now());
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
data.setData(this.jsonHandlingService.toJsonSafe(this.buildDescriptionReferenceDataEntity(model.getData())));
|
|
|
|
}
|
|
|
|
updatedCreatedIds.add(data.getId());
|
|
|
|
|
|
|
|
if (model.getData() != null){
|
|
|
|
if (!fieldToReferenceMap.containsKey(model.getData().getFieldId())) fieldToReferenceMap.put(model.getData().getFieldId(), new ArrayList<>());
|
|
|
|
fieldToReferenceMap.get(model.getData().getFieldId()).add(referenceEntity.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
|
|
|
|
if (isUpdate) this.entityManager.merge(data);
|
|
|
|
else this.entityManager.persist(data);
|
|
|
|
}
|
|
|
|
List<DescriptionReferenceEntity> toDelete = descriptionReferences.stream().filter(x-> updatedCreatedIds.stream().noneMatch(y-> y.equals(x.getId()))).collect(Collectors.toList());
|
|
|
|
this.deleterFactory.deleter(DescriptionReferenceDeleter.class).delete(toDelete);
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
return fieldToReferenceMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull DescriptionReferenceDataEntity buildDescriptionReferenceDataEntity(DescriptionReferenceDataPersist persist){
|
|
|
|
DescriptionReferenceDataEntity data = new DescriptionReferenceDataEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
data.setFieldId(persist.getFieldId());
|
|
|
|
return data;
|
|
|
|
}
|
2023-11-15 16:06:49 +01:00
|
|
|
|
|
|
|
private void persistTags(UUID id, List<String> tagLabels) throws InvalidApplicationException {
|
|
|
|
if (tagLabels == null) tagLabels = new ArrayList<>();
|
|
|
|
tagLabels = tagLabels.stream().filter(x-> x != null && !x.isBlank()).toList();
|
|
|
|
|
|
|
|
List<DescriptionTagEntity> items = this.queryFactory.query(DescriptionTagQuery.class).isActive(IsActive.Active).descriptionIds(id).collect();
|
|
|
|
List<TagEntity> tagsAlreadyLinked = this.queryFactory.query(TagQuery.class).isActive(IsActive.Active).ids(items.stream().map(DescriptionTagEntity::getTagId).collect(Collectors.toList())).collect();
|
|
|
|
List<String> tagLabelsToAdd = tagLabels.stream().filter(x-> tagsAlreadyLinked.stream().noneMatch(y-> y.getLabel() != null && y.getLabel().equalsIgnoreCase(x))).toList();
|
2024-01-03 13:06:26 +01:00
|
|
|
List<TagEntity> existingTags = this.queryFactory.query(TagQuery.class).isActive(IsActive.Active).tags(tagLabelsToAdd).createdByIds(this.userScope.getUserId()).collect();
|
2023-11-15 16:06:49 +01:00
|
|
|
|
|
|
|
List<UUID> updatedCreatedIds = new ArrayList<>();
|
|
|
|
for (String tagLabel : tagLabels) {
|
|
|
|
TagEntity alreadyLinkedTag = tagsAlreadyLinked.stream().filter(x-> x.getLabel() != null && x.getLabel().equalsIgnoreCase(tagLabel)).findFirst().orElse(null);
|
|
|
|
if (alreadyLinkedTag != null){
|
|
|
|
updatedCreatedIds.addAll(items.stream().filter(x-> x.getTagId().equals(alreadyLinkedTag.getId())).map(DescriptionTagEntity::getId).toList());
|
|
|
|
} else{
|
|
|
|
TagEntity existingTag = existingTags.stream().filter(x-> x.getLabel() != null && x.getLabel().equalsIgnoreCase(tagLabel)).findFirst().orElse(null);
|
|
|
|
if (existingTag == null){
|
|
|
|
existingTag = new TagEntity();
|
|
|
|
existingTag.setId(UUID.randomUUID());
|
|
|
|
existingTag.setLabel(tagLabel);
|
|
|
|
existingTag.setIsActive(IsActive.Active);
|
|
|
|
existingTag.setCreatedAt(Instant.now());
|
|
|
|
existingTag.setUpdatedAt(Instant.now());
|
|
|
|
this.entityManager.persist(existingTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
DescriptionTagEntity link = new DescriptionTagEntity();
|
|
|
|
link.setId(UUID.randomUUID());
|
|
|
|
link.setTagId(existingTag.getId());
|
|
|
|
link.setDescriptionId(id);
|
|
|
|
link.setIsActive(IsActive.Active);
|
|
|
|
link.setCreatedAt(Instant.now());
|
|
|
|
link.setUpdatedAt(Instant.now());
|
|
|
|
this.entityManager.persist(link);
|
|
|
|
updatedCreatedIds.add(link.getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
List<DescriptionTagEntity> toDelete = items.stream().filter(x-> updatedCreatedIds.stream().noneMatch(y-> y.equals(x.getId()))).collect(Collectors.toList());
|
|
|
|
|
|
|
|
this.deleterFactory.deleter(DescriptionTagDeleter.class).delete(toDelete);
|
|
|
|
}
|
2023-12-06 17:33:01 +01:00
|
|
|
|
2023-11-15 16:06:49 +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 (eu.eudat.model.persist.referencedefinition.FieldPersist fieldPersist: persist.getFields()) {
|
|
|
|
data.getFields().add(this.buildFieldEntity(fieldPersist));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull eu.eudat.commons.types.reference.FieldEntity buildFieldEntity(eu.eudat.model.persist.referencedefinition.FieldPersist persist){
|
|
|
|
eu.eudat.commons.types.reference.FieldEntity data = new eu.eudat.commons.types.reference.FieldEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
|
|
|
|
data.setCode(persist.getCode());
|
|
|
|
data.setDataType(persist.getDataType());
|
|
|
|
data.setCode(persist.getCode());
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
//endregion
|
|
|
|
|
|
|
|
//region delete
|
|
|
|
|
2023-10-24 14:38:15 +02:00
|
|
|
@Override
|
2023-11-14 12:41:57 +01:00
|
|
|
public void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException, IOException {
|
2023-10-24 14:38:15 +02:00
|
|
|
logger.debug("deleting description: {}", id);
|
|
|
|
|
2024-03-15 13:13:55 +01:00
|
|
|
this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(id)), Permission.DeleteDescription);
|
2023-10-24 14:38:15 +02:00
|
|
|
|
2023-11-14 12:41:57 +01:00
|
|
|
this.deleterFactory.deleter(DescriptionDeleter.class).deleteAndSaveByIds(List.of(id), false);
|
2024-03-29 10:47:27 +01:00
|
|
|
|
|
|
|
this.annotationEntityRemovalIntegrationEventHandler.handleDescription(id);
|
2023-10-24 14:38:15 +02:00
|
|
|
}
|
2023-11-15 16:06:49 +01:00
|
|
|
|
|
|
|
//endregion
|
2023-11-10 15:13:55 +01:00
|
|
|
|
2023-11-15 16:06:49 +01:00
|
|
|
//region clone
|
|
|
|
|
2023-11-10 15:13:55 +01:00
|
|
|
@Override
|
2024-01-09 15:02:12 +01:00
|
|
|
public void clone(UUID dmpId, UUID descriptionId) throws InvalidApplicationException, IOException {
|
2024-02-06 11:27:47 +01:00
|
|
|
logger.debug("cloning description: {} with description: {}", descriptionId, dmpId);
|
2023-11-10 15:13:55 +01:00
|
|
|
|
2024-03-15 13:13:55 +01:00
|
|
|
this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(descriptionId)), Permission.CloneDescription);
|
2023-11-10 15:13:55 +01:00
|
|
|
|
|
|
|
DescriptionEntity existing = this.queryFactory.query(DescriptionQuery.class).ids(descriptionId).isActive(IsActive.Active).first();
|
|
|
|
|
|
|
|
DescriptionEntity newDescription = new DescriptionEntity();
|
|
|
|
newDescription.setId(UUID.randomUUID());
|
|
|
|
newDescription.setLabel(existing.getLabel());
|
|
|
|
newDescription.setDescription(existing.getDescription());
|
2023-11-10 18:11:15 +01:00
|
|
|
newDescription.setStatus(DescriptionStatus.Draft);
|
2023-11-10 15:13:55 +01:00
|
|
|
newDescription.setProperties(existing.getProperties());
|
|
|
|
newDescription.setDmpId(dmpId);
|
|
|
|
newDescription.setDmpDescriptionTemplateId(existing.getDmpDescriptionTemplateId());
|
2023-11-24 11:39:26 +01:00
|
|
|
newDescription.setDescriptionTemplateId(existing.getDescriptionTemplateId());
|
2023-11-10 15:13:55 +01:00
|
|
|
newDescription.setCreatedById(userScope.getUserId());
|
|
|
|
newDescription.setCreatedAt(Instant.now());
|
|
|
|
newDescription.setUpdatedAt(Instant.now());
|
|
|
|
newDescription.setIsActive(IsActive.Active);
|
|
|
|
|
|
|
|
this.entityManager.persist(newDescription);
|
|
|
|
|
|
|
|
List<DescriptionReferenceEntity> descriptionReferences = this.queryFactory.query(DescriptionReferenceQuery.class)
|
|
|
|
.descriptionIds(existing.getId())
|
|
|
|
.isActive(IsActive.Active)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
List<DescriptionTagEntity> descriptionTags = this.queryFactory.query(DescriptionTagQuery.class)
|
|
|
|
.descriptionIds(existing.getId())
|
|
|
|
.isActive(IsActive.Active)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
for (DescriptionReferenceEntity descriptionReference : descriptionReferences) {
|
|
|
|
DescriptionReferenceEntity newReference = new DescriptionReferenceEntity();
|
|
|
|
newReference.setId(UUID.randomUUID());
|
|
|
|
newReference.setDescriptionId(newDescription.getId());
|
|
|
|
newReference.setReferenceId(descriptionReference.getReferenceId());
|
|
|
|
newReference.setCreatedAt(Instant.now());
|
|
|
|
newReference.setUpdatedAt(Instant.now());
|
|
|
|
newReference.setIsActive(IsActive.Active);
|
|
|
|
|
|
|
|
this.entityManager.persist(newReference);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(DescriptionTagEntity descriptionTag : descriptionTags) {
|
|
|
|
DescriptionTagEntity newTag = new DescriptionTagEntity();
|
|
|
|
newTag.setId(UUID.randomUUID());
|
|
|
|
newTag.setDescriptionId(newDescription.getId());
|
|
|
|
newTag.setTagId(descriptionTag.getTagId());
|
|
|
|
newTag.setCreatedAt(Instant.now());
|
|
|
|
newTag.setUpdatedAt(Instant.now());
|
|
|
|
newTag.setIsActive(IsActive.Active);
|
|
|
|
|
|
|
|
this.entityManager.persist(newTag);
|
|
|
|
}
|
|
|
|
|
2023-11-10 18:11:15 +01:00
|
|
|
this.entityManager.flush();
|
2023-11-10 15:13:55 +01:00
|
|
|
|
2024-01-09 15:02:12 +01:00
|
|
|
this.elasticService.persistDescription(newDescription);
|
|
|
|
|
2024-03-29 10:47:27 +01:00
|
|
|
this.annotationEntityTouchedIntegrationEventHandler.handleDescription(newDescription.getId());
|
|
|
|
this.annotationEntityTouchedIntegrationEventHandler.handleDescription(existing.getId());
|
2023-11-10 15:13:55 +01:00
|
|
|
}
|
2023-11-15 16:06:49 +01:00
|
|
|
|
|
|
|
//endregion
|
2023-11-10 15:13:55 +01:00
|
|
|
|
2023-12-18 11:55:19 +01:00
|
|
|
//region file export
|
|
|
|
|
|
|
|
@Override
|
2023-12-19 17:09:09 +01:00
|
|
|
public ResponseEntity<byte[]> export(UUID id, String exportType) throws InvalidApplicationException, IOException {
|
2023-12-18 11:55:19 +01:00
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
2024-03-22 12:49:26 +01:00
|
|
|
FileEnvelope fileEnvelope = this.fileTransformerService.exportDescription(id, null, exportType); //TODO get repo from config
|
2023-12-18 11:55:19 +01:00
|
|
|
headers.add("Content-Disposition", "attachment;filename=" + fileEnvelope.getFilename());
|
2024-02-19 16:28:46 +01:00
|
|
|
byte[] data = fileEnvelope.getFile();
|
2023-12-19 17:09:09 +01:00
|
|
|
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
|
|
|
return new ResponseEntity<>(data, headers, HttpStatus.OK);
|
2023-12-18 11:55:19 +01:00
|
|
|
}
|
|
|
|
//endregion
|
2024-02-06 14:03:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public StorageFile uploadFieldFile(DescriptionFieldFilePersist model, MultipartFile file, FieldSet fields) throws IOException {
|
2024-03-15 13:13:55 +01:00
|
|
|
//this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(descriptionId)), Permission.CloneDescription);
|
|
|
|
this.authorizationService.authorizeForce(Permission.EditDescription);//TODO: Missing Description or dmp for authz
|
2024-02-06 14:03:49 +01:00
|
|
|
|
2024-03-12 17:27:16 +01:00
|
|
|
DescriptionTemplateEntity descriptionTemplate = this.queryFactory.query(DescriptionTemplateQuery.class).ids(model.getDescriptionTemplateId()).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).first();
|
2024-02-06 14:03:49 +01:00
|
|
|
if (descriptionTemplate == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getDescriptionTemplateId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
eu.eudat.commons.types.descriptiontemplate.DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(eu.eudat.commons.types.descriptiontemplate.DefinitionEntity.class, descriptionTemplate.getDefinition());
|
|
|
|
if (definition == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getDescriptionTemplateId(), eu.eudat.commons.types.descriptiontemplate.DefinitionEntity.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
eu.eudat.commons.types.descriptiontemplate.FieldEntity fieldEntity = definition.getFieldById(model.getFieldId()).stream().filter(x -> x != null && x.getData() != null && x.getData().getFieldType().equals(FieldType.UPLOAD)).findFirst().orElse(null);
|
|
|
|
if (fieldEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getFieldId(), eu.eudat.commons.types.descriptiontemplate.FieldEntity.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
UploadDataEntity uploadDataEntity = (UploadDataEntity)fieldEntity.getData();
|
|
|
|
if (DataSize.ofBytes(file.getSize()).equals(DataSize.ofMegabytes(uploadDataEntity.getMaxFileSizeInMB()))) {
|
|
|
|
throw new MyValidationException("The uploaded file is too large");
|
|
|
|
}
|
|
|
|
if(!this.conventionService.isListNullOrEmpty(uploadDataEntity.getTypes())) {
|
|
|
|
boolean isContentTypeAccepted = false;
|
|
|
|
for (UploadDataEntity.UploadDataOptionEntity option: uploadDataEntity.getTypes()) {
|
|
|
|
if(Objects.equals(file.getContentType(), option.getValue())) {
|
|
|
|
isContentTypeAccepted = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isContentTypeAccepted){
|
|
|
|
throw new MyValidationException("The uploaded file has an unaccepted type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StorageFilePersist storageFilePersist = new StorageFilePersist();
|
|
|
|
storageFilePersist.setName(FilenameUtils.removeExtension(file.getName()));
|
|
|
|
storageFilePersist.setExtension(FilenameUtils.getExtension(file.getName()));
|
|
|
|
storageFilePersist.setMimeType(URLConnection.guessContentTypeFromName(file.getName()));
|
|
|
|
storageFilePersist.setOwnerId(this.userScope.getUserIdSafe());
|
|
|
|
storageFilePersist.setStorageType(StorageType.Temp);
|
|
|
|
storageFilePersist.setLifetime(Duration.ofSeconds(this.storageFileConfig.getTempStoreLifetimeSeconds()));
|
|
|
|
this.validatorFactory.validator(StorageFilePersist.StorageFilePersistValidator.class).validateForce(storageFilePersist);
|
|
|
|
return this.storageFileService.persistBytes(storageFilePersist, file.getBytes(), BaseFieldSet.build(fields, StorageFile._id, StorageFile._name));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public StorageFileEntity getFieldFile(UUID descriptionId, UUID storageFileId) {
|
2024-03-15 13:13:55 +01:00
|
|
|
this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(descriptionId)), Permission.BrowseDescription);
|
|
|
|
|
2024-02-06 14:03:49 +01:00
|
|
|
DescriptionEntity descriptionEntity = this.queryFactory.query(DescriptionQuery.class).isActive(IsActive.Active).ids(descriptionId).first();
|
|
|
|
if (descriptionEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{descriptionId, Description.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
DmpDescriptionTemplateEntity dmpDescriptionTemplateEntity = this.queryFactory.query(DmpDescriptionTemplateQuery.class).ids(descriptionEntity.getDmpDescriptionTemplateId()).isActive(IsActive.Active).first();
|
|
|
|
if (dmpDescriptionTemplateEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{descriptionEntity.getDmpDescriptionTemplateId(), DmpDescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
DmpEntity dmpEntity = this.queryFactory.query(DmpQuery.class).ids(dmpDescriptionTemplateEntity.getDmpId()).isActive(IsActive.Active).first();
|
|
|
|
if (dmpEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{dmpDescriptionTemplateEntity.getDmpId(), Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
if (!dmpEntity.getAccessType().equals(DmpAccessType.Public))
|
|
|
|
{
|
|
|
|
boolean isDmpUser = this.queryFactory.query(DmpUserQuery.class).dmpIds(dmpEntity.getId()).userIds(this.userScope.getUserIdSafe()).isActives(IsActive.Active).count() > 0;
|
|
|
|
if (!isDmpUser) throw new MyUnauthorizedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
StorageFileEntity storageFile = this.queryFactory.query(StorageFileQuery.class).ids(storageFileId).first();
|
|
|
|
if (storageFile == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{storageFileId, StorageFile.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
return storageFile;
|
|
|
|
}
|
2024-03-19 12:54:23 +01:00
|
|
|
|
2024-04-05 17:32:49 +02:00
|
|
|
|
2024-03-19 12:54:23 +01:00
|
|
|
//region build persist
|
|
|
|
|
2024-04-01 17:36:03 +02:00
|
|
|
private @NotNull DescriptionPersist buildDescriptionPersist(DescriptionEntity data) throws InvalidApplicationException {
|
2024-03-19 12:54:23 +01:00
|
|
|
DescriptionPersist persist = new DescriptionPersist();
|
|
|
|
if (data == null) return persist;
|
|
|
|
|
|
|
|
DescriptionTemplateEntity descriptionTemplateEntity = this.entityManager.find(DescriptionTemplateEntity.class, data.getDescriptionTemplateId());
|
|
|
|
if (descriptionTemplateEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{data.getDescriptionTemplateId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
persist.setLabel(data.getLabel());
|
2024-04-05 12:16:22 +02:00
|
|
|
persist.setStatus(DescriptionStatus.Finalized);
|
2024-03-19 12:54:23 +01:00
|
|
|
persist.setDescription(data.getDescription());
|
|
|
|
persist.setDescriptionTemplateId(data.getDescriptionTemplateId());
|
|
|
|
persist.setDmpId(data.getDmpId());
|
|
|
|
persist.setDmpDescriptionTemplateId(data.getDmpDescriptionTemplateId());
|
|
|
|
|
|
|
|
eu.eudat.commons.types.descriptiontemplate.DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(eu.eudat.commons.types.descriptiontemplate.DefinitionEntity.class, descriptionTemplateEntity.getDefinition());
|
|
|
|
|
|
|
|
persist.setProperties(this.buildPropertyDefinitionPersist( this.jsonHandlingService.fromJsonSafe(PropertyDefinitionEntity.class, data.getProperties()), definition));
|
|
|
|
|
|
|
|
return persist;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull PropertyDefinitionPersist buildPropertyDefinitionPersist(PropertyDefinitionEntity data, eu.eudat.commons.types.descriptiontemplate.DefinitionEntity definition){
|
|
|
|
PropertyDefinitionPersist persist = new PropertyDefinitionPersist();
|
|
|
|
if (data == null) return persist;
|
|
|
|
if (data.getFieldSets() != null && !data.getFieldSets().isEmpty()){
|
|
|
|
persist.setFieldSets(new HashMap<>());
|
|
|
|
for (String key: data.getFieldSets().keySet()) {
|
|
|
|
FieldSetEntity fieldSetEntity = definition != null ? definition.getFieldSetById(key).stream().findFirst().orElse(null) : null;
|
|
|
|
persist.getFieldSets().put(key, this.buildPropertyDefinitionFieldSetPersist(data.getFieldSets().get(key), fieldSetEntity));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return persist;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull PropertyDefinitionFieldSetPersist buildPropertyDefinitionFieldSetPersist(PropertyDefinitionFieldSetEntity data, FieldSetEntity fieldSetEntity){
|
|
|
|
PropertyDefinitionFieldSetPersist persist = new PropertyDefinitionFieldSetPersist();
|
|
|
|
if (data == null) return persist;
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(data.getItems())){
|
|
|
|
persist.setItems(new ArrayList<>());
|
|
|
|
for (PropertyDefinitionFieldSetItemEntity itemsPersist: data.getItems()) {
|
|
|
|
persist.getItems().add(this.buildPropertyDefinitionFieldSetItemPersist(itemsPersist, fieldSetEntity));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return persist;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull PropertyDefinitionFieldSetItemPersist buildPropertyDefinitionFieldSetItemPersist(PropertyDefinitionFieldSetItemEntity data, FieldSetEntity fieldSetEntity){
|
|
|
|
PropertyDefinitionFieldSetItemPersist persist = new PropertyDefinitionFieldSetItemPersist();
|
|
|
|
if (data == null) return persist;
|
|
|
|
if (data.getFields() != null && !data.getFields().isEmpty()){
|
|
|
|
persist.setOrdinal(data.getOrdinal());
|
|
|
|
persist.setComment(data.getComment());
|
|
|
|
persist.setFields(new HashMap<>());
|
|
|
|
for (String key: data.getFields().keySet()) {
|
|
|
|
eu.eudat.commons.types.descriptiontemplate.FieldEntity fieldEntity = fieldSetEntity != null ? fieldSetEntity.getAllField().stream().findFirst().orElse(null) : null;
|
|
|
|
persist.getFields().put(key, this.buildFieldPersist(data.getFields().get(key), fieldEntity));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return persist;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull FieldPersist buildFieldPersist(FieldEntity data, eu.eudat.commons.types.descriptiontemplate.FieldEntity fieldEntity) {
|
|
|
|
FieldType fieldType = fieldEntity != null && fieldEntity.getData() != null ? fieldEntity.getData().getFieldType() : FieldType.FREE_TEXT;
|
|
|
|
|
|
|
|
FieldPersist persist = new FieldPersist();
|
|
|
|
if (data == null) return persist;
|
|
|
|
|
|
|
|
if (FieldType.isTextType(fieldType)) persist.setTextValue(data.getTextValue());
|
|
|
|
else if (FieldType.isTextListType(fieldType)) persist.setTextListValue(data.getTextListValue());
|
|
|
|
else if (FieldType.isDateType(fieldType)) persist.setDateValue(persist.getDateValue());
|
|
|
|
else if (FieldType.isExternalIdentifierType(fieldType) && data.getExternalIdentifier() != null) persist.setExternalIdentifier(this.buildExternalIdentifierPersist(data.getExternalIdentifier()));
|
|
|
|
else if (FieldType.isReferenceType(fieldType) && fieldEntity != null ) persist.setTextListValue(data.getTextListValue());
|
|
|
|
|
|
|
|
return persist;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull ExternalIdentifierPersist buildExternalIdentifierPersist(ExternalIdentifierEntity data){
|
|
|
|
ExternalIdentifierPersist persist = new ExternalIdentifierPersist();
|
|
|
|
if (data == null) return persist;
|
|
|
|
|
|
|
|
persist.setIdentifier(data.getIdentifier());
|
|
|
|
persist.setType(data.getType());
|
|
|
|
return persist;
|
|
|
|
}
|
2023-10-24 14:38:15 +02:00
|
|
|
}
|