Project

General

Profile

« Previous | Next » 

Revision 8946

look up annotations when reindexing a given pid. still very much a prototype in that we are looking up annotations from an external annotator-store. TODO: add pid filtering to query when annotateit.org supports it (pending upgrade on their site).

View differences:

test/edu/ucsb/nceas/metacat/annotation/AnnotatorServiceTest.java
1
/**  '$RCSfile$'
2
 *  Copyright: 2010 Regents of the University of California and the
3
 *              National Center for Ecological Analysis and Synthesis
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
package edu.ucsb.nceas.metacat.annotation;
20

  
21
import java.util.List;
22
import java.util.Map;
23

  
24
import junit.framework.Test;
25
import junit.framework.TestSuite;
26
import edu.ucsb.nceas.MCTestCase;
27

  
28
public class AnnotatorServiceTest extends MCTestCase {
29

  
30
	
31
	/**
32
	 * constructor for the test
33
	 */
34
	public AnnotatorServiceTest(String name) {
35
		super(name);
36
	}
37

  
38
	/**
39
	 * Establish a testing framework by initializing appropriate objects
40
	 */
41
	public void setUp() throws Exception {
42
		super.setUp();
43
	}
44

  
45
	/**
46
	 * Release any objects after tests are complete
47
	 */
48
	public void tearDown() {
49
	}
50

  
51
	/**
52
	 * Create a suite of tests to be run together
53
	 */
54
	public static Test suite() {
55
		TestSuite suite = new TestSuite();
56
		suite.addTest(new AnnotatorServiceTest("testLookup"));
57

  
58
		return suite;
59
	}
60
	
61
	public void testLookup() {
62
		String pid = "test.2014090915254865302.1";
63
		Map<String, List<Object>> fields = AnnotatorService.lookUpAnnotations(pid);
64
		assertTrue(fields.containsKey("characteristic_sm"));
65
	}
66
	
67

  
68
}
0 69

  
src/edu/ucsb/nceas/metacat/MetacatHandler.java
87 87
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlForSingleFile;
88 88
import edu.ucsb.nceas.utilities.access.AccessControlInterface;
89 89
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlList;
90
import edu.ucsb.nceas.metacat.annotation.AnnotatorService;
90 91
import edu.ucsb.nceas.metacat.cart.CartManager;
91 92
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
92 93
import edu.ucsb.nceas.metacat.common.query.EnabledQueryEngines;
......
2688 2689
						try {
2689 2690
							// submit for indexing
2690 2691
						    Map<String, List<Object>> fields = EventLog.getInstance().getIndexFields(identifier, Event.READ.xmlValue());
2692
						    Map<String, List<Object>> annotations = AnnotatorService.lookUpAnnotations(identifier.getValue());
2693
						    if (annotations != null) {
2694
						    	fields.putAll(annotations);
2695
						    }
2691 2696
	                        MetacatSolrIndex.getInstance().submit(identifier, sysMeta, fields, false);
2692 2697
						} catch (Exception e) {
2693 2698
							failedList.add(id);
src/edu/ucsb/nceas/metacat/annotation/AnnotatorService.java
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

  
36
		try {
37
			
38
			annotatorUrl = PropertyService.getProperty("annotator.store.url");
39
			
40
			// skip if not configured to query the annotator-store
41
			if (annotatorUrl == null || annotatorUrl.length() == 0) {
42
				return null;
43
			}
44
			
45
			//String urlParameters = "pid=" + URLEncoder.encode(pid, "UTF-8");
46
			String urlParameters = "user=leinfelder";
47
			
48
			String url = annotatorUrl + "?" + urlParameters;
49
			HttpClient client = new HttpClient();
50
			HttpMethod method = new GetMethod(url);
51
			method.addRequestHeader("Accept", "application/json");
52
			client.executeMethod(method);
53
			InputStream is = method.getResponseBodyAsStream();
54
			
55
			String results = IOUtils.toString(is, "UTF-8");
56
			logMetacat.debug("RESULTS: " + results);
57
			JSONObject jo = (JSONObject) JSONValue.parse(results);
58
			
59
			JSONArray rows = (JSONArray) jo.get("rows");
60
			int count = rows.size();
61
			Map<String, List<Object>> annotations = new HashMap<String, List<Object>>();
62
			List<Object> values = null;
63

  
64
			// default the tags to a catch-all dynamic field as we develop
65
			String solrKey = "annotation_sm";
66
			
67
			for (int i = 0; i < count; i++){
68
				JSONObject row = (JSONObject) rows.get(i);
69
				
70
				// skip this row if it is not about this pid
71
				// Bug in annotator-store prevents effective search by pid
72
				String pidValue = row.get("pid").toString();
73
				if (!pidValue.equals(pid)) {
74
					continue;
75
				}
76
				
77
				// if the annotation told us the target index field, then use it
78
				Object field = row.get("field");
79
				if (field != null) {
80
					solrKey = field.toString();
81
				}
82
				
83
				values = annotations.get(solrKey);
84
				if (values == null) {
85
					values = new ArrayList<Object>();
86
				}
87
				String key = "tags";
88
				Object obj = row.get(key);
89
				if (obj instanceof JSONArray) {
90
					JSONArray tags = (JSONArray) row.get(key);
91
					values.addAll(tags);
92
				} else {
93
					String value = row.get(key).toString();
94
					values.add(value);
95
				}
96
				annotations.put(solrKey, values);
97

  
98
			}
99
			// just populate this one field for example
100
			return annotations;
101
			
102
		} catch (Exception e) {
103
			logMetacat.error("Could not lookup annotation using: " + annotatorUrl, e);
104
		}
105
		
106
		return null;
107
	}
108
	
109
	
110
}
0 111

  

Also available in: Unified diff