private Response updateOrCreateEntity(String id,Map<String,Representation> parsed,
String method,
boolean create,
boolean update,
HttpHeaders headers){
Entityhub entityhub = ContextHelper.getServiceFromContext(Entityhub.class, servletContext);
MediaType accepted = getAcceptableMediaType(headers,
JerseyUtils.ENTITY_SUPPORTED_MEDIA_TYPES,
MediaType.APPLICATION_JSON_TYPE);
if(entityhub == null){
return Response.status(Status.INTERNAL_SERVER_ERROR).
entity("The Entityhub is currently unavailable.")
.header(HttpHeaders.ACCEPT, accepted).build();
}
//(1) if an id is parsed we need to ignore all other representations
if(id != null && !"*".equals(id)){
Representation r = parsed.get(id);
if(r == null){
return Response.status(Status.BAD_REQUEST)
.entity(String.format("Parsed RDF data do not contain any "
+ "Information about the parsed id '%s'",id))
.header(HttpHeaders.ACCEPT, accepted).build();
} else {
parsed = Collections.singletonMap(id, r);
}
}
//First check if all parsed Representation can be created/updated
if(!(create && update)){ //if both create and update are enabled skip this
long start = System.currentTimeMillis();
log.debug(" ... validate parsed Representation state (create: {}| update: {})",
create,update);
for(Entry<String,Representation> entry : parsed.entrySet()){
boolean exists;
try {
exists = entityhub.isRepresentation(entry.getKey());
} catch (EntityhubException e) {
log.error(String.format("Exception while checking the existance " +
"of an Entity with id %s in the Entityhub.",
entry.getKey()),e);
return Response.status(Status.INTERNAL_SERVER_ERROR)
.entity(String.format("Unable to process Entity %s because of" +
"an Error while checking the current version of that" +
"Entity within the Entityhub (Message: %s)",
entry.getKey(),e.getMessage()))
.header(HttpHeaders.ACCEPT, accepted).build();
}
if((exists && !update) || (!exists && !create)){
return Response.status(Status.BAD_REQUEST).entity(String.format(
"Unable to %s an Entity '%s' becuase it %s and request parameter '%s' is set. " +
" To allow both creating and updating of Entities you need to set "+
"'%s=true' in the request",
exists ? "update" : "create", entry.getKey(),
exists ? "exists " : "does not exist",
exists ? "update=false" : "create=false",
exists ? "update" : "create"))
.header(HttpHeaders.ACCEPT, accepted).build();
}
}
log.debug(" > checked {} entities in {}ms",
parsed.size(),System.currentTimeMillis()-start);
}
//store the Representations
//If someone parses data for more than a single Entity, but does not
//provide an ID for the Entity to update, this will update/create all
//the parsed entity. However the response can only return a single
//Entity!
//This can not be changed easily as long as there are no local URIs
//for remote Entiteis as suggested by
// http://incubator.apache.org/stanbol/docs/trunk/entityhub/entityhubandlinkeddata.html
Map<String,Entity> updated = new HashMap<String,Entity>();
for(Representation representation : parsed.values()){
try {
Entity entity = entityhub.store(representation);
updated.put(entity.getId(), entity);
}catch (EntityhubException e) {
log.error(String.format("Exception while storing Entity %s" +
"in the Entityhub.",representation),e);
}