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