Project

General

Profile

« Previous | Next » 

Revision 8775

switch to the production ORCID server for looking up orcid matches for our creators.
add test to summarize how many creator matches we can actually find. https://projects.ecoinformatics.org/ecoinfo/issues/6423

View differences:

test/edu/ucsb/nceas/metacat/annotation/OrcidServiceTest.java
18 18
 */
19 19
package edu.ucsb.nceas.metacat.annotation;
20 20

  
21
import java.util.Arrays;
22
import java.util.HashMap;
23
import java.util.List;
24
import java.util.Map;
25
import java.util.Map.Entry;
26

  
21 27
import junit.framework.Test;
22 28
import junit.framework.TestSuite;
23 29
import edu.ucsb.nceas.MCTestCase;
......
51 57
	public static Test suite() {
52 58
		TestSuite suite = new TestSuite();
53 59
		suite.addTest(new OrcidServiceTest("testLookup"));
60
		suite.addTest(new OrcidServiceTest("findMatches"));
61

  
54 62
		return suite;
55 63
	}
56 64
	
......
59 67
		String orcid = OrcidService.lookupOrcid(null, null, otherNames);
60 68
		assertEquals("http://sandbox-1.orcid.org/0000-0003-2141-4459", orcid);
61 69
	}
70
	
71
	public void findMatches() {
72
		
73
		int count = 0;
74
		Map<String, String> matches = new HashMap<String, String>();
62 75

  
76
		List<String> creators = OrcidService.lookupCreators(true);
77
		for (String creator: creators) {
78
			String orcid = OrcidService.lookupOrcid(null, null, Arrays.asList(creator).toArray(new String[0]));
79
			if (orcid != null) {
80
				matches.put(orcid, creator);
81
				count++;
82
			}
83
 		}
84
		assertTrue(count > 0);
85
		for (Entry<String, String> entry : matches.entrySet()) {
86
			System.out.println("Found ORCID: " + entry.getKey() + " for creator: " + entry.getValue());
87
		}
88
	}
89

  
63 90
}
src/edu/ucsb/nceas/metacat/annotation/OrcidService.java
1 1
package edu.ucsb.nceas.metacat.annotation;
2 2

  
3 3
import java.io.InputStream;
4
import java.io.InputStreamReader;
4 5
import java.io.StringReader;
5 6
import java.net.URL;
6 7
import java.net.URLEncoder;
8
import java.util.ArrayList;
9
import java.util.List;
7 10

  
11

  
8 12
import org.apache.commons.io.IOUtils;
9 13
import org.apache.log4j.Logger;
14
import org.apache.wicket.protocol.http.mock.MockHttpServletRequest;
10 15
import org.w3c.dom.Node;
16
import org.w3c.dom.NodeList;
11 17

  
18
import edu.ucsb.nceas.metacat.dataone.MNodeService;
12 19
import edu.ucsb.nceas.utilities.XMLUtilities;
13 20

  
14 21
public class OrcidService {
15 22
	
16 23
	private static Logger logMetacat = Logger.getLogger(OrcidService.class);
17 24
	
18
    private static final String REST_URL = "http://pub.sandbox.orcid.org/v1.1/search/orcid-bio";
25
    //private static final String REST_URL = "http://pub.sandbox.orcid.org/v1.1/search/orcid-bio";
26
    private static final String REST_URL = "http://pub.orcid.org/v1.1/search/orcid-bio";
19 27

  
28
    
20 29
    /**
21 30
	 * Look up possible ORCID from orcid service.
22 31
	 * @see "http://support.orcid.org/knowledgebase/articles/132354-searching-with-the-public-api"
......
31 40
			
32 41
			String urlParameters = "";
33 42
			if (surName != null) {
43
				surName = surName.replaceAll(" ", "%20");
34 44
				urlParameters += "+family-name:\"" + surName + "\"";
35 45
			}
36 46
			if (otherNames != null) {
37 47
				for (String otherName: otherNames) {
48
					otherName = otherName.replaceAll(" ", "%20");
38 49
					urlParameters += "+other-names:\"" + otherName + "\""; 
39 50
				}
40 51
			}
41 52
			if (givenNames != null) {
42 53
				for (String givenName: givenNames) {
54
					givenName = givenName.replaceAll(" ", "%20");
43 55
					urlParameters += "+given-names:\"" + givenName + "\""; 
44 56
				}
45 57
			}
46 58
			
47
			urlParameters = URLEncoder.encode(urlParameters, "UTF-8");
59
			//urlParameters = URLEncoder.encode(urlParameters, "UTF-8");
48 60
			
49 61
			String url = REST_URL + "?q=" + urlParameters + "&rows=1";
50 62
			URL restURL = new URL(url);
......
64 76
		
65 77
		return null;
66 78
	}
79
	
80
	/**
81
	 * Look up indexed creators
82
	 */
83
	public static List<String> lookupCreators(boolean includeObsolete) {
84
		// Search for the creators if we can find them
85
		List<String> retList = null;
86
		try {
87
			String query = "q=-obsoletedBy:[* TO *]&rows=0&facet=true&facet.limit=-1&facet.field=origin";
88
			if (includeObsolete) {
89
				query = "q=*:*&rows=0&facet=true&facet.limit=-1&facet.field=origin";
90
			}
91
			
92
			MockHttpServletRequest request = new MockHttpServletRequest(null, null, null);
93
			InputStream results = MNodeService.getInstance(request ).query("solr", query);
94
			Node rootNode = XMLUtilities.getXMLReaderAsDOMTreeRootNode(new InputStreamReader(results, "UTF-8"));
95
			//String resultString = XMLUtilities.getDOMTreeAsString(rootNode);
96
			NodeList nodeList = XMLUtilities.getNodeListWithXPath(rootNode, "//lst[@name=\"origin\"]/int/@name");
97
			if (nodeList != null && nodeList.getLength() > 0) {
98
				retList = new ArrayList<String>();
99
				for (int i = 0; i < nodeList.getLength(); i++) {
100
					String found = nodeList.item(i).getFirstChild().getNodeValue();
101
					retList.add(found);
102
				}
103
			}
104
		} catch (Exception e) {
105
			logMetacat.error("Error checking for creators[s]: " + e.getMessage(), e);
106
		}
107
		
108
		return retList;
109
	}
67 110
}

Also available in: Unified diff