- throws CrudException {
- RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version);
-
- try {
- if (payload.getSource() != null) {
- Matcher sourceMatcher = urlPattern.matcher(payload.getSource());
-
- if (!sourceMatcher.matches()) {
- throw new CrudException("Invalid Target Urls", Status.BAD_REQUEST);
- }
- String sourceNodeId = sourceMatcher.group(3);
- if (!sourceNodeId.equals(edge.getSource().getId().get())) {
- throw new CrudException("Source can't be updated", Status.BAD_REQUEST);
- }
- }
-
- if (payload.getTarget() != null) {
- Matcher targetMatcher = urlPattern.matcher(payload.getTarget());
-
- if (!targetMatcher.matches()) {
- throw new CrudException("Invalid Target Urls", Status.BAD_REQUEST);
- }
- String sourceNodeId = targetMatcher.group(3);
- if (!sourceNodeId.equals(edge.getTarget().getId().get())) {
- throw new CrudException("Target can't be updated", Status.BAD_REQUEST);
- }
- }
-
- // Remove the timestamp properties from the existing edge, as these should be managed by Champ.
- Map<String,Object> existingProps = edge.getProperties();
-
- if (existingProps.containsKey(Metadata.CREATED_TS.propertyName())) {
- existingProps.remove(Metadata.CREATED_TS.propertyName());
+ throws CrudException {
+ RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version);
+
+ try {
+ if (payload.getSource() != null) {
+ Matcher sourceMatcher = urlPattern.matcher(payload.getSource());
+
+ if (!sourceMatcher.matches()) {
+ throw new CrudException("Invalid Source Urls", Status.BAD_REQUEST);
+ }
+ String sourceNodeId = sourceMatcher.group(3);
+ if (!sourceNodeId.equals(edge.getSource().getId().get())) {
+ throw new CrudException("Source can't be updated", Status.BAD_REQUEST);
+ }
+ }
+
+ if (payload.getTarget() != null) {
+ Matcher targetMatcher = urlPattern.matcher(payload.getTarget());
+
+ if (!targetMatcher.matches()) {
+ throw new CrudException("Invalid Target Urls", Status.BAD_REQUEST);
+ }
+ String sourceNodeId = targetMatcher.group(3);
+ if (!sourceNodeId.equals(edge.getTarget().getId().get())) {
+ throw new CrudException("Target can't be updated", Status.BAD_REQUEST);
+ }
+ }
+
+ // Remove the timestamp properties from the existing edge, as these should be managed by Champ.
+ Map<String,Object> existingProps = edge.getProperties();
+
+ if (existingProps.containsKey(Metadata.CREATED_TS.propertyName())) {
+ existingProps.remove(Metadata.CREATED_TS.propertyName());
+ }
+ if (existingProps.containsKey(Metadata.UPDATED_TS.propertyName())) {
+ existingProps.remove(Metadata.UPDATED_TS.propertyName());
+ }
+
+ // create key based on source:target:relationshipType
+ String key = edge.getSource().getType() + ":" + edge.getTarget().getType()
+ + ":" + edge.getType();
+
+ // find the validate the key from the schema
+ Map<String, Class<?>> schemaObject = schema.lookupRelation(key);
+
+ if (schemaObject == null) {
+ Logger.warn("key :" + key
+ + " not found in relationship schema . Skipping the schema validation");
+ return edge;
+ }
+
+ Set<Map.Entry<String, JsonElement>> entries = payload.getProperties().getAsJsonObject().entrySet();
+
+ for (Map.Entry<String, JsonElement> entry : entries) {
+ if (!schemaObject.containsKey(entry.getKey())) {
+ throw new CrudException("Invalid property: " + entry.getKey(), Status.BAD_REQUEST);
+ } else if (entry.getValue() instanceof JsonNull && edge.getProperties().containsKey(entry.getKey())) {
+ edge.getProperties().remove(entry.getKey());
+ } else if (!(entry.getValue() instanceof JsonNull)) {
+ Object value = CrudServiceUtil.validateFieldType(entry.getValue().getAsString(), schemaObject.get(entry.getKey()));
+ edge.getProperties().put(entry.getKey(), value);
+ }
+ }
+
+ return edge;
+ } catch (Exception ex) {
+ throw new CrudException(ex.getMessage(), Status.BAD_REQUEST);