Revision 52274
Added by Argiro Kokogiannaki almost 6 years ago
ContextUtils.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.claimsDemo; |
2 | 2 |
|
3 |
import eu.dnetlib.api.enabling.ISLookUpService;
|
|
3 |
import com.google.gson.Gson;
|
|
4 | 4 |
import eu.dnetlib.data.claims.migration.handler.ClaimHandler; |
5 | 5 |
import org.apache.log4j.Logger; |
6 |
import org.w3c.dom.Document; |
|
7 |
import org.w3c.dom.Node; |
|
8 |
import org.w3c.dom.NodeList; |
|
9 |
import org.xml.sax.SAXException; |
|
10 | 6 |
|
11 |
import javax.xml.parsers.DocumentBuilder; |
|
12 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
13 |
import javax.xml.parsers.ParserConfigurationException; |
|
14 |
import javax.xml.xpath.*; |
|
15 |
import java.io.IOException; |
|
16 |
import java.io.InputStream; |
|
7 |
import java.util.List; |
|
17 | 8 |
|
18 | 9 |
/** |
19 | 10 |
* Created by kiatrop on 27/1/2016. |
20 | 11 |
*/ |
21 | 12 |
public class ContextUtils { |
22 | 13 |
private static final Logger logger = Logger.getLogger(ClaimHandler.class); |
14 |
private SearchUtils searchUtils = new SearchUtils(); |
|
23 | 15 |
|
24 |
// private static Document document = null; |
|
16 |
public static String extractLabel(String code) throws Exception { |
|
17 |
String[] codeParts = code.split("::"); |
|
18 |
String level0 = ""; |
|
19 |
String level1 = ""; |
|
20 |
String level2 = ""; |
|
21 |
String level3 = ""; |
|
22 |
if (codeParts.length >0) { |
|
23 |
String json = SearchUtils.fetchContext("s/"); |
|
24 |
level0 = getLabel(json, codeParts[0]); |
|
25 |
} |
|
26 |
if (codeParts.length >1) { |
|
27 |
String json = SearchUtils.fetchContext("/"+codeParts[0]); |
|
28 |
level1 = getLabel(json, codeParts[0]+"::"+codeParts[1]); |
|
29 |
} |
|
30 |
if (codeParts.length >2) { |
|
31 |
String json = SearchUtils.fetchContext("/category/"+codeParts[0]+"::"+codeParts[1]); |
|
32 |
level2 = getLabel(json, codeParts[0]+"::"+codeParts[1]+"::"+codeParts[2]); |
|
33 |
} |
|
34 |
if (codeParts.length >3) { |
|
35 |
String json = SearchUtils.fetchContext("/category/concept/"+codeParts[0]+"::"+codeParts[1]+"::"+codeParts[2]); |
|
36 |
level3 = getSubLabel(json, codeParts[0]+"::"+codeParts[1]+"::"+codeParts[2], codeParts[0]+"::"+codeParts[1]+"::"+codeParts[2]+"::"+codeParts[3],((codeParts.length>4)?(codeParts[0]+"::"+codeParts[1]+"::"+codeParts[2]+"::"+codeParts[3]+"::"+codeParts[4]):null)); |
|
37 |
} |
|
25 | 38 |
|
26 |
//private static String isLookUpAddress = "http://node6.t.openaire.research-infrastructures.eu:8280/is/services/isLookUp"; |
|
27 |
//String registryAddress = "http://node6.t.openaire.research-infrastructures.eu:8280/is/services/isRegistry"; |
|
28 | 39 |
|
29 |
private ISLookUpService isLookUpService = null;
|
|
40 |
return level0+(level1.length()>0?("> "+level1+ (level3.length()>0?("> "+level3):((level2.length()>0)?("> "+level2 ):""))):"");
|
|
30 | 41 |
|
31 |
public static Document getXmlDocument(String communityId) { |
|
32 |
Document document = null; |
|
33 |
InputStream inputStream = ContextUtils.class.getClassLoader().getResourceAsStream("eu/dnetlib/data/claimsDemo/"+communityId+".xml"); |
|
34 |
System.out.println("eu/dnetlib/data/claimsDemo/"+communityId+".xml"); |
|
35 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
36 |
DocumentBuilder builder = null; |
|
37 |
try { |
|
38 |
builder = factory.newDocumentBuilder(); |
|
42 |
} |
|
39 | 43 |
|
40 |
} catch (ParserConfigurationException e) { |
|
41 |
logger.error("Parse configuration Exception",e); |
|
44 |
public static String getLabel(String json, String id) { |
|
45 |
if (json == null) { |
|
46 |
return null; |
|
42 | 47 |
} |
48 |
Gson gson = new Gson(); |
|
49 |
Concept[] concepts = gson.fromJson(json, Concept[].class); |
|
43 | 50 |
|
44 |
//StringBuilder xmlStringBuilder = new StringBuilder(); |
|
45 |
try { |
|
46 |
document = builder.parse(inputStream); |
|
51 |
if (concepts != null && concepts.length > 0) { |
|
52 |
for (int i = 0; i < concepts.length; i++) { |
|
47 | 53 |
|
48 |
} catch (SAXException e) { |
|
49 |
logger.error("SAXException",e); |
|
54 |
Concept concept = concepts[i]; |
|
55 |
if(concept.type !=null && (concept.type.equals("funding"))){ |
|
56 |
continue; |
|
57 |
} |
|
50 | 58 |
|
51 |
} catch (IOException e) { |
|
52 |
logger.error("IOException",e); |
|
53 |
} |
|
54 |
return document; |
|
55 |
} |
|
59 |
if (concept.id.equals(id)) { |
|
60 |
return concept.label; |
|
61 |
} |
|
56 | 62 |
|
57 |
public static String extractEgiLabel(String code) throws XPathExpressionException { |
|
58 |
String[] codeParts = code.split("::"); |
|
59 |
StringBuilder builder = new StringBuilder(); |
|
63 |
} |
|
64 |
} |
|
60 | 65 |
|
61 |
StringBuilder reconstructedCode = new StringBuilder(); |
|
62 |
if (codeParts.length > 0) { |
|
63 |
reconstructedCode.append(codeParts[0]); |
|
66 |
return ""; |
|
67 |
|
|
68 |
} |
|
69 |
private static String getSubLabel(String json, String id, String subId, String subsubId) { |
|
70 |
if (json == null) { |
|
71 |
return null; |
|
64 | 72 |
} |
73 |
Gson gson = new Gson(); |
|
74 |
Concept[] concepts = gson.fromJson(json, Concept[].class); |
|
65 | 75 |
|
66 |
for(int i=0; i< codeParts.length; i++) { |
|
67 |
if (i == 0) { |
|
68 |
//System.out.println("1"); |
|
69 |
builder.append(findContextLabel(reconstructedCode.toString(),codeParts[0])); |
|
76 |
if (concepts != null && concepts.length > 0) { |
|
77 |
for (int i = 0; i < concepts.length; i++) { |
|
70 | 78 |
|
71 |
} else if (i == 1) { |
|
72 |
reconstructedCode.append("::").append(codeParts[1]); |
|
73 |
builder.append("> ").append(findCategoryLabel(reconstructedCode.toString(),codeParts[0])); |
|
79 |
Concept concept = concepts[i]; |
|
80 |
if(concept.type !=null && (concept.type.equals("funding"))){ |
|
81 |
continue; |
|
82 |
} |
|
74 | 83 |
|
75 |
} else { |
|
76 |
reconstructedCode.append("::").append(codeParts[i]); |
|
77 |
builder.append("> ").append(findConceptLabel(reconstructedCode.toString(),codeParts[0])); |
|
84 |
if (concept.id.equals(id)) { |
|
85 |
if (concept.getConcepts() != null) { |
|
86 |
for (int j = 0; j < concept.getConcepts().size(); j++) { |
|
87 |
Concept subConcept = concept.getConcepts().get(j); |
|
88 |
if (subConcept.id.equals(subId)){ |
|
89 |
if(subsubId == null ||subConcept.concepts ==null) { |
|
90 |
return subConcept.label; |
|
91 |
}else{ |
|
92 |
for (int k = 0; k < subConcept.getConcepts().size(); k++) { |
|
93 |
Concept subSubConcept = subConcept.getConcepts().get(k); |
|
94 |
if (subSubConcept.id.equals(subsubId)) { |
|
95 |
return subSubConcept.label; |
|
96 |
} |
|
97 |
} |
|
98 |
} |
|
99 |
break; |
|
100 |
} |
|
101 |
|
|
102 |
} |
|
103 |
} |
|
104 |
} |
|
105 |
|
|
78 | 106 |
} |
107 |
} else { |
|
108 |
return null; |
|
79 | 109 |
} |
110 |
return ""; |
|
80 | 111 |
|
81 |
return builder.toString(); |
|
82 | 112 |
} |
83 | 113 |
|
84 | 114 |
|
85 |
private static String findContextLabel(String conceptCode, String communityId) throws XPathExpressionException { |
|
86 |
// System.out.println("concept code: " + conceptCode) ; |
|
87 |
XPathFactory xPathfactory = XPathFactory.newInstance(); |
|
88 |
XPath xpath = xPathfactory.newXPath(); |
|
89 | 115 |
|
90 |
XPathExpression expr = xpath.compile("//context[@id='"+ conceptCode + "']"); |
|
91 |
NodeList nodeList = (NodeList)expr.evaluate(getXmlDocument(communityId), XPathConstants.NODESET); |
|
116 |
class Concept{ |
|
117 |
String id; |
|
118 |
String label; |
|
119 |
String type; |
|
120 |
Boolean hasSubConcept; |
|
121 |
List<Concept> concepts; |
|
92 | 122 |
|
93 |
Node nNode = nodeList.item(0);
|
|
94 |
return nNode.getAttributes().getNamedItem("label").getTextContent();
|
|
95 |
} |
|
123 |
public String getId() {
|
|
124 |
return id;
|
|
125 |
}
|
|
96 | 126 |
|
97 |
private static String findCategoryLabel(String categoryCode, String communityId) throws XPathExpressionException { |
|
98 |
// System.out.println("Category code: " + categoryCode) ; |
|
99 |
XPathFactory xPathfactory = XPathFactory.newInstance(); |
|
100 |
XPath xpath = xPathfactory.newXPath(); |
|
127 |
public void setId(String id) { |
|
128 |
this.id = id; |
|
129 |
} |
|
101 | 130 |
|
102 |
XPathExpression expr = xpath.compile("//category[@id='"+ categoryCode + "']"); |
|
103 |
NodeList nodeList = (NodeList)expr.evaluate(getXmlDocument(communityId), XPathConstants.NODESET); |
|
131 |
public String getLabel() { |
|
132 |
return label; |
|
133 |
} |
|
104 | 134 |
|
105 |
Node nNode = nodeList.item(0);
|
|
106 |
return nNode.getAttributes().getNamedItem("label").getTextContent();
|
|
107 |
} |
|
135 |
public void setLabel(String label) {
|
|
136 |
this.label = label;
|
|
137 |
}
|
|
108 | 138 |
|
109 |
private static String findConceptLabel(String conceptCode, String communityId) throws XPathExpressionException { |
|
110 |
// System.out.println("Concept code: " + conceptCode) ; |
|
111 |
XPathFactory xPathfactory = XPathFactory.newInstance(); |
|
112 |
XPath xpath = xPathfactory.newXPath(); |
|
139 |
public String getType() { |
|
140 |
return type; |
|
141 |
} |
|
113 | 142 |
|
114 |
XPathExpression expr = xpath.compile("//concept[@id='"+ conceptCode + "']"); |
|
115 |
NodeList nodeList = (NodeList)expr.evaluate(getXmlDocument(communityId), XPathConstants.NODESET); |
|
143 |
public void setType(String type) { |
|
144 |
this.type = type; |
|
145 |
} |
|
116 | 146 |
|
117 |
Node nNode = nodeList.item(0); |
|
118 |
return nNode.getAttributes().getNamedItem("label").getTextContent(); |
|
147 |
public Boolean getHasSubConcept() { |
|
148 |
return hasSubConcept; |
|
149 |
} |
|
150 |
|
|
151 |
public void setHasSubConcept(Boolean hasSubConcept) { |
|
152 |
this.hasSubConcept = hasSubConcept; |
|
153 |
} |
|
154 |
|
|
155 |
public List<Concept> getConcepts() { |
|
156 |
return concepts; |
|
157 |
} |
|
158 |
|
|
159 |
public void setConcepts(List<Concept> concepts) { |
|
160 |
this.concepts = concepts; |
|
161 |
} |
|
162 |
|
|
163 |
@Override |
|
164 |
public String toString() { |
|
165 |
return "Concept{" + |
|
166 |
"id='" + id + '\'' + |
|
167 |
", label='" + label + '\'' + |
|
168 |
", type='" + type + '\'' + |
|
169 |
", hasSubConcept=" + hasSubConcept + |
|
170 |
", concepts=" + concepts + |
|
171 |
'}'; |
|
172 |
} |
|
173 |
} |
|
174 |
|
|
175 |
public static void main(String[] args) throws Exception { |
|
176 |
System.out.println(ContextUtils.extractLabel("egi::classification::natsc::math::applied")); |
|
119 | 177 |
} |
120 |
|
|
121 | 178 |
} |
Also available in: Unified diff
Change ContextUtils to get concept label from new Context API