Skip to content

Commit

Permalink
MODFQMMGR-339: Support ECS columns in query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
bvsharp committed Jul 17, 2024
1 parent 5b8aa67 commit f9ccf11
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
6 changes: 6 additions & 0 deletions descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@
"version": "1.1"
}
],
"optional": [
{
"id": "consortia",
"version": "1.0"
}
],
"launchDescriptor": {
"dockerImage": "@artifactId@:@version@",
"dockerPull": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import lombok.extern.log4j.Log4j2;
import org.folio.fqm.client.SimpleHttpClient;
import org.folio.fqm.repository.EntityTypeRepository;
import org.folio.querytool.domain.dto.EntityType;
import org.folio.spring.FolioExecutionContext;
Expand All @@ -28,16 +33,19 @@ public class EntityTypeInitializationService {

private final ObjectMapper objectMapper;
private final ResourcePatternResolver resourceResolver;
private final SimpleHttpClient ecsClient;

@Autowired
public EntityTypeInitializationService(
EntityTypeRepository entityTypeRepository,
FolioExecutionContext folioExecutionContext,
ResourcePatternResolver resourceResolver
ResourcePatternResolver resourceResolver,
SimpleHttpClient ecsClient
) {
this.entityTypeRepository = entityTypeRepository;
this.folioExecutionContext = folioExecutionContext;
this.resourceResolver = resourceResolver;
this.ecsClient = ecsClient;

// this enables all JSON5 features, except for numeric ones (hex, starting/trailing
// decimal points, use of NaN, etc), as those are not relevant for our use
Expand All @@ -62,7 +70,16 @@ public EntityTypeInitializationService(
// called as part of tenant install/upgrade (see FqmTenantService)
public void initializeEntityTypes() throws IOException {
log.info("Initializing entity types");

String centralTenantId = "${central_tenant_id}";
try {
String rawJson = ecsClient.get("consortia-configuration", Map.of("limit", String.valueOf(100)));
DocumentContext parsedJson = JsonPath.parse(rawJson);
centralTenantId = parsedJson.read("centralTenantId");
log.info("ECS central tenant ID: {}", centralTenantId);
} catch (Exception e) {
log.info("ECS is not enabled for tenant {}", folioExecutionContext.getTenantId());
}
String finalCentralTenantId = centralTenantId;
List<EntityType> desiredEntityTypes = Stream
.concat(
Arrays.stream(resourceResolver.getResources("classpath:/entity-types/**/*.json")),
Expand All @@ -74,7 +91,8 @@ public void initializeEntityTypes() throws IOException {
return objectMapper.readValue(
resource
.getContentAsString(StandardCharsets.UTF_8)
.replace("${tenant_id}", folioExecutionContext.getTenantId()),
.replace("${tenant_id}", folioExecutionContext.getTenantId())
.replace("${central_tenant_id}", finalCentralTenantId),
EntityType.class
);
} catch (IOException e) {
Expand Down
30 changes: 25 additions & 5 deletions src/main/resources/entity-types/inventory/simple_instance.json5
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@
queryable: true,
visibleByDefault: true,
valueGetter: "CASE \
WHEN (SELECT central_tenant_id from ${tenant_id}_mod_consortia.consortia_configuration) = '${tenant_id}' \
WHEN '${central_tenant_id}' = '${tenant_id}' \
THEN 'Shared'\
ELSE \
CASE \
Expand All @@ -1056,7 +1056,17 @@
ELSE 'Local' \
END \
END",
ecsOnly: true
values: [
{
value: 'Shared',
label: 'Shared',
},
{
value: 'Local',
label: 'Local',
},
],
ecsOnly: true,
},
{
name: 'source_tenant_id',
Expand All @@ -1068,16 +1078,26 @@
queryable: true,
visibleByDefault: true,
valueGetter: "CASE \
WHEN (SELECT central_tenant_id from ${tenant_id}_mod_consortia.consortia_configuration) = '${tenant_id}' \
WHEN '${central_tenant_id}' = '${tenant_id}' \
THEN '${tenant_id}' \
ELSE \
CASE \
WHEN \"left\"(lower(:sourceAlias.jsonb ->> 'source'::text), 600) = 'consortium-folio' OR \"left\"(lower(:sourceAlias.jsonb ->> 'source'::text), 600) = 'consortium-marc' \
THEN (SELECT central_tenant_id FROM ${tenant_id}_mod_consortia.consortia_configuration) \
THEN '${central_tenant_id}' \
ELSE '${tenant_id}' \
END \
END",
ecsOnly: true
values: [
{
value: '${tenant_id}',
label: '${tenant_id}',
},
{
value: '${central_tenant_id}',
label: '${central_tenant_id}',
},
],
ecsOnly: true,
}
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import jakarta.annotation.PostConstruct;
import java.io.IOException;
import java.util.Map;
import javax.sql.DataSource;
import liquibase.integration.spring.SpringLiquibase;
import org.folio.fqm.IntegrationTestBase;
import org.folio.fqm.client.SimpleHttpClient;
import org.folio.fqm.repository.EntityTypeRepository;
import org.folio.fqm.service.EntityTypeInitializationService;
import org.folio.spring.FolioExecutionContext;
Expand All @@ -15,6 +17,9 @@
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.support.ResourcePatternResolver;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* This class is responsible for inserting test data into a PostgreSQL test container database.
*/
Expand Down Expand Up @@ -47,6 +52,8 @@ static class EntityTypeInitializer {

@PostConstruct
public void populateEntityTypes() throws IOException {
SimpleHttpClient ecsClient = mock(SimpleHttpClient.class);
when(ecsClient.get("consortia-configuration", Map.of("limit", String.valueOf(100)))).thenReturn("{\"centralTenantId\": \"tenant_01\"}");
new EntityTypeInitializationService(
entityTypeRepository,
new FolioExecutionContext() {
Expand All @@ -55,7 +62,8 @@ public String getTenantId() {
return IntegrationTestBase.TENANT_ID;
}
},
resourceResolver
resourceResolver,
ecsClient
)
.initializeEntityTypes();
}
Expand Down

0 comments on commit f9ccf11

Please sign in to comment.