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
.HashSet
;
17 import java
.util
.List
;
19 import java
.util
.UUID
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.springframework
.stereotype
.Component
;
23 import org
.springframework
.transaction
.TransactionStatus
;
25 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
26 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
27 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
28 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
29 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
30 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
31 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentation
;
32 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentationPart
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
42 public class DwcaImageExport
extends DwcaExportBase
{
43 private static final Logger logger
= Logger
.getLogger(DwcaImageExport
.class);
45 private static final String ROW_TYPE
= "http://rs.gbif.org/terms/1.0/Image";
46 private static final String fileName
= "images.txt";
51 public DwcaImageExport() {
53 this.ioName
= this.getClass().getSimpleName();
56 /** Retrieves data from a CDM DB and serializes them CDM to XML.
57 * Starts with root taxa and traverses the classification to retrieve children taxa, synonyms and relationships.
58 * Taxa that are not part of the classification are not found.
65 protected void doInvoke(DwcaTaxExportState state
){
66 DwcaTaxExportConfigurator config
= state
.getConfig();
67 TransactionStatus txStatus
= startTransaction(true);
69 PrintWriter writer
= null;
71 writer
= createPrintWriter(fileName
, state
);
72 DwcaMetaDataRecord metaRecord
= new DwcaMetaDataRecord(! IS_CORE
, fileName
, ROW_TYPE
);
73 state
.addMetaRecord(metaRecord
);
75 Set
<UUID
> classificationUuidSet
= config
.getClassificationUuids();
76 List
<Classification
> classificationList
;
77 if (classificationUuidSet
.isEmpty()){
78 classificationList
= getClassificationService().list(Classification
.class, null, 0, null, null);
80 classificationList
= getClassificationService().find(classificationUuidSet
);
83 Set
<Classification
> classificationSet
= new HashSet
<Classification
>();
84 classificationSet
.addAll(classificationList
);
85 List
<TaxonNode
> allNodes
;
87 if (state
.getAllNodes().isEmpty()){
88 getAllNodes(state
, classificationSet
);
90 allNodes
= state
.getAllNodes();
91 for (TaxonNode node
: allNodes
){
92 Taxon taxon
= CdmBase
.deproxy(node
.getTaxon(), Taxon
.class);
93 Set
<?
extends DescriptionBase
<?
>> descriptions
= taxon
.getDescriptions();
94 for (DescriptionBase
<?
> description
: descriptions
){
95 for (Object o
: description
.getElements()){
96 DescriptionElementBase el
= CdmBase
.deproxy(o
, DescriptionElementBase
.class);
97 if (el
.getMedia().size() > 0){
98 for (Media media
: el
.getMedia()){
99 for (MediaRepresentation repr
: media
.getRepresentations()){
100 for (MediaRepresentationPart part
: repr
.getParts()){
101 if (! this.recordExists(part
)){
102 DwcaImageRecord record
= new DwcaImageRecord(metaRecord
, config
);
103 handleMedia(record
, media
, repr
, part
, taxon
);
104 record
.write(writer
);
105 this.addExistingRecord(part
);
117 } catch (FileNotFoundException e
) {
119 } catch (UnsupportedEncodingException e
) {
121 } catch (ClassCastException e
) {
123 } catch (IOException e
) {
126 closeWriter(writer
, state
);
129 commitTransaction(txStatus
);
136 private void handleMedia(DwcaImageRecord record
, Media media
, MediaRepresentation repr
, MediaRepresentationPart part
, Taxon taxon
) {
137 record
.setId(taxon
.getId());
138 record
.setUuid(taxon
.getUuid());
139 if (part
.getUri() == null){
140 String message
= "No uri available for media ("+media
.getId()+"). URI is required field. Taxon: " + this.getTaxonLogString(taxon
);
141 logger
.warn(message
);
143 record
.setIdentifier(part
.getUri());
144 record
.setTitle(media
.getTitleCache());
145 //TODO description if default language description is not available
146 LanguageString description
= media
.getDescription(Language
.DEFAULT());
147 record
.setDescription(description
== null ?
null: description
.getText());
149 record
.setSpatial(null);
151 record
.setCoordinates(null);
152 record
.setFormat(repr
.getMimeType());
154 record
.setLicense(media
.getRights());
155 record
.setCreated(media
.getMediaCreated());
156 record
.setCreator(media
.getArtist());
158 record
.setContributor(null);
160 record
.setPublisher(null);
162 record
.setAudience(null);
166 protected boolean doCheck(DwcaTaxExportState state
) {
167 boolean result
= true;
168 logger
.warn("No check implemented for " + this.ioName
);
174 protected boolean isIgnore(DwcaTaxExportState state
) {
175 return ! state
.getConfig().isDoImages();