Project

General

Profile

« Previous | Next » 

Revision 0f6bc0c7

Added by Andreas Müller about 3 years ago

fix #6324 fix taxon node dozer mapping for TDWG Ontology

View differences:

cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/dto/assembler/converter/ConstantConverter.java
1
/**
2
* Copyright (C) 2021 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.remote.dto.assembler.converter;
10

  
11
import com.github.dozermapper.core.DozerConverter;
12

  
13
import eu.etaxonomy.cdm.model.common.CdmBase;
14
import eu.etaxonomy.cdm.remote.dto.tdwg.voc.TaxonRelationshipTerm;
15

  
16
/**
17
 * @author a.mueller
18
 * @since 05.02.2021
19
 */
20
public class ConstantConverter extends DozerConverter<CdmBase,Object> {
21

  
22
    final static String TAX_INCLUDED = "taxIncluded";
23

  
24
    public ConstantConverter() {
25
        super(CdmBase.class, Object.class);
26
    }
27

  
28
    @Override
29
    public Object convertTo(CdmBase source, Object destination) {
30
        if (TAX_INCLUDED.equals(this.getParameter())){
31
            TaxonRelationshipTerm result = new TaxonRelationshipTerm();
32
            result.setTitle("is taxonomically included in");
33
            return result;
34
        }else{
35
            return this.getParameter();
36
        }
37
    }
38

  
39
    @Override
40
    public CdmBase convertFrom(Object source, CdmBase destination) {
41
        throw new RuntimeException("ConstantConverter should be used one-way only");
42
    }
43

  
44

  
45
}
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/dto/assembler/converter/DateTimeConverter.java
14 14

  
15 15
import org.joda.time.DateTime;
16 16

  
17
import com.github.dozermapper.core.CustomConverter;
17 18
import com.github.dozermapper.core.MappingException;
18 19

  
19
public class DateTimeConverter implements com.github.dozermapper.core.CustomConverter {
20
public class DateTimeConverter implements CustomConverter {
20 21

  
21 22
	@Override
22 23
    public Object convert(Object destination, Object source, Class<?> destClass, Class<?> sourceClass) {
cdmlib-remote/src/main/resources/eu/etaxonomy/cdm/remote.xml
101 101
        <entry key="stripTagsConverter">
102 102
          <bean class="eu.etaxonomy.cdm.remote.dto.assembler.converter.StripTagsConverter"/>
103 103
        </entry>
104
        <entry key="constantConverter">
105
          <bean class="eu.etaxonomy.cdm.remote.dto.assembler.converter.ConstantConverter"/>
106
        </entry>
104 107

  
105 108
      </map>
106 109
    </property>
cdmlib-remote/src/test/java/eu/etaxonomy/cdm/remote/dto/assembler/lsid/AssemblerTest.java
11 11
import static org.junit.Assert.assertEquals;
12 12
import static org.junit.Assert.assertNotNull;
13 13
import static org.junit.Assert.assertNull;
14
import static org.junit.Assert.assertTrue;
15 14

  
16 15
import java.io.Serializable;
17 16
import java.lang.reflect.Field;
......
67 66
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
68 67
import eu.etaxonomy.cdm.model.reference.Reference;
69 68
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
69
import eu.etaxonomy.cdm.model.taxon.Classification;
70 70
import eu.etaxonomy.cdm.model.taxon.Synonym;
71 71
import eu.etaxonomy.cdm.model.taxon.SynonymType;
72 72
import eu.etaxonomy.cdm.model.taxon.Taxon;
73 73
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
74
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
74 75
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
75 76
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
76 77
import eu.etaxonomy.cdm.model.term.DefaultTermInitializer;
......
79 80
import eu.etaxonomy.cdm.remote.dto.tdwg.voc.Relationship;
80 81
import eu.etaxonomy.cdm.remote.dto.tdwg.voc.SpeciesProfileModel;
81 82
import eu.etaxonomy.cdm.remote.dto.tdwg.voc.TaxonConcept;
83
import eu.etaxonomy.cdm.remote.dto.tdwg.voc.TaxonRelationshipTerm;
82 84
import eu.etaxonomy.cdm.remote.view.OaiPmhViewTest;
83 85
import net.sf.cglib.proxy.Enhancer;
84 86
import net.sf.cglib.proxy.MethodInterceptor;
......
135 137
        taxon.setTitleCache("titleCache", true);
136 138
        taxon.setLsid(lsid);
137 139

  
138
        for(int i = 2; i < 2+10; i++) {
140
        for(int i = 2; i < 2+1; i++) {
139 141
            Taxon child = Taxon.NewInstance(name, (Reference)sec);
140
            child.setLsid(new LSID("urn:lsid:example.org:taxonconcepts:r" + i ));
142
            child.setLsid(new LSID("urn:lsid:example.org:taxonconcepts:r0" + i ));
141 143
            child.addTaxonRelation(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), null, null);
142 144
        }
143 145

  
......
194 196
    }
195 197

  
196 198
    @Test
197
    public void testDeepMapping() {
199
    public void testDeepMapping() throws MalformedLSIDException {
198 200

  
199 201
        if(!OaiPmhViewTest.dozerXsdIsAvailable()){
200 202
            return;
201 203
        }
202 204

  
205
        //synonyms
203 206
        for(int i = 1; i < 1+3; i++) {
204 207
            Synonym synonym = Synonym.NewInstance(name,(Reference)sec);
205
            try {
206
                synonym.setLsid(new LSID("urn:lsid:example.org:synyonms:" + i ));
207
            } catch (MalformedLSIDException e) {}
208
            synonym.setLsid(new LSID("urn:lsid:example.org:synyonms:" + i ));
208 209
            taxon.addSynonym(synonym, SynonymType.SYNONYM_OF());
209 210
        }
210 211

  
212
        //taxon nodes
213
        TaxonNode rootNode = Classification.NewInstance("Classification").getRootNode();
214
        TaxonNode parentNode = rootNode.addChildTaxon(taxon, null);
215
        for(int i = 11; i < 11+2; i++) {
216
            Taxon child = Taxon.NewInstance(name, (Reference)sec);
217
            child.setLsid(new LSID("urn:lsid:example.org:taxonconcepts:p" + i ));
218
            parentNode.addChildTaxon(child, null);
219
        }
220

  
211 221
        TaxonConcept taxonConcept = mapper.map(taxon, TaxonConcept.class);
212 222

  
213 223
        assertNotNull("map() should return an object", taxonConcept);
......
223 233
        assertNotNull("TaxonBase.name should be mapped to TaxonConcept.hasName",taxonConcept.getHasName());
224 234
        assertEquals("NonViralName.nameCache should be mapped to TaxonName.nameComplete",name.getNameCache(),taxonConcept.getHasName().getNameComplete());
225 235
        assertNotNull("Taxon.relationsToThisTaxon should be copied into TaxonConcept.hasRelationship",taxonConcept.getHasRelationship());
226
        assertEquals("There should be 13 relations in TaxonConcept.hasRelationship",
227
                13, taxonConcept.getHasRelationship().size());
236
        assertEquals("There should be 6 relations in TaxonConcept.hasRelationship",
237
                6, taxonConcept.getHasRelationship().size());
228 238
        int nSynonyms = 0;
229
        int nTaxa = 0;
239
        int nIncludedInTaxa = 0;
240
        int pnParentTaxa = 0;
230 241
        for (Relationship rel : taxonConcept.getHasRelationship()){
231 242
            Assert.assertNotNull(rel.getFromTaxon());
232 243
            System.out.println(rel.getFromTaxon().getIdentifier().toString());
233 244
            if (rel.getFromTaxon().getIdentifier().toString().startsWith("urn:lsid:example.org:synyonms:")){
234 245
                nSynonyms++;
235
            }else if (rel.getFromTaxon().getIdentifier().toString().startsWith("urn:lsid:example.org:taxonconcepts:")){
236
                nTaxa++;
246
            }else if (rel.getFromTaxon().getIdentifier().toString().startsWith("urn:lsid:example.org:taxonconcepts:r")){
247
                nIncludedInTaxa++;
248
            }else if (rel.getFromTaxon().getIdentifier().toString().startsWith("urn:lsid:example.org:taxonconcepts:p")){
249
                TaxonRelationshipTerm category = rel.getRelationshipCategory();
250
                Assert.assertEquals("is taxonomically included in", category.getTitle());
251
                pnParentTaxa++;
237 252
            }
238 253
//            System.out.println(rel);
239 254
        }
240 255
        Assert.assertEquals(3, nSynonyms);
241
        Assert.assertEquals(10, nTaxa);
256
        Assert.assertEquals(1, nIncludedInTaxa);
257
        Assert.assertEquals(2, pnParentTaxa);
242 258
    }
243 259

  
244 260
    @Test
......
275 291
        relationsToThisTaxonField.set(taxon, proxy);
276 292

  
277 293
        TaxonConcept taxonConcept = mapper.map(taxon, TaxonConcept.class);
278
        assertTrue("TaxonBase.relationsToThisTaxon was uninitialized, so TaxonConcept.hasRelationship should be null",
279
                taxonConcept.getHasRelationship().isEmpty());
294
        Assert.assertTrue("TaxonBase.relationsToThisTaxon was uninitialized, so TaxonConcept.hasRelationship should be null",
295
                taxonConcept.getHasRelationship() == null || taxonConcept.getHasRelationship().isEmpty()); //with taxonNode mapping included this results in null, otherwise empty, not yet checked why
280 296
    }
281 297

  
282 298
    @Test
......
347 363
                }else if("initListener".equals(method.getName())) {
348 364
                    return null;
349 365
                } else {
350

  
351 366
                    throw new LazyInitializationException(null);
352 367
                }
353

  
354 368
            }
355 369
        });
356 370

  

Also available in: Unified diff