2023-10-23 10:16:58 +02:00
|
|
|
package eu.eudat.service.descriptiontemplatetype;
|
2023-10-17 14:55:00 +02:00
|
|
|
|
2023-10-17 16:13:34 +02:00
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
|
|
import eu.eudat.authorization.Permission;
|
2023-10-17 16:48:02 +02:00
|
|
|
import eu.eudat.commons.enums.IsActive;
|
2023-10-17 14:55:00 +02:00
|
|
|
import eu.eudat.convention.ConventionService;
|
|
|
|
import eu.eudat.data.DescriptionTemplateTypeEntity;
|
2024-04-01 17:36:03 +02:00
|
|
|
import eu.eudat.data.TenantEntityManager;
|
2023-10-17 14:55:00 +02:00
|
|
|
import eu.eudat.errorcode.ErrorThesaurusProperties;
|
|
|
|
import eu.eudat.event.DescriptionTemplateTypeTouchedEvent;
|
|
|
|
import eu.eudat.event.EventBroker;
|
|
|
|
import eu.eudat.model.DescriptionTemplateType;
|
|
|
|
import eu.eudat.model.builder.DescriptionTemplateTypeBuilder;
|
|
|
|
import eu.eudat.model.deleter.DescriptionTemplateTypeDeleter;
|
|
|
|
import eu.eudat.model.persist.DescriptionTemplateTypePersist;
|
2023-10-17 16:13:34 +02:00
|
|
|
import gr.cite.commons.web.authz.service.AuthorizationService;
|
2023-10-17 14:55:00 +02:00
|
|
|
import gr.cite.tools.data.builder.BuilderFactory;
|
|
|
|
import gr.cite.tools.data.deleter.DeleterFactory;
|
|
|
|
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 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 DescriptionTemplateTypeServiceImpl implements DescriptionTemplateTypeService {
|
2023-10-18 16:58:36 +02:00
|
|
|
|
2023-10-17 14:55:00 +02:00
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionTemplateTypeServiceImpl.class));
|
|
|
|
|
2024-04-01 17:36:03 +02:00
|
|
|
private final TenantEntityManager entityManager;
|
2023-10-18 16:58:36 +02:00
|
|
|
|
2023-10-17 16:13:34 +02:00
|
|
|
private final AuthorizationService authorizationService;
|
2023-10-18 16:58:36 +02:00
|
|
|
|
2023-10-17 14:55:00 +02:00
|
|
|
private final DeleterFactory deleterFactory;
|
2023-10-18 16:58:36 +02:00
|
|
|
|
2023-10-17 14:55:00 +02:00
|
|
|
private final BuilderFactory builderFactory;
|
2023-10-18 16:58:36 +02:00
|
|
|
|
2023-10-17 14:55:00 +02:00
|
|
|
private final ConventionService conventionService;
|
2023-10-18 16:58:36 +02:00
|
|
|
|
2023-10-17 14:55:00 +02:00
|
|
|
private final ErrorThesaurusProperties errors;
|
2023-10-18 16:58:36 +02:00
|
|
|
|
2023-10-17 14:55:00 +02:00
|
|
|
private final MessageSource messageSource;
|
2023-10-18 16:58:36 +02:00
|
|
|
|
2023-10-17 14:55:00 +02:00
|
|
|
private final EventBroker eventBroker;
|
2023-10-18 16:58:36 +02:00
|
|
|
|
2023-10-17 14:55:00 +02:00
|
|
|
@Autowired
|
|
|
|
public DescriptionTemplateTypeServiceImpl(
|
2024-04-01 17:36:03 +02:00
|
|
|
TenantEntityManager entityManager,
|
2023-10-17 16:13:34 +02:00
|
|
|
AuthorizationService authorizationService,
|
2023-10-17 14:55:00 +02:00
|
|
|
DeleterFactory deleterFactory,
|
|
|
|
BuilderFactory builderFactory,
|
|
|
|
ConventionService conventionService,
|
|
|
|
ErrorThesaurusProperties errors,
|
|
|
|
MessageSource messageSource,
|
2023-11-01 11:03:23 +01:00
|
|
|
EventBroker eventBroker) {
|
2023-10-17 14:55:00 +02:00
|
|
|
this.entityManager = entityManager;
|
2023-10-17 16:13:34 +02:00
|
|
|
this.authorizationService = authorizationService;
|
2023-10-17 14:55:00 +02:00
|
|
|
this.deleterFactory = deleterFactory;
|
|
|
|
this.builderFactory = builderFactory;
|
|
|
|
this.conventionService = conventionService;
|
|
|
|
this.errors = errors;
|
|
|
|
this.messageSource = messageSource;
|
|
|
|
this.eventBroker = eventBroker;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DescriptionTemplateType persist(DescriptionTemplateTypePersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException {
|
|
|
|
logger.debug(new MapLogEntry("persisting data descriptionTemplateType").And("model", model).And("fields", fields));
|
|
|
|
|
2023-10-17 16:13:34 +02:00
|
|
|
this.authorizationService.authorizeForce(Permission.EditDescriptionTemplateType);
|
2023-10-17 14:55:00 +02:00
|
|
|
|
|
|
|
Boolean isUpdate = this.conventionService.isValidGuid(model.getId());
|
|
|
|
|
|
|
|
DescriptionTemplateTypeEntity data;
|
|
|
|
if (isUpdate) {
|
|
|
|
data = this.entityManager.find(DescriptionTemplateTypeEntity.class, model.getId());
|
2023-11-01 11:03:23 +01:00
|
|
|
if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DescriptionTemplateType.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-10-17 14:55:00 +02:00
|
|
|
} else {
|
|
|
|
data = new DescriptionTemplateTypeEntity();
|
|
|
|
data.setId(UUID.randomUUID());
|
2023-10-17 16:48:02 +02:00
|
|
|
data.setIsActive(IsActive.Active);
|
2023-10-17 14:55:00 +02:00
|
|
|
data.setCreatedAt(Instant.now());
|
|
|
|
}
|
|
|
|
|
|
|
|
data.setName(model.getName());
|
2023-10-17 16:48:02 +02:00
|
|
|
data.setStatus(model.getStatus());
|
2023-10-17 14:55:00 +02:00
|
|
|
data.setUpdatedAt(Instant.now());
|
2023-10-18 16:58:36 +02:00
|
|
|
if (isUpdate)
|
|
|
|
this.entityManager.merge(data);
|
|
|
|
else
|
|
|
|
this.entityManager.persist(data);
|
2023-10-17 14:55:00 +02:00
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
this.eventBroker.emit(new DescriptionTemplateTypeTouchedEvent(data.getId()));
|
2024-03-12 17:27:16 +01:00
|
|
|
return this.builderFactory.builder(DescriptionTemplateTypeBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(BaseFieldSet.build(fields, DescriptionTemplateType._id), data);
|
2023-10-17 14:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException {
|
2023-11-03 15:02:47 +01:00
|
|
|
logger.debug("deleting descriptionTemplateType: {}", id);
|
2023-10-17 14:55:00 +02:00
|
|
|
|
2023-10-17 16:13:34 +02:00
|
|
|
this.authorizationService.authorizeForce(Permission.DeleteDescriptionTemplateType);
|
2023-10-17 14:55:00 +02:00
|
|
|
|
|
|
|
this.deleterFactory.deleter(DescriptionTemplateTypeDeleter.class).deleteAndSaveByIds(List.of(id));
|
|
|
|
}
|
2023-10-18 16:58:36 +02:00
|
|
|
|
2023-10-17 14:55:00 +02:00
|
|
|
}
|
|
|
|
|