package org.gcube.data.spd.plugin.fwk.util; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import javax.xml.bind.JAXBException; import org.gcube.data.spd.model.binding.Bindings; import org.gcube.data.spd.model.exceptions.IdNotValidException; import org.gcube.data.spd.model.products.OccurrencePoint; import org.gcube.data.spd.model.products.ResultElement; import org.gcube.data.spd.plugin.fwk.readers.OccurrencesReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Util { private static final Logger logger = LoggerFactory.getLogger(Util.class); public static String keyEnrichment(String provider, String key){ return provider+":"+key; } public static String getProviderFromKey(String key) throws IdNotValidException{ int index = key.indexOf(":"); if (index==-1) throw new IdNotValidException(); return key.substring(0, index); } public static String getIdFromKey(String key) throws IdNotValidException { int index = key.indexOf(":"); if (index==-1) throw new IdNotValidException(); return key.substring(index+1, key.length()); } public static T copy(T obj) throws JAXBException { return Bindings.fromXml(Bindings.toXml(obj)); } public static File getDarwinCoreFile(OccurrencesReader reader) throws Exception{ DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); FileWriter writer = null; try{ File returnFile = File.createTempFile("darwinCore", "xml"); writer = new FileWriter(returnFile); writer.append(""); writer.append(""); while (reader.hasNext()){ writer.append(""); writer.append("en"); OccurrencePoint occurrence= reader.next(); if (occurrence.getModified() != null) writer.append("" + df.format(occurrence.getModified().getTime()) + ""); if (occurrence.getBasisOfRecord() != null) writer.append("" + occurrence.getBasisOfRecord().name() + ""); if (occurrence.getInstitutionCode() != null) writer.append("" + occurrence.getInstitutionCode() + ""); if (occurrence.getCollectionCode() != null) writer.append("" + occurrence.getCollectionCode() + ""); if (occurrence.getCatalogueNumber() != null) writer.append("" + occurrence.getCatalogueNumber() + ""); if (occurrence.getRecordedBy() != null) writer.append("" + occurrence.getRecordedBy() + ""); if (occurrence.getScientificName() != null) writer.append("" + occurrence.getScientificName() + ""); if (occurrence.getKingdom() != null) writer.append("" + occurrence.getKingdom() + ""); if (occurrence.getFamily() != null) writer.append("" + occurrence.getFamily() + ""); if (occurrence.getLocality() != null) writer.append("" + occurrence.getLocality() + ""); if (occurrence.getEventDate() != null) { writer.append("" + df.format(occurrence.getEventDate().getTime()) + ""); writer.append("" + occurrence.getEventDate().get(Calendar.YEAR) + ""); } if (occurrence.getDecimalLatitude() != 0.0) writer.append("" + occurrence.getDecimalLatitude() + ""); if (occurrence.getDecimalLongitude() != 0.0) writer.append("" + occurrence.getDecimalLongitude() + ""); if (occurrence.getCoordinateUncertaintyInMeters() != null) writer.append("" + occurrence.getCoordinateUncertaintyInMeters() + ""); if (occurrence.getMaxDepth() != 0.0) writer.append("" + occurrence.getMaxDepth() + ""); if (occurrence.getMinDepth() != 0.0) writer.append("" + occurrence.getMinDepth() + ""); writer.append(""); } writer.append(""); writer.flush(); writer.close(); return returnFile; }catch (Exception e) { logger.error("error writeing occurrences as darwin core",e); throw e; }finally{ try { writer.close(); } catch (IOException e) { logger.warn("error closing the output stream",e); } } } }