1 |
|
package edu.ucsb.nceas.metacat.annotation;
|
2 |
|
|
3 |
|
import java.io.InputStream;
|
4 |
|
import java.util.ArrayList;
|
5 |
|
import java.util.HashMap;
|
6 |
|
import java.util.List;
|
7 |
|
import java.util.Map;
|
8 |
|
|
9 |
|
import net.minidev.json.JSONArray;
|
10 |
|
import net.minidev.json.JSONObject;
|
11 |
|
import net.minidev.json.JSONValue;
|
12 |
|
|
13 |
|
import org.apache.commons.httpclient.HttpClient;
|
14 |
|
import org.apache.commons.httpclient.HttpMethod;
|
15 |
|
import org.apache.commons.httpclient.methods.GetMethod;
|
16 |
|
import org.apache.commons.io.IOUtils;
|
17 |
|
import org.apache.log4j.Logger;
|
18 |
|
|
19 |
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
20 |
|
|
21 |
|
public class AnnotatorService {
|
22 |
|
|
23 |
|
private static Logger logMetacat = Logger.getLogger(AnnotatorService.class);
|
24 |
|
|
25 |
|
/**
|
26 |
|
* Look up annotations from annotator service
|
27 |
|
* @see "http://docs.annotatorjs.org/en/latest/storage.html"
|
28 |
|
* @param pid the identifier to fetch annotations about
|
29 |
|
* @return
|
30 |
|
*/
|
31 |
|
public static Map<String, List<Object>> lookUpAnnotations(String pid) {
|
32 |
|
|
33 |
|
|
34 |
|
String annotatorUrl = null;
|
35 |
|
String consumerKey = null;
|
36 |
|
try {
|
37 |
|
|
38 |
|
annotatorUrl = PropertyService.getProperty("annotator.store.url");
|
39 |
|
consumerKey = PropertyService.getProperty("annotator.consumerKey");
|
40 |
|
|
41 |
|
// skip if not configured to query the annotator-store
|
42 |
|
if (annotatorUrl == null || annotatorUrl.length() == 0) {
|
43 |
|
return null;
|
44 |
|
}
|
45 |
|
|
46 |
|
// TODO: query for matching PID only - wasting time iterating over full list
|
47 |
|
//String urlParameters = "pid=" + URLEncoder.encode(pid, "UTF-8");
|
48 |
|
String urlParameters = "consumer=" + consumerKey;
|
49 |
|
|
50 |
|
String url = annotatorUrl + "?" + urlParameters;
|
51 |
|
HttpClient client = new HttpClient();
|
52 |
|
HttpMethod method = new GetMethod(url);
|
53 |
|
method.addRequestHeader("Accept", "application/json");
|
54 |
|
client.executeMethod(method);
|
55 |
|
InputStream is = method.getResponseBodyAsStream();
|
56 |
|
|
57 |
|
String results = IOUtils.toString(is, "UTF-8");
|
58 |
|
logMetacat.debug("RESULTS: " + results);
|
59 |
|
JSONObject jo = (JSONObject) JSONValue.parse(results);
|
60 |
|
|
61 |
|
JSONArray rows = (JSONArray) jo.get("rows");
|
62 |
|
int count = rows.size();
|
63 |
|
Map<String, List<Object>> annotations = new HashMap<String, List<Object>>();
|
64 |
|
|
65 |
|
// use catch-all annotation field for the tags
|
66 |
|
List<Object> tagValues = null;
|
67 |
|
String tagKey = "annotation_sm";
|
68 |
|
|
69 |
|
// track the comments here
|
70 |
|
List<Object> commentValues = null;
|
71 |
|
String commentKey = "comment_sm";
|
72 |
|
|
73 |
|
for (int i = 0; i < count; i++){
|
74 |
|
JSONObject row = (JSONObject) rows.get(i);
|
75 |
|
|
76 |
|
// skip this row if it is not about this pid
|
77 |
|
// FIXME: Bug in annotator-store prevents effective search by pid
|
78 |
|
String pidValue = row.get("pid").toString();
|
79 |
|
if (!pidValue.equals(pid)) {
|
80 |
|
continue;
|
81 |
|
}
|
82 |
|
|
83 |
|
// index the (semantic) tags
|
84 |
|
// if the annotation told us the target index field, then use it
|
85 |
|
Object field = row.get("field");
|
86 |
|
if (field != null) {
|
87 |
|
tagKey = field.toString();
|
88 |
|
}
|
89 |
|
|
90 |
|
// make sure we have a place to store the values
|
91 |
|
tagValues = annotations.get(tagKey);
|
92 |
|
if (tagValues == null) {
|
93 |
|
tagValues = new ArrayList<Object>();
|
94 |
|
}
|
95 |
|
Object obj = row.get("tags");
|
96 |
|
if (obj instanceof JSONArray) {
|
97 |
|
JSONArray tags = (JSONArray) obj;
|
98 |
|
tagValues.addAll(tags);
|
99 |
|
} else {
|
100 |
|
String value = obj.toString();
|
101 |
|
tagValues.add(value);
|
102 |
|
}
|
103 |
|
annotations.put(tagKey, tagValues);
|
104 |
|
|
105 |
|
// index the comments
|
106 |
|
commentValues = annotations.get(commentKey);
|
107 |
|
if (commentValues == null) {
|
108 |
|
commentValues = new ArrayList<Object>();
|
109 |
|
}
|
110 |
|
Object commentObj = row.get("text");
|
111 |
|
if (commentObj != null) {
|
112 |
|
String value = commentObj.toString();
|
113 |
|
if (value != null && value.length() > 0) {
|
114 |
|
commentValues.add(value);
|
115 |
|
}
|
116 |
|
}
|
117 |
|
annotations.put(commentKey, commentValues);
|
118 |
|
|
119 |
|
}
|
120 |
|
// just populate this one field for example
|
121 |
|
return annotations;
|
122 |
|
|
123 |
|
} catch (Exception e) {
|
124 |
|
logMetacat.error("Could not lookup annotation using: " + annotatorUrl, e);
|
125 |
|
}
|
126 |
|
|
127 |
|
return null;
|
128 |
|
}
|
129 |
|
|
130 |
|
|
131 |
|
}
|
132 |
0 |
|
remove AnnotatorService completely - was moved to cn-index-processor