Skip to content

Commit

Permalink
Issue #142 identifying root node
Browse files Browse the repository at this point in the history
  • Loading branch information
jyotsnaraveendran committed Apr 24, 2018
1 parent f26cc2e commit 433b972
Show file tree
Hide file tree
Showing 16 changed files with 402 additions and 152 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.opensaber.utils.converters;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

import org.apache.jena.rdf.model.RDFNode;
Expand Down Expand Up @@ -60,7 +62,21 @@ private static void updateGraph(Value subjectValue, IRI property, Value objectVa
Literal literal = (Literal)objectValue;
String datatype = literal.getDatatype().toString();
logger.info("TYPE saved is "+datatype);
s.property(property.toString(), literal.getLabel()).property("@type",datatype);
/*VertexProperty vp = s.property(property.toString());
if(vp.isPresent()){
Object value = vp.value();
List valueList = new ArrayList();
if(value instanceof List){
valueList = (List)value;
} else{
String valueStr = (String)value;
valueList.add(valueStr);
}
s.property(property.toString(), valueList);
}else{*/
s.property(property.toString(), literal.getLabel()).property("@type",datatype);
//}
} else if (objectValue instanceof IRI) {
IRI objectIRI = (IRI)objectValue;
Vertex o = getExistingVertexOrAdd(objectIRI.toString(), graph);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -112,5 +114,25 @@ public ResponseEntity<Response> fetchEntity(String url, HttpHeaders headers) {
ResponseEntity<Response> response = restTemplate.exchange(url, HttpMethod.GET, entity, Response.class);
return response;
}

/*public ResponseEntity<Response> addEntity(String jsonldData, String url, HttpHeaders headers) {
HttpEntity<String> entity = new HttpEntity<>(jsonldData, headers);
ResponseEntity<Response> response = restTemplate.postForEntity(url, entity, Response.class);
return response;
}
public ResponseEntity<Response> update(String jsonldData, String url, HttpHeaders headers) {
HttpEntity<String> entity = new HttpEntity<>(jsonldData, headers);
Response response = restTemplate.patchForObject(url, entity, Response.class);
return new ResponseEntity(response, HttpStatus.OK);
}
public ResponseEntity<Response> readEntity(String url, HttpHeaders headers, String id) {
HttpEntity<String> entity = new HttpEntity<>(headers);
Map<String,String> queryParams = new HashMap<String,String>();
queryParams.put("id", id);
ResponseEntity<Response> response = restTemplate.exchange(url, HttpMethod.GET, entity, Response.class,queryParams);
return response;
}*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ Feature: Inserting a record into the registry
When another record issued into the registry
Then record issuing should be successful
And fetching the record from the registry should match the issued record

Scenario: Inserting record with invalid type
Given a record with invalid type
And a valid auth token
When issuing the record into the registry
Then record issuing should be unsuccessful
And error message is Failed to insert due to invalid type

Scenario: Getting an expected response
Given a response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Constants {
public static final String SUBJECT_LABEL_TYPE = "subject.label.type";
public static final String SHAPE_NAME = "validations.entity.shape.name";
public static final String SHAPE_TYPE = "validations.entity.shape.type";
public static final String FEATURE_TOGGLING = "feature.toggling";
public static final String RDF_VALIDATION_MAPPER_OBJECT = "rdfValidationMapper";
public static final String REGISTRY_CONTEXT_BASE = "registry.context.base";
public static final String PRIVACY_PROPERTY = "privateProperties";
Expand All @@ -30,13 +31,14 @@ public class Constants {

public static final String DUPLICATE_RECORD_MESSAGE = "Cannot insert duplicate record";
public static final String FAILED_INSERTION_MESSAGE = "Failed to insert record";
public static final String INVALID_TYPE_MESSAGE = "Failed to insert due to invalid type";
public static final String NO_ENTITY_AVAILABLE_MESSAGE = "No entity to create or update";
public static final String ENTITY_NOT_FOUND = "Entity does not exist";
public static final String TOKEN_EXTRACTION_ERROR = "Unable to extract auth token";
public static final String JSONLD_PARSE_ERROR = "Unable to parse JSON-LD";
public static final String RDF_VALIDATION_ERROR = "Unable to validate RDF";
public static final String RDF_VALIDATION_MAPPING_ERROR = "Unable to map validations";
public static final String CUSTOM_EXCEPTION_ERROR = "Something went wrong!! Please try again later";
public static final String ADD_UPDATE_MULTIPLE_ENTITIES_MESSAGE = "Cannot add/update more than one entity";

public static final String OPENSABER_REGISTRY_API_NAME = "opensaber-registry-api";
public static final String SUNBIRD_ENCRYPTION_SERVICE_NAME = "sunbird.encryption.service";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,23 @@ public RDFValidationMapper rdfValidationMapper(){
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AuthorizationInterceptor(authorizationFilter(), gson()))
.addPathPatterns("/**").excludePathPatterns("/health", "/error").order(1);
registry.addInterceptor(new RDFConversionInterceptor(rdfConverter(), gson()))
.addPathPatterns("/create", "/update/{id}").order(2);
registry.addInterceptor(new RDFValidationMappingInterceptor(rdfValidationMapper(), gson()))
.addPathPatterns("/create", "/update/{id}").order(3);
registry.addInterceptor(new RDFValidationInterceptor(rdfValidator(), gson()))
.addPathPatterns("/create", "/update/{id}").order(4);
.addPathPatterns("/**").excludePathPatterns("/health", "/error").order(1);
boolean featureToggling = Boolean.parseBoolean(environment.getProperty(Constants.FEATURE_TOGGLING));
if(featureToggling){
registry.addInterceptor(new RDFConversionInterceptor(rdfConverter(), gson()))
.addPathPatterns("/add", "/update").order(2);
registry.addInterceptor(new RDFValidationMappingInterceptor(rdfValidationMapper(), gson()))
.addPathPatterns("/add", "/update").order(3);
registry.addInterceptor(new RDFValidationInterceptor(rdfValidator(), gson()))
.addPathPatterns("/add", "/update").order(4);
} else {
registry.addInterceptor(new RDFConversionInterceptor(rdfConverter(), gson()))
.addPathPatterns("/create", "/update/{id}").order(2);
registry.addInterceptor(new RDFValidationMappingInterceptor(rdfValidationMapper(), gson()))
.addPathPatterns("/create", "/update/{id}").order(3);
registry.addInterceptor(new RDFValidationInterceptor(rdfValidator(), gson()))
.addPathPatterns("/create", "/update/{id}").order(4);
}
/*registry.addInterceptor(new JSONLDConversionInterceptor(jsonldConverter()))
.addPathPatterns("/read/{id}").order(2);*/
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;

import io.opensaber.pojos.HealthCheckResponse;
import io.opensaber.registry.middleware.util.Constants;
import io.opensaber.registry.util.JSONUtil;
Expand All @@ -23,6 +26,7 @@
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -39,7 +43,7 @@
import io.opensaber.pojos.ValidationResponse;
import io.opensaber.pojos.ValidationResponseSerializer;
import io.opensaber.registry.exception.DuplicateRecordException;
import io.opensaber.registry.exception.InvalidTypeException;
import io.opensaber.registry.exception.EntityCreationException;
import io.opensaber.registry.exception.RecordNotFoundException;
import io.opensaber.registry.service.RegistryService;

Expand All @@ -59,7 +63,6 @@ public class RegistryController {

@RequestMapping(value = "/create", method = RequestMethod.POST)
public ResponseEntity<Response> addEntity(@RequestAttribute Request requestModel) {

Model rdf = (Model) requestModel.getRequestMap().get("rdf");
ResponseParams responseParams = new ResponseParams();
Response response = new Response(Response.API_ID.CREATE, "OK", responseParams);
Expand All @@ -70,7 +73,34 @@ public ResponseEntity<Response> addEntity(@RequestAttribute Request requestModel
result.put("entity", label);
response.setResult(result);
responseParams.setStatus(Response.Status.SUCCCESSFUL);
} catch (DuplicateRecordException | InvalidTypeException e) {
} catch (DuplicateRecordException | EntityCreationException e) {
response.setResult(result);
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
responseParams.setErrmsg(e.getMessage());
} catch (Exception e) {
response.setResult(result);
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
responseParams.setErrmsg(e.getMessage());
}
return new ResponseEntity<>(response, HttpStatus.OK);
}


@RequestMapping(value = "/add", method = RequestMethod.POST)
public ResponseEntity<Response> addToExistingEntity(@RequestAttribute Request requestModel,
@RequestParam("id") String id, @RequestParam("prop") String property) {

Model rdf = (Model) requestModel.getRequestMap().get("rdf");
ResponseParams responseParams = new ResponseParams();
Response response = new Response(Response.API_ID.CREATE, "OK", responseParams);
Map<String, Object> result = new HashMap<>();

try {
String label = registryService.addToExistingEntity(rdf, id, property);
result.put("entity", label);
response.setResult(result);
responseParams.setStatus(Response.Status.SUCCCESSFUL);
} catch (DuplicateRecordException | EntityCreationException e) {
response.setResult(result);
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
responseParams.setErrmsg(e.getMessage());
Expand Down Expand Up @@ -109,7 +139,7 @@ public ResponseEntity<Response> getEntity(@PathVariable("id") String id) {
}
return new ResponseEntity<>(response, HttpStatus.OK);
}

@ResponseBody
@RequestMapping(value = "/update/{id}", method = RequestMethod.PATCH)
public ResponseEntity<Response> updateEntity(@RequestAttribute Request requestModel,
Expand All @@ -124,7 +154,7 @@ public ResponseEntity<Response> updateEntity(@RequestAttribute Request requestMo
registryService.updateEntity(rdf, id);
responseParams.setErrmsg("");
responseParams.setStatus(Response.Status.SUCCCESSFUL);
} catch (RecordNotFoundException | InvalidTypeException e) {
} catch (RecordNotFoundException | EntityCreationException e) {
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
responseParams.setErrmsg(e.getMessage());

Expand All @@ -137,8 +167,8 @@ public ResponseEntity<Response> updateEntity(@RequestAttribute Request requestMo
}

@ResponseBody
@RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
public ResponseEntity<Response> upsertEntity(@RequestAttribute Request requestModel,
@RequestMapping(value = "/update", method = RequestMethod.POST)
public ResponseEntity<Response> update(@RequestAttribute Request requestModel,
@PathVariable("id") String id) {

Model rdf = (Model) requestModel.getRequestMap().get("rdf");
Expand All @@ -147,10 +177,10 @@ public ResponseEntity<Response> upsertEntity(@RequestAttribute Request requestMo
Response response = new Response(Response.API_ID.UPDATE, "OK", responseParams);

try {
registryService.upsertEntity(rdf, id);
registryService.updateEntity(rdf, id);
responseParams.setErrmsg("");
responseParams.setStatus(Response.Status.SUCCCESSFUL);
} catch (RecordNotFoundException | InvalidTypeException e) {
} catch (RecordNotFoundException | EntityCreationException e) {
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
responseParams.setErrmsg(e.getMessage());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public interface RegistryDao {

public List getEntityList();

public String addEntity(Graph entity, String label) throws DuplicateRecordException, EncryptionException, AuditFailedException, RecordNotFoundException;
public String addEntity(Graph entity, String label) throws DuplicateRecordException, EncryptionException, AuditFailedException;

public boolean updateEntity(Graph entityForUpdate, String rootNodeLabel, String methodOrigin)
throws RecordNotFoundException, NoSuchElementException, EncryptionException, AuditFailedException;

public boolean deleteEntity(String rootLabel, String labelToBeDeleted) throws RecordNotFoundException,AuditFailedException;
//public boolean deleteEntity (Graph entity, String rootLabel) throws RecordNotFoundException,AuditFailedException;

public Graph getEntityById(String label) throws RecordNotFoundException, NoSuchElementException, EncryptionException, AuditFailedException;

Expand Down
Loading

0 comments on commit 433b972

Please sign in to comment.