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
|
|
10
|
package eu.etaxonomy.cdm.io.dwca.out;
|
11
|
|
12
|
import java.io.FileNotFoundException;
|
13
|
import java.io.IOException;
|
14
|
import java.io.PrintWriter;
|
15
|
import java.io.UnsupportedEncodingException;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
|
20
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
21
|
import eu.etaxonomy.cdm.model.common.Language;
|
22
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
23
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
24
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
25
|
import eu.etaxonomy.cdm.model.media.Media;
|
26
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
27
|
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
|
28
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
29
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
30
|
|
31
|
/**
|
32
|
* @author a.mueller
|
33
|
* @created 20.04.2011
|
34
|
*/
|
35
|
public class DwcaImageExport extends DwcaDataExportBase {
|
36
|
private static final long serialVersionUID = -4997807762779037215L;
|
37
|
|
38
|
private static final Logger logger = Logger.getLogger(DwcaImageExport.class);
|
39
|
|
40
|
private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/Image";
|
41
|
protected static final String fileName = "images.txt";
|
42
|
|
43
|
private DwcaMetaDataRecord metaRecord;
|
44
|
|
45
|
/**
|
46
|
* Constructor
|
47
|
*/
|
48
|
public DwcaImageExport(DwcaTaxExportState state) {
|
49
|
super();
|
50
|
this.ioName = this.getClass().getSimpleName();
|
51
|
metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
|
52
|
state.addMetaRecord(metaRecord);
|
53
|
file = DwcaTaxExportFile.IMAGE;
|
54
|
}
|
55
|
|
56
|
@Override
|
57
|
protected void doInvoke(DwcaTaxExportState state){}
|
58
|
|
59
|
/**
|
60
|
* @param state
|
61
|
* @param node
|
62
|
* @throws IOException
|
63
|
* @throws FileNotFoundException
|
64
|
* @throws UnsupportedEncodingException
|
65
|
*/
|
66
|
@Override
|
67
|
protected void handleTaxonNode(DwcaTaxExportState state, TaxonNode node)
|
68
|
throws IOException, FileNotFoundException, UnsupportedEncodingException {
|
69
|
try {
|
70
|
DwcaTaxExportConfigurator config = state.getConfig();
|
71
|
Taxon taxon = CdmBase.deproxy(node.getTaxon());
|
72
|
Set<? extends DescriptionBase<?>> descriptions = taxon.getDescriptions();
|
73
|
for (DescriptionBase<?> description : descriptions){
|
74
|
for (DescriptionElementBase o : description.getElements()){
|
75
|
DescriptionElementBase el = CdmBase.deproxy(o);
|
76
|
if (el.getMedia().size() > 0){
|
77
|
for (Media media: el.getMedia()){
|
78
|
for (MediaRepresentation repr : media.getRepresentations()){
|
79
|
for (MediaRepresentationPart part : repr.getParts()){
|
80
|
if (! state.recordExists(file, part)){
|
81
|
DwcaImageRecord record = new DwcaImageRecord(metaRecord, config);
|
82
|
handleMedia(state, record, media, repr, part, taxon);
|
83
|
PrintWriter writer = createPrintWriter(state, file);
|
84
|
record.write(state, writer);
|
85
|
state.addExistingRecord(file,part);
|
86
|
}
|
87
|
}
|
88
|
}
|
89
|
}
|
90
|
}
|
91
|
}
|
92
|
}
|
93
|
|
94
|
} catch (Exception e) {
|
95
|
String message = "Unexpected exception: " + e.getMessage();
|
96
|
state.getResult().addException(e, message);
|
97
|
}finally{
|
98
|
flushWriter(state, file);
|
99
|
}
|
100
|
}
|
101
|
|
102
|
|
103
|
|
104
|
|
105
|
private void handleMedia(DwcaTaxExportState state, DwcaImageRecord record, Media media, MediaRepresentation repr, MediaRepresentationPart part, Taxon taxon) {
|
106
|
record.setId(taxon.getId());
|
107
|
record.setUuid(taxon.getUuid());
|
108
|
if (part.getUri() == null){
|
109
|
String message = "No uri available for media ("+media.getId()+"). URI is required field. Taxon: " + this.getTaxonLogString(taxon);
|
110
|
state.getResult().addWarning(message);
|
111
|
}
|
112
|
record.setIdentifier(part.getUri());
|
113
|
record.setTitle(media.getTitleCache());
|
114
|
//TODO description if default language description is not available
|
115
|
LanguageString description = media.getDescription(Language.DEFAULT());
|
116
|
record.setDescription(description == null ? null: description.getText());
|
117
|
//TODO missing
|
118
|
record.setSpatial(null);
|
119
|
//TODO missing
|
120
|
record.setCoordinates(null);
|
121
|
record.setFormat(repr.getMimeType());
|
122
|
//FIXME missing ??
|
123
|
record.setLicense(media.getRights());
|
124
|
record.setCreated(media.getMediaCreated());
|
125
|
record.setCreator(media.getArtist());
|
126
|
//TODO missing
|
127
|
record.setContributor(null);
|
128
|
//TODO missing
|
129
|
record.setPublisher(null);
|
130
|
//TODO missing
|
131
|
record.setAudience(null);
|
132
|
}
|
133
|
|
134
|
@Override
|
135
|
protected boolean doCheck(DwcaTaxExportState state) {
|
136
|
boolean result = true;
|
137
|
logger.warn("No check implemented for " + this.ioName);
|
138
|
return result;
|
139
|
}
|
140
|
|
141
|
@Override
|
142
|
public boolean isIgnore(DwcaTaxExportState state) {
|
143
|
return ! state.getConfig().isDoImages();
|
144
|
}
|
145
|
|
146
|
}
|