2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.dwca
.out
;
12 import java
.io
.FileNotFoundException
;
13 import java
.io
.IOException
;
14 import java
.io
.PrintWriter
;
15 import java
.io
.UnsupportedEncodingException
;
16 import java
.util
.List
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.stereotype
.Component
;
22 import org
.springframework
.transaction
.TransactionStatus
;
24 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
25 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
26 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
27 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
28 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipTermBase
;
30 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
31 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
32 import eu
.etaxonomy
.cdm
.model
.description
.TaxonInteraction
;
33 import eu
.etaxonomy
.cdm
.model
.name
.INonViralName
;
34 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
35 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
40 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
43 * Mapps taxon concept relationships, taxon name relationships and taxon interactions
48 public class DwcaResourceRelationExport
extends DwcaExportBase
{
49 private static final Logger logger
= Logger
.getLogger(DwcaResourceRelationExport
.class);
51 private static final String ROW_TYPE
= "http://rs.tdwg.org/dwc/terms/ResourceRelationship";
52 private static final String fileName
= "resourceRelationship.txt";
57 public DwcaResourceRelationExport() {
59 this.ioName
= this.getClass().getSimpleName();
62 /** Retrieves data from a CDM DB and serializes them CDM to XML.
63 * Starts with root taxa and traverses the classification to retrieve children taxa, synonyms and relationships.
64 * Taxa that are not part of the classification are not found.
71 protected void doInvoke(DwcaTaxExportState state
){
72 DwcaTaxExportConfigurator config
= state
.getConfig();
73 TransactionStatus txStatus
= startTransaction(true);
75 PrintWriter writer
= null;
78 writer
= createPrintWriter(fileName
, state
);
80 DwcaMetaDataRecord metaRecord
= new DwcaMetaDataRecord(! IS_CORE
, fileName
, ROW_TYPE
);
81 state
.addMetaRecord(metaRecord
);
83 List
<TaxonNode
> allNodes
= getAllNodes(null);
85 for (TaxonNode node
: allNodes
){
86 Taxon taxon
= CdmBase
.deproxy(node
.getTaxon(), Taxon
.class);
89 Set
<TaxonDescription
> descriptions
= taxon
.getDescriptions();
90 for (TaxonDescription description
: descriptions
){
91 for (DescriptionElementBase el
: description
.getElements()){
92 if (el
.isInstanceOf(TaxonInteraction
.class)){
93 DwcaResourceRelationRecord record
= new DwcaResourceRelationRecord(metaRecord
, config
);
94 TaxonInteraction taxonInteraction
= CdmBase
.deproxy(el
,TaxonInteraction
.class);
95 if (! this.recordExistsUuid(taxonInteraction
)){
96 handleInteraction(record
, taxon
, taxonInteraction
);
98 this.addExistingRecordUuid(taxonInteraction
);
104 //concept relationships
105 for (TaxonRelationship rel
: taxon
.getTaxonRelations()){
106 DwcaResourceRelationRecord record
= new DwcaResourceRelationRecord(metaRecord
, config
);
107 IdentifiableEntity
<?
> subject
= rel
.getFromTaxon();
108 IdentifiableEntity
<?
> object
= rel
.getToTaxon();
110 if (rel
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR()) ){
111 //misapplied names are handled in core (tax)
114 if (! this.recordExistsUuid(rel
)){
115 handleRelationship(record
, subject
, object
, rel
, false);
116 record
.write(writer
);
117 this.addExistingRecordUuid(rel
);
124 INonViralName name
= taxon
.getName();
126 String message
= "There is a taxon node without taxon: " + node
.getId();
127 logger
.warn(message
);
130 Set
<NameRelationship
> rels
= name
.getNameRelations();
131 for (NameRelationship rel
: rels
){
132 DwcaResourceRelationRecord record
= new DwcaResourceRelationRecord(metaRecord
, config
);
133 IdentifiableEntity
<?
> subject
= CdmBase
.deproxy(rel
.getFromName(), TaxonNameBase
.class);
134 IdentifiableEntity
<?
> object
= CdmBase
.deproxy(rel
.getToName(), TaxonNameBase
.class);
135 boolean isInverse
= false;
138 }else if(object
== name
){
143 String message
= "Both, subject and object, are not part of the relationship for " + name
.getTitleCache();
144 logger
.warn(message
);
147 if (! this.recordExistsUuid(rel
)){
149 handleRelationship(record
, subject
, object
, rel
, isInverse
);
150 record
.write(writer
);
151 this.addExistingRecordUuid(rel
);
160 } catch (FileNotFoundException e
) {
162 } catch (UnsupportedEncodingException e
) {
164 } catch (ClassCastException e
) {
166 } catch (IOException e
) {
169 closeWriter(writer
, state
);
171 commitTransaction(txStatus
);
175 private void handleRelationship(DwcaResourceRelationRecord record
, IdentifiableEntity
<?
> subject
, IdentifiableEntity
<?
> object
,
176 RelationshipBase
<?
,?
,?
> rel
, boolean isInverse
) {
177 RelationshipTermBase
<?
> type
= rel
.getType();
179 record
.setId(subject
.getId());
180 record
.setUuid(subject
.getUuid());
183 record
.setResourceRelationshipId(rel
.getId());
184 record
.setResourceRelationshipId(rel
.getUuid());
185 //TODO id / uuid / names ??
186 if (object
.isInstanceOf(TaxonBase
.class)){
187 record
.setRelatedResourceId(object
.getUuid());
189 //TODO transform to controlled voc
192 relTypeLabel
= type
.getInverseLabel();
194 relTypeLabel
= type
.getLabel();
196 record
.setRelationshipOfResource(relTypeLabel
);
197 record
.setRelationshipAccordingTo(rel
.getCitation()== null?
null : rel
.getCitation().getTitleCache());
199 record
.setRelatioshipEstablishedDate(null);
200 record
.setRelationshipRemarks(rel
.getAnnotations());
201 if (object
.isInstanceOf(TaxonNameBase
.class)){
202 record
.setScientificName(object
.getTitleCache());
208 private void handleInteraction(DwcaResourceRelationRecord record
, IdentifiableEntity
<?
> subject
, TaxonInteraction interaction
) {
209 Taxon object
= interaction
.getTaxon2();
210 Map
<Language
, LanguageString
> description
= interaction
.getDescription();
212 record
.setId(subject
.getId());
213 record
.setUuid(subject
.getUuid());
215 record
.setRelatedResourceId(object
.getUuid());
216 //TODO transform to controlled voc
217 if (description
!= null && description
.get(Language
.DEFAULT()) != null){
218 record
.setRelationshipOfResource(description
.get(Language
.DEFAULT()).getText());
220 record
.setRelationshipOfResource(interaction
.getFeature().getLabel());
223 record
.setResourceRelationshipId(interaction
.getId());
224 record
.setResourceRelationshipId(interaction
.getUuid());
226 //FIXME multiple sources
227 record
.setRelationshipAccordingTo(null);
229 record
.setRelatioshipEstablishedDate(null);
230 record
.setRelationshipRemarks(interaction
.getAnnotations());
231 //TODO does this need to be filled?
232 record
.setScientificName(null);
238 protected boolean doCheck(DwcaTaxExportState state
) {
239 boolean result
= true;
240 logger
.warn("No check implemented for " + this.ioName
);
246 protected boolean isIgnore(DwcaTaxExportState state
) {
247 return ! state
.getConfig().isDoResourceRelation();