1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.io.descriptive.owl.out;
|
10
|
|
11
|
import java.io.ByteArrayOutputStream;
|
12
|
import java.io.File;
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.springframework.stereotype.Component;
|
16
|
import org.springframework.transaction.TransactionStatus;
|
17
|
|
18
|
import com.hp.hpl.jena.rdf.model.Model;
|
19
|
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
20
|
import com.hp.hpl.jena.rdf.model.Property;
|
21
|
import com.hp.hpl.jena.rdf.model.Resource;
|
22
|
|
23
|
import eu.etaxonomy.cdm.io.common.CdmExportBase;
|
24
|
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
|
25
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
26
|
import eu.etaxonomy.cdm.model.term.FeatureNode;
|
27
|
import eu.etaxonomy.cdm.model.term.FeatureTree;
|
28
|
|
29
|
/**
|
30
|
* @author pplitzner
|
31
|
* @since Jul 3, 2017
|
32
|
*
|
33
|
*/
|
34
|
@Component
|
35
|
public class OwlExport extends CdmExportBase<OwlExportConfigurator, OwlExportState, IExportTransformer, File> {
|
36
|
|
37
|
private static final long serialVersionUID = 3197379920692366008L;
|
38
|
|
39
|
private static final String BASE_URI = "http://cybertaxonomy.eu/";
|
40
|
private static final String RESOURCE_URI = BASE_URI+"resource/";
|
41
|
private static final String PROPERTY_BASE_URI = BASE_URI+"property/";
|
42
|
private static final String NODE_BASE_URI = RESOURCE_URI+"node/";
|
43
|
|
44
|
@Override
|
45
|
protected boolean doCheck(OwlExportState state) {
|
46
|
return false;
|
47
|
}
|
48
|
|
49
|
@Override
|
50
|
protected void doInvoke(OwlExportState state) {
|
51
|
|
52
|
TransactionStatus txStatus = startTransaction(true);
|
53
|
|
54
|
|
55
|
FeatureTree featureTree = state.getConfig().getFeatureTree();
|
56
|
featureTree = getFeatureTreeService().load(featureTree.getUuid());
|
57
|
|
58
|
FeatureNode rootNode = featureTree.getRoot();
|
59
|
|
60
|
Model model = ModelFactory.createDefaultModel();
|
61
|
Property propHasSubStructure = model.createProperty(PROPERTY_BASE_URI+"hasSubStructure");
|
62
|
Property propUuid = model.createProperty(PROPERTY_BASE_URI+"uuid");
|
63
|
Property propLabel = model.createProperty(PROPERTY_BASE_URI+"label");
|
64
|
|
65
|
Resource resourceRootNode = model.createResource(NODE_BASE_URI + rootNode.getUuid().toString());
|
66
|
|
67
|
model.createResource(RESOURCE_URI+"featureTree/"+featureTree.getUuid().toString())
|
68
|
.addProperty(propUuid, featureTree.getUuid().toString())
|
69
|
.addProperty(propLabel, featureTree.getTitleCache())
|
70
|
.addProperty(model.createProperty(PROPERTY_BASE_URI + "hasRootNode"),
|
71
|
resourceRootNode
|
72
|
.addProperty(propUuid, rootNode.getUuid().toString()));
|
73
|
|
74
|
addChildNode(rootNode, resourceRootNode, propHasSubStructure, propUuid, propLabel, model);
|
75
|
|
76
|
exportStream = new ByteArrayOutputStream();
|
77
|
model.write(exportStream);
|
78
|
state.getResult().addExportData(getByteArray());
|
79
|
|
80
|
commitTransaction(txStatus);
|
81
|
}
|
82
|
|
83
|
private void addChildNode(FeatureNode node, Resource resourceNode, final Property propHasSubStructure, final Property propUuid, Property propLabel, Model model){
|
84
|
List<FeatureNode> childNodes = node.getChildNodes();
|
85
|
for (FeatureNode child : childNodes) {
|
86
|
DefinedTermBase term = child.getTerm();
|
87
|
Resource childResourceNode = model.createResource(NODE_BASE_URI+term.getUuid().toString());
|
88
|
resourceNode.addProperty(propHasSubStructure, childResourceNode
|
89
|
.addProperty(propUuid, term.getUuid().toString())
|
90
|
.addProperty(propLabel, term.getLabel())
|
91
|
);
|
92
|
addChildNode(child, childResourceNode, propHasSubStructure, propUuid, propLabel, model);
|
93
|
}
|
94
|
}
|
95
|
|
96
|
@Override
|
97
|
protected boolean isIgnore(OwlExportState state) {
|
98
|
return false;
|
99
|
}
|
100
|
|
101
|
}
|