Project

General

Profile

« Previous | Next » 

Revision 49189

Refactoring

View differences:

modules/dnet-parthenos-publisher/trunk/test/main/java/eu/dnetlib/parthenos/registry/FacetWriterTest.java
1
package eu.dnetlib.parthenos.registry;
2

  
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.StringWriter;
6

  
7
import com.fasterxml.jackson.core.JsonFactory;
8
import com.fasterxml.jackson.core.JsonGenerator;
9
import eu.dnetlib.parthenos.CRMpe;
10
import org.apache.jena.rdf.model.Model;
11
import org.apache.jena.rdf.model.ResIterator;
12
import org.apache.jena.rdf.model.Resource;
13
import org.apache.jena.vocabulary.RDF;
14
import org.junit.Assert;
15
import org.junit.Test;
16
import org.springframework.core.io.ClassPathResource;
17

  
18
/**
19
 * Created by Alessia Bardi on 02/10/2017.
20
 *
21
 * @author Alessia Bardi
22
 */
23
public class FacetWriterTest {
24

  
25
	private FacetWriter facetWriter = new FacetWriter();
26
	private static String baseURI = "http://parthenos.d4science.org/CRMext/CRMpe.rdfs/";
27
	/**
28
	 *
29
	 * Method: writeIdentifierFacet(final JsonGenerator jg, final String identifier)
30
	 *
31
	 */
32
	@Test
33
	public void testWriteIdentifierFacet() throws Exception {
34
		JsonFactory jsonFactory = new JsonFactory();
35
		StringWriter sw = new StringWriter();
36
		JsonGenerator jg = jsonFactory.createGenerator(sw);
37
		facetWriter.writeIdentifierFacet(jg, "thisIsAnIDentifier");
38
		jg.close();
39
		System.out.println(sw.toString());
40
		Assert.assertEquals("{\"@class\":\"P1_is_identified_by\",\"target\":{\"@class\":\"IdentifierFacet\",\"value\":\"thisIsAnIDentifier\",\"type\":\"URI\"}}", sw.toString());
41
	}
42

  
43

  
44
	/**
45
	 *
46
	 * Method: writeContactReferenceFacet(final JsonGenerator jg, final Resource resource)
47
	 *
48
	 */
49
	@Test
50
	public void testWriteContactReferenceFacet() throws Exception {
51
		GCubeResourceGenerator generator = new GCubeResourceGenerator();
52
		JsonFactory jsonFactory = new JsonFactory();
53
		StringWriter sw = new StringWriter();
54
		JsonGenerator jg = jsonFactory.createGenerator(sw);
55
		Model model = generator.loadBaseModel();
56
		model.read(getStream("eu/dnetlib/parthenos/registry/test.rdf"), baseURI);
57
		ResIterator iter = model.listResourcesWithProperty(RDF.type, CRMpe.PE1_Service);
58
		if(iter.hasNext()){
59
			Resource r = iter.nextResource();
60
			System.out.println("Found service "+r.getURI());
61
			facetWriter.writeContactReferenceFacet(jg, r);
62
			jg.close();
63
			System.out.println(sw.toString());
64
		}
65
		else {
66
			System.out.println("Could not find any service");
67
			throw new Exception("I was expecting some PE1_Service or subclass instances!");
68
		}
69
	}
70

  
71

  
72
	private InputStream getStream(final String classpath) throws IOException {
73
		return new ClassPathResource(classpath).getInputStream();
74
	}
75

  
76
}
modules/dnet-parthenos-publisher/trunk/test/main/java/eu/dnetlib/parthenos/registry/GCubeResourceGeneratorTest.java
2 2

  
3 3
import java.io.IOException;
4 4
import java.io.InputStream;
5
import java.io.StringWriter;
6 5

  
7
import com.fasterxml.jackson.core.JsonFactory;
8
import com.fasterxml.jackson.core.JsonGenerator;
9 6
import com.google.common.collect.Iterables;
10 7
import eu.dnetlib.parthenos.CRMpe;
11 8
import org.apache.commons.io.IOUtils;
......
22 19
 */
23 20
public class GCubeResourceGeneratorTest {
24 21

  
22
	private static String recordPath = "eu/dnetlib/parthenos/registry/sample1.rdf";
23
	private static String baseURI = "http://parthenos.d4science.org/CRMext/CRMpe.rdfs/";
24
	private static GCubeResourceGenerator generator = new GCubeResourceGenerator();
25
	private static InfModel baseModel;
25 26

  
27
	static{
28
		generator.setFacetWriter(new FacetWriter());
29
		baseModel = generator.loadBaseModel();
30
		try {
31
			baseModel.read(getStream(recordPath), baseURI);
32
			baseModel.read(getStream("eu/dnetlib/parthenos/registry/test.rdf"), baseURI);
33
		} catch (IOException e) {
34
			e.printStackTrace();
35
		}
36
	}
26 37

  
27
	private String recordPath = "eu/dnetlib/parthenos/registry/sample1.rdf";
28
	private static String baseURI = "http://parthenos.d4science.org/CRMext/CRMpe.rdfs/";
29

  
30 38
	@Test
31 39
	public void testLoadBaseModel() {
32
		GCubeResourceGenerator generator = new GCubeResourceGenerator();
33
		InfModel model = generator.loadBaseModel();
40
		GCubeResourceGenerator generator1 = new GCubeResourceGenerator();
41
		InfModel model = generator1.loadBaseModel();
34 42
		//let's snapshot to a normal model to write all statements
35 43
		Model snapshot = ModelFactory.createDefaultModel();
36 44
		snapshot.add(model);
......
38 46

  
39 47
	@Test
40 48
	public void testLoadRDFFileWithModel() throws IOException {
41
		GCubeResourceGenerator generator = new GCubeResourceGenerator();
42
		InfModel model = generator.loadBaseModel();
43 49
		//let's snapshot to a normal model to write all statements
44
		model.read(getStream(recordPath), baseURI);
45 50
		Model snapshot = ModelFactory.createDefaultModel();
46
		snapshot.add(model);
51
		snapshot.add(baseModel);
47 52
		snapshot.write(System.out);
48 53
	}
49 54

  
50
	/**
51
	 *
52
	 * Method: getGCubeER(final String rdfRecord)
53
	 *
54
	 */
55
	@Test
56
	public void testGetGCubeER() throws Exception {
57
		//TODO: Test goes here...
58
	}
59 55

  
60
	/**
61
	 *
62
	 * Method: writeIdentifierFacet(final JsonGenerator jg, final String identifier)
63
	 *
64
	 */
65 56
	@Test
66
	public void testWriteIdentifierFacet() throws Exception {
67
		GCubeResourceGenerator generator = new GCubeResourceGenerator();
68
		JsonFactory jsonFactory = new JsonFactory();
69
		StringWriter sw = new StringWriter();
70
		JsonGenerator jg = jsonFactory.createGenerator(sw);
71
		generator.writeIdentifierFacet(jg, "thisIsAnIDentifier");
72
		jg.close();
73
		System.out.println(sw.toString());
74
		Assert.assertEquals("{\"@class\":\"P1_is_identified_by\",\"target\":{\"@class\":\"IdentifierFacet\",\"value\":\"thisIsAnIDentifier\",\"type\":\"URI\"}}", sw.toString());
75
	}
76

  
77

  
78

  
79
	/**
80
	 *
81
	 * Method: writeInfoFacet(final JsonGenerator jg, final String title, final String description, final String competence, final String availability)
82
	 *
83
	 */
84
	@Test
85
	public void testWriteInfoFacet() throws Exception {
86
		GCubeResourceGenerator generator = new GCubeResourceGenerator();
87
		JsonFactory jsonFactory = new JsonFactory();
88
		StringWriter sw = new StringWriter();
89
		JsonGenerator jg = jsonFactory.createGenerator(sw);
90
		generator.writeInfoFacet(jg, "x", "y", "z", "");
91
		jg.close();
92
		System.out.println(sw.toString());
93
	}
94

  
95
	/**
96
	 *
97
	 * Method: writeEventFacet(final JsonGenerator jg)
98
	 *
99
	 */
100
	@Test
101
	public void testWriteEventFacet() throws Exception {
102
		//TODO: Test goes here...
103
	}
104

  
105
	/**
106
	 *
107
	 * Method: writeRightsFacet(final JsonGenerator jg, final Resource res)
108
	 *
109
	 */
110
	@Test
111
	public void testWriteRightsFacet() throws Exception {
112
		//TODO: Test goes here...
113
	}
114

  
115
	/**
116
	 *
117
	 * Method: writeContactReferenceFacet(final JsonGenerator jg, final Resource resource)
118
	 *
119
	 */
120
	@Test
121
	public void testWriteContactReferenceFacet() throws Exception {
122
		GCubeResourceGenerator generator = new GCubeResourceGenerator();
123
		JsonFactory jsonFactory = new JsonFactory();
124
		StringWriter sw = new StringWriter();
125
		JsonGenerator jg = jsonFactory.createGenerator(sw);
126
		Model model = generator.loadBaseModel();
127
		model.read(getStream("eu/dnetlib/parthenos/registry/test.rdf"), baseURI);
128
		ResIterator iter = model.listResourcesWithProperty(RDF.type, CRMpe.PE1_Service);
129
		if(iter.hasNext()){
130
			Resource r = iter.nextResource();
131
			System.out.println("Found service "+r.getURI());
132
			generator.writeContactReferenceFacet(jg, r);
133
			jg.close();
134
			System.out.println(sw.toString());
135
		}
136
		else {
137
			System.out.println("Could not find any service");
138
			throw new Exception("I was expceting some PE1_Service or subclass instances!");
139
		}
140
	}
141

  
142
	@Test
143 57
	public void testRules() throws IOException {
144
		GCubeResourceGenerator generator = new GCubeResourceGenerator();
145
		InfModel model = generator.loadBaseModel();
146
		model.read(getStream("eu/dnetlib/parthenos/registry/test.rdf"), baseURI);
147
		StmtIterator it = model.listStatements(new SimpleSelector(ResourceFactory.createResource("uuid:AAAH"), RDF.type, (Resource) null));
58
		StmtIterator it = baseModel.listStatements(new SimpleSelector(ResourceFactory.createResource("uuid:AAAH"), RDF.type, (Resource) null));
148 59
		while(it.hasNext()){
149 60
			System.out.println(it.nextStatement().toString());
150 61
		}
151 62
		Resource classHS = ResourceFactory.createResource(CRMpe.NS+"PE1_Service");
152
		StmtIterator it2 = model.listStatements(classHS, RDF.type, (Resource) null);
63
		StmtIterator it2 = baseModel.listStatements(classHS, RDF.type, (Resource) null);
153 64
		while(it2.hasNext()){
154 65
			System.out.println(it2.nextStatement().toString());
155 66
		}
156 67
	}
157 68

  
158

  
159 69
	@Test
160 70
	public void testAmbiguousSpecificType() throws IOException {
161
		GCubeResourceGenerator generator = new GCubeResourceGenerator();
162
		InfModel model = generator.loadBaseModel();
163
		model.read(getStream("eu/dnetlib/parthenos/registry/test.rdf"), baseURI);
164
		Resource res = model.getResource("uuid:AAAH");
71
		Resource res = baseModel.getResource("uuid:AAAH");
165 72
		Resource specificType = generator.findSpecificType(res, CRMpe.PE1_Service);
166 73
		System.out.println(specificType.getURI());
167 74
		Assert.assertEquals(CRMpe.PE1_Service.getURI(), specificType.getURI());
......
175 82
	@Test
176 83
	public void testJsonGeneration2() throws IOException {
177 84
		testJsonGeneration(recordPath, 0);
178

  
179

  
180 85
	}
181 86

  
182 87
	private void testJsonGeneration(final String recordClasspath, final int minCount) throws IOException {
183
		GCubeResourceGenerator generator = new GCubeResourceGenerator();
184 88
		Iterable<String> it = generator.getGCubeER(getString(recordClasspath));
185 89
		int count = Iterables.size(it);
186 90
		System.out.println(it);
187 91
		System.out.println(count);
188 92
		Assert.assertTrue(count > minCount);
189 93
	}
190
	/**
191
	 *
192
	 * Method: getTitleFromRDFResource(final Resource resource)
193
	 *
194
	 */
195
	@Test
196
	public void testGetTitleFromRDFResource() throws Exception {
197
		//TODO: Test goes here...
198
	}
199 94

  
200
	/**
201
	 *
202
	 * Method: getDescriptionFromRDFResource(final Resource resource)
203
	 *
204
	 */
205
	@Test
206
	public void testGetDescriptionFromRDFResource() throws Exception {
207
		//TODO: Test goes here...
208
	}
209

  
210
	/**
211
	 *
212
	 * Method: getCompetenceFromRDFResource(final Resource resource)
213
	 *
214
	 */
215
	@Test
216
	public void testGetCompetenceFromRDFResource() throws Exception {
217
		//TODO: Test goes here...
218
	}
219

  
220
	/**
221
	 *
222
	 * Method: getAvailabilityFromRDFResource(final Resource resource)
223
	 *
224
	 */
225
	@Test
226
	public void testGetAvailabilityFromRDFResource() throws Exception {
227
		//TODO: Test goes here...
228
	}
229

  
230
	@Test
231
	public void testJackson() throws IOException {
232
		JsonFactory jsonFactory = new JsonFactory();
233
		StringWriter sw = new StringWriter();
234
		JsonGenerator jgen = jsonFactory.createGenerator(sw);
235
		jgen.writeStartObject();
236
		jgen.writeNumberField("id", 1);
237
		jgen.writeArrayFieldStart("array");
238
		jgen.writeStartObject();
239
		jgen.writeStringField("itemName", "theName");
240
		jgen.writeNumberField("owner", 3);
241
		jgen.writeEndObject();
242
		jgen.writeStartObject();
243
		jgen.writeStringField("itemName", "theName2");
244
		jgen.writeNumberField("owner", 8);
245
		jgen.writeEndObject();
246
		jgen.writeEndArray();
247
		jgen.writeEndObject();
248

  
249
		jgen.close();
250
		//sw.flush();
251
		System.out.println(sw.getBuffer().toString());
252
	}
253

  
254
	@Test
255
	public void testJacksonEmbeddedObject() throws IOException {
256
		JsonFactory jsonFactory = new JsonFactory();
257
		StringWriter sw = new StringWriter();
258
		JsonGenerator jgen = jsonFactory.createGenerator(sw);
259
		jgen.writeStartObject();
260
		jgen.writeNumberField("id", 1);
261
		jgen.writeObjectFieldStart("embeddedObj");
262
		jgen.writeStringField("itemName", "theName");
263
		jgen.writeNumberField("owner", 3);
264
		jgen.writeEndObject();
265
		jgen.writeEndObject();
266

  
267
		jgen.close();
268
		System.out.println(sw.getBuffer().toString());
269
	}
270

  
271 95
	private String getString(final String classpath) {
272 96
		try {
273 97
			final ClassPathResource resource = new ClassPathResource(classpath);
......
277 101
		}
278 102
	}
279 103

  
280
	private InputStream getStream(final String classpath) throws IOException {
104
	private static InputStream getStream(final String classpath) throws IOException {
281 105
		return new ClassPathResource(classpath).getInputStream();
282 106
	}
283 107

  
modules/dnet-parthenos-publisher/trunk/test/main/java/eu/dnetlib/parthenos/registry/JacksonTest.java
1
package eu.dnetlib.parthenos.registry;
2

  
3
import java.io.IOException;
4
import java.io.StringWriter;
5

  
6
import com.fasterxml.jackson.core.JsonFactory;
7
import com.fasterxml.jackson.core.JsonGenerator;
8
import org.junit.Test;
9

  
10
/**
11
 * Created by Alessia Bardi on 02/10/2017.
12
 *
13
 * @author Alessia Bardi
14
 */
15
public class JacksonTest {
16

  
17
	@Test
18
	public void testJackson() throws IOException {
19
		JsonFactory jsonFactory = new JsonFactory();
20
		StringWriter sw = new StringWriter();
21
		JsonGenerator jgen = jsonFactory.createGenerator(sw);
22
		jgen.writeStartObject();
23
		jgen.writeNumberField("id", 1);
24
		jgen.writeArrayFieldStart("array");
25
		jgen.writeStartObject();
26
		jgen.writeStringField("itemName", "theName");
27
		jgen.writeNumberField("owner", 3);
28
		jgen.writeEndObject();
29
		jgen.writeStartObject();
30
		jgen.writeStringField("itemName", "theName2");
31
		jgen.writeNumberField("owner", 8);
32
		jgen.writeEndObject();
33
		jgen.writeEndArray();
34
		jgen.writeEndObject();
35

  
36
		jgen.close();
37
		//sw.flush();
38
		System.out.println(sw.getBuffer().toString());
39
	}
40

  
41
	@Test
42
	public void testJacksonEmbeddedObject() throws IOException {
43
		JsonFactory jsonFactory = new JsonFactory();
44
		StringWriter sw = new StringWriter();
45
		JsonGenerator jgen = jsonFactory.createGenerator(sw);
46
		jgen.writeStartObject();
47
		jgen.writeNumberField("id", 1);
48
		jgen.writeObjectFieldStart("embeddedObj");
49
		jgen.writeStringField("itemName", "theName");
50
		jgen.writeNumberField("owner", 3);
51
		jgen.writeEndObject();
52
		jgen.writeEndObject();
53

  
54
		jgen.close();
55
		System.out.println(sw.getBuffer().toString());
56
	}
57
}
modules/dnet-parthenos-publisher/trunk/src/main/java/eu/dnetlib/parthenos/registry/FacetWriter.java
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.jena.rdf.model.Resource;
9
import org.apache.jena.rdf.model.Statement;
10
import org.apache.jena.rdf.model.StmtIterator;
11
import org.apache.jena.vocabulary.RDF;
12
import org.apache.jena.vocabulary.RDFS;
13
import org.gcube.informationsystem.model.entity.facet.IdentifierFacet.IdentificationType;
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
	protected void writeIdentifierFacet(final JsonGenerator jg, final String identifier) throws IOException {
25
		jg.writeStartObject();
26
		jg.writeStringField("@class", "P1_is_identified_by");
27
		jg.writeObjectFieldStart("target");
28
		jg.writeStringField("@class", "IdentifierFacet");
29
		jg.writeStringField("value", identifier);
30
		jg.writeStringField("type", IdentificationType.URI.name());
31
		jg.writeEndObject();
32
		jg.writeEndObject();
33
	}
34

  
35
	protected void writeInfoFacet(final JsonGenerator jg, final Resource res) throws IOException {
36
		jg.writeStartObject();
37
		jg.writeStringField("@class", "PE_Info_Facet");
38
		jg.writeObjectFieldStart("target");
39
		jg.writeStringField("title", getTitleFromRDFResource(res));
40
		jg.writeStringField("description", getDescriptionFromRDFResource(res));
41
		jg.writeStringField("competence", getCompetenceFromRDFResource(res));
42
		//TODO: uncomment this when George adds the rel to the model, see method getAvailabilityFromRDFResource below
43
		//jg.writeStringField("availability", getAvailabilityFromRDFResource(res));
44
		jg.writeEndObject();
45
		jg.writeEndObject();
46
	}
47

  
48

  
49
	protected void writeEventFacet(final JsonGenerator jg){
50
		//TODO: implement me. get begin/end of operation from PP42_has_declarative_time
51
	}
52

  
53
	protected void writeRightsFacet(final JsonGenerator jg, final Resource res) throws IOException {
54
		//TODO: implement me. E30_Right facet extends from licenseFacet but it is not correct (textUrl is mandatory, we can't use it in Parthenos)
55
	}
56

  
57
	protected void writeContactReferenceFacet(final JsonGenerator jg, final Resource resource) throws IOException {
58
		//PP2_provided_by
59
		final Statement s = resource.getProperty(CRMpe.PP2_provided_by);
60

  
61
		if(s!=null){
62
			String appellation = "";
63
			String description = "";
64
			String legalAddress  = "";
65
			String email ="";
66
			String website = "";
67
			String address  = "";
68
			String phoneNumber ="";
69

  
70
			Resource provider = s.getResource();
71
			if(provider != null){
72
				//more contact point per provider
73
				StmtIterator contactPointsStm = provider.listProperties(CRM.P76_has_contact_point);
74
				while(contactPointsStm.hasNext()){
75
					Resource cp = contactPointsStm.nextStatement().getResource();
76
					appellation = getLabelFromRDFResource(cp);
77
					description = getDescriptionFromRDFResource(cp);
78
					//TODO: where to find email and legaladdress, address, phoneNumber, website?
79
					jg.writeStartObject();
80
					jg.writeStringField("@class", "PE_Contact_Reference_Facet");
81
					jg.writeObjectFieldStart("target");
82
					jg.writeStringField("appellation", appellation);
83
					jg.writeStringField("description", description);
84
					jg.writeStringField("legalAddress", legalAddress);
85
					jg.writeStringField("email", email);
86
					jg.writeStringField("website", website);
87
					jg.writeStringField("address", address);
88
					jg.writeStringField("phoneNumber", phoneNumber);
89
					jg.writeEndObject();
90
					jg.writeEndObject();
91
				}
92
			}
93
		}
94

  
95
	}
96

  
97
	protected void writeDesignatedAccessPointFacet(final JsonGenerator jg, final Resource resource) throws IOException {
98
		//PP28_has_designated_access_point
99
		StmtIterator apStms = resource.listProperties(CRMpe.PP28_has_designated_access_point);
100
		while(apStms.hasNext()){
101
			String entryName = getLabelFromRDFResource(resource);
102
			//(mandatory)
103
			String endpoint= "";
104
			String protocol= "";
105
			String description= getDescriptionFromRDFResource(resource);
106
			//TODO: authorization is a ValueSchema, I do not understand how to use it and how to map it
107
			String authorization= "";
108
			Resource ap = apStms.next().getResource();
109
			endpoint = ap.getURI();
110
			//TODO: where to get protocol and authorization?
111
			jg.writeStartObject();
112
			jg.writeStringField("@class", "PE29_Access_Point");
113
			jg.writeObjectFieldStart("target");
114
			jg.writeStringField("entryName", entryName);
115
			jg.writeStringField("description", description);
116
			jg.writeStringField("endpoint", endpoint);
117
			jg.writeStringField("protocol", protocol);
118
			//TODO: authorization is a ValueSchema, I do not understand how to use it and how to map it
119
			//jg.writeStringField("authorization", authorization);
120
			jg.writeEndObject();
121
			jg.writeEndObject();
122
		}
123

  
124
	}
125

  
126

  
127
	protected String getTitleFromRDFResource(final Resource resource){
128
		String title = "";
129
		final Statement s = resource.getProperty(CRM.P1_is_identified_by);
130
		if(s != null){
131
			Resource titleRes = s.getResource();
132
			if(titleRes != null && (titleRes.hasProperty(RDF.type, CRM.E35_Title) || titleRes.hasProperty(RDF.type, CRM.E41_Appellation) )){
133
				title = getLabelFromRDFResource(titleRes);
134
			}
135
		}
136
		return title;
137
	}
138

  
139
	protected String getDescriptionFromRDFResource(final Resource resource){
140
		if(resource.hasProperty(CRM.P3_has_note)){
141
			return resource.getProperty(CRM.P3_has_note).getString();
142
		}
143
		else return "";
144
	}
145

  
146
	protected String getLabelFromRDFResource(final Resource resource){
147
		if(resource.hasProperty(RDFS.label)){
148
			return resource.getProperty(RDFS.label).getString();
149
		}
150
		else return "";
151
	}
152

  
153
	protected String getCompetenceFromRDFResource(final Resource resource){
154
		String comp = "";
155
		if(resource.hasProperty(CRMpe.PP45_has_competency)){
156
			Resource compRes = resource.getProperty(CRMpe.PP45_has_competency).getResource();
157
			if(compRes.hasProperty(RDFS.label))
158
				comp = compRes.getProperty(RDFS.label).getString();
159
		}
160
		return comp;
161
	}
162

  
163
	protected String getAvailabilityFromRDFResource(final Resource resource){
164
		//TODO: implement this when George adds the rel to the model
165
		return "";
166
	}
167
}
modules/dnet-parthenos-publisher/trunk/src/main/java/eu/dnetlib/parthenos/registry/GCubeResourceGenerator.java
22 22
import org.apache.jena.assembler.exceptions.AmbiguousSpecificTypeException;
23 23
import org.apache.jena.ontology.OntModel;
24 24
import org.apache.jena.ontology.OntModelSpec;
25
import org.apache.jena.rdf.model.*;
25
import org.apache.jena.rdf.model.InfModel;
26
import org.apache.jena.rdf.model.ModelFactory;
27
import org.apache.jena.rdf.model.ResIterator;
28
import org.apache.jena.rdf.model.Resource;
26 29
import org.apache.jena.vocabulary.RDF;
27
import org.apache.jena.vocabulary.RDFS;
28
import org.gcube.informationsystem.model.entity.facet.IdentifierFacet.IdentificationType;
30
import org.springframework.beans.factory.annotation.Autowired;
29 31
import org.springframework.stereotype.Component;
30 32

  
31 33
/**
......
40 42

  
41 43
	private OntModel baseModel;
42 44

  
45
	@Autowired
46
	private FacetWriter facetWriter;
47

  
43 48
	public GCubeResourceGenerator() {
44 49
		final String url = "http://www.w3.org/TR/REC-rdf-syntax/example14.nt";
45 50
		baseModel = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_TRANS_INF);
......
90 95
				jg.writeArrayFieldStart("consistsOf");
91 96
				//list of facets
92 97

  
93
				writeIdentifierFacet(jg, resourceURI);
94
				writeInfoFacet(jg, getTitleFromRDFResource(res), getDescriptionFromRDFResource(res), getCompetenceFromRDFResource(res),
95
						getAvailabilityFromRDFResource(res));
96
				writeEventFacet(jg);
97
				writeRightsFacet(jg, res);
98
				writeContactReferenceFacet(jg, res);
99
				writeDesignatedAccessPointFacet(jg, res);
98
				facetWriter.writeIdentifierFacet(jg, resourceURI);
99
				facetWriter.writeInfoFacet(jg, res);
100
				facetWriter.writeEventFacet(jg);
101
				facetWriter.writeRightsFacet(jg, res);
102
				facetWriter.writeContactReferenceFacet(jg, res);
103
				facetWriter.writeDesignatedAccessPointFacet(jg, res);
100 104

  
101 105
				jg.writeEndArray();
102 106

  
103 107
				// ******* RELATIONSHIPS *****//
108
				jg.writeArrayFieldStart("isRelatedto");
104 109

  
110
				jg.writeEndArray();
111

  
112

  
105 113
				jg.writeEndObject();
106 114
				jg.close();
107 115
				String json = sw.toString();
......
130 138
		}
131 139
		return specType;
132 140
	}
133
	/*
134
	{"@class": "IsIdentifiedBy",
135
			"target": {
136
				"value": "http://pippo",
137
				"type:'URL",
138
				"@class": "IdentifierFacet",
139
			}
140
	}
141
	 */
142
	protected void writeIdentifierFacet(final JsonGenerator jg, final String identifier) throws IOException {
143
		jg.writeStartObject();
144
		jg.writeStringField("@class", "P1_is_identified_by");
145
		jg.writeObjectFieldStart("target");
146
		jg.writeStringField("@class", "IdentifierFacet");
147
		jg.writeStringField("value", identifier);
148
		jg.writeStringField("type", IdentificationType.URI.name());
149
		jg.writeEndObject();
150
		jg.writeEndObject();
151
	}
152 141

  
153
	protected void writeInfoFacet(final JsonGenerator jg, final String title, final String description, final String competence, final String availability) throws IOException {
154
		jg.writeStartObject();
155
		jg.writeStringField("@class", "PE_Info_Facet");
156
		jg.writeObjectFieldStart("target");
157
		jg.writeStringField("title", title);
158
		jg.writeStringField("description", description);
159
		jg.writeStringField("competence", competence);
160
		//TODO: uncomment this when George adds the rel to the model, see method getAvailabilityFromRDFResource below
161
		//jg.writeStringField("availability", availability);
162
		jg.writeEndObject();
163
		jg.writeEndObject();
142
	public FacetWriter getFacetWriter() {
143
		return facetWriter;
164 144
	}
165 145

  
166

  
167
	protected void writeEventFacet(final JsonGenerator jg){
168
		//TODO: implement me. get begin/end of operation from PP42_has_declarative_time
146
	public void setFacetWriter(final FacetWriter facetWriter) {
147
		this.facetWriter = facetWriter;
169 148
	}
170

  
171
	protected void writeRightsFacet(final JsonGenerator jg, final Resource res) throws IOException {
172
		//TODO: implement me. E30_Right facet extends from licenseFacet but it is not correct (textUrl is mandatory, we can't use it in Parthenos)
173
	}
174

  
175
	protected void writeContactReferenceFacet(final JsonGenerator jg, final Resource resource) throws IOException {
176
		//PP2_provided_by
177
		final Statement s = resource.getProperty(CRMpe.PP2_provided_by);
178

  
179
		if(s!=null){
180
			String appellation = "";
181
			String description = "";
182
			String legalAddress  = "";
183
			String email ="";
184
			String website = "";
185
			String address  = "";
186
			String phoneNumber ="";
187

  
188
			Resource provider = s.getResource();
189
			if(provider != null){
190
				//more contact point per provider
191
				StmtIterator contactPointsStm = provider.listProperties(CRM.P76_has_contact_point);
192
				while(contactPointsStm.hasNext()){
193
					Resource cp = contactPointsStm.nextStatement().getResource();
194
					appellation = getLabelFromRDFResource(cp);
195
					description = getDescriptionFromRDFResource(cp);
196
					//TODO: where to find email and legaladdress, address, phoneNumber, website?
197
					jg.writeStartObject();
198
					jg.writeStringField("@class", "PE_Contact_Reference_Facet");
199
					jg.writeObjectFieldStart("target");
200
					jg.writeStringField("appellation", appellation);
201
					jg.writeStringField("description", description);
202
					jg.writeStringField("legalAddress", legalAddress);
203
					jg.writeStringField("email", email);
204
					jg.writeStringField("website", website);
205
					jg.writeStringField("address", address);
206
					jg.writeStringField("phoneNumber", phoneNumber);
207
					jg.writeEndObject();
208
					jg.writeEndObject();
209
				}
210
			}
211
		}
212

  
213
	}
214

  
215
	protected void writeDesignatedAccessPointFacet(final JsonGenerator jg, final Resource resource) throws IOException {
216
		//PP28_has_designated_access_point
217
		StmtIterator apStms = resource.listProperties(CRMpe.PP28_has_designated_access_point);
218
		while(apStms.hasNext()){
219
			String entryName = getLabelFromRDFResource(resource);
220
			//(mandatory)
221
			String endpoint= "";
222
			String protocol= "";
223
			String description= getDescriptionFromRDFResource(resource);
224
			//TODO: authorization is a ValueSchema, I do not understand how to use it and how to map it
225
			String authorization= "";
226
			Resource ap = apStms.next().getResource();
227
			endpoint = ap.getURI();
228
			//TODO: where to get protocol and authorization?
229
			jg.writeStartObject();
230
			jg.writeStringField("@class", "PE29_Access_Point");
231
			jg.writeObjectFieldStart("target");
232
			jg.writeStringField("entryName", entryName);
233
			jg.writeStringField("description", description);
234
			jg.writeStringField("endpoint", endpoint);
235
			jg.writeStringField("protocol", protocol);
236
			//TODO: authorization is a ValueSchema, I do not understand how to use it and how to map it
237
			//jg.writeStringField("authorization", authorization);
238
			jg.writeEndObject();
239
			jg.writeEndObject();
240

  
241
		}
242

  
243
	}
244

  
245

  
246

  
247

  
248

  
249
	protected String getTitleFromRDFResource(final Resource resource){
250
		String title = "";
251
		final Statement s = resource.getProperty(CRM.P1_is_identified_by);
252
		if(s != null){
253
			Resource titleRes = s.getResource();
254
			if(titleRes != null && (titleRes.hasProperty(RDF.type, CRM.E35_Title) || titleRes.hasProperty(RDF.type, CRM.E41_Appellation) )){
255
				title = getLabelFromRDFResource(titleRes);
256
			}
257
		}
258
		return title;
259
	}
260

  
261
	protected String getDescriptionFromRDFResource(final Resource resource){
262
		if(resource.hasProperty(CRM.P3_has_note)){
263
			return resource.getProperty(CRM.P3_has_note).getString();
264
		}
265
		else return "";
266
	}
267

  
268
	protected String getLabelFromRDFResource(final Resource resource){
269
		if(resource.hasProperty(RDFS.label)){
270
			return resource.getProperty(RDFS.label).getString();
271
		}
272
		else return "";
273
	}
274

  
275
	protected String getCompetenceFromRDFResource(final Resource resource){
276
		String comp = "";
277
		if(resource.hasProperty(CRMpe.PP45_has_competency)){
278
			Resource compRes = resource.getProperty(CRMpe.PP45_has_competency).getResource();
279
			if(compRes.hasProperty(RDFS.label))
280
				comp = compRes.getProperty(RDFS.label).getString();
281
		}
282
		return comp;
283
	}
284

  
285
	protected String getAvailabilityFromRDFResource(final Resource resource){
286
		//TODO: implement this when George adds the rel to the model
287
		return "";
288
	}
289

  
290

  
291

  
292

  
293

  
294

  
295

  
296

  
297 149
}

Also available in: Unified diff