Skip to content

Commit 9ed0f5c

Browse files
authored
Merge pull request #277 from Sreejit-K/reference_entity_add_vertex
Reference entity add vertex
2 parents cdf08c0 + ed33b27 commit 9ed0f5c

File tree

18 files changed

+288
-52
lines changed

18 files changed

+288
-52
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,4 @@ release: test
8080
docker push $$image:latest;\
8181
docker push $$image:$(RELEASE_VERSION);\
8282
done
83-
@cd tools/cli/ && npm publish
83+
@cd tools/cli/ && npm publish

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ services:
101101
- redis_host=redis
102102
- redis_port=6379
103103
- manager_type=${MANAGER_TYPE-DefinitionsManager}
104+
- expand_reference=${EXPAND_REFERENCE-false}
104105
- notification_async_enabled=${NOTIFICATION_ASYNC_ENABLED-false}
105106
- notification_enabled=${NOTIFICATION_ENABLED-false}
106107
- notification_url=${NOTIFICATION_URL-http://notification-ms:8765/notification-service/v1/notification}

java/apitest/src/test/java/e2e/registry/inviteFlow.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
},
1212
"instituteSchema": {
1313
"name": "Institute",
14-
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-07/schema\",\n \"type\": \"object\",\n \"properties\": {\n \"Institute\": {\n \"$ref\": \"#/definitions/Institute\"\n }\n },\n \"required\": [\n \"Institute\"\n ],\n \"title\":\"Institute\",\n \"definitions\": {\n \"Institute\": {\n \"$id\": \"#/properties/Institute\",\n \"type\": \"object\",\n \"title\": \"The Institute Schema\",\n \"required\": [\n \n ],\n \"properties\": {\n \"name\": {\n \"type\": \"string\"\n },\n \"email\": {\n \"type\": \"string\"\n },\n \"mobile\": {\n \"type\": \"string\"\n },\n \"address\": {\n \"type\": \"array\", \n \"items\": {\n \"type\": \"object\",\n \"title\": \"Address\",\n \"required\": [],\n \"properties\": {\n \"addressLine1\": {\n \"type\": \"string\",\n \"title\": \"Address Line1\"\n },\n \"postalCode\": {\n \"type\": \"string\",\n \"title\": \"Postal Code\"\n },\n \"phoneNo\": {\n \"type\": \"array\",\n \"items\": {\n \"type\": \"string\"\n }\n }\n }\n }\n }\n }\n }\n },\n \"_osConfig\": {\n \"osComment\": [\"This section contains the OpenSABER specific configuration information\",\n \"privateFields: Optional; list of field names to be encrypted and stored in database\",\n \"signedFields: Optional; list of field names that must be pre-signed\",\n \"indexFields: Optional; list of field names used for creating index. Enclose within braces to indicate it is a composite index. In this definition, (serialNum, studentCode) is a composite index and studentName is a single column index.\",\n \"uniqueIndexFields: Optional; list of field names used for creating unique index. Field names must be different from index field name\",\n \"systemFields: Optional; list of fields names used for system standard information like created, updated timestamps and userid\"],\n \n \"privateFields\": [\n \n ],\n \"signedFields\": [],\n \"indexFields\": [],\n \"uniqueIndexFields\": [],\n \"systemFields\": [\n \"_osCreatedAt\",\n \"_osUpdatedAt\",\n \"_osCreatedBy\",\n \"_osUpdatedBy\",\n \"_osAttestedData\",\n \"_osClaimId\",\n \"_osState\"\n ],\n \"inviteRoles\":[\"Board\"],\n \"enableLogin\": true,\n \"ownershipAttributes\": [\n {\n \"email\": \"/email\",\n \"mobile\": \"/mobile\",\n \"userId\": \"/mobile\"\n }\n ]\n }\n }",
14+
"schema": "{\n \"_osConfig\": {\n \"enableLogin\": true,\n \"indexFields\": [\n ],\n \"inviteRoles\": [\n \"Board\"\n ],\n \"osComment\": [\n \"This section contains the OpenSABER specific configuration information\",\n \"privateFields: Optional; list of field names to be encrypted and stored in database\",\n \"signedFields: Optional; list of field names that must be pre-signed\",\n \"indexFields: Optional; list of field names used for creating index. Enclose within braces to indicate it is a composite index. In this definition, (serialNum, studentCode) is a composite index and studentName is a single column index.\",\n \"uniqueIndexFields: Optional; list of field names used for creating unique index. Field names must be different from index field name\",\n \"systemFields: Optional; list of fields names used for system standard information like created, updated timestamps and userid\"\n ],\n \"ownershipAttributes\": [\n {\n \"email\": \"/email\",\n \"mobile\": \"/mobile\",\n \"userId\": \"/mobile\"\n }\n ],\n \"privateFields\": [\n ],\n \"signedFields\": [\n ],\n \"systemFields\": [\n \"_osCreatedAt\",\n \"_osUpdatedAt\",\n \"_osCreatedBy\",\n \"_osUpdatedBy\",\n \"_osAttestedData\",\n \"_osClaimId\",\n \"_osState\"\n ],\n \"uniqueIndexFields\": [\n ]\n },\n \"$schema\": \"http://json-schema.org/draft-07/schema\",\n \"definitions\": {\n \"Institute\": {\n \"$id\": \"#/properties/Institute\",\n \"properties\": {\n \"address\": {\n \"items\": {\n \"properties\": {\n \"addressLine1\": {\n \"title\": \"Address Line1\",\n \"type\": \"string\"\n },\n \"phoneNo\": {\n \"items\": {\n \"type\": \"string\"\n },\n \"type\": \"array\"\n },\n \"postalCode\": {\n \"title\": \"Postal Code\",\n \"type\": \"string\"\n }\n },\n \"required\": [\n ],\n \"title\": \"Address\",\n \"type\": \"object\"\n },\n \"type\": \"array\"\n },\n \"email\": {\n \"type\": \"string\"\n },\n \"mobile\": {\n \"type\": \"string\"\n },\n \"name\": {\n \"type\": \"string\"\n },\n \"references\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\n ],\n \"title\": \"The Institute Schema\",\n \"type\": \"object\"\n }\n },\n \"properties\": {\n \"Institute\": {\n \"$ref\": \"#/definitions/Institute\"\n }\n },\n \"required\": [\n \"Institute\"\n ],\n \"title\": \"Institute\",\n \"type\": \"object\"\n}",
1515
"status": "PUBLISHED"
1616
},
1717
"instituteRequest": {
1818
"name": "test_institute",
1919
"email": "[email protected]",
2020
"mobile": "456",
21+
"references": "did:Board:123",
2122
"address": [{
2223
"addressLine": "test_address",
2324
"postalCode": "111",

java/apitest/src/test/java/e2e/registry/registry.feature

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ Feature: Registry api tests
295295
And request sample.boardInviteRequest
296296
When method post
297297
Then status 200
298+
* def boardOsid = response.result.Board.osid
298299
# get board token
299300
* url authUrl
300301
* path 'auth/realms/sunbird-rc/protocol/openid-connect/token'
@@ -320,7 +321,9 @@ Feature: Registry api tests
320321
# invite institute with token
321322
Given url registryUrl
322323
And path 'api/v1/Institute/invite'
323-
And request sample.instituteRequest
324+
* def requestBody = sample.instituteRequest
325+
* requestBody.references = 'did:Board:' + boardOsid
326+
And request requestBody
324327
And header Authorization = board_token
325328
When method post
326329
Then status 200

java/middleware-commons/src/main/java/dev/sunbirdrc/registry/middleware/util/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public class Constants {
127127
public static final String MOBILE = "mobile";
128128
public static final String SVG_MEDIA_TYPE = "image/svg+xml";
129129
public static final String CONNECTION_FAILURE = "CONNECTION_FAILURE";
130+
public static final String DID_TYPE = "did";
130131

131132
public enum GraphDatabaseProvider {
132133
NEO4J("NEO4J"), ORIENTDB("ORIENTDB"), SQLG("SQLG"), CASSANDRA("CASSANDRA"), TINKERGRAPH("TINKERGRAPH");

java/registry/src/main/java/dev/sunbirdrc/registry/dao/RegistryDaoImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
public class RegistryDaoImpl implements IRegistryDao {
1919
public String uuidPropertyName;
20+
private final boolean expandReferenceObj;
2021
private IDefinitionsManager definitionsManager;
2122
private DatabaseProvider databaseProvider;
2223
private List<String> privatePropertyList;
@@ -33,10 +34,11 @@ public void setPrivatePropertyList(List<String> privatePropertyList) {
3334
this.privatePropertyList = privatePropertyList;
3435
}
3536

36-
public RegistryDaoImpl(DatabaseProvider dbProvider, IDefinitionsManager defnManager, String uuidPropName) {
37+
public RegistryDaoImpl(DatabaseProvider dbProvider, IDefinitionsManager defnManager, String uuidPropName, boolean expandReferenceObj) {
3738
databaseProvider = dbProvider;
3839
definitionsManager = defnManager;
3940
uuidPropertyName = uuidPropName;
41+
this.expandReferenceObj = expandReferenceObj;
4042
}
4143

4244
public DatabaseProvider getDatabaseProvider() {
@@ -64,7 +66,7 @@ public String addEntity(Graph graph, JsonNode rootNode) {
6466
*/
6567
public JsonNode getEntity(Graph graph, String entityType, String uuid, ReadConfigurator readConfigurator) throws Exception {
6668

67-
VertexReader vr = new VertexReader(getDatabaseProvider(), graph, readConfigurator, uuidPropertyName, definitionsManager);
69+
VertexReader vr = new VertexReader(getDatabaseProvider(), graph, readConfigurator, uuidPropertyName, definitionsManager, expandReferenceObj);
6870
JsonNode result = vr.read(entityType, uuid);
6971

7072
return result;
@@ -73,7 +75,7 @@ public JsonNode getEntity(Graph graph, String entityType, String uuid, ReadConfi
7375

7476
public JsonNode getEntity(Graph graph, Vertex vertex, ReadConfigurator readConfigurator, boolean expandInternal) throws Exception {
7577

76-
VertexReader vr = new VertexReader(getDatabaseProvider(), graph, readConfigurator, uuidPropertyName, definitionsManager);
78+
VertexReader vr = new VertexReader(getDatabaseProvider(), graph, readConfigurator, uuidPropertyName, definitionsManager, expandReferenceObj);
7779
ObjectNode constructObject = vr.constructObject(vertex);
7880
if (expandInternal) {
7981
String entityType = (String) ValueType.getValue(constructObject.get(TypePropertyHelper.getTypeName()));

java/registry/src/main/java/dev/sunbirdrc/registry/dao/VertexReader.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@
88
import dev.sunbirdrc.registry.middleware.util.Constants;
99
import dev.sunbirdrc.registry.middleware.util.JSONUtil;
1010
import dev.sunbirdrc.registry.sink.DatabaseProvider;
11-
import dev.sunbirdrc.registry.util.ArrayHelper;
12-
import dev.sunbirdrc.registry.util.Definition;
13-
import dev.sunbirdrc.registry.util.IDefinitionsManager;
14-
import dev.sunbirdrc.registry.util.ReadConfigurator;
15-
import dev.sunbirdrc.registry.util.RefLabelHelper;
16-
import dev.sunbirdrc.registry.util.TypePropertyHelper;
11+
import dev.sunbirdrc.registry.util.*;
1712
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
1813
import org.apache.tinkerpop.gremlin.structure.Direction;
1914
import org.apache.tinkerpop.gremlin.structure.Graph;
@@ -32,6 +27,8 @@
3227
import java.util.NoSuchElementException;
3328
import java.util.Set;
3429

30+
import static dev.sunbirdrc.registry.middleware.util.Constants.DID_TYPE;
31+
3532
/**
3633
* Given a vertex from the graph, constructs a json out it
3734
*/
@@ -46,15 +43,18 @@ public class VertexReader {
4643
private Vertex rootVertex;
4744
private LinkedHashMap<String, Vertex> uuidVertexMap = new LinkedHashMap<>();
4845

46+
private boolean expandReferenceObj;
4947
private Logger logger = LoggerFactory.getLogger(VertexReader.class);
5048

49+
5150
public VertexReader(DatabaseProvider databaseProvider, Graph graph, ReadConfigurator configurator, String uuidPropertyName,
52-
IDefinitionsManager definitionsManager) {
51+
IDefinitionsManager definitionsManager, boolean expandReferenceObj) {
5352
this.databaseProvider = databaseProvider;
5453
this.graph = graph;
5554
this.configurator = configurator;
5655
this.uuidPropertyName = uuidPropertyName;
5756
this.definitionsManager = definitionsManager;
57+
this.expandReferenceObj = expandReferenceObj;
5858
}
5959

6060
/**
@@ -460,7 +460,7 @@ public JsonNode readInternal(Vertex rootVertex) throws Exception {
460460
throw new RecordNotFoundException("entity status is inactive");
461461
}
462462
ObjectNode rootNode = constructObject(rootVertex);
463-
entityType = (String) ValueType.getValue(rootNode.get(TypePropertyHelper.getTypeName()));
463+
String entityType = (String) ValueType.getValue(rootNode.get(TypePropertyHelper.getTypeName()));
464464

465465
// Set the type for the root node, so as to wrap.
466466
populateMaps(rootNode, rootVertex);
@@ -485,14 +485,42 @@ public JsonNode readInternal(Vertex rootVertex) throws Exception {
485485
// objects.
486486
// The properties could exist anywhere. Refer to the local arrMap.
487487
expandChildObject(rootNode, 0);
488-
488+
if(expandReferenceObj)
489+
expandReferenceNodes(rootNode);
489490
entityNode.set(entityType, rootNode);
490491

492+
491493
// After reading the entire type, now trim the @type property
492494
trimAttributes(entityNode);
493495
return entityNode;
494496
}
495497

498+
private void expandReferenceNodes(ObjectNode rootNode) {
499+
rootNode.fields().forEachRemaining(entry -> {
500+
// Regex pattern to check for a DID
501+
String pattern = "^"+ DID_TYPE+":[^:]+:[^:]+";
502+
if(entry.getValue().isValueNode() && entry.getValue().asText().matches(pattern)) {
503+
String[] dids = entry.getValue().asText().split(":");
504+
String osid = RecordIdentifier.parse(dids[2]).getUuid();
505+
Iterator<Vertex> vertexIterator = graph.traversal().clone().V().hasLabel(dids[1]).has(uuidPropertyName, osid);
506+
while (vertexIterator.hasNext()) {
507+
Vertex dependent = vertexIterator.next();
508+
ObjectNode references;
509+
try {
510+
references = (ObjectNode) readInternal(dependent);
511+
references = (ObjectNode) references.get(references.fieldNames().next());
512+
references = (ObjectNode) JSONUtil.removeNodesByPath(references, definitionsManager.getExcludingFieldsForEntity(dids[1]));
513+
} catch (Exception e) {
514+
throw new RuntimeException(e);
515+
}
516+
if(references != null) {
517+
entry.setValue(references);
518+
}
519+
}
520+
}
521+
});
522+
}
523+
496524
/**
497525
* Trims out local helper attributes like the type, uuid depending on the
498526
* ReadConfigurator

java/registry/src/main/java/dev/sunbirdrc/registry/dao/VertexWriter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.node.ArrayNode;
5+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
56
import com.fasterxml.jackson.databind.node.ObjectNode;
67
import dev.sunbirdrc.registry.middleware.util.Constants;
78
import dev.sunbirdrc.registry.sink.DatabaseProvider;
89
import dev.sunbirdrc.registry.util.ArrayHelper;
10+
import dev.sunbirdrc.registry.util.RecordIdentifier;
911
import dev.sunbirdrc.registry.util.RefLabelHelper;
1012
import dev.sunbirdrc.registry.util.TypePropertyHelper;
1113
import org.apache.tinkerpop.gremlin.process.traversal.P;
@@ -16,12 +18,15 @@
1618
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
1719
import org.slf4j.Logger;
1820
import org.slf4j.LoggerFactory;
21+
import org.springframework.beans.factory.annotation.Value;
1922

2023
import java.util.ArrayList;
2124
import java.util.Iterator;
2225
import java.util.List;
2326
import java.util.Map;
2427

28+
import static dev.sunbirdrc.registry.middleware.util.Constants.DID_TYPE;
29+
2530
/**
2631
* Helps in writing a vertex, edge into the database
2732
*/
@@ -31,7 +36,6 @@ public class VertexWriter {
3136
private DatabaseProvider databaseProvider;
3237
private String parentOSid;
3338
private static final String EMPTY_STR = "";
34-
3539
private Logger logger = LoggerFactory.getLogger(VertexWriter.class);
3640

3741
public VertexWriter(Graph graph, DatabaseProvider databaseProvider, String uuidPropertyName) {
@@ -215,7 +219,6 @@ private Vertex processNode(String label, JsonNode jsonObject) {
215219
jsonObject.fields().forEachRemaining(entry -> {
216220
JsonNode entryValue = entry.getValue();
217221
logger.debug("Processing {} -> {}", entry.getKey(), entry.getValue());
218-
219222
if (entryValue.isValueNode()) {
220223
// Directly add under the vertex as a property
221224
vertex.property(entry.getKey(), ValueType.getValue(entryValue));

java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ public Vertex deleteEntity(String entityName, String entityId, String userId) th
10571057
Shard shard = shardManager.activateShard(shardId);
10581058
ReadConfigurator configurator = ReadConfiguratorFactory.getOne(false);
10591059
Vertex vertex = registryService.deleteEntityById(shard, entityName, userId, recordId.getUuid());
1060-
VertexReader vertexReader = new VertexReader(shard.getDatabaseProvider(), vertex.graph(), configurator, uuidPropertyName, definitionsManager);
1060+
VertexReader vertexReader = new VertexReader(shard.getDatabaseProvider(), vertex.graph(), configurator, uuidPropertyName, definitionsManager, true);
10611061
JsonNode deletedNode = JsonNodeFactory.instance.objectNode().set(entityName, vertexReader.constructObject(vertex));
10621062
if(notificationEnabled) notificationHelper.sendNotification(deletedNode, DELETE);
10631063
return vertex;

0 commit comments

Comments
 (0)