Project

General

Profile

1
package edu.ucsb.nceas.metacat.annotation;
2

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

    
11

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

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

    
21
public class OrcidService {
22
	
23
	private static Logger logMetacat = Logger.getLogger(OrcidService.class);
24
	
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";
27

    
28
    
29
    /**
30
	 * Look up possible ORCID from orcid service.
31
	 * @see "http://support.orcid.org/knowledgebase/articles/132354-searching-with-the-public-api"
32
	 * @param surName
33
	 * @param givenNames
34
	 * @param otherNames
35
	 * @return
36
	 */
37
	public static String lookupOrcid(String surName, String[] givenNames, String[] otherNames) {
38
		
39
		String url = null;
40

    
41
		try {
42
			
43
			String urlParameters = "";
44
			if (surName != null) {
45
//				surName = surName.replaceAll(" ", "%20");
46
				urlParameters += "+family-name:\"" + surName + "\"";
47
			}
48
			if (otherNames != null) {
49
				for (String otherName: otherNames) {
50
//					otherName = otherName.replaceAll(" ", "%20");
51
					urlParameters += "+other-names:\"" + otherName + "\""; 
52
				}
53
			}
54
			if (givenNames != null) {
55
				for (String givenName: givenNames) {
56
//					givenName = givenName.replaceAll(" ", "%20");
57
					urlParameters += "+given-names:\"" + givenName + "\""; 
58
				}
59
			}
60
			
61
			urlParameters = URLEncoder.encode(urlParameters, "UTF-8");
62
			
63
			url = REST_URL + "?q=" + urlParameters + "&rows=1";
64
			URL restURL = new URL(url);
65
			InputStream is = restURL.openStream();
66
			String results = IOUtils.toString(is);
67
			logMetacat.debug("RESULTS: " + results);
68
			Node doc = XMLUtilities.getXMLReaderAsDOMTreeRootNode(new StringReader(results));
69
			Node orcidUriNodeList = XMLUtilities.getNodeWithXPath(doc, "//*[local-name()=\"orcid-identifier\"]/*[local-name()=\"uri\"]");
70
			if (orcidUriNodeList != null) {
71
				String orcidUri = orcidUriNodeList.getFirstChild().getNodeValue();
72
				logMetacat.info("Found ORCID URI: " + orcidUri);
73
				return orcidUri;
74
			}
75
		} catch (Exception e) {
76
			logMetacat.error("Could not lookup ORCID using: " + url, e);
77
		}
78
		
79
		return null;
80
	}
81
	
82
	/**
83
	 * Look up indexed creators
84
	 */
85
	public static List<String> lookupCreators(boolean includeObsolete) {
86
		// Search for the creators if we can find them
87
		List<String> retList = null;
88
		try {
89
			String query = "q=-obsoletedBy:[* TO *]&rows=0&facet=true&facet.limit=-1&facet.field=origin";
90
			if (includeObsolete) {
91
				query = "q=*:*&rows=0&facet=true&facet.limit=-1&facet.field=origin";
92
			}
93
			
94
			MockHttpServletRequest request = new MockHttpServletRequest(null, null, null);
95
			InputStream results = MNodeService.getInstance(request ).query("solr", query);
96
			Node rootNode = XMLUtilities.getXMLReaderAsDOMTreeRootNode(new InputStreamReader(results, "UTF-8"));
97
			//String resultString = XMLUtilities.getDOMTreeAsString(rootNode);
98
			NodeList nodeList = XMLUtilities.getNodeListWithXPath(rootNode, "//lst[@name=\"origin\"]/int/@name");
99
			if (nodeList != null && nodeList.getLength() > 0) {
100
				retList = new ArrayList<String>();
101
				for (int i = 0; i < nodeList.getLength(); i++) {
102
					String found = nodeList.item(i).getFirstChild().getNodeValue();
103
					retList.add(found);
104
				}
105
			}
106
		} catch (Exception e) {
107
			logMetacat.error("Error checking for creators[s]: " + e.getMessage(), e);
108
		}
109
		
110
		return retList;
111
	}
112
}
(3-3/3)