smaller changes in DwC-A Export
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / dwca / out / DwcaImageExport.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.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;
18 import java.util.Set;
19 import java.util.UUID;
20
21 import org.apache.log4j.Logger;
22 import org.springframework.stereotype.Component;
23 import org.springframework.transaction.TransactionStatus;
24
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;
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 private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/Image";
46 private static final String fileName = "images.txt";
47
48 /**
49 * Constructor
50 */
51 public DwcaImageExport() {
52 super();
53 this.ioName = this.getClass().getSimpleName();
54 }
55
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.
59 *
60 * @param exImpConfig
61 * @param dbname
62 * @param filename
63 */
64 @Override
65 protected void doInvoke(DwcaTaxExportState state){
66 DwcaTaxExportConfigurator config = state.getConfig();
67 TransactionStatus txStatus = startTransaction(true);
68
69 PrintWriter writer = null;
70 try {
71 writer = createPrintWriter(fileName, state);
72 DwcaMetaDataRecord metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
73 state.addMetaRecord(metaRecord);
74
75 Set<UUID> classificationUuidSet = config.getClassificationUuids();
76 List<Classification> classificationList;
77 if (classificationUuidSet.isEmpty()){
78 classificationList = getClassificationService().list(Classification.class, null, 0, null, null);
79 }else{
80 classificationList = getClassificationService().find(classificationUuidSet);
81 }
82
83 Set<Classification> classificationSet = new HashSet<Classification>();
84 classificationSet.addAll(classificationList);
85 List<TaxonNode> allNodes;
86
87 if (state.getAllNodes().isEmpty()){
88 getAllNodes(state, classificationSet);
89 }
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);
106 }
107 }
108 }
109 }
110 }
111 }
112 }
113
114 writer.flush();
115
116 }
117 } catch (FileNotFoundException e) {
118 e.printStackTrace();
119 } catch (UnsupportedEncodingException e) {
120 e.printStackTrace();
121 } catch (ClassCastException e) {
122 e.printStackTrace();
123 } catch (IOException e) {
124 e.printStackTrace();
125 }finally {
126 closeWriter(writer, state);
127 }
128
129 commitTransaction(txStatus);
130 return;
131 }
132
133
134
135
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);
142 }
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());
148 //TODO missing
149 record.setSpatial(null);
150 //TODO missing
151 record.setCoordinates(null);
152 record.setFormat(repr.getMimeType());
153 //FIXME missing ??
154 record.setLicense(media.getRights());
155 record.setCreated(media.getMediaCreated());
156 record.setCreator(media.getArtist());
157 //TODO missing
158 record.setContributor(null);
159 //TODO missing
160 record.setPublisher(null);
161 //TODO missing
162 record.setAudience(null);
163 }
164
165 @Override
166 protected boolean doCheck(DwcaTaxExportState state) {
167 boolean result = true;
168 logger.warn("No check implemented for " + this.ioName);
169 return result;
170 }
171
172
173 @Override
174 protected boolean isIgnore(DwcaTaxExportState state) {
175 return ! state.getConfig().isDoImages();
176 }
177
178 }