1
|
package eu.dnetlib.data.claimsDemo;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import eu.dnetlib.data.claims.migration.handler.ClaimHandler;
|
5
|
import org.apache.log4j.Logger;
|
6
|
|
7
|
import java.util.List;
|
8
|
|
9
|
/**
|
10
|
* Created by kiatrop on 27/1/2016.
|
11
|
*/
|
12
|
public class ContextUtils {
|
13
|
private static final Logger logger = Logger.getLogger(ClaimHandler.class);
|
14
|
private SearchUtils searchUtils = new SearchUtils();
|
15
|
|
16
|
public static String extractLabel(String code, Boolean production) 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/", production);
|
24
|
level0 = getLabel(json, codeParts[0]);
|
25
|
}
|
26
|
if (codeParts.length >1) {
|
27
|
String json = SearchUtils.fetchContext("/"+codeParts[0], production);
|
28
|
level1 = getLabel(json, codeParts[0]+"::"+codeParts[1]);
|
29
|
}
|
30
|
if (codeParts.length >2) {
|
31
|
String json = SearchUtils.fetchContext("/category/"+codeParts[0]+"::"+codeParts[1], production);
|
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], production);
|
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
|
}
|
38
|
|
39
|
|
40
|
return level0+(level1.length()>0?("> "+level1+ (level3.length()>0?("> "+level3):((level2.length()>0)?("> "+level2 ):""))):"");
|
41
|
|
42
|
}
|
43
|
|
44
|
public static String getLabel(String json, String id) {
|
45
|
if (json == null) {
|
46
|
return null;
|
47
|
}
|
48
|
Gson gson = new Gson();
|
49
|
Concept[] concepts = gson.fromJson(json, Concept[].class);
|
50
|
|
51
|
if (concepts != null && concepts.length > 0) {
|
52
|
for (int i = 0; i < concepts.length; i++) {
|
53
|
|
54
|
Concept concept = concepts[i];
|
55
|
if(concept.type !=null && (concept.type.equals("funding"))){
|
56
|
continue;
|
57
|
}
|
58
|
|
59
|
if (concept.id.equals(id)) {
|
60
|
return concept.label;
|
61
|
}
|
62
|
|
63
|
}
|
64
|
}
|
65
|
|
66
|
return "";
|
67
|
|
68
|
}
|
69
|
private static String getSubLabel(String json, String id, String subId, String subsubId) {
|
70
|
if (json == null) {
|
71
|
return null;
|
72
|
}
|
73
|
Gson gson = new Gson();
|
74
|
Concept[] concepts = gson.fromJson(json, Concept[].class);
|
75
|
|
76
|
if (concepts != null && concepts.length > 0) {
|
77
|
for (int i = 0; i < concepts.length; i++) {
|
78
|
|
79
|
Concept concept = concepts[i];
|
80
|
if(concept.type !=null && (concept.type.equals("funding"))){
|
81
|
continue;
|
82
|
}
|
83
|
|
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
|
|
106
|
}
|
107
|
} else {
|
108
|
return null;
|
109
|
}
|
110
|
return "";
|
111
|
|
112
|
}
|
113
|
|
114
|
|
115
|
|
116
|
class Concept{
|
117
|
String id;
|
118
|
String label;
|
119
|
String type;
|
120
|
Boolean hasSubConcept;
|
121
|
List<Concept> concepts;
|
122
|
|
123
|
public String getId() {
|
124
|
return id;
|
125
|
}
|
126
|
|
127
|
public void setId(String id) {
|
128
|
this.id = id;
|
129
|
}
|
130
|
|
131
|
public String getLabel() {
|
132
|
return label;
|
133
|
}
|
134
|
|
135
|
public void setLabel(String label) {
|
136
|
this.label = label;
|
137
|
}
|
138
|
|
139
|
public String getType() {
|
140
|
return type;
|
141
|
}
|
142
|
|
143
|
public void setType(String type) {
|
144
|
this.type = type;
|
145
|
}
|
146
|
|
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", false));
|
177
|
System.out.println(ContextUtils.extractLabel("aginfra::projects::10", false));
|
178
|
|
179
|
}
|
180
|
}
|