1 package com.ontotext.ordi.mapper.handler;
2
3 import java.util.Map;
4
5 import org.openrdf.model.Literal;
6 import org.openrdf.model.Resource;
7 import org.openrdf.model.Value;
8 import org.openrdf.model.impl.BNodeImpl;
9 import org.openrdf.model.impl.URIImpl;
10
11 import com.ontotext.ordi.mapper.model.DataHandler;
12
13 public class ResourceImpl implements DataHandler {
14
15 private String pattern = "urn:ordi:default:%s";
16 private boolean isBNodeSubject = false;
17 private int startIgnoreChars;
18 private int endIgnoreChars;
19
20 public Value[] sqlToValue(Object object) {
21 Resource[] values = new Resource[1];
22 if (isBNodeSubject)
23 values[0] = new BNodeImpl(String.format(pattern, object.toString()));
24 else
25 values[0] = new URIImpl(String.format(pattern, object.toString()));
26 return values;
27 }
28
29 public Object valueToSql(Value value) {
30 if (value instanceof Literal) {
31 return null;
32 }
33 return value.stringValue().substring(startIgnoreChars,
34 value.stringValue().length() - endIgnoreChars);
35 }
36
37 public void initialize(Map<String, String> params) {
38 if (params.containsKey("hasPattern")) {
39 pattern = params.get("hasPattern");
40 }
41 if (pattern == null || pattern.indexOf("%s") == -1) {
42 throw new IllegalArgumentException(
43 "The pattern has the be not null and specify %s paramter!");
44 }
45 if (params.containsKey("blankNode")) {
46 isBNodeSubject = true;
47 }
48 startIgnoreChars = pattern.indexOf("%s");
49 endIgnoreChars = pattern.length() - startIgnoreChars - 2;
50 }
51 }