Skip to content

Commit

Permalink
Merge branch 'feature/431555-default-category' into 'develop'
Browse files Browse the repository at this point in the history
Feature/431555 default category

See merge request upm-inesdata/inesdata-connector!14
  • Loading branch information
ralconada-gmv committed May 24, 2024
2 parents 538de9c + 1680760 commit 90a887b
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import org.upm.inesdata.vocabulary.transformer.JsonObjectToVocabularyTransformer;
import org.upm.inesdata.vocabulary.validator.VocabularyValidator;

import java.util.Map;

import static org.eclipse.edc.spi.constants.CoreConstants.JSON_LD;
import static org.upm.inesdata.spi.vocabulary.domain.Vocabulary.EDC_VOCABULARY_TYPE;

import java.util.Map;

/**
* Extension that provides an API for managing vocabularies
*/
Expand Down Expand Up @@ -74,14 +74,6 @@ public VocabularyService vocabularyService() {
return new VocabularyServiceImpl(vocabularyIndex, transactionContext);
}

/**
* Provides a default in memory vocabularyIndex
*/
@Provider(isDefault = true)
public VocabularyIndex defaultVocabularyIndex() {
return getVocabularyIndex();
}

/**
* Initializes the service
*/
Expand All @@ -107,14 +99,4 @@ public void initialize(ServiceExtensionContext context) {
healthCheckService.addLivenessProvider(() -> successResult);
}
}

/**
* Creates a InMemoryVocabularyIndex if not exists
*/
private InMemoryVocabularyIndex getVocabularyIndex() {
if (defaultVocabularyIndex == null) {
defaultVocabularyIndex = new InMemoryVocabularyIndex();
}
return defaultVocabularyIndex;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import jakarta.json.JsonObject;
import org.eclipse.edc.api.model.ApiCoreSchema;
import org.eclipse.edc.connector.controlplane.contract.spi.types.offer.ContractOffer;
import org.upm.inesdata.spi.vocabulary.domain.Vocabulary;

import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
Expand Down Expand Up @@ -125,7 +126,8 @@ record VocabularyOutputSchema(
{
"@id": "vocabularyId",
"name": "vocabulary name",
"jsonSchema": "{ \\"title\\": \\"vocabulary\\", \\"type\\": \\"object\\", \\"properties\\": { \\"name\\": { \\"type\\": \\"string\\", \\"title\\": \\"Name\\" }, \\"dct:keyword\\": { \\"type\\": \\"array\\", \\"title\\": \\"Keywords\\", \\"items\\": { \\"type\\": \\"string\\" } } }, \\"required\\": [ \\"name\\" ], \\"@context\\": { \\"dct\\": \\"http:\\/\\/purl.org\\/dc\\/terms\\/\" } }"
"jsonSchema": "{ \\"title\\": \\"vocabulary\\", \\"type\\": \\"object\\", \\"properties\\": { \\"name\\": { \\"type\\": \\"string\\", \\"title\\": \\"Name\\" }, \\"dct:keyword\\": { \\"type\\": \\"array\\", \\"title\\": \\"Keywords\\", \\"items\\": { \\"type\\": \\"string\\" } } }, \\"required\\": [ \\"name\\" ], \\"@context\\": { \\"dct\\": \\"http:\\/\\/purl.org\\/dc\\/terms\\/\" } }",
"category": "dataset"
}
""";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.eclipse.edc.api.model.IdResponse;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.result.Result;
Expand All @@ -28,8 +28,6 @@
import static org.eclipse.edc.web.spi.exception.ServiceResultHandler.exceptionMapper;
import static org.upm.inesdata.spi.vocabulary.domain.Vocabulary.EDC_VOCABULARY_TYPE;

import org.eclipse.edc.api.model.IdResponse;

/**
* Implementation of the controller for {@link Vocabulary} managing.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.eclipse.edc.spi.result.ServiceResult;
import org.eclipse.edc.transaction.spi.TransactionContext;

import org.upm.inesdata.spi.vocabulary.VocabularyIndex;
import org.upm.inesdata.spi.vocabulary.VocabularyService;
import org.upm.inesdata.spi.vocabulary.domain.Vocabulary;
Expand Down Expand Up @@ -43,7 +42,9 @@ public ServiceResult<List<Vocabulary>> search() {
@Override
public ServiceResult<Vocabulary> create(Vocabulary vocabulary) {
return transactionContext.execute(() -> {
// Create new vocabulary
var createResult = index.create(vocabulary);

if (createResult.succeeded()) {
return ServiceResult.success(vocabulary);
}
Expand All @@ -62,6 +63,7 @@ public ServiceResult<Vocabulary> delete(String vocabularyId) {
@Override
public ServiceResult<Vocabulary> update(Vocabulary vocabulary) {
return transactionContext.execute(() -> {
// Update vocabulary
var updatedVocabulary = index.updateVocabulary(vocabulary);
return ServiceResult.from(updatedVocabulary);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.upm.inesdata.vocabulary.storage;

import org.eclipse.edc.spi.result.StoreResult;
import org.upm.inesdata.spi.vocabulary.domain.Vocabulary;
import org.upm.inesdata.spi.vocabulary.VocabularyIndex;
import org.upm.inesdata.spi.vocabulary.domain.Vocabulary;

import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -103,5 +103,4 @@ public StoreResult<Vocabulary> updateVocabulary(Vocabulary vocabulary) {
private Vocabulary delete(String vocabularyId) {
return cache.remove(vocabularyId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;

import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
Expand All @@ -12,11 +11,12 @@

import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;

import static org.upm.inesdata.spi.vocabulary.domain.Vocabulary.EDC_VOCABULARY_TYPE;
import static org.upm.inesdata.spi.vocabulary.domain.Vocabulary.PROPERTY_CATEGORY;
import static org.upm.inesdata.spi.vocabulary.domain.Vocabulary.PROPERTY_JSON_SCHEMA;
import static org.upm.inesdata.spi.vocabulary.domain.Vocabulary.PROPERTY_NAME;


/**
* Creates a JsonObject from a {@link Vocabulary}
*/
Expand All @@ -39,7 +39,8 @@ public JsonObjectFromVocabularyTransformer(JsonBuilderFactory jsonFactory, Objec
.add(ID, vocabulary.getId())
.add(TYPE, EDC_VOCABULARY_TYPE)
.add(PROPERTY_NAME, vocabulary.getName())
.add(PROPERTY_JSON_SCHEMA, vocabulary.getJsonSchema());
.add(PROPERTY_JSON_SCHEMA, vocabulary.getJsonSchema())
.add(PROPERTY_CATEGORY, vocabulary.getCategory());

return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jetbrains.annotations.Nullable;
import org.upm.inesdata.spi.vocabulary.domain.Vocabulary;

import static org.upm.inesdata.spi.vocabulary.domain.Vocabulary.PROPERTY_CATEGORY;
import static org.upm.inesdata.spi.vocabulary.domain.Vocabulary.PROPERTY_JSON_SCHEMA;
import static org.upm.inesdata.spi.vocabulary.domain.Vocabulary.PROPERTY_NAME;

Expand All @@ -30,6 +31,7 @@ public JsonObjectToVocabularyTransformer() {
visitProperties(jsonObject, key -> switch (key) {
case PROPERTY_NAME -> value -> builder.name(transformString(value, context));
case PROPERTY_JSON_SCHEMA -> value -> builder.jsonSchema(transformString(value, context));
case PROPERTY_CATEGORY -> value -> builder.category(transformString(value, context));
default -> doNothing();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import jakarta.json.stream.JsonParsingException;
import java.io.StringReader;
import org.eclipse.edc.validator.jsonobject.JsonLdPath;
import org.eclipse.edc.validator.spi.ValidationResult;
import org.eclipse.edc.validator.spi.Validator;

import java.io.StringReader;
import java.util.Optional;

import static java.lang.String.format;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.eclipse.edc.validator.jsonobject.validators.OptionalIdNotBlank;
import org.eclipse.edc.validator.spi.Validator;

import static org.upm.inesdata.spi.vocabulary.domain.Vocabulary.PROPERTY_CATEGORY;
import static org.upm.inesdata.spi.vocabulary.domain.Vocabulary.PROPERTY_JSON_SCHEMA;
import static org.upm.inesdata.spi.vocabulary.domain.Vocabulary.PROPERTY_NAME;

Expand All @@ -21,6 +22,7 @@ public static Validator<JsonObject> instance() {
return JsonObjectValidator.newValidator()
.verifyId(OptionalIdNotBlank::new)
.verify(PROPERTY_NAME, MandatoryObject::new)
.verify(PROPERTY_CATEGORY, MandatoryObject::new)
.verify(PROPERTY_JSON_SCHEMA, MandatoryJsonField::new)
.build();
}
Expand Down
1 change: 1 addition & 0 deletions extensions/vocabulary-index-sql/docs/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS edc_vocabulary
created_at BIGINT NOT NULL,
json_schema JSON DEFAULT '{}',
name VARCHAR NOT NULL,
category VARCHAR NOT NULL
PRIMARY KEY (id)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
import org.eclipse.edc.transaction.datasource.spi.DataSourceRegistry;
import org.eclipse.edc.transaction.spi.TransactionContext;
import org.jetbrains.annotations.Nullable;

import org.upm.inesdata.spi.vocabulary.VocabularyIndex;
import org.upm.inesdata.spi.vocabulary.domain.Vocabulary;
import org.upm.inesdata.vocabulary.sql.index.schema.VocabularyStatements;
import org.upm.inesdata.spi.vocabulary.VocabularyIndex;

import java.sql.Connection;
import java.sql.ResultSet;
Expand Down Expand Up @@ -74,14 +73,15 @@ public StoreResult<Void> create(Vocabulary vocabulary) {
return transactionContext.execute(() -> {
try (var connection = getConnection()) {
if (existsById(vocabularyId, connection)) {
var msg = format(VocabularyIndex.VOCABULARY_NOT_FOUND_TEMPLATE, vocabularyId);
var msg = format(VocabularyIndex.VOCABULARY_EXISTS_TEMPLATE, vocabularyId);
return StoreResult.alreadyExists(msg);
}

queryExecutor.execute(connection, vocabularyStatements.getInsertVocabularyTemplate(),
vocabularyId,
vocabulary.getCreatedAt(),
vocabulary.getName(),
vocabulary.getCategory(),
toJson(vocabulary.getJsonSchema())
);

Expand Down Expand Up @@ -121,6 +121,7 @@ public StoreResult<Vocabulary> updateVocabulary(Vocabulary vocabulary) {
queryExecutor.execute(connection, vocabularyStatements.getUpdateVocabularyTemplate(),
vocabulary.getName(),
toJson(vocabulary.getJsonSchema()),
vocabulary.getCategory(),
vocabularyId
);

Expand Down Expand Up @@ -150,6 +151,7 @@ private Vocabulary mapVocabulary(ResultSet resultSet) throws SQLException {
.id(resultSet.getString(vocabularyStatements.getVocabularyIdColumn()))
.createdAt(resultSet.getLong(vocabularyStatements.getCreatedAtColumn()))
.name(resultSet.getString(vocabularyStatements.getNameColumn()))
.category(resultSet.getString(vocabularyStatements.getCategoryColumn()))
.jsonSchema(resultSet.getString(vocabularyStatements.getJsonSchemaColumn()))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.upm.inesdata.vocabulary.sql.index.schema;

import org.upm.inesdata.vocabulary.sql.index.schema.postgres.VocabularyMapping;
import org.eclipse.edc.spi.query.QuerySpec;
import org.eclipse.edc.sql.translation.SqlOperatorTranslator;
import org.eclipse.edc.sql.translation.SqlQueryStatement;
import org.upm.inesdata.vocabulary.sql.index.schema.postgres.VocabularyMapping;

import static java.lang.String.format;

Expand All @@ -24,6 +24,7 @@ public String getInsertVocabularyTemplate() {
.column(getVocabularyIdColumn())
.column(getCreatedAtColumn())
.column(getNameColumn())
.column(getCategoryColumn())
.jsonColumn(getJsonSchemaColumn())
.insertInto(getVocabularyTable());
}
Expand All @@ -33,6 +34,7 @@ public String getUpdateVocabularyTemplate() {
return executeStatement()
.column(getNameColumn())
.jsonColumn(getJsonSchemaColumn())
.column(getCategoryColumn())
.update(getVocabularyTable(), getVocabularyIdColumn());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ default String getJsonSchemaColumn() {
return "json_schema";
}

default String getCategoryColumn() {
return "category";
}

default String getCreatedAtColumn() {
return "created_at";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public VocabularyMapping(VocabularyStatements statements) {
add("id", statements.getVocabularyIdColumn());
add("createdAt", statements.getCreatedAtColumn());
add("name", statements.getNameColumn());
add("category", statements.getCategoryColumn());
add("json_schema", new JsonFieldTranslator(statements.getJsonSchemaColumn()));
}

Expand Down
1 change: 1 addition & 0 deletions resources/sql/vocabulary-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS edc_vocabulary
created_at BIGINT NOT NULL,
json_schema JSON DEFAULT '{}',
name VARCHAR NOT NULL,
category VARCHAR NOT NULL
PRIMARY KEY (id)
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.upm.inesdata.spi.vocabulary;

import org.upm.inesdata.spi.vocabulary.domain.Vocabulary;
import org.eclipse.edc.runtime.metamodel.annotation.ExtensionPoint;
import org.eclipse.edc.spi.persistence.EdcPersistenceException;
import org.eclipse.edc.spi.result.StoreResult;
import org.upm.inesdata.spi.vocabulary.domain.Vocabulary;

import java.util.stream.Stream;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ public class Vocabulary extends Entity {
public static final String PROPERTY_ID = EDC_NAMESPACE + "id";
public static final String PROPERTY_NAME = EDC_NAMESPACE + "name";
public static final String PROPERTY_JSON_SCHEMA = EDC_NAMESPACE + "jsonSchema";
public static final String PROPERTY_CATEGORY = EDC_NAMESPACE + "category";
public static final String EDC_VOCABULARY_TYPE = EDC_NAMESPACE + "Vocabulary";


private String name;
private String jsonSchema;
private String category;

private Vocabulary() {
}
Expand All @@ -37,11 +39,17 @@ public String getJsonSchema() {
return jsonSchema;
}

public String getCategory() {
return category;
}


public Builder toBuilder() {
return Vocabulary.Builder.newInstance()
.id(id)
.name(name)
.jsonSchema(jsonSchema)
.category(category)
.createdAt(createdAt);
}

Expand Down Expand Up @@ -74,6 +82,11 @@ public Builder jsonSchema(String jsonSchema) {
return self();
}

public Builder category(String category) {
entity.category = category;
return self();
}

@Override
public Builder createdAt(long value) {
entity.createdAt = value;
Expand Down

0 comments on commit 90a887b

Please sign in to comment.