Project

General

Profile

Download (5.07 KB) Statistics
| Branch: | Tag: | 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

    
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.List;
17
import java.util.Set;
18

    
19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21
import org.springframework.transaction.TransactionStatus;
22

    
23
import eu.etaxonomy.cdm.model.common.CdmBase;
24
import eu.etaxonomy.cdm.model.common.Language;
25
import eu.etaxonomy.cdm.model.common.LanguageString;
26
import eu.etaxonomy.cdm.model.description.DescriptionBase;
27
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
28
import eu.etaxonomy.cdm.model.media.Media;
29
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
30
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
31
import eu.etaxonomy.cdm.model.taxon.Taxon;
32
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
33

    
34
/**
35
 * @author a.mueller
36
 * @created 20.04.2011
37
 */
38
@Component
39
public class DwcaImageExport extends DwcaExportBase {
40
	private static final Logger logger = Logger.getLogger(DwcaImageExport.class);
41

    
42
	private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/Image";
43
	private static final String fileName = "images.txt";
44
	
45
	/**
46
	 * Constructor
47
	 */
48
	public DwcaImageExport() {
49
		super();
50
		this.ioName = this.getClass().getSimpleName();
51
	}
52

    
53
	/** Retrieves data from a CDM DB and serializes them CDM to XML.
54
	 * Starts with root taxa and traverses the classification to retrieve children taxa, synonyms and relationships.
55
	 * Taxa that are not part of the classification are not found.
56
	 * 
57
	 * @param exImpConfig
58
	 * @param dbname
59
	 * @param filename
60
	 */
61
	@Override
62
	protected void doInvoke(DwcaTaxExportState state){
63
		DwcaTaxExportConfigurator config = state.getConfig();
64
		TransactionStatus txStatus = startTransaction(true);
65

    
66
		PrintWriter writer = null;
67
		try {
68
			writer = createPrintWriter(fileName, state);
69
			DwcaMetaDataRecord metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
70
			state.addMetaRecord(metaRecord);
71

    
72
			List<TaxonNode> allNodes =  getAllNodes(null);
73
			for (TaxonNode node : allNodes){
74
				Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
75
				Set<? extends DescriptionBase<?>> descriptions = taxon.getDescriptions();
76
				for (DescriptionBase<?> description : descriptions){
77
					for (Object o : description.getElements()){
78
						DescriptionElementBase el = CdmBase.deproxy(o, DescriptionElementBase.class);
79
						if (el.getMedia().size() > 0){
80
							for (Media media: el.getMedia()){
81
								for (MediaRepresentation repr : media.getRepresentations()){
82
									for (MediaRepresentationPart part : repr.getParts()){
83
										if (! this.recordExists(part)){
84
											DwcaImageRecord record = new DwcaImageRecord(metaRecord, config);
85
											handleMedia(record, media, repr, part, taxon);
86
											record.write(writer);
87
											this.addExistingRecord(part);
88
										}
89
									}
90
								}
91
							}
92
						}
93
					}
94
				}
95
				
96
				writer.flush();
97
				
98
			}
99
		} catch (FileNotFoundException e) {
100
			e.printStackTrace();
101
		} catch (UnsupportedEncodingException e) {
102
			e.printStackTrace();
103
		} catch (ClassCastException e) {
104
			e.printStackTrace();
105
		} catch (IOException e) {
106
			e.printStackTrace();
107
		}finally {
108
			closeWriter(writer, state);
109
		}
110
		
111
		commitTransaction(txStatus);
112
		return;
113
	}
114
	
115

    
116

    
117

    
118
	private void handleMedia(DwcaImageRecord record, Media media, MediaRepresentation repr, MediaRepresentationPart part, Taxon taxon) {
119
		record.setId(taxon.getId());
120
		record.setUuid(taxon.getUuid());
121
		if (part.getUri() == null){
122
			String message = "No uri available for media ("+media.getId()+"). URI is required field. Taxon: " + this.getTaxonLogString(taxon);
123
			logger.warn(message);
124
		}
125
		record.setIdentifier(part.getUri());
126
		record.setTitle(media.getTitleCache());
127
		//TODO description if default language description is not available
128
		LanguageString description = media.getDescription(Language.DEFAULT());
129
		record.setDescription(description == null ? null: description.getText());
130
		//TODO missing
131
		record.setSpatial(null);
132
		//TODO missing
133
		record.setCoordinates(null);
134
		record.setFormat(repr.getMimeType());
135
		//FIXME missing ??
136
		record.setLicense(media.getRights());
137
		record.setCreated(media.getMediaCreated());
138
		record.setCreator(media.getArtist());
139
		//TODO missing
140
		record.setContributor(null);
141
		//TODO missing
142
		record.setPublisher(null);
143
		//TODO missing
144
		record.setAudience(null);
145
	}
146
	
147
	@Override
148
	protected boolean doCheck(DwcaTaxExportState state) {
149
		boolean result = true;
150
		logger.warn("No check implemented for " + this.ioName);
151
		return result;
152
	}
153

    
154

    
155
	@Override
156
	protected boolean isIgnore(DwcaTaxExportState state) {
157
		return ! state.getConfig().isDoImages();
158
	}
159
	
160
}
(9-9/30)