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
|
public void writeP1Facet(final JsonGenerator jg, final String value, final IdentificationType type) 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", value);
|
30
|
jg.writeStringField("type", type.name());
|
31
|
jg.writeEndObject();
|
32
|
jg.writeEndObject();
|
33
|
}
|
34
|
|
35
|
public void writeIdentifierFacet(final JsonGenerator jg, final String identifier) throws IOException {
|
36
|
jg.writeStartObject();
|
37
|
jg.writeStringField("@class", "isIdentifiedBy");
|
38
|
jg.writeObjectFieldStart("target");
|
39
|
jg.writeStringField("@class", "IdentifierFacet");
|
40
|
jg.writeStringField("value", identifier);
|
41
|
jg.writeStringField("type", IdentificationType.URI.name());
|
42
|
jg.writeEndObject();
|
43
|
jg.writeEndObject();
|
44
|
}
|
45
|
|
46
|
public void writeInfoFacet(final JsonGenerator jg, final Resource res) throws IOException {
|
47
|
jg.writeStartObject();
|
48
|
jg.writeStringField("@class", "PE_Info_Facet");
|
49
|
jg.writeObjectFieldStart("target");
|
50
|
jg.writeStringField("title", getTitleFromRDFResource(res));
|
51
|
jg.writeStringField("description", getDescriptionFromRDFResource(res));
|
52
|
//new object comp + field value + schema="noSchema"
|
53
|
jg.writeStringField("competence", getCompetenceFromRDFResource(res));
|
54
|
//TODO: uncomment this when George adds the rel to the model, see method getAvailabilityFromRDFResource below
|
55
|
//jg.writeStringField("availability", getAvailabilityFromRDFResource(res));
|
56
|
jg.writeEndObject();
|
57
|
jg.writeEndObject();
|
58
|
}
|
59
|
|
60
|
public void writeEventFacet(final JsonGenerator jg) {
|
61
|
//TODO: implement me. get begin/end of operation from PP42_has_declarative_time
|
62
|
}
|
63
|
|
64
|
public void writeRightsFacet(final JsonGenerator jg, final Resource res) throws IOException {
|
65
|
//TODO: implement me. E30_Right facet extends from licenseFacet but it is not correct (textUrl is mandatory, we can't use it in Parthenos)
|
66
|
}
|
67
|
|
68
|
public void writeContactReferenceFacet(final JsonGenerator jg, final Resource actor) throws IOException {
|
69
|
if (actor != null) {
|
70
|
String appellation = getTitleFromRDFResource(actor);
|
71
|
String description = getDescriptionFromRDFResource(actor);
|
72
|
String legalAddress = "";
|
73
|
String email = "";
|
74
|
String website = "";
|
75
|
String address = "";
|
76
|
String phoneNumber = "";
|
77
|
//more contact point per provider
|
78
|
StmtIterator contactPointsStm = actor.listProperties(CRM.P76_has_contact_point);
|
79
|
while (contactPointsStm.hasNext()) {
|
80
|
Resource cp = contactPointsStm.nextStatement().getResource();
|
81
|
//contact points can bean E45_Address or E51_Contact_Point
|
82
|
if ((cp.hasProperty(RDF.type, CRM.E45_Address) || cp.hasProperty(RDF.type, CRM.E51_Contact_Point)) && cp.hasProperty(CRM.P2_has_type)) {
|
83
|
Resource addressType = cp.getPropertyResourceValue(CRM.P2_has_type);
|
84
|
String info = getLabelFromRDFResource(cp);
|
85
|
String addressTypeLabel = getLabelFromRDFResource(addressType);
|
86
|
switch (addressTypeLabel.toLowerCase()) {
|
87
|
case "email":
|
88
|
email = info;
|
89
|
break;
|
90
|
case "legal address":
|
91
|
legalAddress = info;
|
92
|
break;
|
93
|
case "address":
|
94
|
address = info;
|
95
|
break;
|
96
|
case "phone":
|
97
|
phoneNumber = info;
|
98
|
break;
|
99
|
case "web":
|
100
|
website = info;
|
101
|
break;
|
102
|
}
|
103
|
}
|
104
|
}
|
105
|
jg.writeStartObject();
|
106
|
jg.writeStringField("@class", "PE_Contact_Reference_Facet");
|
107
|
jg.writeObjectFieldStart("target");
|
108
|
jg.writeStringField("appellation", appellation);
|
109
|
jg.writeStringField("description", description);
|
110
|
jg.writeStringField("legalAddress", legalAddress);
|
111
|
jg.writeStringField("email", email);
|
112
|
jg.writeStringField("website", website);
|
113
|
jg.writeStringField("address", address);
|
114
|
jg.writeStringField("phoneNumber", phoneNumber);
|
115
|
jg.writeEndObject();
|
116
|
jg.writeEndObject();
|
117
|
}
|
118
|
}
|
119
|
|
120
|
public void writeDesignatedAccessPointFacet(final JsonGenerator jg, final Resource resource) throws IOException {
|
121
|
//PP28_has_designated_access_point
|
122
|
StmtIterator apStms = resource.listProperties(CRMpe.PP28_has_designated_access_point);
|
123
|
while (apStms.hasNext()) {
|
124
|
String entryName = getLabelFromRDFResource(resource);
|
125
|
//(mandatory)
|
126
|
String endpoint = "";
|
127
|
String protocol = "";
|
128
|
String description = getDescriptionFromRDFResource(resource);
|
129
|
//TODO: authorization is a ValueSchema, I do not understand how to use it and how to map it
|
130
|
String authorization = "";
|
131
|
Resource ap = apStms.next().getResource();
|
132
|
endpoint = ap.getURI();
|
133
|
//TODO: where to get protocol and authorization?
|
134
|
jg.writeStartObject();
|
135
|
jg.writeStringField("@class", "PE29_Access_Point");
|
136
|
jg.writeObjectFieldStart("target");
|
137
|
jg.writeStringField("entryName", entryName);
|
138
|
jg.writeStringField("description", description);
|
139
|
jg.writeStringField("endpoint", endpoint);
|
140
|
jg.writeStringField("protocol", protocol);
|
141
|
//TODO: authorization is a ValueSchema, I do not understand how to use it and how to map it
|
142
|
//jg.writeStringField("authorization", authorization);
|
143
|
jg.writeEndObject();
|
144
|
jg.writeEndObject();
|
145
|
}
|
146
|
|
147
|
}
|
148
|
|
149
|
public void writeTemporalCoverageFacet(final JsonGenerator jg, final Resource resource) {
|
150
|
//TODO: CoverageFacet wants a value and a mandatory schema. Can't be applied to Parthenos.
|
151
|
}
|
152
|
|
153
|
protected String getTitleFromRDFResource(final Resource resource) {
|
154
|
String title = "";
|
155
|
final Statement s = resource.getProperty(CRM.P1_is_identified_by);
|
156
|
if (s != null) {
|
157
|
Resource titleRes = s.getResource();
|
158
|
if (titleRes != null && (titleRes.hasProperty(RDF.type, CRM.E35_Title) || titleRes.hasProperty(RDF.type, CRM.E41_Appellation))) {
|
159
|
title = getLabelFromRDFResource(titleRes);
|
160
|
}
|
161
|
}
|
162
|
return title;
|
163
|
}
|
164
|
|
165
|
protected String getDescriptionFromRDFResource(final Resource resource) {
|
166
|
if (resource.hasProperty(CRM.P3_has_note)) {
|
167
|
return resource.getProperty(CRM.P3_has_note).getString();
|
168
|
} else return "";
|
169
|
}
|
170
|
|
171
|
protected String getLabelFromRDFResource(final Resource resource) {
|
172
|
if (resource.hasProperty(RDFS.label)) {
|
173
|
return resource.getProperty(RDFS.label).getString();
|
174
|
} else return "";
|
175
|
}
|
176
|
|
177
|
protected String getCompetenceFromRDFResource(final Resource resource) {
|
178
|
String comp = "";
|
179
|
if (resource.hasProperty(CRMpe.PP45_has_competency)) {
|
180
|
Resource compRes = resource.getProperty(CRMpe.PP45_has_competency).getResource();
|
181
|
if (compRes.hasProperty(RDFS.label))
|
182
|
comp = compRes.getProperty(RDFS.label).getString();
|
183
|
}
|
184
|
return comp;
|
185
|
}
|
186
|
|
187
|
protected String getAvailabilityFromRDFResource(final Resource resource) {
|
188
|
//TODO: implement this when George adds the rel to the model
|
189
|
return "";
|
190
|
}
|
191
|
}
|