use of eu.etaxonomy.cdm.model.common.TimePeriod in project cdmlib by cybertaxonomy.
the class MarkupSpecimenImport method handleDates.
private TimePeriod handleDates(MarkupImportState state, XMLEventReader reader, XMLEvent parent) throws XMLStreamException {
checkNoAttributes(parent);
TimePeriod result = TimePeriod.NewInstance();
String parseMessage = "%s can not be parsed: %s";
boolean hasFullDate = false;
boolean hasAtomised = false;
boolean hasUnparsedAtomised = false;
while (reader.hasNext()) {
XMLEvent next = readNoWhitespace(reader);
if (isMyEndingElement(next, parent)) {
if (!isAlternative(hasFullDate, hasAtomised, hasUnparsedAtomised)) {
String message = "Some problems exist when defining the date";
fireWarningEvent(message, parent, 4);
}
return result;
} else if (isStartingElement(next, FULL_DATE)) {
String fullDate = getCData(state, reader, next, true);
if (fullDate.endsWith(".")) {
fullDate = fullDate.substring(0, fullDate.length() - 1);
}
result = TimePeriodParser.parseString(fullDate);
if (result.getFreeText() != null) {
fireWarningEvent(String.format(parseMessage, FULL_DATE, fullDate), parent, 1);
}
hasFullDate = true;
} else if (isStartingElement(next, DAY)) {
String day = getCData(state, reader, next, true).trim();
day = normalizeDate(day);
if (CdmUtils.isNumeric(day)) {
result.setStartDay(Integer.valueOf(day));
hasAtomised = true;
} else {
fireWarningEvent(String.format(parseMessage, "Day", day), parent, 2);
hasUnparsedAtomised = true;
}
} else if (isStartingElement(next, MONTH)) {
String month = getCData(state, reader, next, true).trim();
month = normalizeDate(month);
if (CdmUtils.isNumeric(month)) {
result.setStartMonth(Integer.valueOf(month));
hasAtomised = true;
} else {
fireWarningEvent(String.format(parseMessage, "Month", month), parent, 2);
hasUnparsedAtomised = true;
}
} else if (isStartingElement(next, YEAR)) {
String year = getCData(state, reader, next, true).trim();
year = normalizeDate(year);
if (CdmUtils.isNumeric(year)) {
result.setStartYear(Integer.valueOf(year));
hasAtomised = true;
} else {
fireWarningEvent(String.format(parseMessage, "Year", year), parent, 2);
hasUnparsedAtomised = true;
}
} else {
handleUnexpectedElement(next);
}
}
throw new IllegalStateException("Dates has no closing tag.");
}
use of eu.etaxonomy.cdm.model.common.TimePeriod in project cdmlib by cybertaxonomy.
the class ReferencingObjectFormatter method getCache.
private static String getCache(GatheringEvent gatheringEvent) {
String ALTITUDE_PREFIX = "alt. ";
final String METER = "m";
String result = "";
// collector
AgentBase<?> collector = CdmBase.deproxy(gatheringEvent.getCollector());
String collectorStr = collector == null ? null : collector.getTitleCache();
result = CdmUtils.concat(", ", result, collectorStr);
// gathering period
TimePeriod gatheringPeriod = gatheringEvent.getTimeperiod();
result = CdmUtils.concat(", ", result, (gatheringPeriod == null ? null : gatheringPeriod.toString()));
// country
String strCountry = null;
NamedArea country = gatheringEvent.getCountry();
Representation repCountry = country == null ? null : country.getRepresentation(Language.DEFAULT());
strCountry = repCountry == null ? null : repCountry.getLabel();
result = CdmUtils.concat(", ", result, strCountry);
// locality
LanguageString locality = gatheringEvent.getLocality();
if (locality != null) {
result = CdmUtils.concat(", ", result, locality.getText());
}
// elevation
String elevationStr;
if (isNotBlank(gatheringEvent.getAbsoluteElevationText())) {
elevationStr = gatheringEvent.getAbsoluteElevationText();
} else {
String text = gatheringEvent.getAbsoluteElevationText();
Integer min = gatheringEvent.getAbsoluteElevation();
Integer max = gatheringEvent.getAbsoluteElevationMax();
elevationStr = DistanceStringFormatter.distanceString(min, max, text, METER);
}
if (isNotBlank(elevationStr)) {
result = CdmUtils.concat(", ", result, ALTITUDE_PREFIX);
result += elevationStr;
}
// exact locality
if (gatheringEvent.getExactLocation() != null) {
String exactLocation = gatheringEvent.getExactLocation().toSexagesimalString(false, false);
result = CdmUtils.concat(", ", result, exactLocation);
}
return result;
}
use of eu.etaxonomy.cdm.model.common.TimePeriod in project cdmlib by cybertaxonomy.
the class TaxonXExtractor method extractSpecimenOrObservation.
@SuppressWarnings({ "unused", "rawtypes" })
protected MySpecimenOrObservation extractSpecimenOrObservation(Node specimenObservationNode, DerivedUnit derivedUnitBase, SpecimenOrObservationType defaultAssociation, TaxonName typifiableName) {
String country = null;
String locality = null;
String stateprov = null;
String collector = null;
String fieldNumber = null;
Double latitude = null, longitude = null;
TimePeriod tp = null;
String day, month, year = "";
String descr = "not available";
String type = "";
boolean asso = false;
NodeList eventContent = null;
// create facade
DerivedUnitFacade derivedUnitFacade = null;
UnitsGatheringEvent unitsGatheringEvent;
UnitsGatheringArea unitsGatheringArea;
DefinedTermBase areaCountry;
MySpecimenOrObservation specimenOrObservation = new MySpecimenOrObservation();
NodeList xmldata = specimenObservationNode.getChildNodes();
for (int n = 0; n < xmldata.getLength(); n++) {
eventContent = xmldata.item(n).getChildNodes();
if (xmldata.item(n).getNodeName().equalsIgnoreCase("tax:xmldata")) {
asso = true;
country = null;
locality = null;
stateprov = null;
collector = null;
fieldNumber = null;
latitude = null;
longitude = null;
day = "";
month = "";
year = "";
type = "";
for (int j = 0; j < eventContent.getLength(); j++) {
if (eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:country")) {
country = eventContent.item(j).getTextContent().trim();
} else if (eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:locality")) {
locality = eventContent.item(j).getTextContent().trim();
} else if (eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:stateprovince")) {
stateprov = eventContent.item(j).getTextContent().trim();
} else if (eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:collector")) {
collector = eventContent.item(j).getTextContent().trim();
} else if (eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:yearcollected")) {
year = eventContent.item(j).getTextContent().trim();
} else if (eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:monthcollected")) {
month = eventContent.item(j).getTextContent().trim();
} else if (eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:daycollected")) {
day = eventContent.item(j).getTextContent().trim();
} else if (eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:decimallongitude")) {
String tmp = eventContent.item(j).getTextContent().trim();
try {
longitude = Double.valueOf(tmp);
} catch (Exception e) {
logger.warn("longitude is not a number");
}
} else if (eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:decimallatitude")) {
String tmp = eventContent.item(j).getTextContent().trim();
try {
latitude = Double.valueOf(tmp);
} catch (Exception e) {
logger.warn("latitude is not a number");
}
} else if (eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:TypeStatus")) {
type = eventContent.item(j).getTextContent().trim();
} else if (eventContent.item(j).getNodeName().equalsIgnoreCase("#text") && StringUtils.isBlank(eventContent.item(j).getTextContent())) {
// do nothing
} else {
logger.info("UNEXTRACTED FIELD FOR SPECIMEN " + eventContent.item(j).getNodeName() + ", " + eventContent.item(j).getTextContent());
}
}
if (!day.isEmpty() || !month.isEmpty() || !year.isEmpty()) {
try {
if (!year.isEmpty()) {
tp = TimePeriod.NewInstance(Integer.parseInt(year));
if (!month.isEmpty()) {
tp.setStartMonth(Integer.parseInt(month));
if (!day.isEmpty()) {
tp.setStartDay(Integer.parseInt(day));
}
}
}
} catch (Exception e) {
logger.warn("Collection date error " + e);
}
}
}
if (xmldata.item(n).getNodeName().equalsIgnoreCase("#text")) {
descr = xmldata.item(n).getTextContent().replaceAll(";", "").trim();
if (descr.length() > 1 && containsDistinctLetters(descr)) {
specimenOrObservation.setDescr(descr);
asso = true;
}
}
if (xmldata.item(n).getNodeName().equalsIgnoreCase("tax:p")) {
descr = xmldata.item(n).getTextContent().replaceAll(";", "").trim();
if (descr.length() > 1 && containsDistinctLetters(descr)) {
specimenOrObservation.setDescr(descr);
asso = true;
}
}
}
// logger.info("DESCR: "+descr);
if (!type.isEmpty()) {
if (!containsDistinctLetters(type)) {
type = "no description text";
}
derivedUnitFacade = getFacade(type.replaceAll(";", ""), defaultAssociation);
SpecimenTypeDesignation designation = SpecimenTypeDesignation.NewInstance();
if (typifiableName != null) {
typifiableName.addTypeDesignation(designation, true);
} else {
logger.warn("No typifiable name available");
}
SpecimenTypeDesignationStatus stds = getSpecimenTypeDesignationStatusByKey(type);
if (stds != null) {
stds = (SpecimenTypeDesignationStatus) importer.getTermService().find(stds.getUuid());
}
designation.setTypeStatus(stds);
derivedUnitFacade.innerDerivedUnit().addSpecimenTypeDesignation(designation);
derivedUnitBase = derivedUnitFacade.innerDerivedUnit();
// System.out.println("derivedUnitBase: "+derivedUnitBase);
// designation.setTypeSpecimen(derivedUnitBase);
// TaxonName name = taxon.getName();
// name.addTypeDesignation(designation, true);
} else {
if (!containsDistinctLetters(descr.replaceAll(";", ""))) {
descr = "no description text";
}
derivedUnitFacade = getFacade(descr.replaceAll(";", ""), defaultAssociation);
derivedUnitBase = derivedUnitFacade.innerDerivedUnit();
// System.out.println("derivedUnitBase2: "+derivedUnitBase);
}
unitsGatheringEvent = new UnitsGatheringEvent(importer.getTermService(), locality, collector, longitude, latitude, state2.getConfig(), importer.getAgentService());
if (tp != null) {
unitsGatheringEvent.setGatheringDate(tp);
}
// country
unitsGatheringArea = new UnitsGatheringArea();
unitsGatheringArea.setParams(null, country, state2.getConfig(), importer.getTermService(), importer.getVocabularyService());
// TODO other areas
if (StringUtils.isNotBlank(stateprov)) {
Map<String, String> namedAreas = new HashMap<>();
namedAreas.put(stateprov, null);
unitsGatheringArea.setAreaNames(namedAreas, state2.getConfig(), importer.getTermService(), importer.getVocabularyService());
}
areaCountry = unitsGatheringArea.getCountry();
// // other areas
// unitsGatheringArea = new UnitsGatheringArea(namedAreaList,dataHolder.getTermService());
// ArrayList<DefinedTermBase> nas = unitsGatheringArea.getAreas();
// for (DefinedTermBase namedArea : nas) {
// unitsGatheringEvent.addArea(namedArea);
// }
// copy gathering event to facade
GatheringEvent gatheringEvent = unitsGatheringEvent.getGatheringEvent();
derivedUnitFacade.setGatheringEvent(gatheringEvent);
derivedUnitFacade.setLocality(gatheringEvent.getLocality());
derivedUnitFacade.setExactLocation(gatheringEvent.getExactLocation());
derivedUnitFacade.setCollector(gatheringEvent.getCollector());
derivedUnitFacade.setCountry((NamedArea) areaCountry);
for (DefinedTermBase<?> area : unitsGatheringArea.getAreas()) {
derivedUnitFacade.addCollectingArea((NamedArea) area);
}
// add fieldNumber
if (fieldNumber != null) {
derivedUnitFacade.setFieldNumber(fieldNumber);
}
specimenOrObservation.setDerivedUnitBase(derivedUnitBase);
// }
return specimenOrObservation;
}
use of eu.etaxonomy.cdm.model.common.TimePeriod in project cdmlib by cybertaxonomy.
the class ExcelImportBase method getTimePeriod.
/**
* @param start
* @param end
* @return
*/
protected TimePeriod getTimePeriod(String start, String end) {
String strPeriod = CdmUtils.concat(" - ", start, end);
TimePeriod result = TimePeriodParser.parseString(strPeriod);
return result;
}
use of eu.etaxonomy.cdm.model.common.TimePeriod in project cdmlib by cybertaxonomy.
the class DwcaEmlExport method handleTermporalCoverage.
/**
* @param writer
* @param emlRecord
* @throws XMLStreamException
*/
private void handleTermporalCoverage(XMLStreamWriter writer, DwcaEmlRecord emlRecord) throws XMLStreamException {
TimePeriod timePeriod = emlRecord.getDate();
if (timePeriod == null) {
return;
}
writer.writeStartElement("termporalCoverage");
if (!timePeriod.isPeriod()) {
// singleDateTime
writer.writeStartElement("singleDateTime");
writeCalendarDate(writer, timePeriod.getStart());
writer.writeEndElement();
} else {
// rangeOfDates
writer.writeStartElement("rangeOfDates");
writer.writeStartElement("beginDate");
writeCalendarDate(writer, timePeriod.getStart());
writer.writeEndElement();
writer.writeStartElement("endDate");
writeCalendarDate(writer, timePeriod.getStart());
writer.writeEndElement();
writer.writeEndElement();
}
// termporalCoverage
writer.writeEndElement();
}
Aggregations