package eu.balticdiversity;
import eu.balticdiversity.search.models.SimpleDarwinRecord;
import eu.balticdiversity.search.models.SimpleDarwinRecord.VernacularName;
import fi.luomus.commons.xml.Document;
import fi.luomus.commons.xml.Document.Node;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Utils {
private static SimpleDateFormat getIsoDateFormat() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf;
}
public static Document createSimpleDarwinRecordSet() {
Document doc = new Document("SimpleDarwinRecordSet");
Node root = doc.getRootNode();
root.addAttribute("xmlns", "http://rs.tdwg.org/dwc/xsd/simpledarwincore/");
root.addAttribute("xmlns:dc", "http://purl.org/dc/terms/");
root.addAttribute("xmlns:dwc", "http://rs.tdwg.org/dwc/terms/");
root.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
root.addAttribute("xsi:schemaLocation", "http://rs.tdwg.org/dwc/xsd/simpledarwincore/ http://rs.tdwg.org/dwc/xsd/tdwg_dwc_simple.xsd");
return doc;
}
public static void addRecordToRecordSet(Node recordSet, SimpleDarwinRecord record) {
Node entry = recordSet.addChildNode("SimpleDarwinRecord");
entry.addChildNode("dwc:occurrenceID").setContents(record.getOccurrenceID());
entry.addChildNode("dwc:recordedBy").setContents(record.getRecordedBy());
entry.addChildNode("dwc:basisOfRecord").setContents(record.getBasisOfRecord());
entry.addChildNode("dwc:scientificName").setContents(record.getScientificName());
for (VernacularName vernacularName : record.getVernacularNames()) {
if (vernacularName.given()) {
entry.addChildNode("dwc:vernacularName").setContents(vernacularName.getVernacularName()).addAttribute("xml:lang", vernacularName.getLangcode());
}
}
entry.addChildNode("dwc:individualCount").setContents(record.getIndividualCount());
entry.addChildNode("dwc:eventDate").setContents(record.getEventStartEndDate());
entry.addChildNode("dwc:locality").setContents(record.getLocality());
entry.addChildNode("dwc:decimalLatitude").setContents(round(record.getDecimalLatitude()));
entry.addChildNode("dwc:decimalLongitude").setContents(round(record.getDecimalLongitude()));
entry.addChildNode("dwc:coordinatePrecision").setContents(round(record.getCoordinatePrecision()));
entry.addChildNode("dwc:geodeticDatum").setContents("WGS84");
entry.addChildNode("dwc:collectionID").setContents(record.getCollectionID());
entry.addChildNode("dwc:collectionCode").setContents(record.getCollectionCode());
entry.addChildNode("dwc:occurrenceRemarks").setContents(record.getOccurrenceRemarks());
}
private static String round(Double value) {
if (value == null) return null;
return String.format(Locale.US, "%.6f%n", value);
}
public static String toIsoDateFormat(Date date) {
if (date == null) return null;
try {
return getIsoDateFormat().format(date);
} catch (Exception e) {
return null;
}
}
}