Project

General

Profile

1
package eu.dnetlib.wds.resolver;
2

    
3
import com.google.gson.JsonArray;
4
import com.google.gson.JsonObject;
5
import eu.dnetlib.miscutils.collections.Pair;
6
import eu.dnetlib.pid.resolver.AbstractPIDResolver;
7
import eu.dnetlib.pid.resolver.model.ObjectType;
8
import eu.dnetlib.pid.resolver.model.PID;
9
import eu.dnetlib.pid.resolver.model.SubjectType;
10
import org.apache.commons.lang3.StringUtils;
11
import org.springframework.http.ResponseEntity;
12
import org.springframework.web.client.RestTemplate;
13

    
14
import java.util.ArrayList;
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
18

    
19
public class DLIResolver extends AbstractPIDResolver {
20

    
21
    private static String INDEX_URL = "http://ip-90-147-167-27.ct1.garrservices.it:9200/dli_shadow/object/_search?pretty";
22

    
23
    @Override
24
    protected boolean canResolvePid(String pidType) {
25
        return StringUtils.isNotEmpty(pidType);
26
    }
27

    
28
    public WDSResolvedObject resolve(final String pid, final String pidType) {
29
        final RestTemplate restTemplate = new RestTemplate();
30

    
31
        final ResponseEntity<JsonObject> jsonObjectResponseEntity = restTemplate.postForEntity(INDEX_URL, createJson(pid, pidType), JsonObject.class);
32
        final JsonObject hits = jsonObjectResponseEntity.getBody().getAsJsonObject("hits");
33
        final int total = hits.get("total").getAsInt();
34
        if (total > 0) {
35
            final JsonArray results = hits.getAsJsonArray("hits");
36
            return parseDLIResponse(results.get(0).getAsJsonObject());
37
        }
38
        return null;
39
    }
40

    
41

    
42
    private WDSResolvedObject parseDLIResponse(final JsonObject input) {
43
        final WDSResolvedObject result = new WDSResolvedObject();
44
        final JsonObject source = input.getAsJsonObject("_source");
45
        if (source == null || source.isJsonNull())
46
            return null;
47
        final JsonArray localIdentifiers = source.getAsJsonArray("localIdentifier");
48
        if (localIdentifiers == null || localIdentifiers.isJsonNull())
49
            return null;
50
        localIdentifiers.forEach(it -> result.addPid(new PID(it.getAsJsonObject().get("id").getAsString(), it.getAsJsonObject().get("type").getAsString())));
51
        final String typology = source.get("typology").getAsString();
52
        if (typology.equals("publication") || typology.equals("dataset")) {
53
            result.setType(ObjectType.valueOf(typology));
54
        } else {
55
            return null;
56
        }
57
        result.setTitles(getArrayFromJson("title", source));
58
        result.setAuthors(getArrayFromJson("author", source));
59
        final List<String> date = getArrayFromJson("date", source);
60
        Map<String, String> d = new HashMap<>();
61
        if (date != null && date.size() > 0) {
62
            d.put("unknown", date.get(0));
63
        }
64
        result.setDates(d);
65
        final String description = source.get("abstract") != null ? source.get("abstract").getAsString() : null;
66
        if (description != null && StringUtils.isNotEmpty(description))
67
            result.addDescription(new Pair<>("unknown", description));
68
        final JsonArray subjectArray = source.getAsJsonArray("subject");
69
        if (subjectArray != null && !subjectArray.isJsonNull()) {
70
            subjectArray.forEach(it -> result.addSubject(new SubjectType(it.getAsJsonObject().get("scheme").getAsString(), it.getAsJsonObject().get("value").getAsString())));
71
        }
72

    
73
        final JsonArray publisherArray = source.getAsJsonArray("publisher");
74

    
75
        if (publisherArray != null && !publisherArray.isJsonNull()) {
76
            publisherArray.forEach(it -> result.setPublisher(it.getAsString()));
77
        }
78
        return result;
79
    }
80

    
81

    
82
    private List<String> getArrayFromJson(final String key, final JsonObject source) {
83

    
84
        final List<String> result = new ArrayList<>();
85
        final JsonArray inputArray = source.getAsJsonArray(key);
86
        if (inputArray != null && !inputArray.isJsonNull()) {
87
            inputArray.forEach(it -> {
88
                if (StringUtils.isNotEmpty(it.getAsString())) result.add(it.getAsString());
89

    
90
            });
91
        }
92
        return result;
93

    
94
    }
95

    
96

    
97
    private JsonObject createJson(final String pid, final String pidType) {
98

    
99
        final JsonObject root = new JsonObject();
100

    
101
        final JsonObject nested = new JsonObject();
102
        final JsonObject n1 = new JsonObject();
103
        final JsonObject nestedQuery = new JsonObject();
104
        final JsonObject boolQuery = new JsonObject();
105
        final JsonArray mustArray = new JsonArray();
106

    
107
        final JsonObject firstMatch = new JsonObject();
108
        final JsonObject firstMatchId = new JsonObject();
109
        final JsonObject secondMatch = new JsonObject();
110
        final JsonObject secondMatchType = new JsonObject();
111

    
112

    
113
        root.add("query", n1);
114
        n1.add("nested", nested);
115
        nested.addProperty("path", "localIdentifier");
116
        nested.add("query", nestedQuery);
117
        nestedQuery.add("bool", boolQuery);
118
        boolQuery.add("must", mustArray);
119

    
120
        mustArray.add(firstMatch);
121
        mustArray.add(secondMatch);
122

    
123
        firstMatch.add("match", firstMatchId);
124
        secondMatch.add("match", secondMatchType);
125

    
126
        firstMatchId.addProperty("localIdentifier.id", pid.toLowerCase().trim());
127
        secondMatchType.addProperty("localIdentifier.type", pidType.toLowerCase().trim());
128

    
129

    
130
        return root;
131

    
132

    
133
    }
134

    
135
}
(1-1/7)