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 com.google.common.base.CharMatcher;
7
import eu.dnetlib.parthenos.CRM;
8
import eu.dnetlib.parthenos.CRMpe;
9
import org.apache.commons.lang3.StringUtils;
10
import org.apache.jena.rdf.model.Resource;
11
import org.apache.jena.rdf.model.Statement;
12
import org.apache.jena.rdf.model.StmtIterator;
13
import org.apache.jena.vocabulary.RDF;
14
import org.apache.jena.vocabulary.RDFS;
15
import org.gcube.informationsystem.model.entity.facet.IdentifierFacet.IdentificationType;
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
	//final IdentificationType type
27
	public void writeP1Facet(final JsonGenerator jg, final String value) throws IOException {
28
		jg.writeStartObject();
29
		jg.writeStringField("@class", "P1_is_identified_by");
30
		jg.writeObjectFieldStart("target");
31
		jg.writeStringField("@class", "SimpleFacet");
32
		jg.writeStringField("value", value);
33
		jg.writeEndObject();
34
		jg.writeEndObject();
35
	}
36

    
37
	public void writeIdentifierFacet(final JsonGenerator jg, final String identifier) throws IOException {
38
		jg.writeStartObject();
39
		jg.writeStringField("@class", "isIdentifiedBy");
40
		jg.writeObjectFieldStart("target");
41
		jg.writeStringField("@class", "IdentifierFacet");
42
		jg.writeStringField("value", identifier);
43
		jg.writeStringField("type", IdentificationType.URI.name());
44
		jg.writeEndObject();
45
		jg.writeEndObject();
46
	}
47

    
48
	public void writeP1Facets(final JsonGenerator jg, final Resource res) throws IOException {
49
		if(res.hasProperty(CRM.P1_is_identified_by)){
50
			StmtIterator idRelsIt = res.listProperties(CRM.P1_is_identified_by);
51
			while(idRelsIt.hasNext()){
52
				Resource target = idRelsIt.nextStatement().getResource();
53
				String label = getLabelFromRDFResource(target);
54
				if(StringUtils.isNotBlank(label)) {
55
					writeP1Facet(jg, label);
56
				}
57
			}
58
		}
59
	}
60

    
61
	public void writeInfoFacet(final JsonGenerator jg, final Resource res) throws IOException {
62
		jg.writeStartObject();
63
		jg.writeStringField("@class", "consistsOf");
64
		jg.writeObjectFieldStart("target");
65
		jg.writeStringField("@class", "PE_Info_Facet");
66
		jg.writeStringField("title", getTitleFromRDFResource(res));
67
		jg.writeStringField("description", getDescriptionFromRDFResource(res));
68
		//new object comp + field value + schema="noSchema"
69
		String competence = getCompetenceFromRDFResource(res);
70
		if(StringUtils.isNotBlank(competence)) {
71
			writeValueSchema(jg, "competence", competence, "noSchema");
72
		}
73
		//TODO: uncomment this when George adds the rel to the model, see method getAvailabilityFromRDFResource below
74
		//String availability = getAvailabilityFromRDFResource(res);
75
//		if(StringUtils.isNotBlank(availability)) {
76
//			writeValueSchema(jg, "availability", availability, "noSchema");
77
//		}
78
		jg.writeEndObject();
79
		jg.writeEndObject();
80
	}
81

    
82
	protected void writeValueSchema(final JsonGenerator jg, final String fieldName, final String value, final String schema) throws IOException {
83
		jg.writeObjectFieldStart(fieldName);
84
		jg.writeStringField("value", value);
85
		jg.writeStringField("schema", schema);
86
		jg.writeEndObject();
87
	}
88

    
89
	public void writeEventFacet(final JsonGenerator jg) {
90
		//TODO: implement me. get begin/end of operation from PP42_has_declarative_time
91
	}
92

    
93
	public void writeRightsFacet(final JsonGenerator jg, final Resource res) throws IOException {
94
		//TODO: implement me. E30_Right facet extends from licenseFacet but it is not correct (textUrl is mandatory, we can't use it in Parthenos)
95
	}
96

    
97
	public void writeContactReferenceFacet(final JsonGenerator jg, final Resource actor) throws IOException {
98
		if (actor != null) {
99
			String appellation = getTitleFromRDFResource(actor);
100
			String description = getDescriptionFromRDFResource(actor);
101
			String legalAddress = "";
102
			String email = "";
103
			String website = "";
104
			String address = "";
105
			String phoneNumber = "";
106
			//more contact point per provider
107
			StmtIterator contactPointsStm = actor.listProperties(CRM.P76_has_contact_point);
108
			while (contactPointsStm.hasNext()) {
109
				Resource cp = contactPointsStm.nextStatement().getResource();
110
				//contact points can bean E45_Address or E51_Contact_Point
111
				if ((cp.hasProperty(RDF.type, CRM.E45_Address) || cp.hasProperty(RDF.type, CRM.E51_Contact_Point)) && cp.hasProperty(CRM.P2_has_type)) {
112
					Resource addressType = cp.getPropertyResourceValue(CRM.P2_has_type);
113
					String info = getLabelFromRDFResource(cp);
114
					String addressTypeLabel = getLabelFromRDFResource(addressType);
115
					switch (addressTypeLabel.toLowerCase()) {
116
					case "email":
117
						email = info;
118
						break;
119
					case "legal address":
120
						legalAddress = info;
121
						break;
122
					case "address":
123
						address = info;
124
						break;
125
					case "phone":
126
						phoneNumber = info;
127
						break;
128
					case "web":
129
						website = info;
130
						break;
131
					}
132
				}
133
			}
134
			jg.writeStartObject();
135
			jg.writeStringField("@class", "consistsOf");
136
			jg.writeObjectFieldStart("target");
137
			jg.writeStringField("@class", "PE_Contact_Reference_Facet");
138
			jg.writeStringField("appellation", appellation);
139
			jg.writeStringField("description", description);
140
			jg.writeStringField("legalAddress", legalAddress);
141
			if(StringUtils.isNotBlank(email)) jg.writeStringField("email", email);
142
			jg.writeStringField("website", website);
143
			jg.writeStringField("address", address);
144
			jg.writeStringField("phoneNumber", phoneNumber);
145
			jg.writeEndObject();
146
			jg.writeEndObject();
147
		}
148
	}
149

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

    
178
	}
179

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

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

    
196
	protected String getDescriptionFromRDFResource(final Resource resource) {
197
		if (resource.hasProperty(CRM.P3_has_note)) {
198
			return CharMatcher.javaIsoControl().removeFrom(resource.getProperty(CRM.P3_has_note).getString().replace('"', '\"'));
199
		} else return "";
200
	}
201

    
202
	protected String getLabelFromRDFResource(final Resource resource) {
203
		if (resource.hasProperty(RDFS.label)) {
204
			return CharMatcher.javaIsoControl().removeFrom(resource.getProperty(RDFS.label).getString().replace('"', '\"'));
205
		} else return "";
206
	}
207

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

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