Project

General

Profile

« Previous | Next » 

Revision 973082b3

Added by Andreas Müller over 5 years ago

ref #6682 further fixes for taxonRelationship service

View differences:

cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/CdmUtils.java
504 504
     * @return
505 505
     */
506 506
    public static boolean nullSafeEqual(Object obj1, Object obj2) {
507
        if (obj1 == null && obj2 == null){
508
            return true;
509
        }
510
        if (obj1 == null && obj2 != null){
511
            return false;
507
        if (obj1 == null){
508
            return obj2 == null;
512 509
        }
513 510
        return (obj1.equals(obj2));
514 511
    }
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/TaxonServiceImpl.java
3213 3213
//
3214 3214
//        return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);;
3215 3215

  
3216
            //TODO
3216
            //TODO type
3217
            TaxonRelationshipType type = null;
3218
            //TODO languages
3217 3219
            List<Language> languages = null;
3218 3220
            if (doDirect){
3219 3221
                direction = Direction.relatedTo;
3220 3222
                //TODO order hints, property path
3221
                List<TaxonRelationship> relations = dao.getTaxonRelationships(taxon, null, includeUnpublished, pageSize, pageNumber, null, null, direction);
3223
                List<TaxonRelationship> relations = dao.getTaxonRelationships(taxon, type, includeUnpublished, pageSize, pageNumber, null, null, direction);
3222 3224
                for (TaxonRelationship relation : relations){
3223 3225
                    dto.addRelation(relation, direction, languages);
3224 3226
                }
......
3226 3228
            if (doInvers){
3227 3229
                direction = Direction.relatedFrom;
3228 3230
                //TODO order hints, property path
3229
                List<TaxonRelationship> relations = dao.getTaxonRelationships(taxon, null, includeUnpublished, pageSize, pageNumber, null, null, direction);
3231
                List<TaxonRelationship> relations = dao.getTaxonRelationships(taxon, type, includeUnpublished, pageSize, pageNumber, null, null, direction);
3230 3232
                for (TaxonRelationship relation : relations){
3231 3233
                    dto.addRelation(relation, direction, languages);
3232 3234
                }
3233 3235
            }
3234 3236
            if (deduplicateMisapplications){
3235
//                dto.createMisapplicationString();
3237
                dto.createMisapplicationString();
3236 3238
            }
3237 3239
            return dto;
3238 3240
        }
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/dto/TaxonRelationshipsDTO.java
220 220
        int j = 0;
221 221
        int sensuEndInFirst = -1;
222 222
        int sensuStartInSecond = -1;
223
        int lastEndInSecond = -1;
223
        int senusEndInSecond = -1;
224 224

  
225
        while (i < first.size() && j< second.size()){
225
        boolean sensuHandled = false;
226
        boolean isDuplicate = true;
227
        while (isDuplicate && i < first.size() && j< second.size()){
226 228
            if (tagEqualsMisapplied(first.get(i), second.get(i))){
227
                i++;j++;
228
            }else{
229
                //do nothing
230
            }else if (!sensuHandled){
229 231
                while (i < first.size() && tagIsSensu(first.get(i))){
230 232
                    i++;
233
                    sensuEndInFirst = i;
231 234
                }
232
                sensuEndInFirst = i;
233 235
                sensuStartInSecond = j;
234 236
                while (j< second.size() && tagIsSensu(second.get(j))){
235 237
                    j++;
238
                    senusEndInSecond = j;
236 239
                }
237
                lastEndInSecond = j;
240
                sensuHandled = true;
241
            }else{
242
                isDuplicate = false;
238 243
            }
244
            i++;j++;
239 245
        }
240
        boolean isDuplicate = (i == first.size() || j == second.size());
246
        isDuplicate = isDuplicate && (i == first.size() || j == second.size());
241 247
        if (isDuplicate && sensuEndInFirst > -1 && sensuStartInSecond > -1){
242
            first.addAll(sensuEndInFirst, second.subList(sensuStartInSecond, lastEndInSecond));
248
            first.addAll(sensuEndInFirst, second.subList(sensuStartInSecond, senusEndInSecond));
243 249
            return true;
244 250
        }else{
245 251
            return false;
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/dto/TaxonRelationshipsDTOTest.java
1
/**
2
* Copyright (C) 2018 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.api.service.dto;
10

  
11
import java.util.List;
12

  
13
import org.junit.Assert;
14
import org.junit.Before;
15
import org.junit.BeforeClass;
16
import org.junit.Test;
17

  
18
import eu.etaxonomy.cdm.format.taxon.TaxonRelationshipFormatter;
19
import eu.etaxonomy.cdm.model.agent.Person;
20
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
21
import eu.etaxonomy.cdm.model.common.Language;
22
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
23
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
24
import eu.etaxonomy.cdm.model.name.Rank;
25
import eu.etaxonomy.cdm.model.name.TaxonName;
26
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
27
import eu.etaxonomy.cdm.model.reference.Reference;
28
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
31
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
32
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
33

  
34
/**
35
 * @author a.mueller
36
 * @since 15.08.2018
37
 *
38
 */
39
public class TaxonRelationshipsDTOTest {
40
    private TaxonRelationship taxonRel;
41
    private Reference relSec;
42

  
43
    private Taxon fromTaxon;
44
    private TaxonName fromName;
45
    private Reference fromSec;
46

  
47
    private Taxon toTaxon;
48
    private TaxonName toName;
49
    private Reference toSec;
50

  
51
    private TaxonRelationshipFormatter formatter;
52
    private boolean reverse;
53

  
54
    Person toNameAuthor;
55
    private List<Language> languages;
56

  
57
    /**
58
     * @throws java.lang.Exception
59
     */
60
    @BeforeClass
61
    public static void setUpBeforeClass() throws Exception {
62
        if (Language.DEFAULT() == null){
63
            new DefaultTermInitializer().initialize();
64
        }
65
    }
66

  
67
    /**
68
     * @throws java.lang.Exception
69
     */
70
    @Before
71
    public void setUp() throws Exception {
72
        fromName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
73
        fromName.setGenusOrUninomial("Abies");
74
        fromName.setSpecificEpithet("alba");
75

  
76
        toName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
77
        toName.setGenusOrUninomial("Pinus");
78
        toName.setSpecificEpithet("pinova");
79
        toNameAuthor = Person.NewInstance("Mill.", "Miller", "A.", "Andrew");
80
        toName.setCombinationAuthorship(toNameAuthor);
81

  
82
        fromSec = ReferenceFactory.newGeneric();
83
        fromSec.setTitle("From Sec");
84
        String initials = "J.M.";
85
        fromSec.setAuthorship(Person.NewInstance(null, "Macfarlane", initials, null));
86
        fromSec.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1918));
87

  
88
        relSec = ReferenceFactory.newGeneric();
89
        relSec.setTitle("From rel reference");
90
        initials = null; //"M.R.";
91
        relSec.setAuthorship(Person.NewInstance(null, "Cheek", initials, null));
92
        relSec.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1919));
93

  
94
        toSec = ReferenceFactory.newGeneric();
95
        toSec.setTitle("To Sec");
96
        toSec.setAuthorship(Person.NewTitledInstance("ToSecAuthor"));
97
        toSec.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1928));
98

  
99
        fromTaxon = Taxon.NewInstance(fromName, fromSec);
100
        toTaxon = Taxon.NewInstance(toName, toSec);
101

  
102
        TaxonRelationshipType type = TaxonRelationshipType.MISAPPLIED_NAME_FOR();
103
        taxonRel = fromTaxon.addTaxonRelation(toTaxon, type, relSec, "123");
104
        reverse = false;
105

  
106
        formatter = new TaxonRelationshipFormatter();
107

  
108
        languages = null;
109
    }
110

  
111
    @Test
112
    public void test() {
113
        Reference sec2 = ReferenceFactory.newGeneric();
114
        sec2.setAuthorship(Person.NewInstance("Mue.", "Mueller", "I.", "Inger"));
115
        sec2.setDatePublished(TimePeriodParser.parseStringVerbatim("1987"));
116
        Taxon from2 = Taxon.NewInstance(fromName, sec2);
117
        TaxonRelationship rel2 = toTaxon.addMisappliedName(from2, sec2, "333");
118
        //same as rel1 except for sec
119
        TaxonRelationship rel3 = toTaxon.addMisappliedName(from2, taxonRel.getCitation(), taxonRel.getCitationMicroReference());
120

  
121
        TaxonRelationshipsDTO dto = new TaxonRelationshipsDTO();
122

  
123

  
124
        dto.addRelation(taxonRel, Direction.relatedFrom, languages);
125
        dto.addRelation(rel2, Direction.relatedFrom, languages);
126
        dto.addRelation(rel3, Direction.relatedFrom, languages);
127
        dto.createMisapplicationString();
128
        Assert.assertEquals(2, dto.getMisapplications().size());
129
    }
130

  
131
}

Also available in: Unified diff