Project

General

Profile

Download (5.28 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.File;
13
import java.io.FileNotFoundException;
14
import java.io.FileOutputStream;
15
import java.io.IOException;
16
import java.io.OutputStreamWriter;
17
import java.io.PrintWriter;
18
import java.io.UnsupportedEncodingException;
19
import java.util.List;
20
import java.util.Set;
21

    
22
import org.apache.log4j.Logger;
23
import org.springframework.stereotype.Component;
24
import org.springframework.transaction.TransactionStatus;
25

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

    
37
/**
38
 * @author a.mueller
39
 * @created 20.04.2011
40
 */
41
@Component
42
public class DwcaImageExport extends DwcaExportBase {
43
	private static final Logger logger = Logger.getLogger(DwcaImageExport.class);
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 boolean doInvoke(DwcaTaxExportState state){
63
		DwcaTaxExportConfigurator config = state.getConfig();
64
		String dbname = config.getSource() != null ? config.getSource().getName() : "unknown";
65
    	String fileName = config.getDestinationNameString();
66
		logger.info("Serializing DB " + dbname + " to file " + fileName);
67
		TransactionStatus txStatus = startTransaction(true);
68

    
69
		try {
70
			
71
			final String coreTaxFileName = "images.txt";
72
			fileName = fileName + File.separatorChar + coreTaxFileName;
73
			File f = new File(fileName);
74
			if (!f.exists()){
75
				f.createNewFile();
76
			}
77
			FileOutputStream fos = new FileOutputStream(f);
78
			PrintWriter writer = new PrintWriter(new OutputStreamWriter(fos, "UTF8"), true);
79

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

    
123

    
124

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

    
160

    
161
	@Override
162
	protected boolean isIgnore(DwcaTaxExportState state) {
163
		return ! state.getConfig().isDoImages();
164
	}
165
	
166
}
(6-6/23)