Project

General

Profile

« Previous | Next » 

Revision 54649625

Added by Andreas Müller almost 13 years ago

Dwca images export first implementation

View differences:

.gitattributes
253 253
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaDistributionExport.java -text
254 254
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaDistributionRecord.java -text
255 255
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaExportBase.java -text
256
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaImagesExport.java -text
257
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaImagesRecord.java -text
256 258
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaRecordBase.java -text
257 259
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaTaxExport.java -text
258 260
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaTaxExportConfigurator.java -text
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaDistributionExport.java
65 65

  
66 66
		try {
67 67
			
68
			final String coreTaxFileName = "vernacular.txt";
68
			final String coreTaxFileName = "distribution.txt";
69 69
			fileName = fileName + File.separatorChar + coreTaxFileName;
70 70
			File f = new File(fileName);
71 71
			if (!f.exists()){
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaImagesExport.java
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.description.DescriptionBase;
29
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
30
import eu.etaxonomy.cdm.model.description.Distribution;
31
import eu.etaxonomy.cdm.model.description.Feature;
32
import eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity;
33
import eu.etaxonomy.cdm.model.media.Media;
34
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
35
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
36
import eu.etaxonomy.cdm.model.taxon.Taxon;
37
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
38

  
39
/**
40
 * @author a.mueller
41
 * @created 18.04.2011
42
 */
43
@Component
44
public class DwcaImagesExport extends DwcaExportBase {
45
	private static final Logger logger = Logger.getLogger(DwcaImagesExport.class);
46

  
47
	/**
48
	 * Constructor
49
	 */
50
	public DwcaImagesExport() {
51
		super();
52
		this.ioName = this.getClass().getSimpleName();
53
	}
54

  
55
	/** Retrieves data from a CDM DB and serializes them CDM to XML.
56
	 * Starts with root taxa and traverses the classification to retrieve children taxa, synonyms and relationships.
57
	 * Taxa that are not part of the classification are not found.
58
	 * 
59
	 * @param exImpConfig
60
	 * @param dbname
61
	 * @param filename
62
	 */
63
	@Override
64
	protected boolean doInvoke(DwcaTaxExportState state){
65
		DwcaTaxExportConfigurator config = state.getConfig();
66
		String dbname = config.getSource() != null ? config.getSource().getName() : "unknown";
67
    	String fileName = config.getDestinationNameString();
68
		logger.info("Serializing DB " + dbname + " to file " + fileName);
69
		TransactionStatus txStatus = startTransaction(true);
70

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

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

  
122

  
123

  
124
	private void handleMedia(DwcaImagesRecord record, Media media, MediaRepresentation repr, MediaRepresentationPart part, Taxon taxon) {
125
		record.setCoreid(taxon.getId());
126
		record.setIdentifier(part.getUri());
127
		record.setTitle(media.getTitleCache());
128
		//TODO description if default language description is not available
129
		record.setDescription(media.getDescription(Language.DEFAULT()).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 Jaxb export");
151
		return result;
152
	}
153

  
154

  
155
	@Override
156
	protected boolean isIgnore(DwcaTaxExportState state) {
157
		return false;
158
	}
159
	
160
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaImagesRecord.java
1
// $Id$
2
/**
3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.io.dwca.out;
11

  
12
import java.io.PrintWriter;
13
import java.net.URI;
14
import java.util.Set;
15

  
16
import org.apache.log4j.Logger;
17
import org.joda.time.DateTime;
18

  
19
import eu.etaxonomy.cdm.model.agent.AgentBase;
20
import eu.etaxonomy.cdm.model.location.Point;
21
import eu.etaxonomy.cdm.model.media.Rights;
22

  
23
/**
24
 * @author a.mueller
25
 * @date 20.04.2011
26
 *
27
 */
28
public class DwcaImagesRecord extends DwcaRecordBase{
29
	@SuppressWarnings("unused")
30
	private static final Logger logger = Logger.getLogger(DwcaImagesRecord.class);
31
	private Integer coreid;
32
	private URI identifier;
33
	private String title;
34
	private String description;
35
	
36
	//TODO ??
37
	private String spatial;
38
	private Point coordinates;
39
	private String format;
40
	private Set<Rights> license;
41
	private DateTime created;
42
	private AgentBase<?> creator;
43
	private AgentBase<?> contributor;
44
	private AgentBase<?> publisher;
45
	
46

  
47

  
48

  
49
	private String audience;
50
	
51
	public void write(PrintWriter writer) {
52
		print(coreid, writer, IS_FIRST);
53
		print(identifier, writer, IS_NOT_FIRST);
54
		print(title, writer, IS_NOT_FIRST);
55
		print(description, writer, IS_NOT_FIRST);
56
		print(spatial, writer, IS_NOT_FIRST);
57
		print(coordinates, writer, IS_NOT_FIRST);
58
		print(license, writer, IS_NOT_FIRST);
59
		print(getDate(created), writer, IS_NOT_FIRST);
60
		print(getAgent(creator), writer, IS_NOT_FIRST);
61
		print(getAgent(contributor), writer, IS_NOT_FIRST);
62
		print(getAgent(publisher), writer, IS_NOT_FIRST);
63
		print(audience, writer, IS_NOT_FIRST);
64

  
65
		writer.println();
66
	}
67

  
68

  
69
	public Integer getCoreid() {
70
		return coreid;
71
	}
72

  
73
	public void setCoreid(Integer coreid) {
74
		this.coreid = coreid;
75
	}
76
	
77
	public URI getIdentifier() {
78
		return identifier;
79
	}
80
	
81
	public void setIdentifier(URI identifier) {
82
		this.identifier = identifier;
83
	}
84

  
85
	public String getTitle() {
86
		return title;
87
	}
88

  
89
	public void setTitle(String title) {
90
		this.title = title;
91
	}
92

  
93
	public String getDescription() {
94
		return description;
95
	}
96

  
97
	public void setDescription(String description) {
98
		this.description = description;
99
	}
100

  
101
	public String getSpatial() {
102
		return spatial;
103
	}
104

  
105
	public void setSpatial(String spatial) {
106
		this.spatial = spatial;
107
	}
108

  
109
	public Point getCoordinates() {
110
		return coordinates;
111
	}
112

  
113
	public void setCoordinates(Point coordinates) {
114
		this.coordinates = coordinates;
115
	}
116

  
117
	public String getFormat() {
118
		return format;
119
	}
120

  
121
	public void setFormat(String format) {
122
		this.format = format;
123
	}
124

  
125
	public Set<Rights> getLicense() {
126
		return license;
127
	}
128

  
129
	public void setLicense(Set<Rights> set) {
130
		this.license = set;
131
	}
132

  
133
	public DateTime getCreated() {
134
		return created;
135
	}
136

  
137
	public void setCreated(DateTime created) {
138
		this.created = created;
139
	}
140

  
141
	public AgentBase<?> getCreator() {
142
		return creator;
143
	}
144

  
145
	public void setCreator(AgentBase<?> creator) {
146
		this.creator = creator;
147
	}
148

  
149
	public AgentBase<?> getContributor() {
150
		return contributor;
151
	}
152

  
153
	public void setContributor(AgentBase<?> contributor) {
154
		this.contributor = contributor;
155
	}
156

  
157
	public AgentBase<?> getPublisher() {
158
		return publisher;
159
	}
160

  
161
	public void setPublisher(AgentBase<?> publisher) {
162
		this.publisher = publisher;
163
	}
164

  
165
	public String getAudience() {
166
		return audience;
167
	}
168

  
169
	public void setAudience(String audience) {
170
		this.audience = audience;
171
	}
172

  
173

  
174
	
175
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaRecordBase.java
10 10
package eu.etaxonomy.cdm.io.dwca.out;
11 11

  
12 12
import java.io.PrintWriter;
13
import java.net.URI;
14
import java.util.Set;
13 15

  
14 16
import org.apache.commons.lang.StringUtils;
15 17
import org.apache.log4j.Logger;
16 18
import org.joda.time.DateTime;
17 19
import org.joda.time.Partial;
18 20

  
19
import eu.etaxonomy.cdm.model.common.CdmBase;
21
import eu.etaxonomy.cdm.common.CdmUtils;
22
import eu.etaxonomy.cdm.model.agent.AgentBase;
20 23
import eu.etaxonomy.cdm.model.common.Language;
21 24
import eu.etaxonomy.cdm.model.common.TimePeriod;
22 25
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
23 26
import eu.etaxonomy.cdm.model.description.Sex;
24 27
import eu.etaxonomy.cdm.model.description.Stage;
25
import eu.etaxonomy.cdm.model.location.NamedArea;
26
import eu.etaxonomy.cdm.model.location.WaterbodyOrCountry;
28
import eu.etaxonomy.cdm.model.location.Point;
27 29
import eu.etaxonomy.cdm.model.media.Rights;
28 30
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
29 31
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
......
38 40
	@SuppressWarnings("unused")
39 41
	private static final Logger logger = Logger.getLogger(DwcaRecordBase.class);
40 42

  
43
	protected static final CharSequence COLLECTION_SEPARATOR = null;
44

  
41 45
	protected static String FIELD_ENCLOSER = "\"";
42 46

  
43 47
	final protected boolean IS_FIRST = false;
......
50 54
	public abstract void write(PrintWriter writer);
51 55
	
52 56

  
57
	protected void print(Set<Rights> rights, PrintWriter writer, boolean addSeparator) {
58
		String rightsString = getRights(rights);
59
		print(rightsString, writer, addSeparator);
60
	}
61
	protected void print(URI uri, PrintWriter writer, boolean addSeparator) {
62
		print(uri == null ? null : String.valueOf(uri), writer, addSeparator);
63
	}
64
	
65
	protected void print(Point point, PrintWriter writer, boolean addSeparator) {
66
		if (point == null){
67
			String toPrint = null;
68
			print(toPrint, writer, addSeparator);
69
			print(toPrint, writer, addSeparator);
70
		}else{
71
			String latitude = point.getLatitude().toString();
72
			String longitude = point.getLongitude().toString();
73
			print(latitude, writer, addSeparator);
74
			print(longitude, writer, addSeparator);
75
		}
76
	}
53 77
	protected void print(Boolean boolValue, PrintWriter writer, boolean addSeparator) {
54 78
		print(boolValue == null ? null : String.valueOf(boolValue), writer, addSeparator);
55 79
	}
......
148 172
		}
149 173
	}
150 174
	
175
	protected String getAgent(AgentBase agent) {
176
		if (agent == null){
177
			return "";
178
		}else{
179
			//TODO
180
			return agent.getTitleCache();
181
		}
182
	}
183

  
184
	
151 185
	protected String getTimePeriod(TimePeriod period) {
152 186
		if (period == null){
153 187
			return "";
......
158 192
	}
159 193
	
160 194
	protected String getTimePeriodPart(TimePeriod period, boolean useEnd) {
161
		Partial date = useEnd? period.getEnd(): period.getStart();
162
		if (date == null){
195
		if (period == null){
163 196
			return "";
164 197
		}else{
165
			//TODO
166
			return date.toString();
198
			Partial date = useEnd? period.getEnd(): period.getStart();
199
			if (date == null){
200
				return "";
201
			}else{
202
				//TODO
203
				return date.toString();
204
			}
205
		}
206
	}
207

  
208
	private String getRights(Set<Rights> rights) {
209
		if (rights == null || rights.isEmpty()){
210
			return null;
211
		}else{
212
			String result = null;
213
			for (Rights right: rights){
214
				//TODO usi uri if available ??
215
				String message = "Rights not yet fully implemented";
216
				logger.warn(message);
217
				result = CdmUtils.concat(COLLECTION_SEPARATOR, result, right.getAbbreviatedText());
218
			}
219
			return result;
167 220
		}
168 221
	}
169
	
170 222
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaTaxExport.java
207 207
		record.setVerbatimTaxonRank(name.getRank().getTitleCache());
208 208
		record.setScientificNameAuthorship(name.getAuthorshipCache());
209 209
		
210
		// ???
210
		// ??? - use for TextData names?
211 211
		record.setVernacularName(null);
212 212
		
213 213
		record.setNomenclaturalCode(name.getNomenclaturalCode());
......
221 221
		// ???
222 222
		record.setLanguage(null);
223 223
		
224
		record.setRights(taxonBase.getRights());
225
		
224 226
		//....
225 227
		
226 228
		return;
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaTaxExportConfigurator.java
55 55
		ioClassList = new Class[] {
56 56
				DwcaTaxExport.class,
57 57
				DwcaVernacularExport.class,
58
				DwcaDistributionExport.class
58
				DwcaDistributionExport.class,
59
				DwcaImagesExport.class
59 60
		};
60 61
	};
61 62

  
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaTaxRecord.java
10 10
package eu.etaxonomy.cdm.io.dwca.out;
11 11

  
12 12
import java.io.PrintWriter;
13
import java.util.Set;
13 14

  
14 15
import org.apache.log4j.Logger;
15 16
import org.joda.time.DateTime;
......
68 69
	private String taxonRemarks;
69 70
	private DateTime modified;
70 71
	private Language language;
71
	private Rights rights;
72
	private Set<Rights> rights;
72 73
	private String rightsHolder;
73 74
	private String accessRights;
74 75
	private String bibliographicCitation;
......
119 120
		print(taxonRemarks, writer, IS_NOT_FIRST);
120 121
		print(getDate(modified), writer, IS_NOT_FIRST);
121 122
		print(getLanguage(language), writer, IS_NOT_FIRST);
122
		print(getRights(rights), writer, IS_NOT_FIRST);
123
		print(rights, writer, IS_NOT_FIRST);
123 124
		print(rightsHolder, writer, IS_NOT_FIRST);
124 125
		print(accessRights, writer, IS_NOT_FIRST);
125 126
		print(bibliographicCitation, writer, IS_NOT_FIRST);
......
284 285
	public void setLanguage(Language language) {
285 286
		this.language = language;
286 287
	}
287
	public Rights getRights() {
288
	public Set<Rights> getRights() {
288 289
		return rights;
289 290
	}
290
	public void setRights(Rights rights) {
291
	public void setRights(Set<Rights> rights) {
291 292
		this.rights = rights;
292 293
	}
293 294
	public String getRightsHolder() {

Also available in: Unified diff