1
|
package eu.dnetlib.parthenos.registry;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
|
5
|
import com.fasterxml.jackson.core.JsonGenerator;
|
6
|
import eu.dnetlib.parthenos.CRM;
|
7
|
import eu.dnetlib.parthenos.CRMpe;
|
8
|
import org.apache.jena.rdf.model.Resource;
|
9
|
import org.apache.jena.rdf.model.Statement;
|
10
|
import org.apache.jena.rdf.model.StmtIterator;
|
11
|
import org.apache.jena.vocabulary.RDF;
|
12
|
import org.apache.jena.vocabulary.RDFS;
|
13
|
import org.gcube.informationsystem.model.entity.facet.IdentifierFacet.IdentificationType;
|
14
|
import org.springframework.stereotype.Component;
|
15
|
|
16
|
/**
|
17
|
* Created by Alessia Bardi on 02/10/2017.
|
18
|
*
|
19
|
* @author Alessia Bardi
|
20
|
*/
|
21
|
@Component
|
22
|
public class FacetWriter {
|
23
|
|
24
|
protected void writeIdentifierFacet(final JsonGenerator jg, final String identifier) throws IOException {
|
25
|
jg.writeStartObject();
|
26
|
jg.writeStringField("@class", "P1_is_identified_by");
|
27
|
jg.writeObjectFieldStart("target");
|
28
|
jg.writeStringField("@class", "IdentifierFacet");
|
29
|
jg.writeStringField("value", identifier);
|
30
|
jg.writeStringField("type", IdentificationType.URI.name());
|
31
|
jg.writeEndObject();
|
32
|
jg.writeEndObject();
|
33
|
}
|
34
|
|
35
|
protected void writeInfoFacet(final JsonGenerator jg, final Resource res) throws IOException {
|
36
|
jg.writeStartObject();
|
37
|
jg.writeStringField("@class", "PE_Info_Facet");
|
38
|
jg.writeObjectFieldStart("target");
|
39
|
jg.writeStringField("title", getTitleFromRDFResource(res));
|
40
|
jg.writeStringField("description", getDescriptionFromRDFResource(res));
|
41
|
jg.writeStringField("competence", getCompetenceFromRDFResource(res));
|
42
|
//TODO: uncomment this when George adds the rel to the model, see method getAvailabilityFromRDFResource below
|
43
|
//jg.writeStringField("availability", getAvailabilityFromRDFResource(res));
|
44
|
jg.writeEndObject();
|
45
|
jg.writeEndObject();
|
46
|
}
|
47
|
|
48
|
|
49
|
protected void writeEventFacet(final JsonGenerator jg){
|
50
|
//TODO: implement me. get begin/end of operation from PP42_has_declarative_time
|
51
|
}
|
52
|
|
53
|
protected void writeRightsFacet(final JsonGenerator jg, final Resource res) throws IOException {
|
54
|
//TODO: implement me. E30_Right facet extends from licenseFacet but it is not correct (textUrl is mandatory, we can't use it in Parthenos)
|
55
|
}
|
56
|
|
57
|
protected void writeContactReferenceFacet(final JsonGenerator jg, final Resource resource) throws IOException {
|
58
|
//PP2_provided_by
|
59
|
final Statement s = resource.getProperty(CRMpe.PP2_provided_by);
|
60
|
|
61
|
if(s!=null){
|
62
|
String appellation = "";
|
63
|
String description = "";
|
64
|
String legalAddress = "";
|
65
|
String email ="";
|
66
|
String website = "";
|
67
|
String address = "";
|
68
|
String phoneNumber ="";
|
69
|
|
70
|
Resource provider = s.getResource();
|
71
|
if(provider != null){
|
72
|
//more contact point per provider
|
73
|
StmtIterator contactPointsStm = provider.listProperties(CRM.P76_has_contact_point);
|
74
|
while(contactPointsStm.hasNext()){
|
75
|
Resource cp = contactPointsStm.nextStatement().getResource();
|
76
|
appellation = getLabelFromRDFResource(cp);
|
77
|
description = getDescriptionFromRDFResource(cp);
|
78
|
//TODO: where to find email and legaladdress, address, phoneNumber, website?
|
79
|
jg.writeStartObject();
|
80
|
jg.writeStringField("@class", "PE_Contact_Reference_Facet");
|
81
|
jg.writeObjectFieldStart("target");
|
82
|
jg.writeStringField("appellation", appellation);
|
83
|
jg.writeStringField("description", description);
|
84
|
jg.writeStringField("legalAddress", legalAddress);
|
85
|
jg.writeStringField("email", email);
|
86
|
jg.writeStringField("website", website);
|
87
|
jg.writeStringField("address", address);
|
88
|
jg.writeStringField("phoneNumber", phoneNumber);
|
89
|
jg.writeEndObject();
|
90
|
jg.writeEndObject();
|
91
|
}
|
92
|
}
|
93
|
}
|
94
|
|
95
|
}
|
96
|
|
97
|
protected void writeDesignatedAccessPointFacet(final JsonGenerator jg, final Resource resource) throws IOException {
|
98
|
//PP28_has_designated_access_point
|
99
|
StmtIterator apStms = resource.listProperties(CRMpe.PP28_has_designated_access_point);
|
100
|
while(apStms.hasNext()){
|
101
|
String entryName = getLabelFromRDFResource(resource);
|
102
|
//(mandatory)
|
103
|
String endpoint= "";
|
104
|
String protocol= "";
|
105
|
String description= getDescriptionFromRDFResource(resource);
|
106
|
//TODO: authorization is a ValueSchema, I do not understand how to use it and how to map it
|
107
|
String authorization= "";
|
108
|
Resource ap = apStms.next().getResource();
|
109
|
endpoint = ap.getURI();
|
110
|
//TODO: where to get protocol and authorization?
|
111
|
jg.writeStartObject();
|
112
|
jg.writeStringField("@class", "PE29_Access_Point");
|
113
|
jg.writeObjectFieldStart("target");
|
114
|
jg.writeStringField("entryName", entryName);
|
115
|
jg.writeStringField("description", description);
|
116
|
jg.writeStringField("endpoint", endpoint);
|
117
|
jg.writeStringField("protocol", protocol);
|
118
|
//TODO: authorization is a ValueSchema, I do not understand how to use it and how to map it
|
119
|
//jg.writeStringField("authorization", authorization);
|
120
|
jg.writeEndObject();
|
121
|
jg.writeEndObject();
|
122
|
}
|
123
|
|
124
|
}
|
125
|
|
126
|
|
127
|
protected String getTitleFromRDFResource(final Resource resource){
|
128
|
String title = "";
|
129
|
final Statement s = resource.getProperty(CRM.P1_is_identified_by);
|
130
|
if(s != null){
|
131
|
Resource titleRes = s.getResource();
|
132
|
if(titleRes != null && (titleRes.hasProperty(RDF.type, CRM.E35_Title) || titleRes.hasProperty(RDF.type, CRM.E41_Appellation) )){
|
133
|
title = getLabelFromRDFResource(titleRes);
|
134
|
}
|
135
|
}
|
136
|
return title;
|
137
|
}
|
138
|
|
139
|
protected String getDescriptionFromRDFResource(final Resource resource){
|
140
|
if(resource.hasProperty(CRM.P3_has_note)){
|
141
|
return resource.getProperty(CRM.P3_has_note).getString();
|
142
|
}
|
143
|
else return "";
|
144
|
}
|
145
|
|
146
|
protected String getLabelFromRDFResource(final Resource resource){
|
147
|
if(resource.hasProperty(RDFS.label)){
|
148
|
return resource.getProperty(RDFS.label).getString();
|
149
|
}
|
150
|
else return "";
|
151
|
}
|
152
|
|
153
|
protected String getCompetenceFromRDFResource(final Resource resource){
|
154
|
String comp = "";
|
155
|
if(resource.hasProperty(CRMpe.PP45_has_competency)){
|
156
|
Resource compRes = resource.getProperty(CRMpe.PP45_has_competency).getResource();
|
157
|
if(compRes.hasProperty(RDFS.label))
|
158
|
comp = compRes.getProperty(RDFS.label).getString();
|
159
|
}
|
160
|
return comp;
|
161
|
}
|
162
|
|
163
|
protected String getAvailabilityFromRDFResource(final Resource resource){
|
164
|
//TODO: implement this when George adds the rel to the model
|
165
|
return "";
|
166
|
}
|
167
|
}
|