diff --git a/backend/core/pom.xml b/backend/core/pom.xml index 6f168c8a7..da86e1101 100644 --- a/backend/core/pom.xml +++ b/backend/core/pom.xml @@ -95,6 +95,11 @@ oidc-authn 2.2.2 + + org.opencdmp + evaluator-base + 0.0.2 + gr.cite diff --git a/backend/core/src/main/java/org/opencdmp/audit/AuditableAction.java b/backend/core/src/main/java/org/opencdmp/audit/AuditableAction.java index c8d18b37d..19f17d56a 100644 --- a/backend/core/src/main/java/org/opencdmp/audit/AuditableAction.java +++ b/backend/core/src/main/java/org/opencdmp/audit/AuditableAction.java @@ -155,6 +155,8 @@ public class AuditableAction { public static final EventId FileTransformer_GetAvailableConfigurations = new EventId(20000, "FileTransformer_GetAvailableConfigurations"); + public static final EventId Evaluator_GetAvailableConfigurations = new EventId(20001, "Evaluator_GetAvailableConfigurations"); + public static final EventId ContactSupport_Sent = new EventId(210000, "ContactSupport_Sent"); public static final EventId ContactSupport_PublicSent = new EventId(210001, "ContactSupport_PublicSent"); diff --git a/backend/core/src/main/java/org/opencdmp/authorization/Permission.java b/backend/core/src/main/java/org/opencdmp/authorization/Permission.java index da34e8d1d..c2888f6fb 100644 --- a/backend/core/src/main/java/org/opencdmp/authorization/Permission.java +++ b/backend/core/src/main/java/org/opencdmp/authorization/Permission.java @@ -89,6 +89,7 @@ public final class Permission { public static String AssignPlanUsers = "AssignPlanUsers"; public static String InvitePlanUsers = "InvitePlanUsers"; public static String AnnotatePlan = "AnnotatePlan"; + public static String EvaluatePlan = "EvaluatePlan"; //PlanStatus public static String BrowsePlanStatus = "BrowsePlanStatus"; @@ -137,6 +138,7 @@ public final class Permission { public static String DeleteDescription = "DeleteDescription"; public static String CloneDescription = "CloneDescription"; public static String ExportDescription = "ExportDescription"; + public static String EvaluateDescription = "EvaluateDescription"; //DescriptionTag public static String BrowseDescriptionTag = "BrowseDescriptionTag"; diff --git a/backend/core/src/main/java/org/opencdmp/commons/enums/TenantConfigurationType.java b/backend/core/src/main/java/org/opencdmp/commons/enums/TenantConfigurationType.java index 1bfa0dfff..e84a6aed3 100644 --- a/backend/core/src/main/java/org/opencdmp/commons/enums/TenantConfigurationType.java +++ b/backend/core/src/main/java/org/opencdmp/commons/enums/TenantConfigurationType.java @@ -11,7 +11,8 @@ public enum TenantConfigurationType implements DatabaseEnum { FileTransformerPlugins((short) 1), DefaultUserLocale((short) 2), Logo((short) 3), - CssColors((short) 4); + CssColors((short) 4), + EvaluatorPlugins((short) 5); private final Short value; diff --git a/backend/core/src/main/java/org/opencdmp/commons/types/evaluator/EvaluatorSourceEntity.java b/backend/core/src/main/java/org/opencdmp/commons/types/evaluator/EvaluatorSourceEntity.java new file mode 100644 index 000000000..2b922b567 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/commons/types/evaluator/EvaluatorSourceEntity.java @@ -0,0 +1,68 @@ +package org.opencdmp.commons.types.evaluator; + +public class EvaluatorSourceEntity { + + private String url; + private String evaluatorId; + private String issuerUrl; + private String clientId; + private String clientSecret; + private String scope; + private int maxInMemorySizeInBytes; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getEvaluatorId() { + return evaluatorId; + } + + public void setEvaluatorId(String evaluatorId) { + this.evaluatorId = evaluatorId; + } + + public String getIssuerUrl() { + return issuerUrl; + } + + public void setIssuerUrl(String issuerUrl) { + this.issuerUrl = issuerUrl; + } + + public String getClientId() { + return clientId; + } + + public void setClientId(String clientId) { + this.clientId = clientId; + } + + public String getClientSecret() { + return clientSecret; + } + + public void setClientSecret(String clientSecret) { + this.clientSecret = clientSecret; + } + + public String getScope() { + return scope; + } + + public void setScope(String scope) { + this.scope = scope; + } + + public int getMaxInMemorySizeInBytes() { + return maxInMemorySizeInBytes; + } + + public void setMaxInMemorySizeInBytes(int maxInMemorySizeInBytes) { + this.maxInMemorySizeInBytes = maxInMemorySizeInBytes; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/commons/types/tenantconfiguration/EvaluatorTenantConfigurationEntity.java b/backend/core/src/main/java/org/opencdmp/commons/types/tenantconfiguration/EvaluatorTenantConfigurationEntity.java new file mode 100644 index 000000000..10a318c25 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/commons/types/tenantconfiguration/EvaluatorTenantConfigurationEntity.java @@ -0,0 +1,26 @@ +package org.opencdmp.commons.types.tenantconfiguration; + +import org.opencdmp.commons.types.evaluator.EvaluatorSourceEntity; + +import java.util.List; + +public class EvaluatorTenantConfigurationEntity { + + private List sources; + private boolean disableSystemSources; + + public List getSources() { + return sources; + } + + public void setSources(List sources) { + this.sources = sources; + } + + public boolean getDisableSystemSources(){ + return disableSystemSources; + } + public void setDisableSystemSources(boolean disableSystemSources) { + this.disableSystemSources = disableSystemSources; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/plan/PlanCommonModelBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/plan/PlanCommonModelBuilder.java index 1a4ace692..74968a139 100644 --- a/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/plan/PlanCommonModelBuilder.java +++ b/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/plan/PlanCommonModelBuilder.java @@ -61,6 +61,7 @@ public class PlanCommonModelBuilder extends BaseCommonModelBuilder authorize = EnumSet.of(AuthorizationFlags.None); @@ -96,6 +97,11 @@ public class PlanCommonModelBuilder extends BaseCommonModelBuilder evaluatorEntityTypes; + private boolean useSharedStorage; + private boolean hasLogo; + + public String getEvaluatorId() { + return evaluatorId; + } + + public void setEvaluatorId(String evaluatorId) { + this.evaluatorId = evaluatorId; + } + + public RankConfig getRankConfig() { + return rankConfig; + } + + public void setRankConfig(RankConfig rankConfig) { + this.rankConfig = rankConfig; + } + + public List getEvaluatorEntityTypes() { + return evaluatorEntityTypes; + } + + public void setEvaluatorEntityTypes(List evaluatorEntityTypes) { + this.evaluatorEntityTypes = evaluatorEntityTypes; + } + + public boolean isUseSharedStorage() { + return useSharedStorage; + } + + public void setUseSharedStorage(boolean useSharedStorage) { + this.useSharedStorage = useSharedStorage; + } + + public boolean isHasLogo() { + return hasLogo; + } + + public void setHasLogo(boolean hasLogo) { + this.hasLogo = hasLogo; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/evaluator/EvaluatorSource.java b/backend/core/src/main/java/org/opencdmp/model/evaluator/EvaluatorSource.java new file mode 100644 index 000000000..785fd3b74 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/evaluator/EvaluatorSource.java @@ -0,0 +1,76 @@ +package org.opencdmp.model.evaluator; + +public class EvaluatorSource { + + private String url; + + public static final String _url = "url"; + + private String evaluatorId; + + public static final String _evaluatorId = "evaluatorId"; + + private String issuerUrl; + + public static final String _issuerUrl = "issuerUrl"; + + private String clientId; + + public static final String _clientId = "clientId"; + + private String clientSecret; + + public static final String _clientSecret = "clientSecret"; + + private String scope; + + public static final String _scope = "scope"; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getEvaluatorId() { + return evaluatorId; + } + + public void setEvaluatorId(String evaluatorId) { + this.evaluatorId = evaluatorId; + } + + public String getIssuerUrl() { + return issuerUrl; + } + + public void setIssuerUrl(String issuerUrl) { + this.issuerUrl = issuerUrl; + } + + public String getClientId() { + return clientId; + } + + public void setClientId(String clientId) { + this.clientId = clientId; + } + + public String getClientSecret() { + return clientSecret; + } + + public void setClientSecret(String clientSecret) { + this.clientSecret = clientSecret; + } + + public String getScope() { + return scope; + } + + public void setScope(String scope) { + this.scope = scope; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/tenantconfiguration/TenantConfiguration.java b/backend/core/src/main/java/org/opencdmp/model/tenantconfiguration/TenantConfiguration.java index bb7826f4c..0d7240bb8 100644 --- a/backend/core/src/main/java/org/opencdmp/model/tenantconfiguration/TenantConfiguration.java +++ b/backend/core/src/main/java/org/opencdmp/model/tenantconfiguration/TenantConfiguration.java @@ -2,6 +2,7 @@ package org.opencdmp.model.tenantconfiguration; import org.opencdmp.commons.enums.IsActive; import org.opencdmp.commons.enums.TenantConfigurationType; +import org.opencdmp.commons.types.tenantconfiguration.EvaluatorTenantConfigurationEntity; import java.time.Instant; import java.util.UUID; @@ -36,6 +37,10 @@ public class TenantConfiguration { public static final String _fileTransformerPlugins = "fileTransformerPlugins"; + private EvaluatorTenantConfigurationEntity evaluatorPlugins; + + public static final String _evaluatorPlugins = "evaluatorPlugins"; + private LogoTenantConfiguration logo; public static final String _logo = "logo"; @@ -150,4 +155,12 @@ public class TenantConfiguration { public void setType(TenantConfigurationType type) { this.type = type; } + + public EvaluatorTenantConfigurationEntity getEvaluatorPlugins() { + return evaluatorPlugins; + } + + public void setEvaluatorPlugins(EvaluatorTenantConfigurationEntity evaluatorPlugins) { + this.evaluatorPlugins = evaluatorPlugins; + } } diff --git a/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorClientImpl.java b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorClientImpl.java new file mode 100644 index 000000000..1c387d538 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorClientImpl.java @@ -0,0 +1,58 @@ +package org.opencdmp.service.evaluator; + +import com.sun.jdi.InvalidTypeException; +import gr.cite.tools.logging.LoggerService; +import gr.cite.tools.logging.MapLogEntry; +import org.opencdmp.commonmodels.models.description.DescriptionModel; +import org.opencdmp.commonmodels.models.plan.PlanModel; +import org.opencdmp.evaluatorbase.interfaces.EvaluatorClient; +import org.opencdmp.evaluatorbase.interfaces.EvaluatorConfiguration; +import org.opencdmp.evaluatorbase.models.misc.RankModel; +import org.opencdmp.service.filetransformer.FileTransformerRepository; +import org.slf4j.LoggerFactory; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; + +import javax.management.InvalidApplicationException; +import java.io.IOException; + +public class EvaluatorClientImpl implements EvaluatorClient { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(FileTransformerRepository.class)); + + private final WebClient transformerClient; + + public EvaluatorClientImpl(WebClient transformerClient) { + this.transformerClient = transformerClient; + } + + @Override + public RankModel rankPlan(PlanModel planModel) throws InvalidApplicationException, IOException, InvalidTypeException { + logger.debug(new MapLogEntry("rankPlan").And("planModel", planModel)); + + return this.transformerClient.post().uri("/rank/plan").bodyValue(planModel) // Send planModel in the body + .exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(RankModel.class)).block(); + } + + @Override + public RankModel rankDescription(DescriptionModel descriptionModel) throws InvalidApplicationException, IOException { + logger.debug(new MapLogEntry("rankDescription").And("descriptionModel", descriptionModel)); + return this.transformerClient.post().uri("/rank/description").bodyValue(descriptionModel) // Send descriptionModel in the body + .exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(RankModel.class)).block(); + + } + + @Override + public EvaluatorConfiguration getConfiguration() { + logger.debug(new MapLogEntry("getConfiguration")); + return this.transformerClient.get().uri("/config") + .exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(new ParameterizedTypeReference() {})).block(); + } + + @Override + public String getLogo() { + logger.debug(new MapLogEntry("getLogo")); + return this.transformerClient.get().uri("/logo").exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(String.class)).block(); + } +} diff --git a/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorConfiguration.java b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorConfiguration.java new file mode 100644 index 000000000..93b0c7e34 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorConfiguration.java @@ -0,0 +1,9 @@ +package org.opencdmp.service.evaluator; + +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableConfigurationProperties({EvaluatorProperties.class}) +public class EvaluatorConfiguration { +} diff --git a/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorConfigurationCacheOptions.java b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorConfigurationCacheOptions.java new file mode 100644 index 000000000..0f31da03b --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorConfigurationCacheOptions.java @@ -0,0 +1,10 @@ +package org.opencdmp.service.evaluator; + +import gr.cite.tools.cache.CacheOptions; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "cache.evaluator-config-by-id") +public class EvaluatorConfigurationCacheOptions extends CacheOptions { +} diff --git a/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorConfigurationCacheService.java b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorConfigurationCacheService.java new file mode 100644 index 000000000..458dd83a3 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorConfigurationCacheService.java @@ -0,0 +1,76 @@ +package org.opencdmp.service.evaluator; + +import gr.cite.tools.cache.CacheService; +import org.opencdmp.evaluatorbase.interfaces.EvaluatorConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; + +@Service +public class EvaluatorConfigurationCacheService extends CacheService { + + public static class EvaluatorConfigurationCacheValue { + + public EvaluatorConfigurationCacheValue() { + } + + public EvaluatorConfigurationCacheValue(String repositoryId, String tenantCode, EvaluatorConfiguration configuration) { + this.evaluatorId = repositoryId; + this.configuration = configuration; + this.tenantCode = tenantCode == null ? "" : tenantCode; + } + + private String evaluatorId; + private String tenantCode; + + public String getEvaluatorId() { + return evaluatorId; + } + + public void setEvaluatorId(String evaluatorId) { + this.evaluatorId = evaluatorId; + } + + private EvaluatorConfiguration configuration; + + public EvaluatorConfiguration getConfiguration() { + return configuration; + } + + public void setConfiguration(EvaluatorConfiguration configuration) { + this.configuration = configuration; + } + + public String getTenantCode() { + return tenantCode; + } + + public void setTenantCode(String tenantCode) { + this.tenantCode = tenantCode; + } + } + + @Autowired + public EvaluatorConfigurationCacheService(EvaluatorConfigurationCacheOptions options) { + super(options); + } + + @Override + protected Class valueClass() { + return EvaluatorConfigurationCacheService.EvaluatorConfigurationCacheValue.class; + } + + @Override + public String keyOf(EvaluatorConfigurationCacheService.EvaluatorConfigurationCacheValue value) { + return this.buildKey(value.getEvaluatorId(), value.getTenantCode()); + } + + + public String buildKey(String evaluatorId, String tenantCod) { + HashMap keyParts = new HashMap<>(); + keyParts.put("$evaluatorId$", evaluatorId); + keyParts.put("$tenantCode$", tenantCod); + return this.generateKey(keyParts); + } +} diff --git a/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorProperties.java b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorProperties.java new file mode 100644 index 000000000..1781a19c0 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorProperties.java @@ -0,0 +1,21 @@ +package org.opencdmp.service.evaluator; + +import org.opencdmp.commons.types.evaluator.EvaluatorSourceEntity; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.util.List; + +@ConfigurationProperties(prefix = "evaluator") +public class EvaluatorProperties { + + private List sources; + + public List getSources() { + return sources; + } + + public void setSources(List sources) { + this.sources = sources; + } +} + diff --git a/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorService.java b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorService.java new file mode 100644 index 000000000..5c4b1b050 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorService.java @@ -0,0 +1,26 @@ +package org.opencdmp.service.evaluator; + +import com.sun.jdi.InvalidTypeException; + +import javax.crypto.BadPaddingException; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.management.InvalidApplicationException; +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.util.List; +import java.util.UUID; + +public interface EvaluatorService { + + org.opencdmp.evaluatorbase.models.misc.RankModel rankPlan(UUID planId, String repositoryId, String format, boolean isPublic) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException, InvalidTypeException; + + org.opencdmp.evaluatorbase.models.misc.RankModel rankDescription(UUID descriptionId, String repositoryId, String format, boolean isPublic) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException; + + List getAvailableEvaluators() throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException; + + String getLogo(String evaluatorId) throws InvalidApplicationException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException; + +} diff --git a/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorServiceImpl.java new file mode 100644 index 000000000..761f492b0 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorServiceImpl.java @@ -0,0 +1,326 @@ +package org.opencdmp.service.evaluator; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.sun.jdi.InvalidTypeException; +import gr.cite.commons.web.authz.service.AuthorizationService; +import gr.cite.commons.web.oidc.filter.webflux.TokenExchangeCacheService; +import gr.cite.commons.web.oidc.filter.webflux.TokenExchangeFilterFunction; +import gr.cite.commons.web.oidc.filter.webflux.TokenExchangeModel; +import gr.cite.tools.data.builder.BuilderFactory; +import gr.cite.tools.data.query.QueryFactory; +import gr.cite.tools.exception.MyNotFoundException; +import gr.cite.tools.fieldset.BaseFieldSet; +import gr.cite.tools.logging.LoggerService; +import gr.cite.tools.logging.MapLogEntry; +import org.opencdmp.authorization.AuthorizationFlags; +import org.opencdmp.authorization.Permission; +import org.opencdmp.commonmodels.models.description.DescriptionModel; +import org.opencdmp.commons.JsonHandlingService; +import org.opencdmp.commons.enums.*; +import org.opencdmp.commons.scope.tenant.TenantScope; +import org.opencdmp.commons.types.evaluator.EvaluatorSourceEntity; +import org.opencdmp.depositbase.repository.DepositClient; +import org.opencdmp.evaluatorbase.interfaces.EvaluatorClient; +import org.opencdmp.evaluatorbase.interfaces.EvaluatorConfiguration; +import org.opencdmp.commons.types.tenantconfiguration.EvaluatorTenantConfigurationEntity; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.data.DescriptionEntity; +import org.opencdmp.data.PlanEntity; +import org.opencdmp.data.TenantConfigurationEntity; +import org.opencdmp.data.TenantEntityManager; +import org.opencdmp.evaluatorbase.models.misc.RankModel; +import org.opencdmp.event.TenantConfigurationTouchedEvent; +import org.opencdmp.model.builder.commonmodels.DepositConfigurationBuilder; +import org.opencdmp.model.builder.commonmodels.description.DescriptionCommonModelBuilder; +import org.opencdmp.model.builder.commonmodels.plan.PlanCommonModelBuilder; +import org.opencdmp.model.description.Description; +import org.opencdmp.model.plan.Plan; +import org.opencdmp.commonmodels.models.plan.PlanModel; +import org.opencdmp.model.tenantconfiguration.TenantConfiguration; +import org.opencdmp.query.DescriptionQuery; +import org.opencdmp.query.PlanQuery; +import org.opencdmp.query.TenantConfigurationQuery; +import org.opencdmp.service.accounting.AccountingService; +import org.opencdmp.service.encryption.EncryptionService; +import org.opencdmp.service.tenant.TenantProperties; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.MessageSource; +import org.springframework.context.event.EventListener; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.http.MediaType; +import org.springframework.http.codec.json.Jackson2JsonDecoder; +import org.springframework.http.codec.json.Jackson2JsonEncoder; +import org.springframework.stereotype.Service; +import org.springframework.web.reactive.function.client.ExchangeFilterFunction; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; + +import javax.crypto.BadPaddingException; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.management.InvalidApplicationException; +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.util.*; + +import static org.opencdmp.authorization.AuthorizationFlags.Public; + +@Service +public class EvaluatorServiceImpl implements EvaluatorService { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(EvaluatorServiceImpl.class)); + private final EvaluatorProperties evaluatorProperties; + private final Map clients; + private final TokenExchangeCacheService tokenExchangeCacheService; + private final EvaluatorConfigurationCacheService evaluatorConfigurationCacheService; + private final AuthorizationService authorizationService; + private final QueryFactory queryFactory; + private final BuilderFactory builderFactory; + private final MessageSource messageSource; + private final ConventionService conventionService; + private final TenantScope tenantScope; + private final EncryptionService encryptionService; + private final TenantProperties tenantProperties; + private final JsonHandlingService jsonHandlingService; + private final EvaluatorSourcesCacheService evaluatorSourcesCacheService; + private final AccountingService accountingService; + private final TenantEntityManager entityManager; + + @Autowired + public EvaluatorServiceImpl(EvaluatorProperties evaluatorProperties, Map clients, TokenExchangeCacheService tokenExchangeCacheService, EvaluatorConfigurationCacheService evaluatorConfigurationCacheService, AuthorizationService authorizationService, QueryFactory queryFactory, BuilderFactory builderFactory, MessageSource messageSource, ConventionService conventionService, TenantScope tenantScope, EncryptionService encryptionService, TenantProperties tenantProperties, JsonHandlingService jsonHandlingService, EvaluatorSourcesCacheService evaluatorSourcesCacheService, AccountingService accountingService, TenantEntityManager entityManager) { + this.evaluatorProperties = evaluatorProperties; + this.clients = clients; + this.tokenExchangeCacheService = tokenExchangeCacheService; + this.evaluatorConfigurationCacheService = evaluatorConfigurationCacheService; + this.authorizationService = authorizationService; + this.queryFactory = queryFactory; + this.builderFactory = builderFactory; + this.messageSource = messageSource; + this.conventionService = conventionService; + this.tenantScope = tenantScope; + this.encryptionService = encryptionService; + this.tenantProperties = tenantProperties; + this.jsonHandlingService = jsonHandlingService; + this.evaluatorSourcesCacheService = evaluatorSourcesCacheService; + this.accountingService = accountingService; + this.entityManager = entityManager; + } + private EvaluatorClientImpl getEvaluatorClient(String repoId) throws InvalidApplicationException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException { + String repositoryIdByTenant = this.getRepositoryIdByTenant(repoId); + if (this.clients.containsKey(repositoryIdByTenant)) { + return this.clients.get(repositoryIdByTenant); + } + + EvaluatorSourceEntity source = this.getEvaluatorSources().stream() + .filter(evaluatorSourceEntity -> evaluatorSourceEntity.getEvaluatorId().equals(repoId)) + .findFirst().orElse(null); + + try { + TokenExchangeModel tokenExchangeModel = new TokenExchangeModel( + "evaluator:" + repositoryIdByTenant, + source.getIssuerUrl(), + source.getClientId(), + source.getClientSecret(), + source.getScope() + ); + + TokenExchangeFilterFunction tokenExchangeFilterFunction = new TokenExchangeFilterFunction( + this.tokenExchangeCacheService, + tokenExchangeModel + ); + + EvaluatorClientImpl repository = new EvaluatorClientImpl( + WebClient.builder().baseUrl(source.getUrl() + "/api/evaluator") + .filters(exchangeFilterFunctions -> { + exchangeFilterFunctions.add(tokenExchangeFilterFunction); + exchangeFilterFunctions.add(logRequest()); + exchangeFilterFunctions.add(logResponse()); + }) + .codecs(codecs -> { + codecs.defaultCodecs().maxInMemorySize(source.getMaxInMemorySizeInBytes()); + codecs.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(new ObjectMapper().registerModule(new JavaTimeModule()), MediaType.APPLICATION_JSON)); + codecs.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(new ObjectMapper().registerModule(new JavaTimeModule()), MediaType.APPLICATION_JSON)); + }) + .build() + ); + + this.clients.put(repositoryIdByTenant, repository); + return repository; + } catch (Exception e) { + logger.error("Exception occurred while creating EvaluatorClientImpl", e); + return null; + } + } + + + private List getEvaluatorSources() throws InvalidApplicationException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException { + String tenantCode = this.tenantScope.isSet() && this.tenantScope.isMultitenant() ? this.tenantScope.getTenantCode() : ""; + EvaluatorSourcesCacheService.EvaluatorSourceCacheValue cacheValue = this.evaluatorSourcesCacheService.lookup(this.evaluatorSourcesCacheService.buildKey(tenantCode)); + if (cacheValue == null) { + List evaluatorSourceEntities = new ArrayList<>(this.evaluatorProperties.getSources()); + + if (this.tenantScope.isSet() && this.tenantScope.isMultitenant()) { + TenantConfigurationQuery tenantConfigurationQuery = this.queryFactory.query(TenantConfigurationQuery.class).disableTracking().isActive(IsActive.Active).types(TenantConfigurationType.EvaluatorPlugins); + if (this.tenantScope.isDefaultTenant()) tenantConfigurationQuery.tenantIsSet(false); + else tenantConfigurationQuery.tenantIsSet(true).tenantIds(this.tenantScope.getTenant()); + TenantConfigurationEntity tenantConfiguration = tenantConfigurationQuery.firstAs(new BaseFieldSet().ensure(TenantConfiguration._evaluatorPlugins)); + + if (tenantConfiguration != null && !this.conventionService.isNullOrEmpty(tenantConfiguration.getValue())) { + EvaluatorTenantConfigurationEntity evaluatorTenantConfigurationEntity = this.jsonHandlingService.fromJsonSafe(EvaluatorTenantConfigurationEntity.class, tenantConfiguration.getValue()); + if (evaluatorTenantConfigurationEntity != null) { + if (evaluatorTenantConfigurationEntity.getDisableSystemSources()) evaluatorSourceEntities = new ArrayList<>(); + evaluatorSourceEntities.addAll(this.buildEvaluatorSourceItems(evaluatorTenantConfigurationEntity.getSources())); + } + } + } + cacheValue = new EvaluatorSourcesCacheService.EvaluatorSourceCacheValue(tenantCode, evaluatorSourceEntities); + this.evaluatorSourcesCacheService.put(cacheValue); + } + return cacheValue.getSources(); + } + + @EventListener + public void handleTenantConfigurationTouchedEvent(TenantConfigurationTouchedEvent event) { + if (!event.getType().equals(TenantConfigurationType.FileTransformerPlugins)) return; + EvaluatorSourcesCacheService.EvaluatorSourceCacheValue evaluatorSourceCacheValue = this.evaluatorSourcesCacheService.lookup(this.evaluatorSourcesCacheService.buildKey(event.getTenantCode())); + if (evaluatorSourceCacheValue != null && evaluatorSourceCacheValue.getSources() != null){ + for (EvaluatorSourceEntity source : evaluatorSourceCacheValue.getSources()){ + String repositoryIdByTenant = source.getEvaluatorId() + "_" + event.getTenantCode(); + this.clients.remove(repositoryIdByTenant); + this.evaluatorConfigurationCacheService.evict(this.evaluatorConfigurationCacheService.buildKey(source.getEvaluatorId(), event.getTenantCode())); + } + } + this.evaluatorConfigurationCacheService.evict(this.evaluatorSourcesCacheService.buildKey(event.getTenantCode())); + } + + private List buildEvaluatorSourceItems(List sources) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException { + List items = new ArrayList<>(); + if (this.conventionService.isListNullOrEmpty(sources)) return items; + for (EvaluatorSourceEntity source : sources){ + EvaluatorSourceEntity item = new EvaluatorSourceEntity(); + item.setEvaluatorId(source.getEvaluatorId()); + item.setUrl(source.getUrl()); + item.setIssuerUrl(source.getIssuerUrl()); + item.setClientId(source.getClientId()); + if (!this.conventionService.isNullOrEmpty(source.getClientSecret())) item.setClientSecret(this.encryptionService.decryptAES(source.getClientSecret(), this.tenantProperties.getConfigEncryptionAesKey(), this.tenantProperties.getConfigEncryptionAesIv())); + item.setScope(source.getScope()); + items.add(item); + } + return items; + } + + private String getRepositoryIdByTenant(String repositoryId) throws InvalidApplicationException { + if (this.tenantScope.isSet() && this.tenantScope.isMultitenant()) { + return repositoryId + "_" + this.tenantScope.getTenantCode(); + } else { + return repositoryId; + } + } + + @Override + public List getAvailableEvaluators() throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException { + + this.authorizationService.authorizeForce(Permission.BrowsePlan, Permission.DeferredAffiliation); + List configurations = new ArrayList<>(); + + for(EvaluatorSourceEntity evaluatorSource : this.getEvaluatorSources()){ + + String tenantCode = this.tenantScope.isSet() && this.tenantScope.isMultitenant() ? this.tenantScope.getTenantCode() : ""; + EvaluatorConfigurationCacheService.EvaluatorConfigurationCacheValue cacheValue = this.evaluatorConfigurationCacheService.lookup(this.evaluatorConfigurationCacheService.buildKey(evaluatorSource.getEvaluatorId(), tenantCode)); + + if(cacheValue == null){ + try{ + EvaluatorClientImpl evaluatorClient = this.getEvaluatorClient(evaluatorSource.getEvaluatorId()); + if(evaluatorClient == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{evaluatorSource.getEvaluatorId(), EvaluatorClientImpl.class.getSimpleName()}, LocaleContextHolder.getLocale())); + + EvaluatorConfiguration configuration = evaluatorClient.getConfiguration(); + cacheValue = new EvaluatorConfigurationCacheService.EvaluatorConfigurationCacheValue(evaluatorSource.getEvaluatorId(), tenantCode, configuration); + this.evaluatorConfigurationCacheService.put(cacheValue); + }catch (Exception e){ + logger.error(e.getMessage(), e); + } + } + if(cacheValue != null){ + configurations.add(cacheValue.getConfiguration()); + } + } + + return configurations; + } + + @Override + public RankModel rankPlan(UUID planId, String evaluatorId, String format, boolean isPublic) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException, InvalidTypeException { + this.authorizationService.authorizeForce(Permission.EvaluatePlan); + EvaluatorClientImpl repository = this.getEvaluatorClient(evaluatorId); + + if(repository == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{format, EvaluatorClientImpl.class.getSimpleName()}, LocaleContextHolder.getLocale())); + + PlanEntity planEntity = this.queryFactory.query(PlanQuery.class).disableTracking().ids(planId).first(); + if (planEntity == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{planId, PlanEntity.class.getSimpleName()}, LocaleContextHolder.getLocale())); + + PlanModel evaluatorModel = this.builderFactory.builder(PlanCommonModelBuilder.class).useSharedStorage(repository.getConfiguration().isUseSharedStorage()).setEvaluatorId(repository.getConfiguration().getEvaluatorId()).isPublic(isPublic).authorize(AuthorizationFlags.All).build(planEntity); + if(evaluatorModel == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{planId, Plan.class.getSimpleName()}, LocaleContextHolder.getLocale())); + + this.accountingService.increase(UsageLimitTargetMetric.FILE_TRANSFORMER_EXPORT_PLAN_EXECUTION_COUNT.getValue()); + this.increaseTargetMetricWithRepositoryId(UsageLimitTargetMetric.FILE_TRANSFORMER_EXPORT_PLAN_EXECUTION_COUNT_FOR, evaluatorId); + + return repository.rankPlan(evaluatorModel); + } + + @Override + public RankModel rankDescription(UUID descriptionId, String repositoryId, String format, boolean isPublic) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException { + this.authorizationService.authorizeForce(Permission.EvaluateDescription); + EvaluatorClientImpl repository = this.getEvaluatorClient(repositoryId); + + if(repository == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{format, EvaluatorClientImpl.class.getSimpleName()}, LocaleContextHolder.getLocale())); + + DescriptionEntity descriptionEntity = this.queryFactory.query(DescriptionQuery.class).disableTracking().ids(descriptionId).first(); + if (descriptionEntity == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{descriptionId, DescriptionEntity.class.getSimpleName()}, LocaleContextHolder.getLocale())); + + DescriptionModel descriptionEvaluatorModel = this.builderFactory.builder(DescriptionCommonModelBuilder.class).setRepositoryId(repository.getConfiguration().getEvaluatorId()).useSharedStorage(repository.getConfiguration().isUseSharedStorage()).isPublic(isPublic).authorize(AuthorizationFlags.All).build(descriptionEntity); + if (descriptionEvaluatorModel == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{descriptionId, Description.class.getSimpleName()}, LocaleContextHolder.getLocale())); + + this.accountingService.increase(UsageLimitTargetMetric.FILE_TRANSFORMER_EXPORT_DESCRIPTIONS_EXECUTION_COUNT.getValue()); + this.increaseTargetMetricWithRepositoryId(UsageLimitTargetMetric.FILE_TRANSFORMER_EXPORT_DESCRIPTIONS_EXECUTION_COUNT_FOR, repositoryId); + + return repository.rankDescription(descriptionEvaluatorModel); + } + + @Override + public String getLogo(String evaluatorId) throws InvalidApplicationException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException { + + this.authorizationService.authorizeForce(Permission.BrowseDeposit, Permission.DeferredAffiliation); + EvaluatorClient evaluatorClient = this.getEvaluatorClient(evaluatorId); + if(evaluatorClient == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{evaluatorId, EvaluatorClient.class.getSimpleName()}, LocaleContextHolder.getLocale())); + return evaluatorClient.getLogo(); + } + + private static ExchangeFilterFunction logRequest() { + return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { + logger.debug(new MapLogEntry("Request").And("method", clientRequest.method().toString()).And("url", clientRequest.url())); + return Mono.just(clientRequest); + }); + } + + private static ExchangeFilterFunction logResponse() { + return ExchangeFilterFunction.ofResponseProcessor(response -> { + if (response.statusCode().isError()) { + return response.mutate().build().bodyToMono(String.class) + .flatMap(body -> { + logger.error(new MapLogEntry("Response").And("method", response.request().getMethod().toString()).And("url", response.request().getURI()).And("status", response.statusCode().toString()).And("body", body)); + return Mono.just(response); + }); + } + return Mono.just(response); + + }); + } + + private void increaseTargetMetricWithRepositoryId(UsageLimitTargetMetric metric, String repositoryId) throws InvalidApplicationException { + this.accountingService.increase(metric.getValue() + repositoryId); + } +} diff --git a/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorSourcesCacheOptions.java b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorSourcesCacheOptions.java new file mode 100644 index 000000000..1261aaad2 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorSourcesCacheOptions.java @@ -0,0 +1,10 @@ +package org.opencdmp.service.evaluator; + +import gr.cite.tools.cache.CacheOptions; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "cache.evaluator-sources-by-tenant") +public class EvaluatorSourcesCacheOptions extends CacheOptions { +} diff --git a/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorSourcesCacheService.java b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorSourcesCacheService.java new file mode 100644 index 000000000..7475be717 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/evaluator/EvaluatorSourcesCacheService.java @@ -0,0 +1,66 @@ +package org.opencdmp.service.evaluator; + +import gr.cite.tools.cache.CacheService; +import org.opencdmp.commons.types.evaluator.EvaluatorSourceEntity; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; + +@Service +public class EvaluatorSourcesCacheService extends CacheService { + + public static class EvaluatorSourceCacheValue { + + public EvaluatorSourceCacheValue() { + } + + public EvaluatorSourceCacheValue(String tenantCode, List sources) { + this.tenantCode = tenantCode; + this.sources = sources; + } + + private String tenantCode; + + private List sources; + + + public String getTenantCode() { + return tenantCode; + } + + public void setTenantCode(String tenantCode) { + this.tenantCode = tenantCode; + } + + public List getSources() { + return sources; + } + + public void setSources(List sources) { + this.sources = sources; + } + } + + @Autowired + public EvaluatorSourcesCacheService(EvaluatorSourcesCacheOptions options) { + super(options); + } + + @Override + protected Class valueClass() { + return EvaluatorSourceCacheValue.class; + } + + @Override + public String keyOf(EvaluatorSourceCacheValue value) { + return this.buildKey(value.getTenantCode()); + } + + public String buildKey(String tenantCod) { + HashMap keyParts = new HashMap<>(); + keyParts.put("$tenantCode$", tenantCod); + return this.generateKey(keyParts); + } +} diff --git a/backend/core/src/main/java/org/opencdmp/service/filetransformer/FileTransformerServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/filetransformer/FileTransformerServiceImpl.java index 5c81519f6..d9054cc21 100644 --- a/backend/core/src/main/java/org/opencdmp/service/filetransformer/FileTransformerServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/filetransformer/FileTransformerServiceImpl.java @@ -151,8 +151,6 @@ public class FileTransformerServiceImpl implements FileTransformerService { return null; } - - private List getFileTransformerSources() throws InvalidApplicationException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException { String tenantCode = this.tenantScope.isSet() && this.tenantScope.isMultitenant() ? this.tenantScope.getTenantCode() : ""; FileTransformerSourcesCacheService.FileTransformerSourceCacheValue cacheValue = this.fileTransformerSourcesCacheService.lookup(this.fileTransformerSourcesCacheService.buildKey(tenantCode)); diff --git a/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java index 66c3b9311..d39b3db91 100644 --- a/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java @@ -869,8 +869,6 @@ public class PlanServiceImpl implements PlanService { } - - private void updateVersionStatusAndSave(PlanEntity data, PlanStatus previousStatus, PlanStatus newStatus) throws InvalidApplicationException { if (previousStatus == null && newStatus == null) return; diff --git a/backend/web/src/main/java/org/opencdmp/controllers/EvaluatorController.java b/backend/web/src/main/java/org/opencdmp/controllers/EvaluatorController.java new file mode 100644 index 000000000..b4e218890 --- /dev/null +++ b/backend/web/src/main/java/org/opencdmp/controllers/EvaluatorController.java @@ -0,0 +1,114 @@ +package org.opencdmp.controllers; + +import gr.cite.tools.auditing.AuditService; +import gr.cite.tools.logging.LoggerService; +import gr.cite.tools.logging.MapLogEntry; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import org.opencdmp.audit.AuditableAction; +import org.opencdmp.controllers.swagger.SwaggerHelpers; +import org.opencdmp.controllers.swagger.annotation.OperationWithTenantHeader; +import org.opencdmp.controllers.swagger.annotation.Swagger404; +import org.opencdmp.controllers.swagger.annotation.SwaggerCommonErrorResponses; +import org.opencdmp.evaluatorbase.interfaces.EvaluatorConfiguration; +import org.opencdmp.evaluatorbase.models.misc.RankModel; +import org.opencdmp.model.evaluator.EvaluateRequestModel; +import org.opencdmp.model.file.ExportRequestModel; +import org.opencdmp.service.evaluator.EvaluatorService; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.crypto.BadPaddingException; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.management.InvalidApplicationException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.util.AbstractMap; +import java.util.List; +import java.util.Map; + +@RestController +@CrossOrigin +@RequestMapping(value = "/api/evaluator") +@SwaggerCommonErrorResponses +public class EvaluatorController { + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(EvaluatorController.class)); + + private final EvaluatorService evaluatorService; + + private final AuditService auditService; + + @Autowired + public EvaluatorController(EvaluatorService evaluatorService, AuditService auditService){ + this.evaluatorService = evaluatorService; + this.auditService = auditService; + } + + @GetMapping("/available") + @OperationWithTenantHeader(summary = "Fetch all evaluators", description = SwaggerHelpers.Evaluator.endpoint_get_available_evaluators, + responses = @ApiResponse(description = "OK", responseCode = "200", content = @Content( + array = @ArraySchema( + schema = @Schema( + implementation = EvaluatorConfiguration.class + ))) + )) + public List getAvailableConfigurations() throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, InvalidApplicationException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException { + logger.debug(new MapLogEntry("getAvailableConfigurations")); + + List model = this.evaluatorService.getAvailableEvaluators(); + this.auditService.track(AuditableAction.Evaluator_GetAvailableConfigurations); + + return model; + } + + @PostMapping("/rank-plan") + @OperationWithTenantHeader(summary = "Rank a plan", description = SwaggerHelpers.Evaluator.endpoint_rank_plans, + responses = @ApiResponse(description = "OK", responseCode = "200")) + public ResponseEntity rankPlan(@RequestBody EvaluateRequestModel requestModel) throws Exception { + logger.debug(new MapLogEntry("ranking plan")); + + RankModel rankModel = this.evaluatorService.rankPlan(requestModel.getId(), requestModel.getEvaluatorId(), requestModel.getFormat(), true); + + return new ResponseEntity<>(rankModel, HttpStatus.OK); + } + + @PostMapping("/rank-description") + @OperationWithTenantHeader(summary = "Rank a description", description = SwaggerHelpers.Evaluator.endpoint_rank_descriptions, + responses = @ApiResponse(description = "OK", responseCode = "200")) + public ResponseEntity rankDescription(@RequestBody EvaluateRequestModel requestModel) throws Exception { + logger.debug(new MapLogEntry("ranking description")); + + RankModel rankModel = this.evaluatorService.rankDescription(requestModel.getId(), requestModel.getEvaluatorId(), requestModel.getFormat(), true); + + return new ResponseEntity<>(rankModel, HttpStatus.OK); + } + + @GetMapping("/{evaluatorId}/logo") + @OperationWithTenantHeader(summary = "Fetch a specific evaluator logo by id", description = SwaggerHelpers.Deposit.endpoint_get_logo, + responses = @ApiResponse(description = "OK", responseCode = "200", content = @Content( + schema = @Schema( + implementation = String.class + )) + )) + @Swagger404 + public String getLogo( + @Parameter(name = "evaluatorId", description = "The id of an evaluator of which to fetch the logo", example = "zenodo", required = true) @PathVariable("evaluatorId") String evaluatorId + ) throws InvalidApplicationException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException { + logger.debug(new MapLogEntry("get logo" + EvaluatorConfiguration.class.getSimpleName()).And("evaluatorId", evaluatorId)); + + String logo = this.evaluatorService.getLogo(evaluatorId); + this.auditService.track(AuditableAction.Deposit_GetLogo, Map.ofEntries( + new AbstractMap.SimpleEntry("evaluatorId", evaluatorId) + )); + + return logo; + } +} diff --git a/backend/web/src/main/java/org/opencdmp/controllers/swagger/SwaggerHelpers.java b/backend/web/src/main/java/org/opencdmp/controllers/swagger/SwaggerHelpers.java index a01110aaf..db2bdc656 100644 --- a/backend/web/src/main/java/org/opencdmp/controllers/swagger/SwaggerHelpers.java +++ b/backend/web/src/main/java/org/opencdmp/controllers/swagger/SwaggerHelpers.java @@ -2589,6 +2589,24 @@ public final class SwaggerHelpers { } + public static final class Evaluator { + + public static final String endpoint_get_available_evaluators = + """ + This endpoint is used to fetch all the available evaluators.
+ """; + + public static final String endpoint_rank_plans = + """ + This endpoint is used to rank a plan using a specific evaluator.
+ """; + + public static final String endpoint_rank_descriptions = + """ + This endpoint is used to rank a description using a specific evaluator.
+ """; + } + public static final class EntityDoi { public static final String endpoint_query = diff --git a/backend/web/src/main/resources/config/application.yml b/backend/web/src/main/resources/config/application.yml index 89759d1b5..830787ed4 100644 --- a/backend/web/src/main/resources/config/application.yml +++ b/backend/web/src/main/resources/config/application.yml @@ -29,6 +29,7 @@ spring: optional:classpath:config/public-api.yml[.yml], optional:classpath:config/public-api-${spring.profiles.active}.yml[.yml], optional:file:../config/public-api-${spring.profiles.active}.yml[.yml], optional:classpath:config/dashboard.yml[.yml], optional:classpath:config/dashboard-${spring.profiles.active}.yml[.yml], optional:file:../config/dashboard-${spring.profiles.active}.yml[.yml], optional:classpath:config/file-transformer.yml[.yml], optional:classpath:config/file-transformer-${spring.profiles.active}.yml[.yml], optional:file:../config/file-transformer-${spring.profiles.active}.yml[.yml], + optional:classpath:config/evaluator.yml[.yml], optional:classpath:config/evaluator-${spring.profiles.active}.yml[.yml], optional:file:../config/evaluator-${spring.profiles.active}.yml[.yml], optional:classpath:config/authorization.yml[.yml], optional:classpath:config/authorization-${spring.profiles.active}.yml[.yml], optional:file:../config/authorization-${spring.profiles.active}.yml[.yml], optional:classpath:config/metrics.yml[.yml], optional:classpath:config/metrics-${spring.profiles.active}.yml[.yml], optional:file:../config/metrics-${spring.profiles.active}.yml[.yml], optional:classpath:config/field-set-expander.yml[.yml], optional:classpath:config/field-set-expander-${spring.profiles.active}.yml[.yml], optional:file:../config/field-set-expander-${spring.profiles.active}.yml[.yml], diff --git a/backend/web/src/main/resources/config/cache.yml b/backend/web/src/main/resources/config/cache.yml index 1662b2e82..d81273941 100644 --- a/backend/web/src/main/resources/config/cache.yml +++ b/backend/web/src/main/resources/config/cache.yml @@ -50,12 +50,24 @@ cache: maximumSize: 500 enableRecordStats: false expireAfterWriteSeconds: 600 + - names: [ "evaluatorConfigById" ] + allowNullValues: true + initialCapacity: 100 + maximumSize: 500 + enableRecordStats: false + expireAfterWriteSeconds: 600 - names: [ "fileTransformerSourcesByTenant" ] allowNullValues: true initialCapacity: 100 maximumSize: 500 enableRecordStats: false expireAfterWriteSeconds: 600 + - names: [ "evaluatorSourcesByTenant" ] + allowNullValues: true + initialCapacity: 100 + maximumSize: 500 + enableRecordStats: false + expireAfterWriteSeconds: 600 - names: [ "tokenExchangeKey" ] allowNullValues: true initialCapacity: 100 @@ -126,9 +138,15 @@ cache: fileTransformerConfigById: name: fileTransformerConfigById keyPattern: file_transformer_config_by_id_$transformerId$_$tenantCode$:v0 + evaluatorConfigById: + name: evaluatorConfigById + keyPattern: evaluator_config_by_id_$evaluatorId$_$tenantCode$:v0 fileTransformerSourcesByTenant: name: fileTransformerSourcesByTenant keyPattern: ile_transformer_sources_by_tenant_$tenantCode$:v0 + evaluatorSourcesByTenant: + name: evaluatorSourcesByTenant + keyPattern: evaluator_sources_by_tenant_$tenantCode$:v0 token-exchange-key: name: tokenExchangeKey keyPattern: resolve_$keyhash$:v0 diff --git a/backend/web/src/main/resources/config/evaluator-devel.yml b/backend/web/src/main/resources/config/evaluator-devel.yml new file mode 100644 index 000000000..d16babf20 --- /dev/null +++ b/backend/web/src/main/resources/config/evaluator-devel.yml @@ -0,0 +1,9 @@ +evaluator: + sources: + - url: http://localhost:8084 + evaluatorId: fair + issuer-url: ${IDP_ISSUER_URI_TOKEN} + client-id: ${IDP_APIKEY_CLIENT_ID} + client-secret: ${IDP_APIKEY_CLIENT_SECRET} + scope: ${IDP_APIKEY_SCOPE} + maxInMemorySizeInBytes: 6554000 \ No newline at end of file diff --git a/backend/web/src/main/resources/config/evaluator.yml b/backend/web/src/main/resources/config/evaluator.yml new file mode 100644 index 000000000..24b2af433 --- /dev/null +++ b/backend/web/src/main/resources/config/evaluator.yml @@ -0,0 +1,2 @@ +evaluator: + sources: [] \ No newline at end of file diff --git a/backend/web/src/main/resources/config/permissions.yml b/backend/web/src/main/resources/config/permissions.yml index a53416882..2805ea3ad 100644 --- a/backend/web/src/main/resources/config/permissions.yml +++ b/backend/web/src/main/resources/config/permissions.yml @@ -371,6 +371,17 @@ permissions: clients: [ ] allowAnonymous: false allowAuthenticated: false + EvaluateDescription: + roles: + - Admin + - TenantAdmin + plan: + roles: + - Owner + claims: [ ] + clients: [ ] + allowAnonymous: false + allowAuthenticated: false CloneDescription: roles: - Admin @@ -658,6 +669,17 @@ permissions: clients: [ ] allowAnonymous: false allowAuthenticated: false + EvaluatePlan: + roles: + - Admin + - TenantAdmin + plan: + roles: + - Owner + claims: [ ] + clients: [ ] + allowAnonymous: false + allowAuthenticated: false ClonePlan: roles: - Admin diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 90ff854e4..f0e9c8a22 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -28,8 +28,8 @@ "cookieconsent": "^3.1.1", "dragula": "^3.7.3", "file-saver": "^2.0.5", - "keycloak-angular": "^15.2.1", - "keycloak-js": "^24.0.5", + "keycloak-angular": "^16.0.1", + "keycloak-js": "^25.0.0", "moment": "^2.30.1", "moment-timezone": "^0.5.45", "ng-dialog-animation": "^9.0.4", @@ -8856,23 +8856,23 @@ } }, "node_modules/keycloak-angular": { - "version": "15.2.1", - "resolved": "https://registry.npmjs.org/keycloak-angular/-/keycloak-angular-15.2.1.tgz", - "integrity": "sha512-7w8bkJQ9OBtBJt5eNfqnRG2IL9btvp8Stf2fpVipSE1C/qtd5UQ31skx735PMPgMTUFsdz/0VA32Gmsng54+Xg==", + "version": "16.0.1", + "resolved": "https://registry.npmjs.org/keycloak-angular/-/keycloak-angular-16.0.1.tgz", + "integrity": "sha512-ytkL32R/tfHEyZ3txQtgH1y0WofW/D36zTbo2agDCYUtZETq0wAQ3E/4bVDUAr6ZKwotgAnIyOORfErnvDkXng==", "dependencies": { "tslib": "^2.3.1" }, "peerDependencies": { - "@angular/common": "^17", - "@angular/core": "^17", - "@angular/router": "^17", - "keycloak-js": "^18 || ^19 || ^20 || ^21 || ^22 || ^23 || ^24" + "@angular/common": "^18", + "@angular/core": "^18", + "@angular/router": "^18", + "keycloak-js": "^18 || ^19 || ^20 || ^21 || ^22 || ^23 || ^24 || ^25" } }, "node_modules/keycloak-js": { - "version": "24.0.5", - "resolved": "https://registry.npmjs.org/keycloak-js/-/keycloak-js-24.0.5.tgz", - "integrity": "sha512-VQOSn3j13DPB6OuavKAq+sRjDERhIKrXgBzekoHRstifPuyULILguugX6yxRUYFSpn3OMYUXmSX++tkdCupOjA==", + "version": "25.0.6", + "resolved": "https://registry.npmjs.org/keycloak-js/-/keycloak-js-25.0.6.tgz", + "integrity": "sha512-Km+dc+XfNvY6a4az5jcxTK0zPk52ns9mAxLrHj7lF3V+riVYvQujfHmhayltJDjEpSOJ4C8a57LFNNKnNnRP2g==", "dependencies": { "js-sha256": "^0.11.0", "jwt-decode": "^4.0.0" diff --git a/frontend/src/app/core/common/enum/evaluator-entity-type.ts b/frontend/src/app/core/common/enum/evaluator-entity-type.ts new file mode 100644 index 000000000..911900bfa --- /dev/null +++ b/frontend/src/app/core/common/enum/evaluator-entity-type.ts @@ -0,0 +1,4 @@ +export enum EvaluatorEntityType { + Plan = 0, + Description = 1 +} \ No newline at end of file diff --git a/frontend/src/app/core/common/enum/permission.enum.ts b/frontend/src/app/core/common/enum/permission.enum.ts index dfbf8d2a0..69549d222 100644 --- a/frontend/src/app/core/common/enum/permission.enum.ts +++ b/frontend/src/app/core/common/enum/permission.enum.ts @@ -84,6 +84,7 @@ export enum AppPermission { AssignPlanUsers = "AssignPlanUsers", InvitePlanUsers = "InvitePlanUsers", AnnotatePlan = "AnnotatePlan", + EvaluatePlan = "EvaluatePlan", //PlanStatus BrowsePlanStatus = "BrowsePlanStatus", @@ -121,6 +122,7 @@ export enum AppPermission { DeleteDescription = "DeleteDescription", CloneDescription = "CloneDescription", ExportDescription = "ExportDescription", + EvaluateDescription = "EvaluateDescription", //DescriptionTag BrowseDescriptionTag = "BrowseDescriptionTag", diff --git a/frontend/src/app/core/core-service.module.ts b/frontend/src/app/core/core-service.module.ts index 14fbaeff1..d9481b26f 100644 --- a/frontend/src/app/core/core-service.module.ts +++ b/frontend/src/app/core/core-service.module.ts @@ -52,6 +52,7 @@ import { PlanStatusService } from './services/plan/plan-status.service'; import { DescriptionStatusService } from './services/description-status/description-status.service'; import { PlanWorkflowService } from './services/plan/plan-workflow.service'; import { DescriptionWorkflowService } from './services/description-workflow/description-workflow.service'; +import { EvaluatorHttpService } from './services/evaluator/evaluator.http.service'; // // // This is shared module that provides all the services. Its imported only once on the AppModule. @@ -112,6 +113,7 @@ export class CoreServiceModule { CanDeactivateGuard, FileTransformerService, FileTransformerHttpService, + EvaluatorHttpService, SemanticsService, PrefillingSourceService, VisibilityRulesService, diff --git a/frontend/src/app/core/model/evaluator/evaluator-configuration.ts b/frontend/src/app/core/model/evaluator/evaluator-configuration.ts new file mode 100644 index 000000000..873c636ed --- /dev/null +++ b/frontend/src/app/core/model/evaluator/evaluator-configuration.ts @@ -0,0 +1,11 @@ +import { RankType } from "./rank-type"; +import { EvaluatorEntityType } from "@app/core/common/enum/evaluator-entity-type"; +import { RankConfig } from "./rank-config"; + +export class EvaluatorConfiguration{ + evaluatorId: string; + rankType: RankType[]; + evaluatorEntityTypes: EvaluatorEntityType[]; + rankConfig: RankConfig[]; + hasLogo: boolean; +} \ No newline at end of file diff --git a/frontend/src/app/core/model/evaluator/evaluator-format.model.ts b/frontend/src/app/core/model/evaluator/evaluator-format.model.ts new file mode 100644 index 000000000..f31cc8675 --- /dev/null +++ b/frontend/src/app/core/model/evaluator/evaluator-format.model.ts @@ -0,0 +1,12 @@ +import { EvaluatorEntityType } from "@app/core/common/enum/evaluator-entity-type"; +import { RankType } from "./rank-type"; +import { SelectionConfiguration } from "./evaluator-selection"; +import { ValueRangeConfiguration } from "./evaluator-value-range"; + +export interface EvaluatorFormat { + rankType: RankType[]; + selectionConfiguration: SelectionConfiguration; + valueRangeConfiguration: ValueRangeConfiguration; + evaluatorId: string; + entityTypes: EvaluatorEntityType[]; +} \ No newline at end of file diff --git a/frontend/src/app/core/model/evaluator/evaluator-number-type.model.ts b/frontend/src/app/core/model/evaluator/evaluator-number-type.model.ts new file mode 100644 index 000000000..fb0f1addd --- /dev/null +++ b/frontend/src/app/core/model/evaluator/evaluator-number-type.model.ts @@ -0,0 +1,4 @@ +export enum NumberType { + Decimal = 0, + Integer = 1 +} \ No newline at end of file diff --git a/frontend/src/app/core/model/evaluator/evaluator-plan-model.model.ts b/frontend/src/app/core/model/evaluator/evaluator-plan-model.model.ts new file mode 100644 index 000000000..986c377db --- /dev/null +++ b/frontend/src/app/core/model/evaluator/evaluator-plan-model.model.ts @@ -0,0 +1,5 @@ +export class RankModel { + rank: number; + details: string; + messages: { [key: string]: string }; +} \ No newline at end of file diff --git a/frontend/src/app/core/model/evaluator/evaluator-selection.ts b/frontend/src/app/core/model/evaluator/evaluator-selection.ts new file mode 100644 index 000000000..367c82c86 --- /dev/null +++ b/frontend/src/app/core/model/evaluator/evaluator-selection.ts @@ -0,0 +1,9 @@ +import { ValueSet } from "./evaluator-value-set"; + +export class SelectionConfiguration { + valueSetList: ValueSet[]; + + constructor(valueSetList: ValueSet[]) { + this.valueSetList = valueSetList; + } +} \ No newline at end of file diff --git a/frontend/src/app/core/model/evaluator/evaluator-success-status.model.ts b/frontend/src/app/core/model/evaluator/evaluator-success-status.model.ts new file mode 100644 index 000000000..2405ff26d --- /dev/null +++ b/frontend/src/app/core/model/evaluator/evaluator-success-status.model.ts @@ -0,0 +1,4 @@ +export enum SuccessStatus { + Fail = 0, + Pass = 1 +} \ No newline at end of file diff --git a/frontend/src/app/core/model/evaluator/evaluator-value-range.ts b/frontend/src/app/core/model/evaluator/evaluator-value-range.ts new file mode 100644 index 000000000..d1b8ba304 --- /dev/null +++ b/frontend/src/app/core/model/evaluator/evaluator-value-range.ts @@ -0,0 +1,15 @@ +import { NumberType } from "./evaluator-number-type.model"; + +export class ValueRangeConfiguration { + numberType: NumberType; + min: number; + max: number; + minPassValue: number; + + constructor(numberType: NumberType, min: number, max: number, minPassValue: number) { + this.numberType = numberType; + this.min = min; + this.max = max; + this.minPassValue = minPassValue; + } +} \ No newline at end of file diff --git a/frontend/src/app/core/model/evaluator/evaluator-value-set.ts b/frontend/src/app/core/model/evaluator/evaluator-value-set.ts new file mode 100644 index 000000000..390e9fd6d --- /dev/null +++ b/frontend/src/app/core/model/evaluator/evaluator-value-set.ts @@ -0,0 +1,11 @@ +import { SuccessStatus } from "./evaluator-success-status.model"; + +export class ValueSet { + key: number; + successStatus: SuccessStatus; + + constructor(key: number, successStatus: SuccessStatus) { + this.key = key; + this.successStatus = successStatus; + } +} \ No newline at end of file diff --git a/frontend/src/app/core/model/evaluator/rank-config.ts b/frontend/src/app/core/model/evaluator/rank-config.ts new file mode 100644 index 000000000..d79792116 --- /dev/null +++ b/frontend/src/app/core/model/evaluator/rank-config.ts @@ -0,0 +1,9 @@ +import { RankType } from "./rank-type"; +import { ValueRangeConfiguration } from "./evaluator-value-range"; +import { SelectionConfiguration } from "./evaluator-selection"; + +export class RankConfig{ + rankType: RankType; + valueRangeConfiguration?: ValueRangeConfiguration; + selectionConfiguration?: SelectionConfiguration; +} \ No newline at end of file diff --git a/frontend/src/app/core/model/evaluator/rank-type.ts b/frontend/src/app/core/model/evaluator/rank-type.ts new file mode 100644 index 000000000..daef9a77f --- /dev/null +++ b/frontend/src/app/core/model/evaluator/rank-type.ts @@ -0,0 +1,4 @@ +export enum RankType { + ValueRange = 0, + Selection = 1 +} \ No newline at end of file diff --git a/frontend/src/app/core/services/evaluator/evaluator.http.service.ts b/frontend/src/app/core/services/evaluator/evaluator.http.service.ts new file mode 100644 index 000000000..30a305180 --- /dev/null +++ b/frontend/src/app/core/services/evaluator/evaluator.http.service.ts @@ -0,0 +1,44 @@ +import { HttpHeaders } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { BaseService } from '@common/base/base.service'; +import { Guid } from '@common/types/guid'; +import { Observable, throwError } from 'rxjs'; +import { catchError } from 'rxjs/operators'; +import { ConfigurationService } from '../configuration/configuration.service'; +import { BaseHttpV2Service } from '../http/base-http-v2.service'; +import { EvaluatorFormat } from '@app/core/model/evaluator/evaluator-format.model'; +import { RankModel } from '@app/core/model/evaluator/evaluator-plan-model.model'; + + +@Injectable() +export class EvaluatorHttpService extends BaseService { + + private headers = new HttpHeaders(); + + constructor( + private http: BaseHttpV2Service, + private configurationService: ConfigurationService + ) { super(); } + + private get apiBase(): string { return `${this.configurationService.server}evaluator`; } + + getAvailableConfigurations(): Observable { + const url = `${this.apiBase}/available`; + return this.http.get(url).pipe(catchError((error: any) => throwError(error))); + } + + rankPlan(id: Guid, evaluatorId: string, format: string): Observable { + const url = `${this.apiBase}/rank-plan`; + return this.http.post(url, {id: id, evaluatorId: evaluatorId, format: format}, {responseType: 'json', observe: 'response'}).pipe(catchError((error: any) => throwError(error))); + } + + rankDescription(id: Guid, evaluatorId: string, format: string): Observable { + const url = `${this.apiBase}/rank-description`; + return this.http.post(url, {id: id, evaluatorId: evaluatorId, format: format}, {responseType: 'json', observe: 'response'}).pipe(catchError((error: any) => throwError(error))); + } + + getLogo(evaluatorId: string): Observable{ + const url = `${this.apiBase}/${evaluatorId}/logo`; + return this.http.get(url).pipe(catchError((error: any) => throwError(error))); + } +} \ No newline at end of file diff --git a/frontend/src/app/core/services/evaluator/evaluator.service.ts b/frontend/src/app/core/services/evaluator/evaluator.service.ts new file mode 100644 index 000000000..a276331ff --- /dev/null +++ b/frontend/src/app/core/services/evaluator/evaluator.service.ts @@ -0,0 +1,136 @@ +import { Component, EventEmitter, Input, OnInit, Output, Injectable } from '@angular/core'; +import { BaseService } from '@common/base/base.service'; +import { catchError, takeUntil } from 'rxjs/operators'; +import { EvaluatorHttpService } from './evaluator.http.service'; +import { HttpErrorHandlingService } from '@common/modules/errors/error-handling/http-error-handling.service'; +import { AuthService } from '../auth/auth.service'; +import { EvaluatorEntityType } from '@app/core/common/enum/evaluator-entity-type'; +import { EvaluatorConfiguration } from '@app/core/model/evaluator/evaluator-configuration'; +import { TranslateService } from '@ngx-translate/core'; +import { Guid } from '@common/types/guid'; +import { + SnackBarNotificationLevel, + UiNotificationService +} from '@app/core/services/notification/ui-notification-service'; +import { Observable, throwError } from 'rxjs'; +import { tap, share } from 'rxjs/operators'; +import { RankModel } from '@app/core/model/evaluator/evaluator-plan-model.model'; + + +@Injectable({ + providedIn: 'root' +}) +export class EvaluatorService extends BaseService { + + constructor( + private evaluatorHttpService: EvaluatorHttpService, + private authentication: AuthService, + private httpErrorHandlingService: HttpErrorHandlingService, + private language: TranslateService, + private uiNotificationService: UiNotificationService, + ) { super(); } + + private _initialized: boolean = false; + private _loading: boolean = false; + + private _availableEvaluators: EvaluatorConfiguration[] = []; + + get availableEvaluators(): EvaluatorConfiguration[] { + if (!this.authentication.currentAccountIsAuthenticated()) { + return []; + } + if (!this._initialized && !this._loading) this.init(); // if not initialized and loading calls init to initialize the evaluators. + return this._availableEvaluators; + } + + public availableEvaluatorsFor(entityType: EvaluatorEntityType) { + // Filter evaluators by entity type + // The fetch logo config should be here. + if (this.availableEvaluators) { + const filteredEvaluators = this.availableEvaluators.filter(x => { + return x.evaluatorEntityTypes && x.evaluatorEntityTypes.includes(entityType); + }); + + return filteredEvaluators; + } + + return []; + } + + init() { + this._loading = true; + this.evaluatorHttpService.getAvailableConfigurations() + .pipe(takeUntil(this._destroyed), catchError((error) => { + this._loading = false; + this._initialized = true; + this.httpErrorHandlingService.handleBackedRequestError(error); + return []; + })) + .subscribe(items => { + this._availableEvaluators = items; + this._loading = false; + this._initialized = true; + }); + } + + rankPlan(id: Guid, evaluatorId: string, format: string, isPublic: boolean = false): Observable { + this._loading = true; + + return this.evaluatorHttpService.rankPlan(id, evaluatorId, format).pipe( + tap({ + next: (doi) => { + this.onCallbackSuccess(); + }, + error: (error) => { + this.onCallbackError(error); + // Ensure loading state is turned off in case of error + this._loading = false; + }, + complete: () => { + this._loading = false; + } + }), + catchError((error) => { + // Ensure loading state is turned off in case of error + this._loading = false; + return throwError(error); + }), + share() + ); + } + + rankDescription(id: Guid, evaluatorId: string, format: string, isPublic: boolean = false): Observable { + this._loading = true; + return this.evaluatorHttpService.rankDescription(id, evaluatorId, format) + .pipe( + takeUntil(this._destroyed), + tap(response => { + this._loading = false; + this.onCallbackSuccess(); + }), + catchError(error => { + this._loading = false; + this.onCallbackError(error); + return throwError(error); + }) + ); + } + + getLogo(evaluatorId: string): Observable { + return this.evaluatorHttpService.getLogo(evaluatorId).pipe( + catchError((error) => { + this.httpErrorHandlingService.handleBackedRequestError(error); + return throwError(error); + }) + ); + } + + onCallbackSuccess(): void { + this.uiNotificationService.snackBarNotification(this.language.instant('PLAN-EDITOR.SNACK-BAR.SUCCESSFUL-EVALUATION'), SnackBarNotificationLevel.Success); + } + + onCallbackError(error) { + this.uiNotificationService.snackBarNotification(error.error.message ? error.error.message : this.language.instant('PLAN-EDITOR.SNACK-BAR.UNSUCCESSFUL-EVALUATION'), SnackBarNotificationLevel.Error); + } + +} \ No newline at end of file diff --git a/frontend/src/app/ui/description/description.module.ts b/frontend/src/app/ui/description/description.module.ts index c02df870b..9e6268ccb 100644 --- a/frontend/src/app/ui/description/description.module.ts +++ b/frontend/src/app/ui/description/description.module.ts @@ -3,6 +3,7 @@ import { FormattingModule } from '@app/core/formatting.module'; import { DescriptionRoutingModule, PublicDescriptionRoutingModule } from '@app/ui/description/description.routing'; import { CommonFormsModule } from '@common/forms/common-forms.module'; import { CommonUiModule } from '@common/ui/common-ui.module'; +import { EvaluateDescriptionDialogModule } from './evaluate-description-dialog/evaluate-description-dialog.module'; @NgModule({ imports: [ @@ -24,6 +25,7 @@ export class DescriptionModule { } CommonFormsModule, FormattingModule, PublicDescriptionRoutingModule, + EvaluateDescriptionDialogModule, ], declarations: [ ], diff --git a/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.component.html b/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.component.html new file mode 100644 index 000000000..428c53273 --- /dev/null +++ b/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.component.html @@ -0,0 +1,35 @@ +

{{ 'DESCRIPTION-EVALUATE-DIALOG.HEADER' | translate }}

+
+ + + {{'DESCRIPTION-EVALUATE-DIALOG.DETAILS-SUB-HEADER' | translate}} + + +
+ + {{'DESCRIPTION-EVALUATE-DIALOG.RANK-BODY' | translate}} + + + + {{'DESCRIPTION-EVALUATE-DIALOG.DETAILS-BODY' | translate}} + + + +
+ + {{'DESCRIPTION-EVALUATE-DIALOG.MESSAGES-BODY' | translate}} + + + +
    +
  • + {{ entry.key }}: {{ entry.value }} +
  • +
+
+
+
+
+
+
+
diff --git a/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.component.scss b/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.component.scss new file mode 100644 index 000000000..b44d417e6 --- /dev/null +++ b/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.component.scss @@ -0,0 +1,8 @@ +.dialog-content { + display: flex; + flex-direction: column; /* Stack form fields vertically */ + } + + mat-form-field { + margin-bottom: 16px; /* Space between fields */ + } \ No newline at end of file diff --git a/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.component.spec.ts b/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.component.spec.ts new file mode 100644 index 000000000..da4d38c7f --- /dev/null +++ b/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { EvaluateDescriptionDialogComponent } from './evaluate-description-dialog.component'; + +describe('EvaluateDescriptionDialogComponent', () => { + let component: EvaluateDescriptionDialogComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [EvaluateDescriptionDialogComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(EvaluateDescriptionDialogComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.component.ts b/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.component.ts new file mode 100644 index 000000000..7d91672b1 --- /dev/null +++ b/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.component.ts @@ -0,0 +1,16 @@ +import { Component, Inject } from '@angular/core'; +import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; +import { RankModel } from '@app/core/model/evaluator/evaluator-plan-model.model'; + +@Component({ + selector: 'app-evaluate-description-dialog', + templateUrl: './evaluate-description-dialog.component.html', + styleUrl: './evaluate-description-dialog.component.scss' +}) +export class EvaluateDescriptionDialogComponent { + constructor( + public dialogRef: MatDialogRef, + @Inject(MAT_DIALOG_DATA) public data: { rankData: RankModel } + ) { } +} + diff --git a/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.module.ts b/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.module.ts new file mode 100644 index 000000000..200182ef6 --- /dev/null +++ b/frontend/src/app/ui/description/evaluate-description-dialog/evaluate-description-dialog.module.ts @@ -0,0 +1,24 @@ +import { NgModule } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { MatCardModule } from '@angular/material/card'; +import { MatButtonModule } from '@angular/material/button'; +import { MatInputModule } from '@angular/material/input'; +import { MatFormFieldModule } from '@angular/material/form-field'; +import { NgIf } from '@angular/common'; +import { EvaluateDescriptionDialogComponent } from './evaluate-description-dialog.component'; +import { CommonUiModule } from '@common/ui/common-ui.module'; + +@NgModule({ + imports: [ + CommonModule, + MatCardModule, + MatButtonModule, + MatInputModule, + MatFormFieldModule, + NgIf, + CommonUiModule + ], + declarations: [EvaluateDescriptionDialogComponent], + exports: [EvaluateDescriptionDialogComponent] +}) +export class EvaluateDescriptionDialogModule { } \ No newline at end of file diff --git a/frontend/src/app/ui/description/overview/description-overview.component.html b/frontend/src/app/ui/description/overview/description-overview.component.html index 931ba9d43..9771fe99d 100644 --- a/frontend/src/app/ui/description/overview/description-overview.component.html +++ b/frontend/src/app/ui/description/overview/description-overview.component.html @@ -168,6 +168,28 @@ + +
+
+ +
+
+

{{ 'DESCRIPTION-OVERVIEW.ACTIONS.EVALUATE' | translate }}

+
+
+
+ + + + + + +
+

{{ 'PLAN-OVERVIEW.ACTIONS.EVALUATE' | translate }}

+
+ + + + + + +