260 lines
12 KiB
Java
260 lines
12 KiB
Java
package eu.eudat.service.referencetype;
|
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
import eu.eudat.authorization.Permission;
|
|
import eu.eudat.commons.XmlHandlingService;
|
|
import eu.eudat.commons.enums.IsActive;
|
|
import eu.eudat.commons.enums.ReferenceTypeSourceType;
|
|
import eu.eudat.commons.types.referencetype.*;
|
|
import eu.eudat.convention.ConventionService;
|
|
import eu.eudat.data.ReferenceTypeEntity;
|
|
import eu.eudat.errorcode.ErrorThesaurusProperties;
|
|
import eu.eudat.model.ReferenceType;
|
|
import eu.eudat.model.builder.ReferenceTypeBuilder;
|
|
import eu.eudat.model.deleter.ReferenceTypeDeleter;
|
|
import eu.eudat.model.persist.ReferenceTypePersist;
|
|
import eu.eudat.model.persist.referencetypedefinition.*;
|
|
import eu.eudat.service.dmpblueprint.DmpBlueprintServiceImpl;
|
|
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;
|
|
import jakarta.xml.bind.JAXBException;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.slf4j.LoggerFactory;
|
|
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.ArrayList;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
|
|
@Service
|
|
public class ReferenceTypeServiceImpl implements ReferenceTypeService {
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpBlueprintServiceImpl.class));
|
|
private final EntityManager entityManager;
|
|
private final AuthorizationService authorizationService;
|
|
private final DeleterFactory deleterFactory;
|
|
private final BuilderFactory builderFactory;
|
|
private final ConventionService conventionService;
|
|
private final MessageSource messageSource;
|
|
private final XmlHandlingService xmlHandlingService;
|
|
private final ErrorThesaurusProperties errors;
|
|
|
|
|
|
public ReferenceTypeServiceImpl(
|
|
EntityManager entityManager, AuthorizationService authorizationService, DeleterFactory deleterFactory, BuilderFactory builderFactory,
|
|
ConventionService conventionService, MessageSource messageSource,
|
|
XmlHandlingService xmlHandlingService, ErrorThesaurusProperties errors) {
|
|
this.entityManager = entityManager;
|
|
this.authorizationService = authorizationService;
|
|
this.deleterFactory = deleterFactory;
|
|
this.builderFactory = builderFactory;
|
|
this.conventionService = conventionService;
|
|
this.messageSource = messageSource;
|
|
this.xmlHandlingService = xmlHandlingService;
|
|
this.errors = errors;
|
|
}
|
|
|
|
|
|
public ReferenceType persist(ReferenceTypePersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException{
|
|
logger.debug(new MapLogEntry("persisting data").And("model", model).And("fields", fields));
|
|
|
|
this.authorizationService.authorizeForce(Permission.EditReferenceType);
|
|
|
|
Boolean isUpdate = this.conventionService.isValidGuid(model.getId());
|
|
|
|
ReferenceTypeEntity data;
|
|
if (isUpdate) {
|
|
data = this.entityManager.find(ReferenceTypeEntity.class, model.getId());
|
|
if (data == null)
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), ReferenceType.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
|
} else {
|
|
|
|
data = new ReferenceTypeEntity();
|
|
data.setId(UUID.randomUUID());
|
|
data.setIsActive(IsActive.Active);
|
|
data.setCreatedAt(Instant.now());
|
|
}
|
|
|
|
data.setName(model.getName());
|
|
data.setCode(model.getCode());
|
|
data.setDefinition(this.xmlHandlingService.toXmlSafe(this.buildDefinitionEntity(model.getDefinition())));
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
if (isUpdate) this.entityManager.merge(data);
|
|
else this.entityManager.persist(data);
|
|
|
|
this.entityManager.flush();
|
|
|
|
return this.builderFactory.builder(ReferenceTypeBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(BaseFieldSet.build(fields, ReferenceType._id), data);
|
|
}
|
|
|
|
private @NotNull ReferenceTypeDefinitionEntity buildDefinitionEntity(ReferenceTypeDefinitionPersist persist){
|
|
ReferenceTypeDefinitionEntity data = new ReferenceTypeDefinitionEntity();
|
|
if (persist == null) return data;
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getFields())){
|
|
data.setFields(new ArrayList<>());
|
|
for (ReferenceTypeFieldPersist fieldPersist: persist.getFields()) {
|
|
data.getFields().add(this.buildFieldEntity(fieldPersist));
|
|
}
|
|
}
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getSources())){
|
|
data.setSources(new ArrayList<>());
|
|
for (ReferenceTypeSourceBaseConfigurationPersist sourceBaseConfigPersist: persist.getSources()) {
|
|
data.getSources().add(this.buildSourceBaseConfigEntity(sourceBaseConfigPersist));
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
private @NotNull ReferenceTypeFieldEntity buildFieldEntity(ReferenceTypeFieldPersist persist){
|
|
ReferenceTypeFieldEntity data = new ReferenceTypeFieldEntity();
|
|
if (persist == null) return data;
|
|
|
|
data.setCode(persist.getCode());
|
|
data.setLabel(persist.getLabel());
|
|
data.setDescription(persist.getDescription());
|
|
data.setDataType(persist.getDataType());
|
|
|
|
return data;
|
|
}
|
|
|
|
private @NotNull ReferenceTypeSourceBaseConfigurationEntity buildSourceBaseConfigEntity(ReferenceTypeSourceBaseConfigurationPersist persist){
|
|
if (persist == null) return new ReferenceTypeSourceExternalApiConfigurationEntity();
|
|
|
|
ReferenceTypeSourceBaseConfigurationEntity data;
|
|
|
|
if (ReferenceTypeSourceType.API.equals(persist.getType())) {
|
|
ReferenceTypeSourceExternalApiConfigurationEntity apiEntity = new ReferenceTypeSourceExternalApiConfigurationEntity();
|
|
|
|
apiEntity.setUrl(((ReferenceTypeSourceExternalApiConfigurationPersist) persist).getUrl());
|
|
if (((ReferenceTypeSourceExternalApiConfigurationPersist) persist).getResults() != null ) {
|
|
apiEntity.setResults(this.buildResultsConfigEntity(((ReferenceTypeSourceExternalApiConfigurationPersist) persist).getResults()));
|
|
}
|
|
apiEntity.setPaginationPath(((ReferenceTypeSourceExternalApiConfigurationPersist) persist).getPaginationPath());
|
|
apiEntity.setContentType(((ReferenceTypeSourceExternalApiConfigurationPersist) persist).getContentType());
|
|
apiEntity.setFirstPage(((ReferenceTypeSourceExternalApiConfigurationPersist) persist).getFirstPage());
|
|
apiEntity.setHttpMethod(((ReferenceTypeSourceExternalApiConfigurationPersist) persist).getHttpMethod());
|
|
apiEntity.setRequestBody(((ReferenceTypeSourceExternalApiConfigurationPersist) persist).getRequestBody());
|
|
apiEntity.setFilterType(((ReferenceTypeSourceExternalApiConfigurationPersist) persist).getFilterType());
|
|
if (((ReferenceTypeSourceExternalApiConfigurationPersist) persist).getAuth() != null) {
|
|
apiEntity.setAuth(this.buildAuthConfigEntity(((ReferenceTypeSourceExternalApiConfigurationPersist) persist).getAuth()));
|
|
}
|
|
if (!this.conventionService.isListNullOrEmpty(((ReferenceTypeSourceExternalApiConfigurationPersist) persist).getQueries())){
|
|
apiEntity.setQueries(new ArrayList<>());
|
|
for (QueryConfigPersist queryConfigPersist: ((ReferenceTypeSourceExternalApiConfigurationPersist) persist).getQueries()) {
|
|
apiEntity.getQueries().add(this.buildQueryConfigEntity(queryConfigPersist));
|
|
}
|
|
}
|
|
|
|
data = apiEntity;
|
|
}else {
|
|
ReferenceTypeSourceStaticOptionConfigurationEntity staticEntity = new ReferenceTypeSourceStaticOptionConfigurationEntity();
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(((ReferenceTypeSourceStaticOptionConfigurationPersist) persist).getOptions())){
|
|
staticEntity.setOptions(new ArrayList<>());
|
|
for (ReferenceTypeStaticOptionPersist optionPersist: ((ReferenceTypeSourceStaticOptionConfigurationPersist) persist).getOptions()) {
|
|
staticEntity.getOptions().add(this.buildStaticOptionEntity(optionPersist));
|
|
}
|
|
}
|
|
|
|
data = staticEntity;
|
|
}
|
|
|
|
data.setType(persist.getType());
|
|
data.setKey(persist.getKey());
|
|
data.setLabel(persist.getLabel());
|
|
data.setOrdinal(persist.getOrdinal());
|
|
data.setReferenceTypeDependencyIds(persist.getReferenceTypeDependencyIds());
|
|
|
|
return data;
|
|
}
|
|
|
|
private @NotNull ResultsConfigurationEntity buildResultsConfigEntity(ResultsConfigurationPersist persist){
|
|
ResultsConfigurationEntity data = new ResultsConfigurationEntity();
|
|
if (persist == null) return data;
|
|
|
|
data.setResultsArrayPath(persist.getResultsArrayPath());
|
|
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getFieldsMapping())){
|
|
data.setFieldsMapping(new ArrayList<>());
|
|
for (ResultFieldsMappingConfigurationPersist fieldsMappingPersist: persist.getFieldsMapping()) {
|
|
data.getFieldsMapping().add(this.buildResultFieldsMappingConfigEntity(fieldsMappingPersist));
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
private @NotNull ResultFieldsMappingConfigurationEntity buildResultFieldsMappingConfigEntity(ResultFieldsMappingConfigurationPersist persist){
|
|
ResultFieldsMappingConfigurationEntity data = new ResultFieldsMappingConfigurationEntity();
|
|
if (persist == null) return data;
|
|
|
|
data.setCode(persist.getCode());
|
|
data.setResponsePath(persist.getResponsePath());
|
|
|
|
return data;
|
|
}
|
|
|
|
private @NotNull AuthenticationConfigurationEntity buildAuthConfigEntity(AuthenticationConfigurationPersist persist){
|
|
AuthenticationConfigurationEntity data = new AuthenticationConfigurationEntity();
|
|
if (persist == null) return data;
|
|
|
|
data.setEnabled(persist.getEnabled());
|
|
data.setAuthUrl(persist.getAuthUrl());
|
|
data.setAuthMethod(persist.getAuthMethod());
|
|
data.setAuthRequestBody(persist.getAuthRequestBody());
|
|
data.setType(persist.getType());
|
|
data.setAuthTokenPath(persist.getAuthTokenPath());
|
|
|
|
return data;
|
|
}
|
|
|
|
private @NotNull QueryConfigEntity buildQueryConfigEntity(QueryConfigPersist persist){
|
|
QueryConfigEntity data = new QueryConfigEntity();
|
|
if (persist == null) return data;
|
|
|
|
// data.setCondition(persist.getCondition()); //TODO
|
|
// data.setSeparator(persist.getSeparator());
|
|
// data.setValue(persist.getValue());
|
|
// data.setOrdinal(persist.getOrdinal());
|
|
|
|
return data;
|
|
}
|
|
|
|
private @NotNull ReferenceTypeStaticOptionEntity buildStaticOptionEntity(ReferenceTypeStaticOptionPersist persist){
|
|
ReferenceTypeStaticOptionEntity data = new ReferenceTypeStaticOptionEntity();
|
|
if (persist == null) return data;
|
|
|
|
data.setCode(persist.getCode());
|
|
data.setValue(persist.getValue());
|
|
|
|
return data;
|
|
}
|
|
|
|
|
|
public void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException {
|
|
logger.debug("deleting : {}", id);
|
|
|
|
this.authorizationService.authorizeForce(Permission.DeleteReferenceType);
|
|
|
|
this.deleterFactory.deleter(ReferenceTypeDeleter.class).deleteAndSaveByIds(List.of(id));
|
|
}
|
|
}
|