Project

General

Profile

Download (7.29 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2019 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.in;
10

    
11
import java.util.UUID;
12

    
13
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.Logger;
15
import org.springframework.stereotype.Component;
16

    
17
import com.hp.hpl.jena.rdf.model.Model;
18
import com.hp.hpl.jena.rdf.model.ResIterator;
19
import com.hp.hpl.jena.rdf.model.Resource;
20
import com.hp.hpl.jena.rdf.model.Statement;
21
import com.hp.hpl.jena.rdf.model.StmtIterator;
22

    
23
import eu.etaxonomy.cdm.common.URI;
24
import eu.etaxonomy.cdm.io.common.CdmImportBase;
25
import eu.etaxonomy.cdm.io.descriptive.owl.OwlUtil;
26
import eu.etaxonomy.cdm.model.description.Feature;
27
import eu.etaxonomy.cdm.model.description.FeatureState;
28
import eu.etaxonomy.cdm.model.description.State;
29
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
30
import eu.etaxonomy.cdm.model.term.TermNode;
31
import eu.etaxonomy.cdm.model.term.TermTree;
32
import eu.etaxonomy.cdm.model.term.TermType;
33
import eu.etaxonomy.cdm.model.term.TermVocabulary;
34

    
35
/**
36
 * @author pplitzner
37
 * @since Apr 24, 2019
38
 */
39
@Component("structureTreeOwlImport")
40
public class StructureTreeOwlImport extends CdmImportBase<StructureTreeOwlImportConfigurator, StructureTreeOwlImportState> {
41

    
42
    private static final long serialVersionUID = -3659780404413458511L;
43

    
44
    private final static Logger logger = LogManager.getLogger();
45

    
46
    @Override
47
    protected boolean doCheck(StructureTreeOwlImportState state) {
48
        logger.warn("Checking not yet implemented for " + this.getClass().getSimpleName());
49
        return true;
50
    }
51

    
52
    @Override
53
    public void doInvoke(StructureTreeOwlImportState state) {
54
        URI source = state.getConfig().getSource();
55

    
56
        state.getModel().read(source.toString());
57

    
58
        //get all trees
59
        ResIterator iterator = state.getModel().listResourcesWithProperty(OwlUtil.propHasRootNode);
60
        while(iterator.hasNext()){
61
            Resource treeResource = iterator.next();
62
            UUID treeUuid = UUID.fromString(treeResource.getProperty(OwlUtil.propUuid).getString());
63
            TermTree<?> termTree = getTermTreeService().find(treeUuid);
64
            if(termTree==null){
65
                String type = treeResource.getProperty(OwlUtil.propType).getString();
66
                TermTree<Feature> featureTree = TermTree.NewInstance(TermType.getByKey(type));
67
                featureTree.setTitleCache(treeResource.getProperty(OwlUtil.propLabel).getString(), true);
68
                featureTree.setUuid(treeUuid);
69

    
70
                Resource rootNode = treeResource.getProperty(OwlUtil.propHasRootNode).getResource();
71
                rootNode.listProperties(OwlUtil.propHasSubStructure).forEachRemaining(prop->createNode(featureTree.getRoot(), prop, featureTree.getTitleCache(), state.getModel(), state));
72

    
73
                getTermTreeService().saveOrUpdate(featureTree);
74
            }
75
        }
76
    }
77

    
78
    private <T extends DefinedTermBase> void createNode(TermNode<T> parent, Statement nodeStatement, String treeLabel, Model model, StructureTreeOwlImportState state) {
79
        if(state.getConfig().getProgressMonitor().isCanceled()){
80
            return;
81
        }
82
        Resource nodeResource = model.createResource(nodeStatement.getObject().toString());
83
        UUID nodeUuid = UUID.fromString(nodeResource.getProperty(OwlUtil.propUuid).getString());
84

    
85
        Resource termResource = nodeResource.getPropertyResourceValue(OwlUtil.propHasTerm);
86
        TermNode<?> termNode = getTermNodeService().load(nodeUuid);
87
        if(termNode==null){
88
            //import term vocabulary
89
            Resource vocabularyResource = termResource.getPropertyResourceValue(OwlUtil.propHasVocabulary);
90
            UUID vocUuid = UUID.fromString(vocabularyResource.getProperty(OwlUtil.propUuid).getString());
91
            TermVocabulary vocabulary = getVocabularyService().load(vocUuid);
92
            if(vocabulary==null){
93
                vocabulary = OwlImportUtil.createVocabulary(vocabularyResource, this, model, state);
94
                vocabulary = getVocabularyService().save(vocabulary);
95
            }
96

    
97
            // import term
98
            UUID termUuid = UUID.fromString(termResource.getProperty(OwlUtil.propUuid).getString());
99
            T term = (T)getTermService().find(termUuid);
100
            if(term == null){
101
                term = (T)OwlImportUtil.createTerm(termResource, this, model, state);
102
                term = getTermService().save(term);
103
                vocabulary.addTerm(term); // only add term if it does not already exist
104
            }
105

    
106
            getVocabularyService().saveOrUpdate(vocabulary);
107

    
108
            termNode = parent.addChild(term);
109
            termNode.setUuid(nodeUuid);
110
            // inapplicable if
111
            StmtIterator inapplicableIterator = nodeResource.listProperties(OwlUtil.propNodeIsInapplicableIf);
112
            while(inapplicableIterator.hasNext()){
113
                Statement statement = inapplicableIterator.next();
114
                FeatureState featureState = createFeatureState(statement, model, state);
115
                termNode.addInapplicableState(featureState);
116
            }
117
            // only applicable if
118
            StmtIterator onlyApplicableIterator = nodeResource.listProperties(OwlUtil.propNodeIsOnlyApplicableIf);
119
            while(onlyApplicableIterator.hasNext()){
120
                Statement statement = onlyApplicableIterator.next();
121
                FeatureState featureState = createFeatureState(statement, model, state);
122
                termNode.addApplicableState(featureState);
123
            }
124
        }
125

    
126
        state.getConfig().getProgressMonitor().worked(1);
127

    
128
        StmtIterator listProperties = nodeResource.listProperties(OwlUtil.propHasSubStructure);
129
        while(listProperties.hasNext()){
130
            Statement prop = listProperties.next();
131
            createNode(termNode, prop, treeLabel, model, state);
132
        }
133
    }
134

    
135
    private FeatureState createFeatureState(Statement statement, Model model, StructureTreeOwlImportState state) {
136
        Resource featureStateResource = model.createResource(statement.getObject().toString());
137
        Resource featureResouce = featureStateResource.getPropertyResourceValue(OwlUtil.propFeatureStateHasFeature);
138
        Resource stateResouce = featureStateResource.getPropertyResourceValue(OwlUtil.propFeatureStateHasState);
139

    
140
        UUID featureUuid = UUID.fromString(featureResouce.getProperty(OwlUtil.propUuid).getString());
141
        Feature feature = (Feature)getTermService().find(featureUuid);
142
        if(feature==null){
143
            feature = (Feature) OwlImportUtil.createTerm(featureResouce, this, model, state);
144
            getTermService().saveOrUpdate(feature);
145
        }
146
        UUID stateUuid = UUID.fromString(stateResouce.getProperty(OwlUtil.propUuid).getString());
147
        State stateTerm = (State)getTermService().find(stateUuid);
148
        if(stateTerm==null){
149
            stateTerm = (State) OwlImportUtil.createTerm(stateResouce, this, model, state);
150
            getTermService().saveOrUpdate(stateTerm);
151
        }
152
        FeatureState featureState = FeatureState.NewInstance(feature, stateTerm);
153
        return featureState;
154
    }
155

    
156
    @Override
157
    protected boolean isIgnore(StructureTreeOwlImportState state) {
158
        return false;
159
    }
160
}
(3-3/6)