Project

General

Profile

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.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
	//final IdentificationType type
25
	public void writeP1Facet(final JsonGenerator jg, final String value) throws IOException {
26
		jg.writeStartObject();
27
		jg.writeStringField("@class", "P1_is_identified_by");
28
		jg.writeObjectFieldStart("target");
29
		jg.writeStringField("@class", "IdentifierFacet");
30
		jg.writeStringField("value", value);
31
		//jg.writeStringField("type", type.name());
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", "PE_Contact_Reference_Facet");
135
			jg.writeObjectFieldStart("target");
136
			jg.writeStringField("appellation", appellation);
137
			jg.writeStringField("description", description);
138
			jg.writeStringField("legalAddress", legalAddress);
139
			jg.writeStringField("email", email);
140
			jg.writeStringField("website", website);
141
			jg.writeStringField("address", address);
142
			jg.writeStringField("phoneNumber", phoneNumber);
143
			jg.writeEndObject();
144
			jg.writeEndObject();
145
		}
146
	}
147

    
148
	public void writeDesignatedAccessPointFacet(final JsonGenerator jg, final Resource resource) throws IOException {
149
		//PP28_has_designated_access_point
150
		StmtIterator apStms = resource.listProperties(CRMpe.PP28_has_designated_access_point);
151
		while (apStms.hasNext()) {
152
			String entryName = getLabelFromRDFResource(resource);
153
			//(mandatory)
154
			String endpoint = "";
155
			String protocol = "";
156
			String description = getDescriptionFromRDFResource(resource);
157
			//TODO: authorization is a ValueSchema, I do not understand how to use it and how to map it
158
			String authorization = "";
159
			Resource ap = apStms.next().getResource();
160
			endpoint = ap.getURI();
161
			//TODO: where to get protocol and authorization?
162
			jg.writeStartObject();
163
			jg.writeStringField("@class", "PE29_Access_Point");
164
			jg.writeObjectFieldStart("target");
165
			jg.writeStringField("entryName", entryName);
166
			jg.writeStringField("description", description);
167
			jg.writeStringField("endpoint", endpoint);
168
			jg.writeStringField("protocol", protocol);
169
			//TODO: authorization is a ValueSchema, I do not understand how to use it and how to map it
170
			//jg.writeStringField("authorization", authorization);
171
			jg.writeEndObject();
172
			jg.writeEndObject();
173
		}
174

    
175
	}
176

    
177
	public void writeTemporalCoverageFacet(final JsonGenerator jg, final Resource resource) {
178
		//TODO: CoverageFacet wants a value and a mandatory schema. Can't be applied to Parthenos.
179
	}
180

    
181
	protected String getTitleFromRDFResource(final Resource resource) {
182
		String title = "";
183
		final Statement s = resource.getProperty(CRM.P1_is_identified_by);
184
		if (s != null) {
185
			Resource titleRes = s.getResource();
186
			if (titleRes != null && (titleRes.hasProperty(RDF.type, CRM.E35_Title) || titleRes.hasProperty(RDF.type, CRM.E41_Appellation))) {
187
				title = getLabelFromRDFResource(titleRes);
188
			}
189
		}
190
		return title;
191
	}
192

    
193
	protected String getDescriptionFromRDFResource(final Resource resource) {
194
		if (resource.hasProperty(CRM.P3_has_note)) {
195
			return resource.getProperty(CRM.P3_has_note).getString();
196
		} else return "";
197
	}
198

    
199
	protected String getLabelFromRDFResource(final Resource resource) {
200
		if (resource.hasProperty(RDFS.label)) {
201
			return resource.getProperty(RDFS.label).getString();
202
		} else return "";
203
	}
204

    
205
	protected String getCompetenceFromRDFResource(final Resource resource) {
206
		String comp = "";
207
		if (resource.hasProperty(CRMpe.PP45_has_competency)) {
208
			Resource compRes = resource.getProperty(CRMpe.PP45_has_competency).getResource();
209
			if (compRes.hasProperty(RDFS.label))
210
				comp = compRes.getProperty(RDFS.label).getString();
211
		}
212
		return comp;
213
	}
214

    
215
	protected String getAvailabilityFromRDFResource(final Resource resource) {
216
		//TODO: implement this when George adds the rel to the model
217
		return "";
218
	}
219
}
(1-1/6)