ref #8248 Extract CDM entity creation to util
authorPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 27 May 2019 07:11:06 +0000 (09:11 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 27 May 2019 07:11:06 +0000 (09:11 +0200)
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/in/OwlImportUtil.java [new file with mode: 0644]
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/in/StructureTreeOwlImport.java

diff --git a/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/in/OwlImportUtil.java b/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/in/OwlImportUtil.java
new file mode 100644 (file)
index 0000000..c9c8806
--- /dev/null
@@ -0,0 +1,124 @@
+/**
+* Copyright (C) 2019 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.cdm.io.descriptive.owl.in;
+
+import java.net.URI;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.UUID;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.Resource;
+import com.hp.hpl.jena.rdf.model.Statement;
+
+import eu.etaxonomy.cdm.api.service.ITermService;
+import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
+import eu.etaxonomy.cdm.io.descriptive.owl.OwlUtil;
+import eu.etaxonomy.cdm.model.common.IdentifiableSource;
+import eu.etaxonomy.cdm.model.common.Language;
+import eu.etaxonomy.cdm.model.description.Feature;
+import eu.etaxonomy.cdm.model.term.DefinedTerm;
+import eu.etaxonomy.cdm.model.term.DefinedTermBase;
+import eu.etaxonomy.cdm.model.term.Representation;
+import eu.etaxonomy.cdm.model.term.TermType;
+import eu.etaxonomy.cdm.model.term.TermVocabulary;
+
+/**
+ * @author pplitzner
+ * @since May 26, 2019
+ *
+ */
+public class OwlImportUtil {
+
+    static DefinedTermBase createTerm(Resource termResource, ITermService termService, Model model, StructureTreeOwlImportState state){
+        TermType termType = TermType.getByKey(termResource.getProperty(OwlUtil.propType).getString());
+        DefinedTermBase term;
+        // create new term
+        if(termType.equals(TermType.Feature)){
+            term = Feature.NewInstance();
+        }
+        else{
+            term = DefinedTerm.NewInstance(termType);
+        }
+    
+        // term URI
+        String uriString = termResource.hasProperty(OwlUtil.propUri)?termResource.getProperty(OwlUtil.propUri).toString():null;
+        if(CdmUtils.isNotBlank(uriString)){
+            term.setUri(URI.create(uriString));
+        }
+    
+        // import representations
+        Set<Representation> representations = new HashSet<>();
+        termResource.listProperties(OwlUtil.propHasRepresentation).forEachRemaining(r->representations.add(OwlImportUtil.createRepresentation(termService, r, model)));
+        if(representations.isEmpty()){
+            StructureTreeOwlImport.logger.error("No representations found for term: "+termResource.getProperty(OwlUtil.propUuid));
+        }
+        representations.forEach(rep->term.addRepresentation(rep));
+    
+        IdentifiableSource importSource = IdentifiableSource.NewDataImportInstance(termResource.getURI());
+        importSource.setCitation(state.getConfig().getSourceReference());
+        term.addSource(importSource);
+    
+        return term;
+    }
+
+    static TermVocabulary createVocabulary(Resource vocabularyResource, ITermService termService, Model model, StructureTreeOwlImportState state){
+        TermType termType = TermType.getByKey(vocabularyResource.getProperty(OwlUtil.propType).getString());
+        // create new vocabulary
+        TermVocabulary vocabulary = TermVocabulary.NewInstance(termType);
+    
+        // voc URI
+        String vocUriString = vocabularyResource.hasProperty(OwlUtil.propUri)?vocabularyResource.getProperty(OwlUtil.propUri).toString():null;
+        if(CdmUtils.isNotBlank(vocUriString)){
+            vocabulary.setUri(URI.create(vocUriString));
+        }
+    
+        // voc representations
+        Set<Representation> vocRepresentations = new HashSet<>();
+        vocabularyResource.listProperties(OwlUtil.propHasRepresentation).forEachRemaining(r->vocRepresentations.add(OwlImportUtil.createRepresentation(termService, r, model)));
+        if(vocRepresentations.isEmpty()){
+            StructureTreeOwlImport.logger.error("No representations found for vocabulary: "+vocabularyResource.getProperty(OwlUtil.propUuid));
+        }
+        vocRepresentations.forEach(rep->vocabulary.addRepresentation(rep));
+    
+        IdentifiableSource importSource = IdentifiableSource.NewDataImportInstance(vocabularyResource.getURI());
+        importSource.setCitation(state.getConfig().getSourceReference());
+        vocabulary.addSource(importSource);
+    
+    
+        return vocabulary;
+    }
+
+    static Representation createRepresentation(ITermService termService, Statement repr, Model model) {
+        Resource repsentationResource = model.createResource(repr.getObject().toString());
+    
+        String languageLabel = repsentationResource.getProperty(OwlUtil.propLanguage).getString();
+        UUID languageUuid = UUID.fromString(repsentationResource.getProperty(OwlUtil.propLanguageUuid).getString());
+        DefinedTermBase termBase = termService.load(languageUuid);
+        Language language = null;
+        if(termBase.isInstanceOf(Language.class)){
+            language = HibernateProxyHelper.deproxy(termBase, Language.class);
+        }
+        if(language==null){
+            language = termService.getLanguageByLabel(languageLabel);
+        }
+        if(language==null){
+            language = Language.getDefaultLanguage();
+        }
+    
+        String abbreviatedLabel = repsentationResource.hasProperty(OwlUtil.propLabelAbbrev)?repsentationResource.getProperty(OwlUtil.propLabelAbbrev).toString():null;
+        String label = repsentationResource.getProperty(OwlUtil.propLabel).getString();
+        String description = repsentationResource.hasProperty(OwlUtil.propDescription)?repsentationResource.getProperty(OwlUtil.propDescription).getString():null;
+        Representation representation = Representation.NewInstance(description, label, abbreviatedLabel, language);
+    
+        return representation;
+    }
+
+}
index abb4dc4421f27c7733564b5c150c12a1dedf55bc..adeae60cf3cfcb509b8220aca56424e453a48719 100644 (file)
@@ -9,10 +9,6 @@
 package eu.etaxonomy.cdm.io.descriptive.owl.in;
 
 import java.net.URI;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
 import java.util.UUID;
 
 import org.springframework.stereotype.Component;
@@ -22,21 +18,13 @@ import com.hp.hpl.jena.rdf.model.ResIterator;
 import com.hp.hpl.jena.rdf.model.Resource;
 import com.hp.hpl.jena.rdf.model.Statement;
 
-import eu.etaxonomy.cdm.common.CdmUtils;
-import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.io.common.CdmImportBase;
 import eu.etaxonomy.cdm.io.descriptive.owl.OwlUtil;
-import eu.etaxonomy.cdm.model.common.IdentifiableSource;
-import eu.etaxonomy.cdm.model.common.Language;
-import eu.etaxonomy.cdm.model.description.Feature;
-import eu.etaxonomy.cdm.model.term.DefinedTerm;
 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
 import eu.etaxonomy.cdm.model.term.FeatureNode;
 import eu.etaxonomy.cdm.model.term.FeatureTree;
-import eu.etaxonomy.cdm.model.term.Representation;
 import eu.etaxonomy.cdm.model.term.TermType;
 import eu.etaxonomy.cdm.model.term.TermVocabulary;
-import eu.etaxonomy.cdm.persistence.dto.TermDto;
 
 /**
  * @author pplitzner
@@ -48,7 +36,7 @@ public class StructureTreeOwlImport extends CdmImportBase<StructureTreeOwlImport
 
     private static final long serialVersionUID = -3659780404413458511L;
 
-    private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(StructureTreeOwlImport.class);
+    static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(StructureTreeOwlImport.class);
 
 
     @Override
@@ -81,77 +69,31 @@ public class StructureTreeOwlImport extends CdmImportBase<StructureTreeOwlImport
     private void createNode(FeatureNode parent, Statement nodeStatement, String treeLabel, Model model, StructureTreeOwlImportState state) {
         Resource nodeResource = model.createResource(nodeStatement.getObject().toString());
 
-        Collection<TermDto> dtos = new ArrayList<>();
-
-        // import representations
-        Set<Representation> representations = new HashSet<>();
-        nodeResource.listProperties(OwlUtil.propHasRepresentation).forEachRemaining(r->representations.add(createRepresentation(r, model)));
-        if(representations.isEmpty()){
-            logger.error("No representations found for term: "+nodeResource.getProperty(OwlUtil.propUuid));
-            return;
+        // import term
+        Resource termResource = nodeResource.getPropertyResourceValue(OwlUtil.propHasTerm);
+        UUID termUuid = UUID.fromString(termResource.getProperty(OwlUtil.propUuid).getString());
+        DefinedTermBase term = getTermService().load(termUuid);
+        if(term==null){
+            term = OwlImportUtil.createTerm(termResource, getTermService(), model, state);
+            getTermService().saveOrUpdate(term);
         }
 
-        DefinedTermBase term = getTermService().load(dtos.iterator().next().getUuid());
-        if(term==null){
-            TermType termType = TermType.getByKey(nodeResource.getProperty(OwlUtil.propType).getString());
-            // create new term
-            if(termType.equals(TermType.Feature)){
-                term = Feature.NewInstance();
-                for (Representation representation : representations) {
-                    term.addRepresentation(representation);
-                }
-            }
-            else{
-                term = DefinedTerm.NewInstance(termType);
-                for (Representation representation : representations) {
-                    term.addRepresentation(representation);
-                }
-            }
-            String uriString = nodeResource.hasProperty(OwlUtil.propUri)?nodeResource.getProperty(OwlUtil.propUri).toString():null;
-            if(CdmUtils.isNotBlank(uriString)){
-                term.setUri(URI.create(uriString));
-            }
-
-            IdentifiableSource importSource = IdentifiableSource.NewDataImportInstance("Import of term tree "+treeLabel);
-            importSource.setCitation(state.getConfig().getSourceReference());
-            term.addSource(importSource);
-            getTermService().save(term);
-
-            TermVocabulary vocabulary = state.getConfig().getVocabulary(termType, treeLabel);
-            vocabulary.addTerm(term);
+        //import term vocabulary
+        Resource vocabularyResource = termResource.getPropertyResourceValue(OwlUtil.propHasVocabulary);
+        UUID vocUuid = UUID.fromString(vocabularyResource.getProperty(OwlUtil.propUuid).getString());
+        TermVocabulary vocabulary = getVocabularyService().load(vocUuid);
+        if(vocabulary==null){
+            vocabulary = OwlImportUtil.createVocabulary(vocabularyResource, getTermService(), model, state);
             getVocabularyService().saveOrUpdate(vocabulary);
         }
+        vocabulary.addTerm(term);
+        getVocabularyService().saveOrUpdate(vocabulary);
 
         FeatureNode<?> childNode = parent.addChild(term);
 
         nodeResource.listProperties(OwlUtil.propHasSubStructure).forEachRemaining(prop->createNode(childNode, prop, treeLabel, model, state));
     }
 
-    private Representation createRepresentation(Statement repr, Model model) {
-        Resource repsentationResource = model.createResource(repr.getObject().toString());
-
-        String languageLabel = repsentationResource.getProperty(OwlUtil.propLanguage).getString();
-        UUID languageUuid = UUID.fromString(repsentationResource.getProperty(OwlUtil.propLanguageUuid).getString());
-        DefinedTermBase termBase = getTermService().load(languageUuid);
-        Language language = null;
-        if(termBase.isInstanceOf(Language.class)){
-            language = HibernateProxyHelper.deproxy(termBase, Language.class);
-        }
-        if(language==null){
-            language = getTermService().getLanguageByLabel(languageLabel);
-        }
-        if(language==null){
-            language = Language.getDefaultLanguage();
-        }
-
-        String abbreviatedLabel = repsentationResource.hasProperty(OwlUtil.propLabelAbbrev)?repsentationResource.getProperty(OwlUtil.propLabelAbbrev).toString():null;
-        String label = repsentationResource.getProperty(OwlUtil.propLabel).getString();
-        String description = repsentationResource.hasProperty(OwlUtil.propDescription)?repsentationResource.getProperty(OwlUtil.propDescription).getString():null;
-        Representation representation = Representation.NewInstance(description, label, abbreviatedLabel, language);
-
-        return representation;
-    }
-
     @Override
     protected boolean isIgnore(StructureTreeOwlImportState state) {
         return false;