2023-10-24 14:38:15 +02:00
|
|
|
package eu.eudat.service.description;
|
|
|
|
|
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
|
|
import eu.eudat.authorization.Permission;
|
|
|
|
import eu.eudat.commons.JsonHandlingService;
|
2023-11-10 15:13:55 +01:00
|
|
|
import eu.eudat.commons.enums.DescriptionStatus;
|
2023-10-24 14:38:15 +02:00
|
|
|
import eu.eudat.commons.enums.IsActive;
|
2023-11-10 15:13:55 +01:00
|
|
|
import eu.eudat.commons.scope.user.UserScope;
|
2023-10-24 14:38:15 +02:00
|
|
|
import eu.eudat.convention.ConventionService;
|
|
|
|
import eu.eudat.data.DescriptionEntity;
|
2023-11-10 15:13:55 +01:00
|
|
|
import eu.eudat.data.DescriptionReferenceEntity;
|
|
|
|
import eu.eudat.data.DescriptionTagEntity;
|
2023-10-24 14:38:15 +02:00
|
|
|
import eu.eudat.errorcode.ErrorThesaurusProperties;
|
|
|
|
import eu.eudat.event.DescriptionTouchedEvent;
|
|
|
|
import eu.eudat.event.EventBroker;
|
|
|
|
import eu.eudat.model.Description;
|
|
|
|
import eu.eudat.model.builder.DescriptionBuilder;
|
|
|
|
import eu.eudat.model.deleter.DescriptionDeleter;
|
|
|
|
import eu.eudat.model.persist.DescriptionPersist;
|
2023-11-10 15:13:55 +01:00
|
|
|
import eu.eudat.query.DescriptionQuery;
|
|
|
|
import eu.eudat.query.DescriptionReferenceQuery;
|
|
|
|
import eu.eudat.query.DescriptionTagQuery;
|
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;
|
|
|
|
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 gr.cite.tools.logging.MapLogEntry;
|
|
|
|
import jakarta.persistence.EntityManager;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.context.MessageSource;
|
|
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import javax.management.InvalidApplicationException;
|
|
|
|
import java.time.Instant;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
@Service
|
|
|
|
public class DescriptionServiceImpl implements DescriptionService {
|
|
|
|
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionServiceImpl.class));
|
|
|
|
|
|
|
|
private final EntityManager entityManager;
|
|
|
|
|
|
|
|
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-10-24 14:38:15 +02:00
|
|
|
@Autowired
|
|
|
|
public DescriptionServiceImpl(
|
|
|
|
EntityManager entityManager,
|
|
|
|
AuthorizationService authorizationService,
|
|
|
|
DeleterFactory deleterFactory,
|
|
|
|
BuilderFactory builderFactory,
|
|
|
|
ConventionService conventionService,
|
|
|
|
ErrorThesaurusProperties errors,
|
|
|
|
MessageSource messageSource,
|
|
|
|
EventBroker eventBroker,
|
|
|
|
QueryFactory queryFactory,
|
2023-11-10 15:13:55 +01:00
|
|
|
JsonHandlingService jsonHandlingService, UserScope userScope) {
|
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-10-24 14:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Description persist(DescriptionPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException {
|
|
|
|
logger.debug(new MapLogEntry("persisting data dmp").And("model", model).And("fields", fields));
|
|
|
|
|
|
|
|
this.authorizationService.authorizeForce(Permission.EditDescription);
|
|
|
|
|
|
|
|
Boolean isUpdate = this.conventionService.isValidGuid(model.getId());
|
|
|
|
|
|
|
|
DescriptionEntity data;
|
|
|
|
if (isUpdate) {
|
|
|
|
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()));
|
|
|
|
} else {
|
|
|
|
data = new DescriptionEntity();
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
data.setCreatedAt(Instant.now());
|
|
|
|
}
|
|
|
|
|
|
|
|
data.setLabel(model.getLabel());
|
|
|
|
data.setProperties(model.getProperties());
|
|
|
|
data.setStatus(model.getStatus());
|
|
|
|
data.setDescription(model.getDescription());
|
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
if (isUpdate)
|
|
|
|
this.entityManager.merge(data);
|
|
|
|
else
|
|
|
|
this.entityManager.persist(data);
|
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
this.eventBroker.emit(new DescriptionTouchedEvent(data.getId()));
|
2023-11-08 17:42:52 +01:00
|
|
|
return this.builderFactory.builder(DescriptionBuilder.class).authorize(AuthorizationFlags.OwnerOrPermissionOrMemberOrPublic).build(BaseFieldSet.build(fields, Description._id), data);
|
2023-10-24 14:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException {
|
|
|
|
logger.debug("deleting description: {}", id);
|
|
|
|
|
|
|
|
this.authorizationService.authorizeForce(Permission.DeleteDescription);
|
|
|
|
|
|
|
|
this.deleterFactory.deleter(DescriptionDeleter.class).deleteAndSaveByIds(List.of(id));
|
|
|
|
}
|
2023-11-10 15:13:55 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clone(UUID dmpId, UUID descriptionId) throws InvalidApplicationException {
|
|
|
|
logger.debug("cloning description: {} with dmp: {}", descriptionId, dmpId);
|
|
|
|
|
|
|
|
this.authorizationService.authorizeForce(Permission.CloneDescription);
|
|
|
|
|
|
|
|
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());
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-24 14:38:15 +02:00
|
|
|
}
|