Revision 2e0a90f1
Added by Patrick Plitzner over 5 years ago
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/in/StructureTreeOwlImport.java | ||
---|---|---|
12 | 12 |
import java.util.ArrayList; |
13 | 13 |
import java.util.Collection; |
14 | 14 |
import java.util.HashSet; |
15 |
import java.util.List; |
|
16 | 15 |
import java.util.Set; |
17 | 16 |
import java.util.UUID; |
18 | 17 |
|
19 | 18 |
import org.springframework.stereotype.Component; |
20 | 19 |
|
21 | 20 |
import com.hp.hpl.jena.rdf.model.Model; |
22 |
import com.hp.hpl.jena.rdf.model.ModelFactory; |
|
23 |
import com.hp.hpl.jena.rdf.model.Property; |
|
24 | 21 |
import com.hp.hpl.jena.rdf.model.ResIterator; |
25 | 22 |
import com.hp.hpl.jena.rdf.model.Resource; |
26 | 23 |
import com.hp.hpl.jena.rdf.model.Statement; |
27 | 24 |
|
25 |
import eu.etaxonomy.cdm.common.CdmUtils; |
|
28 | 26 |
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper; |
29 | 27 |
import eu.etaxonomy.cdm.io.common.CdmImportBase; |
30 |
import eu.etaxonomy.cdm.io.descriptive.owl.OwlConstants; |
|
31 | 28 |
import eu.etaxonomy.cdm.model.common.IdentifiableSource; |
32 | 29 |
import eu.etaxonomy.cdm.model.common.Language; |
33 | 30 |
import eu.etaxonomy.cdm.model.description.Feature; |
... | ... | |
52 | 49 |
|
53 | 50 |
private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(StructureTreeOwlImport.class); |
54 | 51 |
|
55 |
private Property propHasSubStructure; |
|
56 |
private Property propHasRepresentation; |
|
57 |
private Property propHasRootNode; |
|
58 |
private Property propUuid; |
|
59 |
private Property propUri; |
|
60 |
private Property propLabel; |
|
61 |
private Property propLabelAbbrev; |
|
62 |
private Property propLanguage; |
|
63 |
private Property propLanguageUuid; |
|
64 |
private Property propIsA; |
|
65 |
private Property propType; |
|
66 |
private Property propDescription; |
|
67 | 52 |
|
68 | 53 |
@Override |
69 | 54 |
protected boolean doCheck(StructureTreeOwlImportState state) { |
... | ... | |
75 | 60 |
public void doInvoke(StructureTreeOwlImportState state) { |
76 | 61 |
URI source = state.getConfig().getSource(); |
77 | 62 |
|
78 |
Model model = ModelFactory.createDefaultModel(); |
|
79 |
propHasSubStructure = model.createProperty(OwlConstants.PROPERTY_HAS_SUBSTRUCTURE); |
|
80 |
propHasRepresentation = model.createProperty(OwlConstants.PROPERTY_HAS_REPRESENTATION); |
|
81 |
propHasRootNode = model.createProperty(OwlConstants.PROPERTY_HAS_ROOT_NODE); |
|
82 |
propUuid = model.createProperty(OwlConstants.PROPERTY_UUID); |
|
83 |
propUri = model.createProperty(OwlConstants.PROPERTY_URI); |
|
84 |
propLabel = model.createProperty(OwlConstants.PROPERTY_LABEL); |
|
85 |
propLabelAbbrev = model.createProperty(OwlConstants.PROPERTY_LABEL_ABBREV); |
|
86 |
propLanguage = model.createProperty(OwlConstants.PROPERTY_LANGUAGE); |
|
87 |
propLanguageUuid = model.createProperty(OwlConstants.PROPERTY_LANGUAGE_UUID); |
|
88 |
propIsA = model.createProperty(OwlConstants.PROPERTY_IS_A); |
|
89 |
propType = model.createProperty(OwlConstants.PROPERTY_TYPE); |
|
90 |
propDescription = model.createProperty(OwlConstants.PROPERTY_DESCRIPTION); |
|
91 |
|
|
92 |
model.read(source.toString()); |
|
93 |
|
|
94 |
List<FeatureTree> featureTrees = new ArrayList<>(); |
|
63 |
state.getModel().read(source.toString()); |
|
64 |
|
|
95 | 65 |
//get all trees |
96 |
ResIterator iterator = model.listResourcesWithProperty(propHasRootNode);
|
|
66 |
ResIterator iterator = state.getModel().listResourcesWithProperty(StructureTreeOwlImportState.propHasRootNode);
|
|
97 | 67 |
while(iterator.hasNext()){ |
98 | 68 |
Resource tree = iterator.next(); |
99 |
String type = tree.getProperty(propType).getString(); |
|
69 |
String type = tree.getProperty(StructureTreeOwlImportState.propType).getString();
|
|
100 | 70 |
FeatureTree featureTree = FeatureTree.NewInstance(TermType.getByKey(type)); |
101 |
featureTree.setTitleCache(tree.getProperty(propLabel).getString(), true); |
|
102 |
|
|
103 |
Resource rootNode = tree.getProperty(propHasRootNode).getResource(); |
|
104 |
rootNode.listProperties(propHasSubStructure).forEachRemaining(prop->createNode(featureTree.getRoot(), prop, featureTree.getTitleCache(), model, state)); |
|
71 |
featureTree.setTitleCache(tree.getProperty(StructureTreeOwlImportState.propLabel).getString(), true); |
|
105 | 72 |
|
106 |
featureTrees.add(featureTree); |
|
73 |
Resource rootNode = tree.getProperty(StructureTreeOwlImportState.propHasRootNode).getResource(); |
|
74 |
rootNode.listProperties(StructureTreeOwlImportState.propHasSubStructure).forEachRemaining(prop->createNode(featureTree.getRoot(), prop, featureTree.getTitleCache(), state.getModel(), state)); |
|
107 | 75 |
|
108 | 76 |
getFeatureTreeService().save(featureTree); |
109 | 77 |
} |
... | ... | |
112 | 80 |
private void createNode(FeatureNode parent, Statement nodeStatement, String treeLabel, Model model, StructureTreeOwlImportState state) { |
113 | 81 |
Resource nodeResource = model.createResource(nodeStatement.getObject().toString()); |
114 | 82 |
|
115 |
TermType termType = TermType.getByKey(nodeResource.getProperty(propType).getString()); |
|
116 |
String uriString = nodeResource.hasProperty(propUri)?nodeResource.getProperty(propUri).toString():null; |
|
117 |
|
|
118 | 83 |
Collection<TermDto> dtos = new ArrayList<>(); |
119 | 84 |
|
120 |
// check representations if term already exists
|
|
85 |
// import representations
|
|
121 | 86 |
Set<Representation> representations = new HashSet<>(); |
122 |
nodeResource.listProperties(propHasRepresentation).forEachRemaining(r->representations.add(createRepresentation(r, model))); |
|
87 |
nodeResource.listProperties(StructureTreeOwlImportState.propHasRepresentation).forEachRemaining(r->representations.add(createRepresentation(r, model)));
|
|
123 | 88 |
if(representations.isEmpty()){ |
124 |
logger.error("No representations found for term: "+nodeResource.getProperty(propUuid)); |
|
89 |
logger.error("No representations found for term: "+nodeResource.getProperty(StructureTreeOwlImportState.propUuid));
|
|
125 | 90 |
return; |
126 | 91 |
} |
127 | 92 |
|
128 |
DefinedTermBase term = null; |
|
129 |
String termLabel = null; |
|
130 |
for (Representation representation : representations) { |
|
131 |
termLabel = representation.getLabel(); |
|
132 |
if(uriString!=null){ |
|
133 |
URI uri = URI.create(uriString); |
|
134 |
dtos = getTermService().findByUriAsDto(uri, termLabel, termType); |
|
135 |
} |
|
136 |
else{ |
|
137 |
dtos = getTermService().findByTitleAsDto(termLabel, termType); |
|
138 |
} |
|
139 |
if(dtos.size()>1){ |
|
140 |
logger.warn("More than one term was found for: "+termLabel+"\nUsing the first one found"); |
|
141 |
} |
|
142 |
if(dtos.size()>=1){ |
|
143 |
term = getTermService().load(dtos.iterator().next().getUuid()); |
|
144 |
} |
|
145 |
if(term!=null){ |
|
146 |
break; |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
93 |
DefinedTermBase term = getTermService().load(dtos.iterator().next().getUuid()); |
|
150 | 94 |
if(term==null){ |
95 |
TermType termType = TermType.getByKey(nodeResource.getProperty(StructureTreeOwlImportState.propType).getString()); |
|
151 | 96 |
// create new term |
152 | 97 |
if(termType.equals(TermType.Feature)){ |
153 | 98 |
term = Feature.NewInstance(); |
... | ... | |
161 | 106 |
term.addRepresentation(representation); |
162 | 107 |
} |
163 | 108 |
} |
164 |
IdentifiableSource importSource = IdentifiableSource.NewDataImportInstance(termLabel); |
|
109 |
String uriString = nodeResource.hasProperty(StructureTreeOwlImportState.propUri)?nodeResource.getProperty(StructureTreeOwlImportState.propUri).toString():null; |
|
110 |
if(CdmUtils.isNotBlank(uriString)){ |
|
111 |
term.setUri(URI.create(uriString)); |
|
112 |
} |
|
113 |
|
|
114 |
IdentifiableSource importSource = IdentifiableSource.NewDataImportInstance("Import of term tree "+treeLabel); |
|
165 | 115 |
importSource.setCitation(state.getConfig().getSourceReference()); |
166 | 116 |
term.addSource(importSource); |
167 | 117 |
getTermService().save(term); |
... | ... | |
173 | 123 |
|
174 | 124 |
FeatureNode<?> childNode = parent.addChild(term); |
175 | 125 |
|
176 |
nodeResource.listProperties(propHasSubStructure).forEachRemaining(prop->createNode(childNode, prop, treeLabel, model, state)); |
|
126 |
nodeResource.listProperties(StructureTreeOwlImportState.propHasSubStructure).forEachRemaining(prop->createNode(childNode, prop, treeLabel, model, state));
|
|
177 | 127 |
} |
178 | 128 |
|
179 | 129 |
private Representation createRepresentation(Statement repr, Model model) { |
180 | 130 |
Resource repsentationResource = model.createResource(repr.getObject().toString()); |
181 | 131 |
|
182 |
String languageLabel = repsentationResource.getProperty(propLanguage).getString(); |
|
183 |
UUID languageUuid = UUID.fromString(repsentationResource.getProperty(propLanguageUuid).getString()); |
|
132 |
String languageLabel = repsentationResource.getProperty(StructureTreeOwlImportState.propLanguage).getString();
|
|
133 |
UUID languageUuid = UUID.fromString(repsentationResource.getProperty(StructureTreeOwlImportState.propLanguageUuid).getString());
|
|
184 | 134 |
DefinedTermBase termBase = getTermService().load(languageUuid); |
185 | 135 |
Language language = null; |
186 | 136 |
if(termBase.isInstanceOf(Language.class)){ |
... | ... | |
193 | 143 |
language = Language.getDefaultLanguage(); |
194 | 144 |
} |
195 | 145 |
|
196 |
String abbreviatedLabel = repsentationResource.hasProperty(propLabelAbbrev)?repsentationResource.getProperty(propLabelAbbrev).toString():null;
|
|
197 |
String label = repsentationResource.getProperty(propLabel).getString(); |
|
198 |
String description = repsentationResource.hasProperty(propDescription)?repsentationResource.getProperty(propDescription).getString():null;
|
|
146 |
String abbreviatedLabel = repsentationResource.hasProperty(StructureTreeOwlImportState.propLabelAbbrev)?repsentationResource.getProperty(StructureTreeOwlImportState.propLabelAbbrev).toString():null;
|
|
147 |
String label = repsentationResource.getProperty(StructureTreeOwlImportState.propLabel).getString();
|
|
148 |
String description = repsentationResource.hasProperty(StructureTreeOwlImportState.propDescription)?repsentationResource.getProperty(StructureTreeOwlImportState.propDescription).getString():null;
|
|
199 | 149 |
Representation representation = Representation.NewInstance(description, label, abbreviatedLabel, language); |
200 | 150 |
|
201 | 151 |
return representation; |
Also available in: Unified diff
ref #8248 Move rdf model to import state