1
|
package eu.dnetlib.data.claims.utils;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import eu.dnetlib.data.claims.entity.Context;
|
5
|
import eu.dnetlib.data.claims.handler.ClaimHandler;
|
6
|
import org.apache.log4j.Logger;
|
7
|
import org.springframework.beans.factory.annotation.Autowired;
|
8
|
|
9
|
import java.util.List;
|
10
|
|
11
|
/**
|
12
|
* Created by kiatrop on 27/1/2016.
|
13
|
*/
|
14
|
public class ContextUtils {
|
15
|
private static final Logger logger = Logger.getLogger(ClaimHandler.class);
|
16
|
private SearchUtils searchUtils;
|
17
|
|
18
|
public Context fetchContextById(String openaireId) {
|
19
|
Context context = new Context();
|
20
|
if(openaireId == null ){
|
21
|
return null;
|
22
|
}
|
23
|
context.setOpenaireId(openaireId);
|
24
|
try {
|
25
|
context.setTitle(extractLabel(context.getOpenaireId()));
|
26
|
|
27
|
} catch (Exception e) {
|
28
|
logger.error("ContextUtils: Couldn't get Egi label for id " + context.getOpenaireId(),e);
|
29
|
}
|
30
|
return context;
|
31
|
|
32
|
}
|
33
|
public String extractLabel(String code) throws Exception {
|
34
|
String[] codeParts = code.split("::");
|
35
|
String level0 = "";
|
36
|
String level1 = "";
|
37
|
String level2 = "";
|
38
|
String level3 = "";
|
39
|
if (codeParts.length >0) {
|
40
|
String json = searchUtils.fetchContext("s/");
|
41
|
level0 = getLabel(json, codeParts[0]);
|
42
|
}
|
43
|
if (codeParts.length >1) {
|
44
|
String json = searchUtils.fetchContext("/"+codeParts[0]);
|
45
|
level1 = getLabel(json, codeParts[0]+"::"+codeParts[1]);
|
46
|
}
|
47
|
if (codeParts.length >2) {
|
48
|
String json = searchUtils.fetchContext("/category/"+codeParts[0]+"::"+codeParts[1]);
|
49
|
level2 = getLabel(json, codeParts[0]+"::"+codeParts[1]+"::"+codeParts[2]);
|
50
|
}
|
51
|
if (codeParts.length >3) {
|
52
|
String json = searchUtils.fetchContext("/category/concept/"+codeParts[0]+"::"+codeParts[1]+"::"+codeParts[2]);
|
53
|
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));
|
54
|
}
|
55
|
|
56
|
|
57
|
return level0+(level1.length()>0?("> "+level1+ (level3.length()>0?("> "+level3):((level2.length()>0)?("> "+level2 ):""))):"");
|
58
|
|
59
|
}
|
60
|
|
61
|
public static String getLabel(String json, String id) {
|
62
|
if (json == null) {
|
63
|
return null;
|
64
|
}
|
65
|
Gson gson = new Gson();
|
66
|
Concept[] concepts = gson.fromJson(json, Concept[].class);
|
67
|
|
68
|
if (concepts != null && concepts.length > 0) {
|
69
|
for (int i = 0; i < concepts.length; i++) {
|
70
|
|
71
|
Concept concept = concepts[i];
|
72
|
if(concept.type !=null && (concept.type.equals("funding"))){
|
73
|
continue;
|
74
|
}
|
75
|
|
76
|
if (concept.id.equals(id)) {
|
77
|
return concept.label;
|
78
|
}
|
79
|
|
80
|
}
|
81
|
}
|
82
|
|
83
|
return "";
|
84
|
|
85
|
}
|
86
|
private static String getSubLabel(String json, String id, String subId, String subsubId) {
|
87
|
if (json == null) {
|
88
|
return null;
|
89
|
}
|
90
|
Gson gson = new Gson();
|
91
|
Concept[] concepts = gson.fromJson(json, Concept[].class);
|
92
|
|
93
|
if (concepts != null && concepts.length > 0) {
|
94
|
for (int i = 0; i < concepts.length; i++) {
|
95
|
|
96
|
Concept concept = concepts[i];
|
97
|
if(concept.type !=null && (concept.type.equals("funding"))){
|
98
|
continue;
|
99
|
}
|
100
|
|
101
|
if (concept.id.equals(id)) {
|
102
|
if (concept.getConcepts() != null) {
|
103
|
for (int j = 0; j < concept.getConcepts().size(); j++) {
|
104
|
Concept subConcept = concept.getConcepts().get(j);
|
105
|
if (subConcept.id.equals(subId)){
|
106
|
if(subsubId == null ||subConcept.concepts ==null) {
|
107
|
return subConcept.label;
|
108
|
}else{
|
109
|
for (int k = 0; k < subConcept.getConcepts().size(); k++) {
|
110
|
Concept subSubConcept = subConcept.getConcepts().get(k);
|
111
|
if (subSubConcept.id.equals(subsubId)) {
|
112
|
return subSubConcept.label;
|
113
|
}
|
114
|
}
|
115
|
}
|
116
|
break;
|
117
|
}
|
118
|
|
119
|
}
|
120
|
}
|
121
|
}
|
122
|
|
123
|
}
|
124
|
} else {
|
125
|
return null;
|
126
|
}
|
127
|
return "";
|
128
|
|
129
|
}
|
130
|
|
131
|
|
132
|
|
133
|
class Concept{
|
134
|
String id;
|
135
|
String label;
|
136
|
String type;
|
137
|
Boolean hasSubConcept;
|
138
|
List<Concept> concepts;
|
139
|
|
140
|
public String getId() {
|
141
|
return id;
|
142
|
}
|
143
|
|
144
|
public void setId(String id) {
|
145
|
this.id = id;
|
146
|
}
|
147
|
|
148
|
public String getLabel() {
|
149
|
return label;
|
150
|
}
|
151
|
|
152
|
public void setLabel(String label) {
|
153
|
this.label = label;
|
154
|
}
|
155
|
|
156
|
public String getType() {
|
157
|
return type;
|
158
|
}
|
159
|
|
160
|
public void setType(String type) {
|
161
|
this.type = type;
|
162
|
}
|
163
|
|
164
|
public Boolean getHasSubConcept() {
|
165
|
return hasSubConcept;
|
166
|
}
|
167
|
|
168
|
public void setHasSubConcept(Boolean hasSubConcept) {
|
169
|
this.hasSubConcept = hasSubConcept;
|
170
|
}
|
171
|
|
172
|
public List<Concept> getConcepts() {
|
173
|
return concepts;
|
174
|
}
|
175
|
|
176
|
public void setConcepts(List<Concept> concepts) {
|
177
|
this.concepts = concepts;
|
178
|
}
|
179
|
|
180
|
@Override
|
181
|
public String toString() {
|
182
|
return "Concept{" +
|
183
|
"id='" + id + '\'' +
|
184
|
", label='" + label + '\'' +
|
185
|
", type='" + type + '\'' +
|
186
|
", hasSubConcept=" + hasSubConcept +
|
187
|
", concepts=" + concepts +
|
188
|
'}';
|
189
|
}
|
190
|
}
|
191
|
|
192
|
public SearchUtils getSearchUtils() {
|
193
|
return searchUtils;
|
194
|
}
|
195
|
|
196
|
public void setSearchUtils(SearchUtils searchUtils) {
|
197
|
this.searchUtils = searchUtils;
|
198
|
}
|
199
|
}
|