Project

General

Profile

Revision d2f7f487

IDd2f7f4876dfa953f4586b2af8b0123636395e9ee
Parent dcaa38a3
Child 8eaa553b

Added by Andreas Müller almost 2 years ago

ref #2342 first implementation of higherClassification handling in DwC-A export

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaTaxonExport.java
13 13
import java.io.IOException;
14 14
import java.io.PrintWriter;
15 15
import java.io.UnsupportedEncodingException;
16
import java.util.List;
16 17
import java.util.Set;
17 18

  
18 19
import org.apache.commons.lang.StringUtils;
......
213 214
		record.setTaxonConceptId(taxonBase.getUuid());
214 215

  
215 216
		//Classification
216
		if (state.getConfig().isWithHigherClassification()){
217
			//FIXME all classification and rank specific fields are meant to represent the classification
218
			//currently the information is only compiled for the exact same range but it should be compiled
219
			//for all ranks above the rank of this taxon
220
			//TODO we do not support this yet
217
		if (state.getConfig().isWithHigherClassification()
218
		        && taxonBase.isInstanceOf(Taxon.class)){
219
			//all classification and rank specific fields are meant to represent the classification
220
			Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
221
		    handleHigherClassification(state, record, taxon, classification);
222

  
221 223
			record.setHigherClassification(null);
222 224
			//... higher ranks
223 225
			handleUninomialOrGenus(record, name);
......
296 298
	}
297 299

  
298 300
	/**
301
     * @param state
302
     * @param record
303
     * @param taxonBase
304
     * @param classification
305
     */
306
    private void handleHigherClassification(DwcaTaxExportState state, DwcaTaxonRecord record, Taxon taxon,
307
            Classification classification) {
308
        TaxonNode node = taxon.getTaxonNode(classification);
309
        if (node == null){
310
            return;
311
        }
312
        record.setKingdom(nameOf(node.getAncestorOfRank(Rank.KINGDOM())));
313
        record.setPhylum(nameOf(node.getAncestorOfRank(Rank.PHYLUM())));
314
        record.setClazz(nameOf(node.getAncestorOfRank(Rank.CLASS())));
315
        record.setOrder(nameOf(node.getAncestorOfRank(Rank.ORDER())));
316
        record.setFamily(nameOf(node.getAncestorOfRank(Rank.FAMILY())));
317
        record.setGenus(nameOf(node.getAncestorOfRank(Rank.GENUS())));
318
        List<TaxonNode> ancestors = node.getAncestorList();
319
        String higherClassification = higherClassificationString(ancestors);
320
        record.setHigherClassification(higherClassification);
321
    }
322

  
323
    /**
324
     * @param ancestors
325
     */
326
    private String higherClassificationString(List<TaxonNode> ancestors) {
327
        String result = "";
328
        for (TaxonNode node : ancestors){
329
            String nameOfNode = nameOf(node);
330
            if (StringUtils.isBlank(nameOfNode)){
331
                nameOfNode = "-";
332
            }
333
            result = CdmUtils.concat("|", result, nameOfNode);
334
        }
335
        return result;
336
    }
337

  
338
    /**
339
     * @param ancestorOfRank
340
     * @return
341
     */
342
    private String nameOf(TaxonNode node) {
343
        if (node != null && node.getTaxon()!= null){
344
            Taxon taxon = node.getTaxon();
345
            if (taxon.getName()!= null){
346
                return taxon.getName().getTitleCache();
347
            }else{
348
                return taxon.getTitleCache();
349
            }
350
        }
351
        return null;
352
    }
353

  
354
    /**
299 355
	 * @param record
300 356
	 * @param name
301 357
	 * @param type
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/taxon/Taxon.java
416 416

  
417 417
    }
418 418

  
419

  
419
    /**
420
     * @param classification
421
     */
422
    public TaxonNode getTaxonNode(Classification classification) {
423
        if (classification == null){
424
            return null;
425
        }
426
        for (TaxonNode node : this.getTaxonNodes()){
427
            if (classification.equals(node.getClassification())){
428
                return node;
429
            }
430
        }
431
        return null;
432
    }
420 433

  
421 434

  
422 435
    /**
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/taxon/TaxonNode.java
853 853
            return parent.getAncestorOfRank(rank);
854 854
        }
855 855
		return null;
856
    }
857

  
858
    /**
859
     * Returns the ancestor taxa, starting with the highest (e.g. kingdom)
860
     * @return
861
     */
862
    public List<Taxon> getAncestorTaxaList(){
863
        List<Taxon> result = new ArrayList<>();
864
        TaxonNode current = this;
865
        while (current != null){
866
            if (current.getTaxon() != null){
867
                result.add(0, current.getTaxon());
868
            }
869
            current = current.getParent();
870
        }
871
        return result;
872
    }
856 873

  
874
    /**
875
     * Returns the ancestor taxon nodes, that do have a taxon attached
876
     * (excludes the root node) starting with the highest
877
     *
878
     * @return
879
     */
880
    public List<TaxonNode> getAncestorList(){
881
        List<TaxonNode> result = new ArrayList<>();
882
        TaxonNode current = this.getParent();
883
        while (current != null){
884
            if (current.getTaxon() != null){
885
                result.add(0, current);
886
            }
887
            current = current.getParent();
888
        }
889
        return result;
857 890
    }
858 891

  
859 892

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)