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