Project

General

Profile

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

    
8
import java.util.List;
9

    
10
/**
11
 * Created by kiatrop on 27/1/2016.
12
 */
13
public class ContextUtils {
14
    private static final Logger logger = Logger.getLogger(ClaimHandler.class);
15
    private SearchUtils searchUtils = new SearchUtils();
16

    
17
    public static Context fetchContextById(String openaireId, Boolean production) {
18
        Context context = new Context();
19
        if(openaireId == null ){
20
            return null;
21
        }
22
        context.setOpenaireId(openaireId);
23
        try {
24
            context.setTitle(ContextUtils.extractLabel(context.getOpenaireId(),production));
25

    
26
        } catch (Exception e) {
27
            logger.error("ContextUtils: Couldn't get Egi label for id " + context.getOpenaireId(),e);
28
        }
29
        return context;
30

    
31
    }
32
    public static String extractLabel(String code, Boolean production) throws Exception {
33
        String[] codeParts = code.split("::");
34
        String level0 = "";
35
        String level1 = "";
36
        String level2 = "";
37
        String level3 = "";
38
        if (codeParts.length >0) {
39
            String json = SearchUtils.fetchContext("s/", production);
40
            level0 = getLabel(json, codeParts[0]);
41
        }
42
        if (codeParts.length >1) {
43
            String json = SearchUtils.fetchContext("/"+codeParts[0], production);
44
            level1 = getLabel(json, codeParts[0]+"::"+codeParts[1]);
45
        }
46
        if (codeParts.length >2) {
47
            String json = SearchUtils.fetchContext("/category/"+codeParts[0]+"::"+codeParts[1], production);
48
            level2 = getLabel(json, codeParts[0]+"::"+codeParts[1]+"::"+codeParts[2]);
49
        }
50
        if (codeParts.length >3) {
51
            String json = SearchUtils.fetchContext("/category/concept/"+codeParts[0]+"::"+codeParts[1]+"::"+codeParts[2], production);
52
            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));
53
        }
54

    
55

    
56
        return level0+(level1.length()>0?("> "+level1+ (level3.length()>0?("> "+level3):((level2.length()>0)?("> "+level2 ):""))):"");
57

    
58
    }
59

    
60
    public static String getLabel(String json, String  id) {
61
         if (json == null) {
62
            return null;
63
        }
64
        Gson gson = new Gson();
65
        Concept[] concepts = gson.fromJson(json, Concept[].class);
66

    
67
        if (concepts != null && concepts.length > 0) {
68
            for (int i = 0; i < concepts.length; i++) {
69

    
70
                Concept concept = concepts[i];
71
                if(concept.type !=null && (concept.type.equals("funding"))){
72
                    continue;
73
                }
74

    
75
                    if (concept.id.equals(id)) {
76
                        return concept.label;
77
                    }
78

    
79
                }
80
            }
81

    
82
        return "";
83

    
84
    }
85
    private static String getSubLabel(String json, String  id, String subId, String subsubId) {
86
        if (json == null) {
87
            return null;
88
        }
89
        Gson gson = new Gson();
90
        Concept[] concepts = gson.fromJson(json, Concept[].class);
91

    
92
        if (concepts != null && concepts.length > 0) {
93
            for (int i = 0; i < concepts.length; i++) {
94

    
95
                Concept concept = concepts[i];
96
                if(concept.type !=null && (concept.type.equals("funding"))){
97
                    continue;
98
                }
99

    
100
                    if (concept.id.equals(id)) {
101
                        if (concept.getConcepts() != null) {
102
                            for (int j = 0; j < concept.getConcepts().size(); j++) {
103
                                Concept subConcept = concept.getConcepts().get(j);
104
                                if (subConcept.id.equals(subId)){
105
                                    if(subsubId == null ||subConcept.concepts ==null) {
106
                                        return subConcept.label;
107
                                    }else{
108
                                        for (int k = 0; k < subConcept.getConcepts().size(); k++) {
109
                                            Concept subSubConcept = subConcept.getConcepts().get(k);
110
                                            if (subSubConcept.id.equals(subsubId)) {
111
                                                return subSubConcept.label;
112
                                            }
113
                                        }
114
                                    }
115
                                    break;
116
                                }
117

    
118
                            }
119
                        }
120
                    }
121

    
122
            }
123
        } else {
124
            return null;
125
        }
126
        return "";
127

    
128
    }
129

    
130

    
131

    
132
        class Concept{
133
            String id;
134
            String label;
135
            String type;
136
            Boolean hasSubConcept;
137
            List<Concept> concepts;
138

    
139
            public String getId() {
140
                return id;
141
            }
142

    
143
            public void setId(String id) {
144
                this.id = id;
145
            }
146

    
147
            public String getLabel() {
148
                return label;
149
            }
150

    
151
            public void setLabel(String label) {
152
                this.label = label;
153
            }
154

    
155
            public String getType() {
156
                return type;
157
            }
158

    
159
            public void setType(String type) {
160
                this.type = type;
161
            }
162

    
163
            public Boolean getHasSubConcept() {
164
                return hasSubConcept;
165
            }
166

    
167
            public void setHasSubConcept(Boolean hasSubConcept) {
168
                this.hasSubConcept = hasSubConcept;
169
            }
170

    
171
            public List<Concept> getConcepts() {
172
                return concepts;
173
            }
174

    
175
            public void setConcepts(List<Concept> concepts) {
176
                this.concepts = concepts;
177
            }
178

    
179
            @Override
180
            public String toString() {
181
                return "Concept{" +
182
                        "id='" + id + '\'' +
183
                        ", label='" + label + '\'' +
184
                        ", type='" + type + '\'' +
185
                        ", hasSubConcept=" + hasSubConcept +
186
                        ", concepts=" + concepts +
187
                        '}';
188
            }
189
        }
190

    
191
    public static void main(String[] args) throws Exception {
192
//        System.out.println(ContextUtils.extractLabel("egi::classification::natsc::math::applied", false));
193
        System.out.println(ContextUtils.extractLabel("aginfra::projects::10", false));
194

    
195
    }
196
}
(5-5/9)