1 |
49189
|
alessia.ba
|
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 |
49324
|
alessia.ba
|
import org.apache.commons.lang3.StringUtils;
|
9 |
49189
|
alessia.ba
|
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 |
49422
|
alessia.ba
|
import org.gcube.informationsystem.model.entity.facet.IdentifierFacet.IdentificationType;
|
15 |
49189
|
alessia.ba
|
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 |
49324
|
alessia.ba
|
//final IdentificationType type
|
26 |
|
|
public void writeP1Facet(final JsonGenerator jg, final String value) throws IOException {
|
27 |
49189
|
alessia.ba
|
jg.writeStartObject();
|
28 |
|
|
jg.writeStringField("@class", "P1_is_identified_by");
|
29 |
|
|
jg.writeObjectFieldStart("target");
|
30 |
49422
|
alessia.ba
|
jg.writeStringField("@class", "SimpleFacet");
|
31 |
49200
|
alessia.ba
|
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 |
49189
|
alessia.ba
|
jg.writeStringField("value", identifier);
|
42 |
49422
|
alessia.ba
|
jg.writeStringField("type", IdentificationType.URI.name());
|
43 |
49189
|
alessia.ba
|
jg.writeEndObject();
|
44 |
|
|
jg.writeEndObject();
|
45 |
|
|
}
|
46 |
|
|
|
47 |
49324
|
alessia.ba
|
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 |
49200
|
alessia.ba
|
public void writeInfoFacet(final JsonGenerator jg, final Resource res) throws IOException {
|
61 |
49189
|
alessia.ba
|
jg.writeStartObject();
|
62 |
49324
|
alessia.ba
|
jg.writeStringField("@class", "consistsOf");
|
63 |
|
|
jg.writeObjectFieldStart("target");
|
64 |
49189
|
alessia.ba
|
jg.writeStringField("@class", "PE_Info_Facet");
|
65 |
|
|
jg.writeStringField("title", getTitleFromRDFResource(res));
|
66 |
|
|
jg.writeStringField("description", getDescriptionFromRDFResource(res));
|
67 |
49200
|
alessia.ba
|
//new object comp + field value + schema="noSchema"
|
68 |
49324
|
alessia.ba
|
String competence = getCompetenceFromRDFResource(res);
|
69 |
|
|
if(StringUtils.isNotBlank(competence)) {
|
70 |
|
|
writeValueSchema(jg, "competence", competence, "noSchema");
|
71 |
|
|
}
|
72 |
49189
|
alessia.ba
|
//TODO: uncomment this when George adds the rel to the model, see method getAvailabilityFromRDFResource below
|
73 |
49324
|
alessia.ba
|
//String availability = getAvailabilityFromRDFResource(res);
|
74 |
|
|
// if(StringUtils.isNotBlank(availability)) {
|
75 |
|
|
// writeValueSchema(jg, "availability", availability, "noSchema");
|
76 |
|
|
// }
|
77 |
49189
|
alessia.ba
|
jg.writeEndObject();
|
78 |
|
|
jg.writeEndObject();
|
79 |
|
|
}
|
80 |
|
|
|
81 |
49324
|
alessia.ba
|
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 |
49200
|
alessia.ba
|
public void writeEventFacet(final JsonGenerator jg) {
|
89 |
49189
|
alessia.ba
|
//TODO: implement me. get begin/end of operation from PP42_has_declarative_time
|
90 |
|
|
}
|
91 |
|
|
|
92 |
49200
|
alessia.ba
|
public void writeRightsFacet(final JsonGenerator jg, final Resource res) throws IOException {
|
93 |
49189
|
alessia.ba
|
//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 |
49200
|
alessia.ba
|
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 |
49189
|
alessia.ba
|
String website = "";
|
103 |
49200
|
alessia.ba
|
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 |
49189
|
alessia.ba
|
}
|
132 |
|
|
}
|
133 |
49200
|
alessia.ba
|
jg.writeStartObject();
|
134 |
49435
|
alessia.ba
|
jg.writeStringField("@class", "consistsOf");
|
135 |
|
|
jg.writeObjectFieldStart("target");
|
136 |
49200
|
alessia.ba
|
jg.writeStringField("@class", "PE_Contact_Reference_Facet");
|
137 |
|
|
jg.writeStringField("appellation", appellation);
|
138 |
|
|
jg.writeStringField("description", description);
|
139 |
|
|
jg.writeStringField("legalAddress", legalAddress);
|
140 |
|
|
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 |
49189
|
alessia.ba
|
}
|
147 |
|
|
}
|
148 |
|
|
|
149 |
49200
|
alessia.ba
|
public void writeDesignatedAccessPointFacet(final JsonGenerator jg, final Resource resource) throws IOException {
|
150 |
49189
|
alessia.ba
|
//PP28_has_designated_access_point
|
151 |
|
|
StmtIterator apStms = resource.listProperties(CRMpe.PP28_has_designated_access_point);
|
152 |
49200
|
alessia.ba
|
while (apStms.hasNext()) {
|
153 |
49189
|
alessia.ba
|
String entryName = getLabelFromRDFResource(resource);
|
154 |
|
|
//(mandatory)
|
155 |
49200
|
alessia.ba
|
String endpoint = "";
|
156 |
|
|
String protocol = "";
|
157 |
|
|
String description = getDescriptionFromRDFResource(resource);
|
158 |
49189
|
alessia.ba
|
//TODO: authorization is a ValueSchema, I do not understand how to use it and how to map it
|
159 |
49200
|
alessia.ba
|
String authorization = "";
|
160 |
49189
|
alessia.ba
|
Resource ap = apStms.next().getResource();
|
161 |
|
|
endpoint = ap.getURI();
|
162 |
|
|
//TODO: where to get protocol and authorization?
|
163 |
|
|
jg.writeStartObject();
|
164 |
49435
|
alessia.ba
|
jg.writeStringField("@class", "consistsOf");
|
165 |
|
|
jg.writeObjectFieldStart("target");
|
166 |
49189
|
alessia.ba
|
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 |
49200
|
alessia.ba
|
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 |
49189
|
alessia.ba
|
|
183 |
49200
|
alessia.ba
|
protected String getTitleFromRDFResource(final Resource resource) {
|
184 |
49189
|
alessia.ba
|
String title = "";
|
185 |
|
|
final Statement s = resource.getProperty(CRM.P1_is_identified_by);
|
186 |
49200
|
alessia.ba
|
if (s != null) {
|
187 |
49189
|
alessia.ba
|
Resource titleRes = s.getResource();
|
188 |
49200
|
alessia.ba
|
if (titleRes != null && (titleRes.hasProperty(RDF.type, CRM.E35_Title) || titleRes.hasProperty(RDF.type, CRM.E41_Appellation))) {
|
189 |
49189
|
alessia.ba
|
title = getLabelFromRDFResource(titleRes);
|
190 |
|
|
}
|
191 |
|
|
}
|
192 |
|
|
return title;
|
193 |
|
|
}
|
194 |
|
|
|
195 |
49200
|
alessia.ba
|
protected String getDescriptionFromRDFResource(final Resource resource) {
|
196 |
|
|
if (resource.hasProperty(CRM.P3_has_note)) {
|
197 |
49189
|
alessia.ba
|
return resource.getProperty(CRM.P3_has_note).getString();
|
198 |
49200
|
alessia.ba
|
} else return "";
|
199 |
49189
|
alessia.ba
|
}
|
200 |
|
|
|
201 |
49200
|
alessia.ba
|
protected String getLabelFromRDFResource(final Resource resource) {
|
202 |
|
|
if (resource.hasProperty(RDFS.label)) {
|
203 |
49189
|
alessia.ba
|
return resource.getProperty(RDFS.label).getString();
|
204 |
49200
|
alessia.ba
|
} else return "";
|
205 |
49189
|
alessia.ba
|
}
|
206 |
|
|
|
207 |
49200
|
alessia.ba
|
protected String getCompetenceFromRDFResource(final Resource resource) {
|
208 |
49189
|
alessia.ba
|
String comp = "";
|
209 |
49200
|
alessia.ba
|
if (resource.hasProperty(CRMpe.PP45_has_competency)) {
|
210 |
49189
|
alessia.ba
|
Resource compRes = resource.getProperty(CRMpe.PP45_has_competency).getResource();
|
211 |
49200
|
alessia.ba
|
if (compRes.hasProperty(RDFS.label))
|
212 |
49189
|
alessia.ba
|
comp = compRes.getProperty(RDFS.label).getString();
|
213 |
|
|
}
|
214 |
|
|
return comp;
|
215 |
|
|
}
|
216 |
|
|
|
217 |
49200
|
alessia.ba
|
protected String getAvailabilityFromRDFResource(final Resource resource) {
|
218 |
49189
|
alessia.ba
|
//TODO: implement this when George adds the rel to the model
|
219 |
|
|
return "";
|
220 |
|
|
}
|
221 |
|
|
}
|