Search in sources :

Example 16 with TimePeriod

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.");
}
Also used : TimePeriod(eu.etaxonomy.cdm.model.common.TimePeriod) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 17 with TimePeriod

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;
}
Also used : LanguageString(eu.etaxonomy.cdm.model.common.LanguageString) TimePeriod(eu.etaxonomy.cdm.model.common.TimePeriod) NamedArea(eu.etaxonomy.cdm.model.location.NamedArea) Representation(eu.etaxonomy.cdm.model.term.Representation) LanguageString(eu.etaxonomy.cdm.model.common.LanguageString)

Example 18 with TimePeriod

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;
}
Also used : DerivedUnitFacade(eu.etaxonomy.cdm.api.facade.DerivedUnitFacade) GatheringEvent(eu.etaxonomy.cdm.model.occurrence.GatheringEvent) UnitsGatheringEvent(eu.etaxonomy.cdm.io.specimen.UnitsGatheringEvent) UnitsGatheringArea(eu.etaxonomy.cdm.io.specimen.UnitsGatheringArea) HashMap(java.util.HashMap) TimePeriod(eu.etaxonomy.cdm.model.common.TimePeriod) NodeList(org.w3c.dom.NodeList) SpecimenTypeDesignationStatus(eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus) UnitsGatheringEvent(eu.etaxonomy.cdm.io.specimen.UnitsGatheringEvent) TransformerException(javax.xml.transform.TransformerException) UnknownCdmTypeException(eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException) DefinedTermBase(eu.etaxonomy.cdm.model.term.DefinedTermBase) SpecimenTypeDesignation(eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation)

Example 19 with TimePeriod

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;
}
Also used : TimePeriod(eu.etaxonomy.cdm.model.common.TimePeriod)

Example 20 with TimePeriod

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();
}
Also used : TimePeriod(eu.etaxonomy.cdm.model.common.TimePeriod)

Aggregations

TimePeriod (eu.etaxonomy.cdm.model.common.TimePeriod)30 VerbatimTimePeriod (eu.etaxonomy.cdm.model.common.VerbatimTimePeriod)12 Test (org.junit.Test)11 Institution (eu.etaxonomy.cdm.model.agent.Institution)5 Person (eu.etaxonomy.cdm.model.agent.Person)5 DerivedUnitFacade (eu.etaxonomy.cdm.api.facade.DerivedUnitFacade)4 Reference (eu.etaxonomy.cdm.model.reference.Reference)4 Team (eu.etaxonomy.cdm.model.agent.Team)3 LanguageString (eu.etaxonomy.cdm.model.common.LanguageString)3 NamedArea (eu.etaxonomy.cdm.model.location.NamedArea)3 Media (eu.etaxonomy.cdm.model.media.Media)3 Rank (eu.etaxonomy.cdm.model.name.Rank)3 TaxonName (eu.etaxonomy.cdm.model.name.TaxonName)3 GatheringEvent (eu.etaxonomy.cdm.model.occurrence.GatheringEvent)3 UnknownCdmTypeException (eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException)3 MalformedURLException (java.net.MalformedURLException)3 ArrayList (java.util.ArrayList)3 URI (eu.etaxonomy.cdm.common.URI)2 UnitsGatheringArea (eu.etaxonomy.cdm.io.specimen.UnitsGatheringArea)2 UnitsGatheringEvent (eu.etaxonomy.cdm.io.specimen.UnitsGatheringEvent)2