Java tutorial
/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package edu.artic.geneva.sync; import static com.github.jsonldjava.core.JsonLdProcessor.expand; import static com.github.jsonldjava.core.JsonLdProcessor.toRDF; import static com.github.jsonldjava.utils.JsonUtils.fromString; import static edu.artic.geneva.sync.GenevaSyncHeaders.GENEVA_JSON_LD_DOCUMENT; import static java.util.stream.Collectors.toList; import java.io.IOException; import java.util.List; import java.util.Map; import org.apache.camel.Exchange; import org.apache.camel.Message; import org.apache.camel.Processor; import org.apache.camel.RuntimeCamelException; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonParseException; import com.github.jsonldjava.core.JsonLdError; import com.github.jsonldjava.core.JsonLdOptions; /** * Represents a processor for translating a JSON document into a SparQL update query. * * @author Aaron Coburn */ class UpdateProcessor implements Processor { public void process(final Exchange exchange) throws IOException { final Message in = exchange.getIn(); try { final JsonLdOptions opts = new JsonLdOptions("") { { format = "application/nquads"; } }; final Object doc = fromString(in.getHeader(GENEVA_JSON_LD_DOCUMENT, String.class)); final List<Object> expanded = expand(doc); final Map<String, Object> map = (Map<String, Object>) expanded.get(0); final Object res = toRDF(doc, null, opts); final StringBuilder sb = new StringBuilder("DELETE WHERE {"); map.remove("@id"); map.remove("@context"); if (map.containsKey("@type")) { if (map.get("@type") instanceof List) { for (final Object t : (List) map.get("@type")) { sb.append("<> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <" + (String) t + "> . \n"); } } else if (map.get("@type") instanceof String) { sb.append("<> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <" + (String) map.get("@type") + "> .\n"); } } final List<String> preds = map.entrySet().stream().map(e -> e.getKey()).filter(e -> !e.startsWith("@")) .collect(toList()); for (int i = 0; i < preds.size(); i++) { sb.append("<> <" + preds.get(i) + "> ?o" + i + " .\n"); } sb.append("};\n"); sb.append("INSERT DATA {"); sb.append(res); sb.append("}"); in.setBody(sb.toString()); } catch (final JsonLdError ex) { throw new RuntimeCamelException("Error expanding JSON-LD", ex); } catch (final JsonParseException ex) { throw new RuntimeCamelException("Error parsing JSON", ex); } catch (final JsonGenerationException ex) { throw new RuntimeCamelException("Error generating JSON", ex); } } }