Search in sources :

Example 6 with TimePeriod

use of eu.etaxonomy.cdm.model.common.TimePeriod in project cdmlib by cybertaxonomy.

the class OccurrenceServiceTest method testMoveDerivate.

@Test
@DataSet(loadStrategy = CleanSweepInsertLoadStrategy.class, value = "OccurenceServiceTest.move.xml")
public void testMoveDerivate() {
    DerivedUnit specimenA = (DerivedUnit) occurrenceService.load(UUID.fromString("35cfb0b3-588d-4eee-9db6-ac9caa44e39a"));
    DerivedUnit specimenB = (DerivedUnit) occurrenceService.load(UUID.fromString("09496534-efd0-44c8-b1ce-01a34a8a0229"));
    DerivedUnit dnaSample = (DnaSample) occurrenceService.load(UUID.fromString("5995f852-0e78-405c-b849-d923bd6781d9"));
    occurrenceService.saveOrUpdate(specimenA);
    occurrenceService.saveOrUpdate(specimenB);
    occurrenceService.saveOrUpdate(dnaSample);
    Person derivationActor = Person.NewTitledInstance("Derivation Actor");
    String derivationDescription = "Derivation Description";
    Institution derivationInstitution = Institution.NewInstance();
    TimePeriod derivationTimePeriod = TimePeriod.NewInstance(2015);
    DerivationEvent originalDerivedFromEvent = DerivationEvent.NewSimpleInstance(specimenA, dnaSample, DerivationEventType.DNA_EXTRACTION());
    originalDerivedFromEvent.setActor(derivationActor);
    originalDerivedFromEvent.setDescription(derivationDescription);
    originalDerivedFromEvent.setInstitution(derivationInstitution);
    originalDerivedFromEvent.setTimeperiod(derivationTimePeriod);
    occurrenceService.moveDerivate(specimenA, specimenB, dnaSample);
    assertTrue("DerivationEvent not removed from source!", specimenA.getDerivationEvents().isEmpty());
    assertEquals("DerivationEvent not moved to source!", 1, specimenB.getDerivationEvents().size());
    DerivationEvent derivationEvent = specimenB.getDerivationEvents().iterator().next();
    assertEquals("Moved DerivationEvent not of same type!", DerivationEventType.DNA_EXTRACTION(), derivationEvent.getType());
    assertEquals(derivationActor, derivationEvent.getActor());
    assertEquals(derivationDescription, derivationEvent.getDescription());
    assertEquals(derivationInstitution, derivationEvent.getInstitution());
    assertEquals(derivationTimePeriod, derivationEvent.getTimeperiod());
    assertEquals(DerivationEventType.DNA_EXTRACTION(), derivationEvent.getType());
    assertEquals("Wrong number of derivation originals!", 1, derivationEvent.getOriginals().size());
    SpecimenOrObservationBase<?> newOriginal = derivationEvent.getOriginals().iterator().next();
    assertEquals("Target of moved object not correct", specimenB, newOriginal);
    assertEquals("Wrong number of derivatives!", 1, derivationEvent.getDerivatives().size());
    DerivedUnit movedDerivate = derivationEvent.getDerivatives().iterator().next();
    assertEquals("Moved derivate has wrong type", SpecimenOrObservationType.DnaSample, movedDerivate.getRecordBasis());
    assertNotEquals("DerivationEvent 'derivedFrom' has not been changed after moving", originalDerivedFromEvent, movedDerivate.getDerivedFrom());
}
Also used : DerivedUnit(eu.etaxonomy.cdm.model.occurrence.DerivedUnit) DerivationEvent(eu.etaxonomy.cdm.model.occurrence.DerivationEvent) DnaSample(eu.etaxonomy.cdm.model.molecular.DnaSample) TimePeriod(eu.etaxonomy.cdm.model.common.TimePeriod) Institution(eu.etaxonomy.cdm.model.agent.Institution) Person(eu.etaxonomy.cdm.model.agent.Person) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest) Test(org.junit.Test) DataSet(org.unitils.dbunit.annotation.DataSet)

Example 7 with TimePeriod

use of eu.etaxonomy.cdm.model.common.TimePeriod in project cdmlib by cybertaxonomy.

the class TimePeriodFormatterTest method test.

@Test
public void test() {
    TimePeriod tp1 = TimePeriod.NewInstance(1788, 1799);
    assertNotNull(tp1);
    Assert.assertEquals("1788" + endash + "1799", formatter.format(tp1));
    tp1.setStartDay(3);
    Assert.assertEquals("3 MMM 1788" + endash + "1799", formatter.format(tp1));
    tp1.setEndMonth(11);
    Assert.assertEquals("3 MMM 1788" + endash + "Nov 1799", formatter.format(tp1));
    tp1.setContinued(true);
    Assert.assertEquals("3 MMM 1788+", formatter.format(tp1));
    tp1 = TimePeriod.NewInstance(1788, 1799);
    tp1.setContinued(true);
    Assert.assertEquals("1788+", formatter.format(tp1));
    tp1 = TimePeriod.NewInstance((Integer) null);
    tp1.setContinued(true);
    // this is still undefined, could be something like 'xxxx+' in future
    Assert.assertEquals("+", formatter.format(tp1));
    tp1 = TimePeriod.NewInstance(1788, 1788);
    Assert.assertEquals("1788", formatter.format(tp1));
    tp1.setStartMonth(2);
    tp1.setEndMonth(4);
    Assert.assertEquals("Feb" + endash + "Apr 1788", formatter.format(tp1));
    tp1.setEndMonth(2);
    tp1.setStartDay(19);
    tp1.setEndDay(25);
    Assert.assertEquals("19" + endash + "25 Feb 1788", formatter.format(tp1));
    tp1.setEndYear(1789);
    Assert.assertEquals("19 Feb 1788" + endash + "25 Feb 1789", formatter.format(tp1));
    tp1.setEndYear(1788);
    tp1.setEndMonth(3);
    Assert.assertEquals("19 Feb" + endash + "25 Mar 1788", formatter.format(tp1));
}
Also used : TimePeriod(eu.etaxonomy.cdm.model.common.TimePeriod) Test(org.junit.Test)

Example 8 with TimePeriod

use of eu.etaxonomy.cdm.model.common.TimePeriod in project cdmlib by cybertaxonomy.

the class ValidTaxonomicYearValidator method isValid.

@Override
public boolean isValid(String year, ConstraintValidatorContext constraintContext) {
    boolean valid = true;
    TimePeriod tp = TimePeriodParser.parseString(year);
    if (tp.getStartYear() != null && tp.getStartYear() < 1753) {
        valid = false;
    } else if (tp.getEndYear() != null && tp.getStartYear() < 1753) {
        valid = false;
    }
    return valid;
}
Also used : TimePeriod(eu.etaxonomy.cdm.model.common.TimePeriod)

Example 9 with TimePeriod

use of eu.etaxonomy.cdm.model.common.TimePeriod in project cdmlib by cybertaxonomy.

the class TimePeriodParser method parseEnglishDate.

/**
 * Parses 1 or 2 dates of format yyyy-mm-dd, where y, m and d are numbers.
 *
 * @param timePeriod
 * @param strFrom the string representing the first part of the period
 * @param strTo the string representing the second part of the period
 * @param isAmerican
 * @return the parsed period
 */
private static TimePeriod parseEnglishDate(String strFrom, String strTo, boolean isAmerican) {
    Partial dateTo = parseSingleEnglishDate(strTo, isAmerican);
    if (strFrom.endsWith("+") && dateTo == null) {
        dateTo = TimePeriod.CONTINUED;
        strFrom = strFrom.substring(0, strFrom.length() - 1).trim();
    }
    Partial dateFrom = parseSingleEnglishDate(strFrom, isAmerican);
    TimePeriod result = TimePeriod.NewInstance(dateFrom, dateTo);
    return result;
}
Also used : Partial(org.joda.time.Partial) TimePeriod(eu.etaxonomy.cdm.model.common.TimePeriod) VerbatimTimePeriod(eu.etaxonomy.cdm.model.common.VerbatimTimePeriod)

Example 10 with TimePeriod

use of eu.etaxonomy.cdm.model.common.TimePeriod in project cdmlib by cybertaxonomy.

the class TimePeriodParser method parseString.

public static <T extends TimePeriod> T parseString(T timePeriod, String periodString) {
    T result = timePeriod;
    if (timePeriod == null) {
        return timePeriod;
    }
    if (periodString == null) {
        return result;
    }
    periodString = periodString.trim();
    result.setFreeText(null);
    // case fl. 1806 or c. 1806 or fl. 1806?  => TODO questionable if this should really be handled here, fl. probably stands for flowering and is not part of the date but of the date  context. What stands "c." for? Used by Markup import?
    if (prefixedYearPattern.matcher(periodString).matches()) {
        result.setFreeText(periodString);
        Matcher yearMatcher = firstYearPattern.matcher(periodString);
        yearMatcher.find();
        String startYear = yearMatcher.group();
        result.setStartYear(Integer.valueOf(startYear));
        if (yearMatcher.find()) {
            String endYear = yearMatcher.group();
            result.setEndYear(Integer.valueOf(endYear));
        }
    } else if (dotDatePattern.matcher(periodString).matches()) {
        parseDotDatePattern(periodString, result);
    } else if (slashDatePattern.matcher(periodString).matches()) {
        parseSlashDatePattern(periodString, result);
    } else if (dateWithMonthNamePattern.matcher(periodString).matches()) {
        parseDateWithMonthName(periodString, result);
    } else if (dateYearMonthDayPattern.matcher(periodString).matches()) {
        parseDateYearMonthDay(periodString, result);
    } else if (lifeSpanPattern.matcher(periodString).matches()) {
        parseLifeSpanPattern(periodString, result);
    } else if (standardPattern.matcher(periodString).matches()) {
        parseStandardPattern(periodString, result);
    // TODO first check ambiguity of parser results e.g. for 7/12/11
    // }else if (isDateString(periodString)){
    // String[] startEnd = makeStartEnd(periodString);
    // String start = startEnd[0];
    // DateTime startDateTime = dateStringParse(start, true);
    // result.setStart(startDateTime);
    // if (startEnd.length > 1){
    // DateTime endDateTime = dateStringParse(startEnd[1], true);
    // ;
    // result.setEnd(endDateTime.toLocalDate());
    // }
    } else if (isEnglishParsable(periodString)) {
        TimePeriod enDate = parseEnglishDate(periodString, null);
        result.setStart(enDate.getStart());
    } else if (periodString.contains("T00:00:00")) {
        result = parseString(timePeriod, periodString.replace("T00:00:00", ""));
    } else {
        result.setFreeText(periodString);
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) TimePeriod(eu.etaxonomy.cdm.model.common.TimePeriod) VerbatimTimePeriod(eu.etaxonomy.cdm.model.common.VerbatimTimePeriod)

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