Project

General

Profile

Revision d703fe8b

IDd703fe8b153d79142b597f37926857696623c374
Parent bf3145f7
Child f0fdfff0

Added by Patrick Plitzner over 2 years ago

ref #8248, #6774 Add media to import/export

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/OwlUtil.java
30 30
    public static final String RESOURCE_FEATURE_TREE = RESOURCE_URI+"term_tree/";
31 31
    public static final String RESOURCE_TERM_VOCABULARY = RESOURCE_URI+"term_vocabulary/";
32 32
    public static final String RESOURCE_TERM = RESOURCE_URI+"term/";
33
    public static final String RESOURCE_MEDIA = RESOURCE_URI+"media/";
33 34

  
34 35
    /**
35 36
     * property URIs
......
61 62
     */
62 63
    public static final String PROPERTY_TERM_INCLUDES = PROPERTY_BASE_URI+"term_includes";
63 64
    public static final String PROPERTY_TERM_IS_GENERALIZATION_OF = PROPERTY_BASE_URI+"term_is_generalization_of";
65
    public static final String PROPERTY_TERM_HAS_MEDIA = PROPERTY_BASE_URI+"term_has_media";
64 66
    public static final String PROPERTY_TERM_SYMBOL = PROPERTY_BASE_URI+"term_symbol";
65 67
    public static final String PROPERTY_TERM_SYMBOL2 = PROPERTY_BASE_URI+"term_symbol2";
66 68
    public static final String PROPERTY_TERM_ID_IN_VOCABULARY = PROPERTY_BASE_URI+"term_id_in_vocabulary";
67 69

  
70
    /**
71
     * media properties
72
     */
73
    public static final String PROPERTY_MEDIA_URI = PROPERTY_BASE_URI+"media_uri";
74
    public static final String PROPERTY_MEDIA_TITLE = PROPERTY_BASE_URI+"media_title";
68 75

  
69 76
    /**
70 77
     * types
......
73 80
    public final static String TREE = "tree";
74 81
    public final static String VOCABULARY = "vocabulary";
75 82
    public final static String TERM = "term";
83
    public final static String MEDIA = "media";
76 84

  
77 85
    public static Property propHasSubStructure;
78 86
    public static Property propHasRepresentation;
......
93 101

  
94 102
    public static Property propTermIsGeneralizationOf;
95 103
    public static Property propTermIncludes;
104
    public static Property propTermHasMedia;
96 105
    public static Property propTermSymbol;
97 106
    public static Property propTermSymbol2;
98 107
    public static Property propTermIdInVocabulary;
99 108

  
109
    public static Property propMediaUri;
110
    public static Property propMediaTitle;
111

  
100 112
    public static Model createModel(){
101 113
        Model model = ModelFactory.createDefaultModel();
102 114
        propHasSubStructure = model.createProperty(OwlUtil.PROPERTY_HAS_SUBSTRUCTURE);
......
118 130

  
119 131
        propTermIsGeneralizationOf = model.createProperty(OwlUtil.PROPERTY_TERM_IS_GENERALIZATION_OF);
120 132
        propTermIncludes = model.createProperty(OwlUtil.PROPERTY_TERM_INCLUDES);
133
        propTermHasMedia = model.createProperty(OwlUtil.PROPERTY_TERM_HAS_MEDIA);
121 134
        propTermSymbol = model.createProperty(OwlUtil.PROPERTY_TERM_SYMBOL);
122 135
        propTermSymbol2 = model.createProperty(OwlUtil.PROPERTY_TERM_SYMBOL2);
123 136
        propTermIdInVocabulary = model.createProperty(OwlUtil.PROPERTY_TERM_ID_IN_VOCABULARY);
137
        propMediaUri = model.createProperty(OwlUtil.PROPERTY_MEDIA_URI);
138
        propMediaTitle = model.createProperty(OwlUtil.PROPERTY_MEDIA_TITLE);
124 139

  
125 140
        return model;
126 141
    }
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/in/OwlImportUtil.java
24 24
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
25 25
import eu.etaxonomy.cdm.model.common.Language;
26 26
import eu.etaxonomy.cdm.model.description.Feature;
27
import eu.etaxonomy.cdm.model.media.Media;
27 28
import eu.etaxonomy.cdm.model.term.DefinedTerm;
28 29
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
29 30
import eu.etaxonomy.cdm.model.term.Representation;
......
113 114
        return vocabulary;
114 115
    }
115 116

  
117
    static Media createMedia(Resource mediaResource, StructureTreeOwlImportState state){
118
        URI mediaUri = URI.create(mediaResource.getProperty(OwlUtil.propMediaUri).getString());
119
        // create new media
120
        Media media = Media.NewInstance(mediaUri, null, null, null);
121
        media.setUuid(UUID.fromString(mediaResource.getProperty(OwlUtil.propUuid).getString()));
122

  
123
        if(mediaResource.hasProperty(OwlUtil.propMediaTitle)){
124
            // TODO: support multiple language titles
125
            media.putTitle(Language.DEFAULT(), mediaResource.getProperty(OwlUtil.propMediaTitle).getString());
126
        }
127

  
128
        IdentifiableSource importSource = IdentifiableSource.NewDataImportInstance(mediaResource.getURI());
129
        importSource.setCitation(state.getConfig().getSourceReference());
130
        media.addSource(importSource);
131

  
132
        return media;
133
    }
134

  
116 135
    static Representation createRepresentation(ITermService termService, Statement repr, Model model) {
117 136
        Resource repsentationResource = model.createResource(repr.getObject().toString());
118 137

  
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/in/TermVocabularyOwlImport.java
21 21

  
22 22
import eu.etaxonomy.cdm.io.common.CdmImportBase;
23 23
import eu.etaxonomy.cdm.io.descriptive.owl.OwlUtil;
24
import eu.etaxonomy.cdm.model.media.Media;
24 25
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
25 26
import eu.etaxonomy.cdm.model.term.TermVocabulary;
26 27

  
......
79 80

  
80 81
        UUID termUuid = UUID.fromString(termResource.getProperty(OwlUtil.propUuid).getString());
81 82
        DefinedTermBase term = getTermService().load(termUuid);
82
        if(term==null){
83
            term = OwlImportUtil.createTerm(termResource, getTermService(), model, state);
84
            getTermService().saveOrUpdate(term);
85
            vocabulary.addTerm(term); // only add term if it does not already exist
83
        if(term!=null){
84
            return term;
85
        }
86

  
87
        term = OwlImportUtil.createTerm(termResource, getTermService(), model, state);
88
        getTermService().saveOrUpdate(term);
89
        vocabulary.addTerm(term); // only add term if it does not already exist
90

  
91
        //check media
92
        StmtIterator mediaIterator = termResource.listProperties(OwlUtil.propTermHasMedia);
93
        while(mediaIterator.hasNext()){
94
            Resource mediaResource = model.createResource(mediaIterator.next().getObject().toString());
95
            Media media = OwlImportUtil.createMedia(mediaResource, state);
96
            term.addMedia(media);
86 97
        }
87 98

  
88 99
        // check includes
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/out/OwlExportUtil.java
15 15

  
16 16
import eu.etaxonomy.cdm.common.CdmUtils;
17 17
import eu.etaxonomy.cdm.io.descriptive.owl.OwlUtil;
18
import eu.etaxonomy.cdm.model.common.Language;
19
import eu.etaxonomy.cdm.model.media.Media;
20
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
21
import eu.etaxonomy.cdm.model.media.MediaUtils;
18 22
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
19 23
import eu.etaxonomy.cdm.model.term.FeatureNode;
20 24
import eu.etaxonomy.cdm.model.term.FeatureTree;
......
91 95
        return termResource;
92 96
    }
93 97

  
98
    static Resource createMediaResource(Media media, StructureTreeOwlExportState state) {
99
        Resource mediaResource = state.getModel().createResource(OwlUtil.RESOURCE_MEDIA+media.getUuid().toString())
100
                .addProperty(OwlUtil.propUuid, media.getUuid().toString())
101
                .addProperty(OwlUtil.propIsA, OwlUtil.MEDIA)
102
                ;
103
        // TODO: support for multiple languages
104
        if(media.getTitle()!=null){
105
            mediaResource.addProperty(OwlUtil.propMediaTitle, media.getTitle(Language.DEFAULT()).getText());
106
        }
107

  
108
        // TODO: support for multiple media representations
109
        MediaRepresentationPart part = MediaUtils.getFirstMediaRepresentationPart(media);
110
        if(part!=null){
111
            mediaResource.addProperty(OwlUtil.propMediaUri, part.getUri().toString());
112
        }
113
        return mediaResource;
114
    }
115

  
94 116
    static Resource createFeatureTreeResource(FeatureTree featureTree, StructureTreeOwlExportState state) {
95 117
        Resource featureTreeResource = state.getModel().createResource(OwlUtil.RESOURCE_FEATURE_TREE+featureTree.getUuid().toString())
96 118
                .addProperty(OwlUtil.propUuid, featureTree.getUuid().toString())
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/out/StructureTreeOwlExport.java
11 11
import java.io.ByteArrayOutputStream;
12 12
import java.io.File;
13 13
import java.util.List;
14
import java.util.Set;
14 15
import java.util.UUID;
15 16

  
16 17
import org.springframework.stereotype.Component;
......
21 22
import eu.etaxonomy.cdm.io.common.CdmExportBase;
22 23
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
23 24
import eu.etaxonomy.cdm.io.descriptive.owl.OwlUtil;
25
import eu.etaxonomy.cdm.model.media.Media;
24 26
import eu.etaxonomy.cdm.model.term.FeatureNode;
25 27
import eu.etaxonomy.cdm.model.term.FeatureTree;
26 28

  
......
75 77
            // add term to node
76 78
            Resource termResource = OwlExportUtil.createTermResource(child.getTerm(), state);
77 79
            nodeResource.addProperty(OwlUtil.propHasTerm, termResource);
80

  
81
            // export media
82
            Set<Media> media = child.getTerm().getMedia();
83
            for (Media medium : media) {
84
                Resource mediaResource = OwlExportUtil.createMediaResource(medium, state);
85
                termResource.addProperty(OwlUtil.propTermHasMedia, mediaResource);
86
            }
87

  
78 88
            // add node to parent node
79 89
            parentResourceNode.addProperty(OwlUtil.propHasSubStructure, nodeResource);
80 90

  
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/out/TermVocabularyOwlExport.java
20 20
import eu.etaxonomy.cdm.io.common.CdmExportBase;
21 21
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
22 22
import eu.etaxonomy.cdm.io.descriptive.owl.OwlUtil;
23
import eu.etaxonomy.cdm.model.media.Media;
23 24
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
24 25
import eu.etaxonomy.cdm.model.term.TermVocabulary;
25 26
import eu.etaxonomy.cdm.persistence.dto.TermDto;
......
70 71
        vocabularyResource.addProperty(OwlUtil.propHasTerm, termResource);
71 72
        termResource.addProperty(OwlUtil.propHasVocabulary, vocabularyResource);
72 73

  
74
        // export media
75
        Set<Media> media = term.getMedia();
76
        for (Media medium : media) {
77
            Resource mediaResource = OwlExportUtil.createMediaResource(medium, state);
78
            termResource.addProperty(OwlUtil.propTermHasMedia, mediaResource);
79
        }
80

  
81
        // export includes and generalizationOf
73 82
        Set<DefinedTermBase> generalizationOf = term.getGeneralizationOf();
74 83
        for (DefinedTermBase kindOf : generalizationOf) {
75 84
            Resource kindOfResource = addTerm(kindOf, vocabularyResource, state);
......
80 89
            Resource partOfResource = addTerm(partOf, vocabularyResource, state);
81 90
            termResource.addProperty(OwlUtil.propTermIncludes, partOfResource);
82 91
        }
92

  
83 93
        return termResource;
84 94
    }
85 95

  
cdmlib-io/src/test/java/eu/etaxonomy/cdm/io/owl/in/StructureTreeOwlImportTest.java
20 20
import java.util.ArrayList;
21 21
import java.util.Arrays;
22 22
import java.util.List;
23
import java.util.Set;
23 24

  
24 25
import org.junit.Before;
25 26
import org.junit.Test;
......
34 35
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultImport;
35 36
import eu.etaxonomy.cdm.io.descriptive.owl.in.StructureTreeOwlImportConfigurator;
36 37
import eu.etaxonomy.cdm.model.common.Language;
38
import eu.etaxonomy.cdm.model.media.Media;
39
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
40
import eu.etaxonomy.cdm.model.media.MediaUtils;
37 41
import eu.etaxonomy.cdm.model.term.DefinedTerm;
38 42
import eu.etaxonomy.cdm.model.term.FeatureNode;
39 43
import eu.etaxonomy.cdm.model.term.FeatureTree;
......
89 93
        List<FeatureTree> trees = featureTreeService.listByTitle(FeatureTree.class, treeLabel, MatchMode.EXACT, null, null, null, null, null);
90 94
        List<String> nodeProperties = new ArrayList<>();
91 95
        nodeProperties.add("term");
96
        nodeProperties.add("term.media");
92 97
        FeatureTree tree = featureTreeService.loadWithNodes(trees.iterator().next().getUuid(), null, nodeProperties);
93 98
        assertNotNull("featureTree should not be null", tree);
94 99

  
......
122 127
                assertEquals("wrong symbol", "infloSymbol", inflorescence.getSymbol());
123 128
                assertEquals("wrong symbol2", "infloSymbol2", inflorescence.getSymbol2());
124 129

  
130
                Set<Media> mediaSet = inflorescence.getMedia();
131
                assertEquals("wrong number of media", 1, mediaSet.size());
132
                Media media = mediaSet.iterator().next();
133
                MediaRepresentationPart part = MediaUtils.getFirstMediaRepresentationPart(media);
134
                assertNotNull("media part not found", part);
135
                assertEquals("incorrect URI", URI.create("https://upload.wikimedia.org/wikipedia/commons/8/82/Aloe_hereroensis_Auob_C15.JPG"), part.getUri());
136
                assertEquals("incorrect title", "Aloe hereroensis", media.getTitle(Language.DEFAULT()).getText());
137

  
125 138
                Representation englishRepresentation = inflorescence.getRepresentation(Language.ENGLISH());
126 139
                assertTrue("Description not found", CdmUtils.isNotBlank(englishRepresentation.getDescription()));
127 140
                assertEquals("Description wrong", inflorescenceDescription, englishRepresentation.getDescription());
cdmlib-io/src/test/resources/eu/etaxonomy/cdm/io/owl/in/test_structures.owl
66 66
    <j.0:has_representation rdf:resource="http://cybertaxonomy.eu/resource/representation/bf3ac87b-39e8-4360-8fe1-3dd0aeafb600"/>
67 67
    <j.0:has_representation rdf:resource="http://cybertaxonomy.eu/resource/representation/25d6e3de-9b31-469b-a3a4-4a971a023379"/>
68 68
    <j.0:term_id_in_vocabulary>inflorescence</j.0:term_id_in_vocabulary>
69
    <j.0:term_has_media rdf:resource="http://cybertaxonomy.eu/resource/media/f002a25c-d75c-4b58-8f1c-1188d9b11ad0"/>
69 70
    <j.0:term_symbol2>infloSymbol2</j.0:term_symbol2>
70 71
    <j.0:term_symbol>infloSymbol</j.0:term_symbol>
71 72
    <j.0:type>STRU</j.0:type>
......
126 127
    <j.0:is_a>term</j.0:is_a>
127 128
    <j.0:uuid>a8063aed-83d5-422d-b5b2-2777369c45fd</j.0:uuid>
128 129
  </rdf:Description>
130
  <rdf:Description rdf:about="http://cybertaxonomy.eu/resource/media/f002a25c-d75c-4b58-8f1c-1188d9b11ad0">
131
    <j.0:media_uri>https://upload.wikimedia.org/wikipedia/commons/8/82/Aloe_hereroensis_Auob_C15.JPG</j.0:media_uri>
132
    <j.0:media_title>Aloe hereroensis</j.0:media_title>
133
    <j.0:is_a>media</j.0:is_a>
134
    <j.0:uuid>f002a25c-d75c-4b58-8f1c-1188d9b11ad0</j.0:uuid>
135
  </rdf:Description>
129 136
</rdf:RDF>

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)