2023-10-30 14:07:59 +01:00
|
|
|
package eu.eudat.service.descriptiontemplate;
|
|
|
|
|
2023-10-30 17:02:20 +01:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2023-10-30 14:07:59 +01:00
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
|
|
import eu.eudat.authorization.Permission;
|
2023-10-30 17:02:20 +01:00
|
|
|
import eu.eudat.commons.XmlHandlingService;
|
2023-10-30 14:07:59 +01:00
|
|
|
import eu.eudat.commons.enums.IsActive;
|
2023-10-31 16:24:03 +01:00
|
|
|
import eu.eudat.commons.enums.UserDescriptionTemplateRole;
|
|
|
|
import eu.eudat.commons.scope.user.UserScope;
|
2023-10-30 17:02:20 +01:00
|
|
|
import eu.eudat.commons.types.descriptiontemplate.*;
|
|
|
|
import eu.eudat.commons.types.descriptiontemplate.fielddata.BaseFieldDataEntity;
|
2023-10-30 14:07:59 +01:00
|
|
|
import eu.eudat.convention.ConventionService;
|
|
|
|
import eu.eudat.data.DescriptionTemplateEntity;
|
2023-10-31 16:24:03 +01:00
|
|
|
import eu.eudat.data.UserDescriptionTemplateEntity;
|
2023-10-30 14:07:59 +01:00
|
|
|
import eu.eudat.model.DescriptionTemplate;
|
|
|
|
import eu.eudat.model.builder.DescriptionTemplateBuilder;
|
|
|
|
import eu.eudat.model.deleter.DescriptionTemplateDeleter;
|
|
|
|
import eu.eudat.model.persist.DescriptionTemplatePersist;
|
2023-10-30 17:02:20 +01:00
|
|
|
import eu.eudat.model.persist.descriptiontemplatedefinition.*;
|
|
|
|
import eu.eudat.model.persist.descriptiontemplatedefinition.fielddata.BaseFieldDataPersist;
|
|
|
|
import eu.eudat.service.fielddatahelper.FieldDataHelperServiceProvider;
|
2023-10-30 14:07:59 +01: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.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;
|
2023-10-30 17:02:20 +01:00
|
|
|
import jakarta.xml.bind.JAXBException;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
2023-10-30 14:07:59 +01:00
|
|
|
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;
|
2023-10-30 17:02:20 +01:00
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import javax.xml.transform.TransformerException;
|
2023-10-30 14:07:59 +01:00
|
|
|
import java.time.Instant;
|
2023-10-30 17:02:20 +01:00
|
|
|
import java.util.ArrayList;
|
2023-10-30 14:07:59 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
@Service
|
|
|
|
public class DescriptionTemplateServiceImpl implements DescriptionTemplateService {
|
|
|
|
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionTemplateServiceImpl.class));
|
|
|
|
|
|
|
|
private final EntityManager entityManager;
|
2023-10-31 16:24:03 +01:00
|
|
|
private final UserScope userScope;
|
2023-10-30 14:07:59 +01:00
|
|
|
|
|
|
|
private final AuthorizationService authorizationService;
|
|
|
|
|
|
|
|
private final DeleterFactory deleterFactory;
|
|
|
|
|
|
|
|
private final BuilderFactory builderFactory;
|
|
|
|
private final ConventionService conventionService;
|
|
|
|
private final MessageSource messageSource;
|
2023-10-30 17:02:20 +01:00
|
|
|
private final XmlHandlingService xmlHandlingService;
|
|
|
|
private final FieldDataHelperServiceProvider fieldDataHelperServiceProvider;
|
|
|
|
|
2023-10-30 14:07:59 +01:00
|
|
|
@Autowired
|
|
|
|
public DescriptionTemplateServiceImpl(
|
|
|
|
EntityManager entityManager,
|
2023-10-31 16:24:03 +01:00
|
|
|
UserScope userScope, AuthorizationService authorizationService,
|
2023-10-30 14:07:59 +01:00
|
|
|
DeleterFactory deleterFactory,
|
|
|
|
BuilderFactory builderFactory,
|
|
|
|
ConventionService conventionService,
|
2023-10-30 17:02:20 +01:00
|
|
|
MessageSource messageSource,
|
2023-10-31 16:24:03 +01:00
|
|
|
XmlHandlingService xmlHandlingService,
|
2023-10-30 17:02:20 +01:00
|
|
|
FieldDataHelperServiceProvider fieldDataHelperServiceProvider) {
|
2023-10-30 14:07:59 +01:00
|
|
|
this.entityManager = entityManager;
|
2023-10-31 16:24:03 +01:00
|
|
|
this.userScope = userScope;
|
2023-10-30 14:07:59 +01:00
|
|
|
this.authorizationService = authorizationService;
|
|
|
|
this.deleterFactory = deleterFactory;
|
|
|
|
this.builderFactory = builderFactory;
|
|
|
|
this.conventionService = conventionService;
|
|
|
|
this.messageSource = messageSource;
|
2023-10-30 17:02:20 +01:00
|
|
|
this.xmlHandlingService = xmlHandlingService;
|
|
|
|
this.fieldDataHelperServiceProvider = fieldDataHelperServiceProvider;
|
2023-10-30 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
2023-10-30 17:02:20 +01:00
|
|
|
public DescriptionTemplate persist(DescriptionTemplatePersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException {
|
2023-10-30 14:07:59 +01:00
|
|
|
logger.debug(new MapLogEntry("persisting data descriptionTemplateType").And("model", model).And("fields", fields));
|
|
|
|
|
|
|
|
this.authorizationService.authorizeForce(Permission.EditDescriptionTemplate);
|
|
|
|
|
|
|
|
Boolean isUpdate = this.conventionService.isValidGuid(model.getId());
|
|
|
|
|
|
|
|
DescriptionTemplateEntity data;
|
|
|
|
if (isUpdate) {
|
|
|
|
data = this.entityManager.find(DescriptionTemplateEntity.class, model.getId());
|
2023-10-30 17:02:20 +01:00
|
|
|
if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
2023-10-30 14:07:59 +01:00
|
|
|
} else {
|
|
|
|
data = new DescriptionTemplateEntity();
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
data.setCreatedAt(Instant.now());
|
2023-10-30 17:02:20 +01:00
|
|
|
data.setGroupId(UUID.randomUUID());
|
|
|
|
data.setVersion((short)1);
|
2023-10-30 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data.setDescription(model.getDescription());
|
|
|
|
data.setLabel(model.getLabel());
|
|
|
|
data.setType(model.getType());
|
|
|
|
data.setLanguage(model.getLanguage());
|
|
|
|
data.setStatus(model.getStatus());
|
|
|
|
data.setUpdatedAt(Instant.now());
|
2023-10-30 17:02:20 +01:00
|
|
|
data.setDefinition(this.xmlHandlingService.toXml(this.buildDefinitionEntity(model.getDefinition())));
|
2023-10-30 14:07:59 +01:00
|
|
|
if (isUpdate)
|
|
|
|
this.entityManager.merge(data);
|
|
|
|
else
|
|
|
|
this.entityManager.persist(data);
|
2023-10-31 16:24:03 +01:00
|
|
|
|
|
|
|
if (!isUpdate) this.addOwner(data);
|
2023-10-30 14:07:59 +01:00
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
return this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(BaseFieldSet.build(fields, DescriptionTemplate._id), data);
|
|
|
|
}
|
2023-10-31 16:24:03 +01:00
|
|
|
|
|
|
|
private void addOwner(DescriptionTemplateEntity descriptionTemplateEntity) throws InvalidApplicationException {
|
|
|
|
UserDescriptionTemplateEntity data = new UserDescriptionTemplateEntity();
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
data.setCreatedAt(Instant.now());
|
|
|
|
data.setRole(UserDescriptionTemplateRole.Owner);
|
|
|
|
data.setUser(userScope.getUserId());
|
|
|
|
data.setDescriptionTemplate(descriptionTemplateEntity.getId());
|
|
|
|
this.entityManager.persist(data);
|
|
|
|
}
|
2023-10-30 14:07:59 +01:00
|
|
|
|
2023-10-30 17:02:20 +01:00
|
|
|
private @NotNull DefinitionEntity buildDefinitionEntity(DefinitionPersist persist){
|
|
|
|
DefinitionEntity data = new DefinitionEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getSections())){
|
|
|
|
data.setSections(new ArrayList<>());
|
|
|
|
for (SectionPersist sectionPersist: persist.getSections()) {
|
|
|
|
data.getSections().add(this.buildSectionEntity(sectionPersist));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getPages())){
|
|
|
|
data.setPages(new ArrayList<>());
|
|
|
|
for (PagePersist pagePersist: persist.getPages()) {
|
|
|
|
data.getPages().add(this.buildPageEntity(pagePersist));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull SectionEntity buildSectionEntity(SectionPersist persist){
|
|
|
|
SectionEntity data = new SectionEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
|
|
|
|
data.setId(persist.getId());
|
|
|
|
data.setDescription(persist.getDescription());
|
|
|
|
data.setOrdinal(persist.getOrdinal());
|
|
|
|
data.setDefaultVisibility(persist.getDefaultVisibility());
|
|
|
|
data.setMultiplicity(persist.getMultiplicity());
|
|
|
|
data.setPage(persist.getPage());
|
|
|
|
data.setTitle(persist.getTitle());
|
|
|
|
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getSections())){
|
|
|
|
data.setSections(new ArrayList<>());
|
|
|
|
for (SectionPersist sectionPersist: persist.getSections()) {
|
|
|
|
data.getSections().add(this.buildSectionEntity(sectionPersist));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getFieldSets())){
|
|
|
|
data.setFieldSets(new ArrayList<>());
|
|
|
|
for (FieldSetPersist fieldSetPersist: persist.getFieldSets()) {
|
|
|
|
data.getFieldSets().add(this.buildFieldSetEntity(fieldSetPersist));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull FieldSetEntity buildFieldSetEntity(FieldSetPersist persist){
|
|
|
|
FieldSetEntity data = new FieldSetEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
|
|
|
|
data.setId(persist.getId());
|
|
|
|
data.setDescription(persist.getDescription());
|
|
|
|
data.setOrdinal(persist.getOrdinal());
|
|
|
|
data.setTitle(persist.getTitle());
|
|
|
|
data.setNumbering(persist.getNumbering());
|
|
|
|
data.setAdditionalInformation(persist.getAdditionalInformation());
|
|
|
|
data.setTitle(persist.getTitle());
|
|
|
|
if (persist.getMultiplicity() != null) data.setMultiplicity(this.buildMultiplicityEntity(persist.getMultiplicity()));
|
|
|
|
data.setHasCommentField(persist.getHasCommentField());
|
|
|
|
data.setTitle(persist.getTitle());
|
|
|
|
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getFields())){
|
|
|
|
data.setFields(new ArrayList<>());
|
|
|
|
for (FieldPersist fieldPersist: persist.getFields()) {
|
|
|
|
data.getFields().add(this.buildFieldEntity(fieldPersist));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull FieldEntity buildFieldEntity(FieldPersist persist){
|
|
|
|
FieldEntity data = new FieldEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
|
|
|
|
data.setId(persist.getId());
|
|
|
|
data.setOrdinal(persist.getOrdinal());
|
|
|
|
data.setSchematics(persist.getSchematics());
|
|
|
|
//data.setNumbering(persist.get()); //TODO
|
|
|
|
data.setDefaultValue(persist.getDefaultValue());
|
|
|
|
data.setValidations(persist.getValidations());
|
|
|
|
data.setIncludeInExport(persist.getIncludeInExport());
|
|
|
|
if (persist.getData() != null) data.setData(this.buildFieldDataEntity(persist.getData()));
|
|
|
|
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getVisibilityRules())){
|
|
|
|
data.setVisibilityRules(new ArrayList<>());
|
|
|
|
for (RulePersist fieldPersist: persist.getVisibilityRules()) {
|
|
|
|
data.getVisibilityRules().add(this.buildRuleEntity(fieldPersist));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private BaseFieldDataEntity<?> buildFieldDataEntity(BaseFieldDataPersist persist){
|
|
|
|
if (persist == null) return null;
|
|
|
|
return this.fieldDataHelperServiceProvider.get(persist.getFieldType()).applyPersist(persist);
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull RuleEntity buildRuleEntity(RulePersist persist){
|
|
|
|
RuleEntity data = new RuleEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
|
|
|
|
data.setTarget(persist.getTarget());
|
|
|
|
data.setValue(persist.getValue());
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull MultiplicityEntity buildMultiplicityEntity(MultiplicityPersist persist){
|
|
|
|
MultiplicityEntity data = new MultiplicityEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
|
|
|
|
data.setMax(persist.getMax());
|
|
|
|
data.setMin(persist.getMin());
|
|
|
|
data.setPlaceholder(persist.getPlaceholder());
|
|
|
|
data.setTableView(persist.getTableView());
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NotNull PageEntity buildPageEntity(PagePersist persist){
|
|
|
|
PageEntity data = new PageEntity();
|
|
|
|
if (persist == null) return data;
|
|
|
|
|
|
|
|
data.setId(persist.getId());
|
|
|
|
data.setOrdinal(persist.getOrdinal());
|
|
|
|
data.setTitle(persist.getTitle());
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2023-10-30 14:07:59 +01:00
|
|
|
public void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException {
|
|
|
|
logger.debug("deleting dataset: {}", id);
|
|
|
|
|
|
|
|
this.authorizationService.authorizeForce(Permission.DeleteDescriptionTemplate);
|
|
|
|
|
|
|
|
this.deleterFactory.deleter(DescriptionTemplateDeleter.class).deleteAndSaveByIds(List.of(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|