-/**\r
-* Copyright (C) 2007 EDIT\r
-* European Distributed Institute of Taxonomy \r
-* http://www.e-taxonomy.eu\r
-* \r
-* The contents of this file are subject to the Mozilla Public License Version 1.1\r
-* See LICENSE.TXT at the top of this package for the full license terms.\r
-*/\r
-\r
-package eu.etaxonomy.cdm.io.dwca.out;\r
-\r
-import java.io.File;\r
-import java.io.FileNotFoundException;\r
-import java.io.FileOutputStream;\r
-import java.io.IOException;\r
-import java.io.OutputStreamWriter;\r
-import java.io.PrintWriter;\r
-import java.io.UnsupportedEncodingException;\r
-import java.util.List;\r
-import java.util.Set;\r
-\r
-import org.apache.log4j.Logger;\r
-import org.springframework.stereotype.Component;\r
-import org.springframework.transaction.TransactionStatus;\r
-\r
-import eu.etaxonomy.cdm.model.common.CdmBase;\r
-import eu.etaxonomy.cdm.model.common.Language;\r
-import eu.etaxonomy.cdm.model.common.LanguageString;\r
-import eu.etaxonomy.cdm.model.description.DescriptionBase;\r
-import eu.etaxonomy.cdm.model.description.DescriptionElementBase;\r
-import eu.etaxonomy.cdm.model.media.Media;\r
-import eu.etaxonomy.cdm.model.media.MediaRepresentation;\r
-import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;\r
-import eu.etaxonomy.cdm.model.taxon.Taxon;\r
-import eu.etaxonomy.cdm.model.taxon.TaxonNode;\r
-\r
-/**\r
- * @author a.mueller\r
- * @created 20.04.2011\r
- */\r
-@Component\r
-public class DwcaImageExport extends DwcaExportBase {\r
- private static final Logger logger = Logger.getLogger(DwcaImageExport.class);\r
-\r
- /**\r
- * Constructor\r
- */\r
- public DwcaImageExport() {\r
- super();\r
- this.ioName = this.getClass().getSimpleName();\r
- }\r
-\r
- /** Retrieves data from a CDM DB and serializes them CDM to XML.\r
- * Starts with root taxa and traverses the classification to retrieve children taxa, synonyms and relationships.\r
- * Taxa that are not part of the classification are not found.\r
- * \r
- * @param exImpConfig\r
- * @param dbname\r
- * @param filename\r
- */\r
- @Override\r
- protected boolean doInvoke(DwcaTaxExportState state){\r
- DwcaTaxExportConfigurator config = state.getConfig();\r
- String dbname = config.getSource() != null ? config.getSource().getName() : "unknown";\r
- String fileName = config.getDestinationNameString();\r
- logger.info("Serializing DB " + dbname + " to file " + fileName);\r
- TransactionStatus txStatus = startTransaction(true);\r
-\r
- try {\r
- \r
- final String coreTaxFileName = "images.txt";\r
- fileName = fileName + File.separatorChar + coreTaxFileName;\r
- File f = new File(fileName);\r
- if (!f.exists()){\r
- f.createNewFile();\r
- }\r
- FileOutputStream fos = new FileOutputStream(f);\r
- PrintWriter writer = new PrintWriter(new OutputStreamWriter(fos, "UTF8"), true);\r
-\r
- \r
- \r
- List<TaxonNode> allNodes = getAllNodes(null);\r
- for (TaxonNode node : allNodes){\r
- Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);\r
- Set<? extends DescriptionBase> descriptions = taxon.getDescriptions();\r
- for (DescriptionBase description : descriptions){\r
- for (Object o : description.getElements()){\r
- DescriptionElementBase el = CdmBase.deproxy(o, DescriptionElementBase.class);\r
- if (el.getMedia().size() > 0){\r
- for (Media media: el.getMedia()){\r
- for (MediaRepresentation repr : media.getRepresentations()){\r
- for (MediaRepresentationPart part : repr.getParts()){\r
- DwcaImageRecord record = new DwcaImageRecord();\r
- handleMedia(record, media, repr, part, taxon);\r
- record.write(writer);\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- \r
- writer.flush();\r
- \r
- }\r
- } catch (FileNotFoundException e) {\r
- e.printStackTrace();\r
- } catch (UnsupportedEncodingException e) {\r
- e.printStackTrace();\r
- } catch (ClassCastException e) {\r
- e.printStackTrace();\r
- } catch (IOException e) {\r
- e.printStackTrace();\r
- }\r
- commitTransaction(txStatus);\r
- return true;\r
- }\r
- \r
-\r
-\r
-\r
- private void handleMedia(DwcaImageRecord record, Media media, MediaRepresentation repr, MediaRepresentationPart part, Taxon taxon) {\r
- record.setCoreid(taxon.getId());\r
- record.setIdentifier(part.getUri());\r
- record.setTitle(media.getTitleCache());\r
- //TODO description if default language description is not available\r
- LanguageString description = media.getDescription(Language.DEFAULT());\r
- record.setDescription(description == null ? null: description.getText());\r
- //TODO missing\r
- record.setSpatial(null);\r
- //TODO missing\r
- record.setCoordinates(null);\r
- record.setFormat(repr.getMimeType());\r
- //FIXME missing ??\r
- record.setLicense(media.getRights());\r
- record.setCreated(media.getMediaCreated());\r
- record.setCreator(media.getArtist());\r
- //TODO missing\r
- record.setContributor(null);\r
- //TODO missing\r
- record.setPublisher(null);\r
- //TODO missing\r
- record.setAudience(null);\r
- }\r
- \r
- @Override\r
- protected boolean doCheck(DwcaTaxExportState state) {\r
- boolean result = true;\r
- logger.warn("No check implemented for " + this.ioName);\r
- return result;\r
- }\r
-\r
-\r
- @Override\r
- protected boolean isIgnore(DwcaTaxExportState state) {\r
- return ! state.getConfig().isDoImages();\r
- }\r
- \r
-}\r
+/**
+* Copyright (C) 2007 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.cdm.io.dwca.out;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
+import java.util.Set;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.common.Language;
+import eu.etaxonomy.cdm.model.common.LanguageString;
+import eu.etaxonomy.cdm.model.description.DescriptionBase;
+import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
+import eu.etaxonomy.cdm.model.media.Media;
+import eu.etaxonomy.cdm.model.media.MediaRepresentation;
+import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
+import eu.etaxonomy.cdm.model.taxon.Taxon;
+import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+
+/**
+ * @author a.mueller
+ * @since 20.04.2011
+ */
+public class DwcaImageExport extends DwcaDataExportBase {
+
+ private static final long serialVersionUID = -4997807762779037215L;
+ private static final Logger logger = LogManager.getLogger();
+
+ private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/Image";
+ protected static final String fileName = "images.txt";
+
+ private DwcaMetaDataRecord metaRecord;
+
+ /**
+ * Constructor
+ */
+ public DwcaImageExport(DwcaTaxExportState state) {
+ super();
+ this.ioName = this.getClass().getSimpleName();
+ metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
+ state.addMetaRecord(metaRecord);
+ file = DwcaTaxExportFile.IMAGE;
+ }
+
+ @Override
+ protected void doInvoke(DwcaTaxExportState state){}
+
+ @Override
+ protected void handleTaxonNode(DwcaTaxExportState state, TaxonNode node)
+ throws IOException, FileNotFoundException, UnsupportedEncodingException {
+ try {
+ DwcaTaxExportConfigurator config = state.getConfig();
+ Taxon taxon = CdmBase.deproxy(node.getTaxon());
+ Set<? extends DescriptionBase<?>> descriptions = taxon.getDescriptions();
+ for (DescriptionBase<?> description : descriptions){
+ for (DescriptionElementBase o : description.getElements()){
+ DescriptionElementBase el = CdmBase.deproxy(o);
+ if (el.getMedia().size() > 0){
+ for (Media media: el.getMedia()){
+ for (MediaRepresentation repr : media.getRepresentations()){
+ for (MediaRepresentationPart part : repr.getParts()){
+ if (! state.recordExists(file, part)){
+ DwcaImageRecord record = new DwcaImageRecord(metaRecord, config);
+ handleMedia(state, record, media, repr, part, taxon);
+ PrintWriter writer = createPrintWriter(state, file);
+ record.write(state, writer);
+ state.addExistingRecord(file,part);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ } catch (Exception e) {
+ String message = "Unexpected exception: " + e.getMessage();
+ state.getResult().addException(e, message);
+ }finally{
+ flushWriter(state, file);
+ }
+ }
+
+ private void handleMedia(DwcaTaxExportState state, DwcaImageRecord record, Media media, MediaRepresentation repr, MediaRepresentationPart part, Taxon taxon) {
+ record.setId(taxon.getId());
+ record.setUuid(taxon.getUuid());
+ if (part.getUri() == null){
+ String message = "No uri available for media ("+media.getId()+"). URI is required field. Taxon: " + this.getTaxonLogString(taxon);
+ state.getResult().addWarning(message);
+ }
+ record.setIdentifier(part.getUri());
+ record.setTitle(media.getTitleCache());
+ //TODO description if default language description is not available
+ LanguageString description = media.getDescription(Language.DEFAULT());
+ record.setDescription(description == null ? null: description.getText());
+ //TODO missing
+ record.setSpatial(null);
+ //TODO missing
+ record.setCoordinates(null);
+ record.setFormat(repr.getMimeType());
+ //FIXME missing ??
+ record.setLicense(media.getRights());
+ record.setCreated(media.getMediaCreated());
+ record.setCreator(media.getArtist());
+ //TODO missing
+ record.setContributor(null);
+ //TODO missing
+ record.setPublisher(null);
+ //TODO missing
+ record.setAudience(null);
+ }
+
+ @Override
+ protected boolean doCheck(DwcaTaxExportState state) {
+ boolean result = true;
+ logger.warn("No check implemented for " + this.ioName);
+ return result;
+ }
+
+ @Override
+ public boolean isIgnore(DwcaTaxExportState state) {
+ return ! state.getConfig().isDoImages();
+ }
+}
\ No newline at end of file