Project

General

Profile

Download (5.37 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2007 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.redlist.bfnXml.out;
10

    
11
import java.util.List;
12
import java.util.Set;
13
import java.util.UUID;
14

    
15
import org.jdom2.Document;
16
import org.jdom2.Element;
17
import org.springframework.stereotype.Component;
18

    
19
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
20
import eu.etaxonomy.cdm.io.redlist.bfnXml.BfnXmlConstants;
21
import eu.etaxonomy.cdm.io.redlist.bfnXml.in.BfnXmlTransformer;
22
import eu.etaxonomy.cdm.model.common.DefinedTerm;
23
import eu.etaxonomy.cdm.model.taxon.Classification;
24
import eu.etaxonomy.cdm.model.taxon.Taxon;
25
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
26
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
27

    
28

    
29

    
30
/**
31
 *
32
 * @author pplitzner
33
 * @since May 3, 2016
34
 *
35
 */
36
@Component
37
public class BfnXmlExportConceptRelations extends BfnXmlExportBase {
38

    
39
    private static final long serialVersionUID = -931703660108981011L;
40

    
41

    
42
	public BfnXmlExportConceptRelations(){
43
		super();
44
	}
45

    
46
	@Override
47
    protected void doInvoke(BfnXmlExportState state){
48
	    startTransaction(true);
49

    
50
	    Document document = state.getConfig().getDocument();
51
	    Element konzeptbeziehungen = null;
52
	    List<Classification> list = getClassificationService().list(Classification.class, null, null, null, null);
53
	    DefinedTerm taxNrIdentifierType = HibernateProxyHelper.deproxy(getTermService().load(BfnXmlTransformer.UUID_TAX_NR_IDENTIFIER_TYPE), DefinedTerm.class);
54
	    for (Classification classification : list) {
55
            List<TaxonNode> childNodes = classification.getChildNodes();
56
            for (TaxonNode taxonNode : childNodes) {
57
                Set<TaxonRelationship> relationsFromThisTaxon = taxonNode.getTaxon().getRelationsFromThisTaxon();
58
                for (TaxonRelationship taxonRelationship : relationsFromThisTaxon) {
59
                    UUID uuid = taxonRelationship.getUuid();
60
                    //all of the relationships should have been processed
61
                    //when iterating over the taxa of the first classification
62
                    //if there are only 2 of them
63
                    if(!state.getKnownConceptRelations().contains(uuid)) {
64
                        if(konzeptbeziehungen==null){
65
                            konzeptbeziehungen = new Element(BfnXmlConstants.EL_KONZEPTBEZIEHUNGEN);
66
                            document.getRootElement().addContent(konzeptbeziehungen);
67
                        }
68
                        Element konzeptbeziehung = new Element(BfnXmlConstants.EL_KONZEPTBEZIEHUNG);
69
                        konzeptbeziehungen.addContent(konzeptbeziehung);
70

    
71
                        state.getKnownConceptRelations().add(uuid);
72

    
73
                        //fromTaxon = TAXONYM1
74
                        Taxon fromTaxon = taxonRelationship.getFromTaxon();
75
                        Element taxonym1 = new Element(BfnXmlConstants.EL_TAXONYM1);
76
                        konzeptbeziehung.addContent(taxonym1);
77
                        addConceptTaxonym(taxNrIdentifierType, taxonym1, fromTaxon);
78

    
79
                        //toTaxon = TAXONYM2
80
                        Taxon toTaxon = taxonRelationship.getToTaxon();
81
                        Element taxonym2 = new Element(BfnXmlConstants.EL_TAXONYM2);
82
                        konzeptbeziehung.addContent(taxonym2);
83
                        addConceptTaxonym(taxNrIdentifierType, taxonym2, toTaxon);
84

    
85
                        //relation type
86
                        Element konzeptSynonymStatus = new Element(BfnXmlConstants.EL_STATUS);
87
                        konzeptbeziehung.addContent(konzeptSynonymStatus);
88
                        konzeptSynonymStatus.setAttribute(BfnXmlConstants.ATT_STANDARDNAME, BfnXmlConstants.ATT_STATUS_KONZEPTSYNONYM_STATUS);
89
                        konzeptSynonymStatus.addContent(BfnXmlTransformer.getConceptCodeForTaxonRelation(taxonRelationship.getType()));
90
                    }
91
                }
92
            }
93
        }
94

    
95
	}
96

    
97
    private void addConceptTaxonym(DefinedTerm taxNrIdentifierType, Element taxonym, Taxon taxon) {
98
        String taxNr = getTaxNr(taxon, taxNrIdentifierType);
99
        if(taxon.getTaxonNodes().size()>1){
100
            logger.error(taxon+" has more than one taxon node. First one is used");
101
        }
102
        Classification classification = taxon.getTaxonNodes().iterator().next().getClassification();
103
        taxonym.setAttribute(BfnXmlConstants.ATT_TAXNR, taxNr);
104
        taxonym.setAttribute(BfnXmlConstants.ATT_QUELLE, classification.getTitleCache());
105
        addIwert(taxonym, BfnXmlConstants.ATT_NAME, taxon.getName().getTitleCache());
106
    }
107

    
108
    private String getTaxNr(Taxon taxon, DefinedTerm taxNrIdentifierType) {
109
        String taxNr = null;
110
        Set<String> identifiers = taxon.getIdentifiers(taxNrIdentifierType);
111
        if(identifiers.size()==1){
112
            taxNr = identifiers.iterator().next();
113
        }
114
        else{
115
            logger.error("Taxon "+taxon.getTitleCache()+" has none or multiple identifiers of type 'taxNr'");
116
        }
117
        return taxNr;
118
    }
119

    
120
    /**
121
     * {@inheritDoc}
122
     */
123
    @Override
124
    protected boolean doCheck(BfnXmlExportState state) {
125
        return false;
126
    }
127

    
128
    /**
129
     * {@inheritDoc}
130
     */
131
    @Override
132
    protected boolean isIgnore(BfnXmlExportState state) {
133
        return false;
134
    }
135

    
136
}
(2-2/6)