Project

General

Profile

1
package eu.dnetlib.dli.proto;
2

    
3
import java.util.ArrayList;
4
import java.util.HashMap;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.function.Function;
8
import java.util.function.Supplier;
9
import java.util.stream.Collectors;
10

    
11
import eu.dnetlib.data.proto.*;
12
import eu.dnetlib.data.proto.KindProtos.Kind;
13

    
14
import static eu.dnetlib.data.proto.DNGFProtos.*;
15
import static eu.dnetlib.data.proto.dli.DLIObjectProtos.*;
16

    
17

    
18
/**
19
 * Created by sandro on 2/13/17.
20
 */
21
public class DNGFDLIConverter {
22

    
23
    private DNGF mainDNGFEntity;
24

    
25
    private List<DNGFRel> relations = new ArrayList<>();
26

    
27
    public void setMainEntity(final DNGF mainEntity) {
28
        this.mainDNGFEntity = mainEntity;
29
    }
30

    
31
    public void addRelation(final DNGFRel relation) {
32
        this.relations.add(relation);
33
    }
34

    
35
    public boolean isValid() {
36
	    return !relations.isEmpty() && mainDNGFEntity != null && mainDNGFEntity.getKind().equals(Kind.entity);
37
    }
38

    
39
    public DLIObject convert() {
40
        if (relations.isEmpty() || mainDNGFEntity == null) {
41
            throw new IllegalStateException("missing relations or mainEntity");
42
        }
43
        if (mainDNGFEntity.getKind().equals(KindProtos.Kind.relation)) {
44
            throw new IllegalStateException("the main Entity must be an Entity and not a relation");
45
        }
46

    
47
        final DNGFEntity entity = mainDNGFEntity.getEntity();
48
        final DLIObject.Builder builder = makeDLIObject(entity);
49

    
50

    
51
        this.relations.forEach(r -> manageRelation(r, builder));
52

    
53
        //Setting Titles
54
        return builder.build();
55
    }
56

    
57
    public DLIObjectSummary summaryOf(final DLIObject input) {
58
        final DLIObjectSummary.Builder summary = DLIObjectSummary.newBuilder();
59
        summary.setId(input.getId());
60
        if (input.getMetadata().getTitleList() != null)
61
            summary.addAllTitle(input.getMetadata().getTitleList());
62
        if (input.getMetadata().getAuthorList() != null)
63
            summary.addAllAuthor(input.getMetadata().getAuthorList());
64
        if (input.getMetadata().getDateList() != null)
65
            summary.addAllDate(input.getMetadata().getDateList());
66

    
67
        summary.setAbstract(input.getMetadata().getAbstract());
68

    
69
        if (input.getMetadata().getSubjectList() != null)
70
            summary.addAllSubject(input.getMetadata().getSubjectList());
71

    
72
        if (input.getMetadata().getPublisherList() != null)
73
            summary.addAllPublisher(input.getMetadata().getPublisherList());
74

    
75
        if (input.getLocalIdentifierList() != null)
76
            summary.addAllLocalIdentifier(input.getLocalIdentifierList());
77

    
78
        final Map<String, CollectedFromType> datasources = new HashMap<>();
79

    
80
        final List<CollectedFromType> collectedFromList = input.getMetadata().getCollectedFromList();
81
        if (collectedFromList != null) {
82
            collectedFromList.forEach(c -> {
83
                if (c.getDatasourceId() != null) {
84
                    datasources.put(c.getDatasourceId(), c);
85
                }
86
            });
87
        }
88

    
89
        if (input.getRelationsList() != null) {
90
            Map<Typology, Integer> countRelations = new HashMap<>();
91
            countRelations.put(Typology.publication, 0);
92
            countRelations.put(Typology.dataset, 0);
93
            countRelations.put(Typology.unknown, 0);
94

    
95
            summary.setTypology(input.getMetadata().getType());
96
            input.getRelationsList().forEach(rel -> {
97
                Typology t = rel.getTarget().getMetadata().getType();
98
                switch (t) {
99
                    case publication:
100
                        countRelations.put(Typology.publication, countRelations.get(Typology.publication) + 1);
101
                        break;
102
                    case dataset:
103
                        countRelations.put(Typology.dataset, countRelations.get(Typology.dataset) + 1);
104
                        break;
105
                    case unknown:
106
                        countRelations.put(Typology.unknown, countRelations.get(Typology.unknown) + 1);
107
                        break;
108
                }
109

    
110
                final List<CollectedFromType> collectedFromListRel = rel.getCollectedFromList();
111
                if (collectedFromListRel != null) {
112
                    collectedFromListRel.forEach(c -> {
113
                        if (c.getDatasourceId() != null) {
114
                            datasources.put(c.getDatasourceId(), c);
115
                        }
116

    
117
                    });
118
                }
119

    
120

    
121
            });
122
            summary.setRelatedPublications(countRelations.get(Typology.publication));
123
            summary.setRelatedDatasets(countRelations.get(Typology.dataset));
124
            summary.setRelatedUnknown(countRelations.get(Typology.unknown));
125

    
126

    
127
            final List<ResolvedFromType> resolvedFromTypeList = input.getMetadata().getResolvedFromList();
128
            resolvedFromTypeList.forEach(r -> {
129
                if (r.getDatasourceId() != null) {
130
                    datasources.put(r.getDatasourceId(),
131
                            CollectedFromType.newBuilder()
132
                                    .setDatasourceId(r.getDatasourceId())
133
                                    .setDatasourceName(r.getDatasourceName())
134
                                    .setCompletionStatus(CompletionStatus.complete)
135
                                    .build());
136
                }
137
            });
138

    
139

    
140
            summary.addAllDatasources(datasources.values());
141

    
142

    
143
        }
144

    
145
        return summary.build();
146

    
147
    }
148

    
149
    private DLIObject.Builder makeDLIObject(DNGFEntity entity) {
150
        //Set main Identifier
151
        final DLIObject.Builder builder = DLIObject.newBuilder();
152
        builder.setId(entity.getId());
153

    
154
        //Set all typed Identifier
155
        final List<FieldTypeProtos.StructuredProperty> identifiers = entity.getExtension(DliProtos.typedIdentifier);
156
        if (identifiers == null || identifiers.isEmpty()) {
157
            throw new IllegalStateException(String.format("missing typedIdentifiers on main Entity \n%s", entity));
158

    
159
        }
160

    
161
        identifiers.stream()
162
                .collect(
163
                        Collectors.groupingBy(s -> String.format("%s::%s", s.getValue().toLowerCase().trim(), s.getQualifier().getClassid().toLowerCase().trim())))
164
                .values()
165
                .forEach(
166
                        v -> builder.addLocalIdentifier(
167
                        TypedIdentifier.newBuilder()
168
                                .setId(v.get(0).getValue())
169
                                .setType(v.get(0).getQualifier().getClassid())
170
                ));
171

    
172
        Metadata.Builder dliMetadata = Metadata.newBuilder();
173

    
174
        //ADD Resolved From
175
        final List<FieldTypeProtos.KeyValue> resolvedFromList = entity.getExtension(DliProtos.resolvedfrom);
176
        if (resolvedFromList != null) {
177
            resolvedFromList.stream().collect(
178
                    Collectors.groupingBy(rf ->
179
                            String.format("%s::%s", rf.getKey(), rf.getValue())
180
                    )
181
            ).values().forEach(r -> dliMetadata.addResolvedFrom(
182
                    ResolvedFromType.newBuilder()
183
                            .setDatasourceId(r.get(0).getKey())
184
                            .setDatasourceName(r.get(0).getValue())
185
                            .build()
186
            ));
187
        }
188

    
189
        final List<FieldTypeProtos.KeyValue> collectedfromList = entity.getCollectedfromList();
190
        if (collectedfromList != null) {
191
            collectedfromList.stream().collect(
192
                    Collectors.groupingBy(rf ->
193
                            String.format("%s::%s::%s", rf.getKey(), rf.getValue(), rf.getExtension(DliFieldTypeProtos.completionStatus))
194
                    )
195
            ).values().forEach(c -> {
196
                final String completionStatus = c.get(0).getExtension(DliFieldTypeProtos.completionStatus);
197
                        CollectedFromType.Builder cType = CollectedFromType.newBuilder();
198
                cType.setDatasourceId(c.get(0).getKey());
199
                cType.setDatasourceName(c.get(0).getValue());
200
                        cType.setCompletionStatus(CompletionStatus.valueOf(completionStatus));
201
                        dliMetadata.addCollectedFrom(cType);
202
                    }
203
            );
204
        }
205

    
206
        final String completionStatus = entity.getExtension(DliProtos.completionStatus);
207
        dliMetadata.setCompletionStatus(CompletionStatus.valueOf(completionStatus));
208

    
209
        //Setting Metadata
210
        switch (entity.getType()) {
211
            case dataset:
212
                dliMetadata.setType(Typology.dataset);
213
                setMetadataFromDataset(entity.getDataset(), dliMetadata);
214
                break;
215
            case publication:
216
                dliMetadata.setType(Typology.publication);
217
                setMetadataFromPublication(entity.getPublication(), dliMetadata);
218
                break;
219
            case unknown:
220
                dliMetadata.setType(Typology.unknown);
221
                break;
222
            default:
223
                throw new IllegalStateException(String.format("wrong Type for mainEntity: \n%s", entity));
224
        }
225
        return builder.setMetadata(dliMetadata);
226
    }
227

    
228
    private void manageRelation(final DNGFRel relation, final DLIObject.Builder dliObj) {
229
        final DLIObject.Builder targetObject = makeDLIObject(relation.getCachedTarget());
230

    
231
        final DLIRelation.Builder relBuilder = DLIRelation.newBuilder()
232
		        .setTarget(targetObject)
233
		        .setRelationSemantic(SchemeValue.newBuilder().setScheme("datacite").setValue(relation.getRelType().getClassid()));
234

    
235
        final List<FieldTypeProtos.KeyValue> collectedFromList = relation.getCollectedfromList();
236
        if (collectedFromList != null) {
237
            collectedFromList.forEach(c ->
238
                    relBuilder.addCollectedFrom(
239
                            CollectedFromType.newBuilder()
240
                                    .setDatasourceName(c.getValue())
241
                                    .setDatasourceId(c.getKey())
242
		                            .setCompletionStatus(CompletionStatus.complete)
243
                                    .build()
244
                    )
245
            );
246
        }
247

    
248
        dliObj.addRelations(relBuilder.build());
249
    }
250

    
251
    private void setMetadataFromDataset(final DatasetProtos.Dataset dataset, final Metadata.Builder metadata) {
252
        final List<FieldTypeProtos.StructuredProperty> titleList = dataset.getMetadata().getTitleList();
253
        //ADD Titles
254
        if (titleList != null) {
255
            titleList.forEach(title -> metadata.addTitle(title.getValue()));
256
        }
257
        //ADD AUTHORS
258
        final List<PersonProtos.Person> authorList = dataset.getAuthorList();
259
        if (authorList != null) {
260
            authorList.forEach(person -> metadata.addAuthor(person.getMetadata().getFullname().getValue()));
261
        }
262

    
263
        //ADD Abstract
264
        manageDatasetDates(dataset, metadata);
265
        if (dataset.getMetadata().getDescriptionList() != null) {
266
            dataset.getMetadata().getDescriptionList().forEach(desc -> metadata.setAbstract(desc.getValue()));
267
        }
268

    
269
        //ADD Subjects
270
        if (dataset.getMetadata().getSubjectList() != null) {
271
            dataset.getMetadata().getSubjectList().forEach(
272
                    subject -> metadata.addSubject(
273
		                    SchemeValue.newBuilder()
274
                                    .setValue(subject.getValue())
275
                                    .setScheme(subject.getQualifier().getClassid())
276
                                    .build()
277
                    )
278
            );
279
        }
280
        //SET Publisher
281
        if (dataset.getMetadata().getPublisher() != null) {
282
            metadata.addPublisher(dataset.getMetadata().getPublisher().getValue());
283
        }
284

    
285

    
286
    }
287

    
288
    private void setMetadataFromPublication(final PublicationProtos.Publication publication, final Metadata.Builder metadata) {
289
        final List<FieldTypeProtos.StructuredProperty> titleList = publication.getMetadata().getTitleList();
290
        //ADD Titles
291
        if (titleList != null) {
292
            titleList.forEach(title -> metadata.addTitle(title.getValue()));
293
        }
294
        //ADD AUTHORS
295
        final List<PersonProtos.Person> authorList = publication.getAuthorList();
296
        if (authorList != null) {
297
            authorList.forEach(person -> metadata.addAuthor(person.getMetadata().getFullname().getValue()));
298
        }
299

    
300
        //ADD Abstract
301
        managePublicationDates(publication, metadata);
302
        if (publication.getMetadata().getDescriptionList() != null) {
303
            publication.getMetadata().getDescriptionList().forEach(desc -> metadata.setAbstract(desc.getValue()));
304
        }
305

    
306
        //ADD Subjects
307
        if (publication.getMetadata().getSubjectList() != null) {
308
            publication.getMetadata().getSubjectList().forEach(
309
                    subject -> metadata.addSubject(
310
		                    SchemeValue.newBuilder()
311
                                    .setValue(subject.getValue())
312
                                    .setScheme(subject.getQualifier().getClassid())
313
                                    .build()
314
                    )
315
            );
316
        }
317
        //SET Publisher
318
        if (publication.getMetadata().getPublisher() != null) {
319
            metadata.addPublisher(publication.getMetadata().getPublisher().getValue());
320
        }
321

    
322
    }
323

    
324
    private void managePublicationDates(final PublicationProtos.Publication publication, final Metadata.Builder metadata) {
325
        if (publication.getMetadata().getDateofacceptance() != null) {
326
            metadata.addDate(publication.getMetadata().getDateofacceptance().getValue());
327
        }
328

    
329
        if (publication.getMetadata().getRelevantdateList() != null && !publication.getMetadata().getRelevantdateList().isEmpty()) {
330
            publication.getMetadata().getRelevantdateList().forEach(
331
                    date -> metadata.addDate(date.getValue())
332
            );
333
        }
334
    }
335

    
336
    private void manageDatasetDates(final DatasetProtos.Dataset dataset, final Metadata.Builder metadata) {
337
        if (dataset.getMetadata().getDateofacceptance() != null) {
338
            metadata.addDate(dataset.getMetadata().getDateofacceptance().getValue());
339
        }
340

    
341
        if (dataset.getMetadata().getRelevantdateList() != null && !dataset.getMetadata().getRelevantdateList().isEmpty()) {
342
            dataset.getMetadata().getRelevantdateList().forEach(
343
                    date -> metadata.addDate(date.getValue())
344
            );
345
        }
346

    
347
    }
348

    
349

    
350
}
    (1-1/1)