Skip to content

Commit

Permalink
#436844 - Create Where clause for '@id' properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ppel-gmv committed Jul 26, 2024
1 parent 0c40709 commit 35d9f8b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,23 @@ private WhereClause generateVocabularyWhereClause(Criterion criterion) {

switch (propertiesList.length) {
case 3 ->
generateNonObjectPropertySQL(sqlWhereBuilder, propertiesList, criterion.getOperandRight().toString());
case 4 ->
generateNonObjectPropertySQL(sqlWhereBuilder, propertiesList, criterion.getOperandRight().toString(), false);
case 4 -> {
if (propertiesList[3].equals("'@id'")) {
generateNonObjectPropertySQL(sqlWhereBuilder, propertiesList, criterion.getOperandRight().toString(), true);
} else {
generateObjectPropertySQL(sqlWhereBuilder, propertiesList, criterion.getOperandRight().toString());
}

}

default -> throw new InvalidRequestException("Invalid vocabulary argument in the operandLeft: %s"
.formatted(criterion.getOperandLeft().toString()));
}

return new WhereClause(sqlWhereBuilder.toString(), unmodifiableCollection(new ArrayList<>()));
}

private void generateNonObjectPropertySQL(StringBuilder sqlWhereBuilder, String[] propertiesList, String operandRight) {
sqlWhereBuilder.append("(properties::jsonb -> ")
.append(propertiesList[0])
.append(" -> ")
.append(propertiesList[1])
.append(")::jsonb @> '[{")
.append(propertiesList[2].replaceAll("'", "\""))
.append(": [{\"@value\": \"")
.append(operandRight)
.append("\"}]}]'::jsonb");
}

private void generateObjectPropertySQL(StringBuilder sqlWhereBuilder, String[] propertiesList, String operandRight) {
sqlWhereBuilder.append("EXISTS (SELECT 1 FROM jsonb_array_elements((properties::jsonb -> ")
.append(propertiesList[0])
Expand All @@ -143,6 +138,18 @@ private void generateObjectPropertySQL(StringBuilder sqlWhereBuilder, String[] p
.append("\"}]}]')");
}

private void generateNonObjectPropertySQL(StringBuilder sqlWhereBuilder, String[] propertiesList, String operandRight, boolean isIdProperty) {
sqlWhereBuilder.append("(properties::jsonb -> ")
.append(propertiesList[0])
.append(" -> ")
.append(propertiesList[1])
.append(")::jsonb @> '[{")
.append(propertiesList[2].replaceAll("'", "\""))
.append(isIdProperty ? ": [{\"@id\": \"" : ": [{\"@value\": \"")
.append(operandRight)
.append("\"}]}]'::jsonb");
}

private String[] splitByDotOutsideQuotes(String input) {
List<String> parts = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class VocabularySharedRetrievalExtension implements ServiceExtension {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);



@Override
public void initialize(ServiceExtensionContext context) {

Expand All @@ -49,18 +48,23 @@ public void initialize(ServiceExtensionContext context) {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ParticipantRegistrationService participantRegistrationService = new ParticipantRegistrationService(context.getMonitor(), objectMapper);
var periodSeconds = context.getSetting(EXECUTION_PLAN_PERIOD_SECONDS, DEFAULT_EXECUTION_PERIOD_SECONDS);
var monitor = context.getMonitor();
var vocabularySharedRetrievalService = new VocabularySharedRetrievalService(vocabularySharedService, monitor, participantRegistrationService);
VocabularySharedRetrievalService vocabularySharedRetrievalService = new VocabularySharedRetrievalService(vocabularySharedService, context.getMonitor(), participantRegistrationService);

retrieveVocabularies(vocabularySharedRetrievalService, context);
// Schedule periodic updates
scheduler.scheduleAtFixedRate(() -> {
try {
Result<TokenRepresentation> tokenRepresentationResult = identityService.obtainClientCredentials(
TokenParameters.Builder.newInstance().build());
vocabularySharedRetrievalService.retrieveVocabularies(context.getConfig(), tokenRepresentationResult, context.getParticipantId());
} catch (Exception e) {
monitor.severe("Error getting vocabularies from connectors", e);
}
retrieveVocabularies(vocabularySharedRetrievalService, context);
}, periodSeconds, periodSeconds, TimeUnit.SECONDS);
}

private void retrieveVocabularies(VocabularySharedRetrievalService vocabularySharedRetrievalService, ServiceExtensionContext context) {
try {
Result<TokenRepresentation> tokenRepresentationResult = identityService.obtainClientCredentials(
TokenParameters.Builder.newInstance().build());
vocabularySharedRetrievalService.retrieveVocabularies(context.getConfig(), tokenRepresentationResult, context.getParticipantId());
} catch (Exception e) {
context.getMonitor().severe("Error getting vocabularies from connectors", e);
}

}
}

0 comments on commit 35d9f8b

Please sign in to comment.