Search in sources :

Example 6 with Date

use of org.openforis.idm.model.Date in project collect by openforis.

the class DataMarshallerTest method createTestRecord.

private CollectRecord createTestRecord(CollectSurvey survey) throws RecordPersistenceException {
    RecordBuilder recordBuilder = record(attribute("id", "123_456"), attribute("gps_realtime", "true"), attribute("region", "001"), attribute("district", "XXX"), attribute("crew_no", 10), attribute("map_sheet", "value 1"), attribute("map_sheet", "value 2"), attribute("vehicle_location", new Coordinate(432423423d, 4324324d, "srs")), attribute("gps_model", "TomTom 1.232"), attribute("remarks", "Remarks with UTF-8 character: Í"), entity("time_study", attribute("date", new Date(2011, 2, 14)), attribute("start_time", new Time(8, 15)), attribute("end_time", new Time(15, 29))), entity("time_study", attribute("date", new Date(2011, 2, 15)), attribute("start_time", new Time(8, 32)), attribute("end_time", new Time(11, 20))), entity("plot", attribute("no", new Code("1")), entity("tree", attribute("tree_no", 1), attribute("dbh", 54.2), attribute("total_height", 2.0), attribute("bole_height", (Double) null)), entity("tree", attribute("tree_no", 2), attribute("dbh", 82.8), attribute("total_height", 3.0))), entity("plot", attribute("no", new Code("2")), entity("tree", attribute("tree_no", 1), attribute("dbh", 34.2), attribute("total_height", 2.0)), entity("tree", attribute("tree_no", 2), attribute("dbh", 85.8), attribute("total_height", 4.0))));
    CollectRecord record = recordBuilder.build(survey, "cluster", "2.0");
    record.setCreationDate(new GregorianCalendar(2011, 11, 31, 23, 59).getTime());
    record.setStep(Step.ENTRY);
    record.updateSummaryFields();
    RecordUpdater recordUpdater = new RecordUpdater();
    recordUpdater.initializeRecord(record);
    Entity cluster = record.getRootEntity();
    recordUpdater.confirmError((Attribute<?, ?>) record.findNodeByPath("/cluster/district"));
    recordUpdater.approveMissingValue(cluster, "accessibility");
    NumberAttribute<?, ?> boleHeight = (NumberAttribute<?, ?>) record.findNodeByPath("/cluster/plot[1]/tree[1]/bole_height");
    recordUpdater.updateAttribute(boleHeight, FieldSymbol.BLANK_ON_FORM);
    recordUpdater.updateRemarks(boleHeight.getNumberField(), "No value specified");
    return record;
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Entity(org.openforis.idm.model.Entity) Coordinate(org.openforis.idm.model.Coordinate) RecordBuilder(org.openforis.idm.testfixture.RecordBuilder) GregorianCalendar(java.util.GregorianCalendar) NumberAttribute(org.openforis.idm.model.NumberAttribute) Time(org.openforis.idm.model.Time) Code(org.openforis.idm.model.Code) RecordUpdater(org.openforis.collect.model.RecordUpdater) Date(org.openforis.idm.model.Date)

Example 7 with Date

use of org.openforis.idm.model.Date in project collect by openforis.

the class ComparisonCheckTest method testDateGreaterThanCurrentDate.

@Test
public void testDateGreaterThanCurrentDate() {
    Entity timeStudy = EntityBuilder.addEntity(cluster, "time_study");
    DateAttribute date = EntityBuilder.addValue(timeStudy, "date", new Date(3015, 10, 20));
    ValidationResults results = validate(date);
    assertTrue(containsComparisonCheck(results.getErrors()));
}
Also used : Entity(org.openforis.idm.model.Entity) Date(org.openforis.idm.model.Date) DateAttribute(org.openforis.idm.model.DateAttribute) Test(org.junit.Test)

Example 8 with Date

use of org.openforis.idm.model.Date in project collect by openforis.

the class ComparisonCheckTest method testDateNotGreaterThanCurrentDate.

@Test
public void testDateNotGreaterThanCurrentDate() {
    Entity timeStudy = EntityBuilder.addEntity(cluster, "time_study");
    DateAttribute date = EntityBuilder.addValue(timeStudy, "date", new Date(2015, 9, 20));
    ValidationResults results = validate(date);
    assertFalse(containsComparisonCheck(results.getErrors()));
}
Also used : Entity(org.openforis.idm.model.Entity) Date(org.openforis.idm.model.Date) DateAttribute(org.openforis.idm.model.DateAttribute) Test(org.junit.Test)

Example 9 with Date

use of org.openforis.idm.model.Date in project collect by openforis.

the class CSVValueFormatter method format.

public String format(AttributeDefinition defn, Value value) {
    if (value == null) {
        return "";
    } else if (value instanceof BooleanValue) {
        return ((BooleanValue) value).getValue().toString();
    } else if (value instanceof Code) {
        CodeListService codeListService = defn.getSurvey().getContext().getCodeListService();
        CodeList list = ((CodeAttributeDefinition) defn).getList();
        if (codeListService.hasQualifiableItems(list)) {
            return String.format("%s: %s", ((Code) value).getCode(), ((Code) value).getQualifier());
        } else {
            return ((Code) value).getCode();
        }
    } else if (value instanceof Coordinate) {
        return value.toString();
    } else if (value instanceof Date) {
        Date date = (Date) value;
        return String.format("%d/%d/%d", ((Date) value).getDay(), ((Date) value).getMonth(), date.getYear());
    } else if (value instanceof File) {
        return ((File) value).getFilename();
    } else if (value instanceof NumberValue) {
        Number val = ((NumberValue<?>) value).getValue();
        NumberFormat numberFormat = NumberFormat.getInstance(Locale.ENGLISH);
        String formattedVal = numberFormat.format(val);
        return formattedVal;
    } else if (value instanceof NumericRange) {
        Number from = ((NumericRange<?>) value).getFrom();
        Number to = ((NumericRange<?>) value).getFrom();
        String format;
        if (value instanceof IntegerRange) {
            format = "%d-%d";
        } else {
            format = "%f-%f";
        }
        String formattedValue = String.format(Locale.ENGLISH, format, from, to);
        return formattedValue;
    } else if (value instanceof TextValue) {
        return ((TextValue) value).getValue();
    } else if (value instanceof Time) {
        Time time = (Time) value;
        return String.format("%d:%d", time.getHour(), time.getMinute());
    } else
        throw new IllegalArgumentException("Unsupported attribute value type: " + value.getClass().getName());
}
Also used : IntegerRange(org.openforis.idm.model.IntegerRange) CodeListService(org.openforis.idm.metamodel.CodeListService) Time(org.openforis.idm.model.Time) Code(org.openforis.idm.model.Code) Date(org.openforis.idm.model.Date) NumericRange(org.openforis.idm.model.NumericRange) CodeList(org.openforis.idm.metamodel.CodeList) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) Coordinate(org.openforis.idm.model.Coordinate) NumberValue(org.openforis.idm.model.NumberValue) TextValue(org.openforis.idm.model.TextValue) BooleanValue(org.openforis.idm.model.BooleanValue) File(org.openforis.idm.model.File) NumberFormat(java.text.NumberFormat)

Example 10 with Date

use of org.openforis.idm.model.Date in project collect by openforis.

the class CSVDataImportJobIntegrationTest method singleEntityTest.

@Test
public void singleEntityTest() throws Exception {
    {
        CollectRecord record = createTestRecord(survey, "10_114");
        recordDao.insert(record);
    }
    EntityDefinition clusterDefn = survey.getSchema().getRootEntityDefinition("cluster");
    EntityDefinition plotDefn = (EntityDefinition) clusterDefn.getChildDefinition("plot");
    CSVDataImportJob process = importCSVFile(VALID_SINGLE_ENTITY_TEST_CSV, plotDefn.getId());
    assertTrue(process.isCompleted());
    assertTrue(process.getParsingErrors().isEmpty());
    {
        CollectRecord reloadedRecord = loadRecord("10_114");
        Entity reloadedCluster = reloadedRecord.getRootEntity();
        {
            Entity plot = reloadedCluster.findChildEntitiesByKeys("plot", "1", "A").get(0);
            Entity timeStudy = (Entity) plot.getChild("time_study");
            DateAttribute date = (DateAttribute) timeStudy.getChild("date");
            assertEquals(new Date(2012, 2, 15), date.getValue());
        }
        {
            Entity plot = reloadedCluster.findChildEntitiesByKeys("plot", "2", "B").get(0);
            Entity timeStudy = (Entity) plot.getChild("time_study");
            DateAttribute date = (DateAttribute) timeStudy.getChild("date");
            assertEquals(new Date(2013, 5, 18), date.getValue());
        }
    }
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entity(org.openforis.idm.model.Entity) Date(org.openforis.idm.model.Date) DateAttribute(org.openforis.idm.model.DateAttribute) CollectIntegrationTest(org.openforis.collect.CollectIntegrationTest) Test(org.junit.Test)

Aggregations

Date (org.openforis.idm.model.Date)20 Entity (org.openforis.idm.model.Entity)16 CollectRecord (org.openforis.collect.model.CollectRecord)10 Code (org.openforis.idm.model.Code)10 Time (org.openforis.idm.model.Time)10 Coordinate (org.openforis.idm.model.Coordinate)8 Test (org.junit.Test)7 DateAttribute (org.openforis.idm.model.DateAttribute)7 GregorianCalendar (java.util.GregorianCalendar)6 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)4 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)4 RealAttribute (org.openforis.idm.model.RealAttribute)4 NumberAttribute (org.openforis.idm.model.NumberAttribute)3 RecordBuilder (org.openforis.idm.testfixture.RecordBuilder)3 ParsingError (org.openforis.collect.io.metadata.parsing.ParsingError)2 RecordUpdater (org.openforis.collect.model.RecordUpdater)2 NumberFormat (java.text.NumberFormat)1 Calendar (java.util.Calendar)1 User (org.openforis.collect.model.User)1 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)1