Search in sources :

Example 1 with ModelSerializer

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

the class RecordDao method createRecordDataFieldValueMap.

private Map<Field<?>, Object> createRecordDataFieldValueMap(int recordId, Integer sequenceNumber, Step step, CollectRecord r) {
    Map<Field<?>, Object> map = new HashMap<Field<?>, Object>();
    map.put(OFC_RECORD_DATA.RECORD_ID, recordId);
    if (sequenceNumber != null) {
        map.put(OFC_RECORD_DATA.SEQ_NUM, sequenceNumber);
    }
    map.put(OFC_RECORD_DATA.DATE_CREATED, toTimestamp(defaultIfNull(r.getDataCreationDate(), r.getCreationDate())));
    map.put(OFC_RECORD_DATA.CREATED_BY, getUserId(defaultIfNull(r.getDataCreatedBy(), r.getCreatedBy())));
    map.put(OFC_RECORD_DATA.DATE_MODIFIED, toTimestamp(defaultIfNull(r.getDataModifiedDate(), r.getModifiedDate())));
    map.put(OFC_RECORD_DATA.MODIFIED_BY, getUserId(defaultIfNull(r.getDataModifiedBy(), r.getModifiedBy())));
    map.put(OFC_RECORD_DATA.STEP, step.getStepNumber());
    map.put(OFC_RECORD_DATA.STATE, r.getState() != null ? r.getState().getCode() : null);
    map.put(OFC_RECORD_DATA.SKIPPED, r.getSkipped());
    map.put(OFC_RECORD_DATA.MISSING, r.getMissing());
    map.put(OFC_RECORD_DATA.ERRORS, r.getErrors());
    map.put(OFC_RECORD_DATA.WARNINGS, r.getWarnings());
    map.put(OFC_RECORD_DATA.APP_VERSION, r.getApplicationVersion().toString());
    addValuesToMap(map, RECORD_DATA_KEY_FIELDS, r.getRootEntityKeyValues());
    addValuesToMap(map, RECORD_DATA_COUNT_FIELDS, r.getEntityCounts());
    addValuesToMap(map, RECORD_DATA_QUALIFIER_FIELDS, r.getQualifierValues());
    addValuesToMap(map, RECORD_DATA_SUMMARY_FIELDS, r.getDataSummaryValues());
    Entity rootEntity = r.getRootEntity();
    byte[] data = new ModelSerializer(SERIALIZATION_BUFFER_SIZE).toByteArray(rootEntity);
    map.put(OFC_RECORD_DATA.DATA, data);
    return map;
}
Also used : Field(org.jooq.Field) TableField(org.jooq.TableField) RecordSummarySortField(org.openforis.collect.model.RecordSummarySortField) Entity(org.openforis.idm.model.Entity) HashMap(java.util.HashMap) ModelSerializer(org.openforis.idm.model.ModelSerializer)

Example 2 with ModelSerializer

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

the class RecordDao method fromQueryResult.

public CollectRecord fromQueryResult(CollectSurvey survey, Record r, boolean recordToBeUpdated) {
    int rootEntityId = r.getValue(OFC_RECORD.ROOT_ENTITY_DEFINITION_ID);
    String version = r.getValue(OFC_RECORD.MODEL_VERSION);
    Schema schema = survey.getSchema();
    EntityDefinition rootEntityDefn = schema.getDefinitionById(rootEntityId);
    CollectRecord c = new CollectRecord(survey, version, rootEntityDefn, recordToBeUpdated);
    c.setId(r.getValue(OFC_RECORD.ID));
    c.setStep(Step.valueOf(r.getValue(OFC_RECORD.STEP)));
    c.setWorkflowSequenceNumber(r.getValue(OFC_RECORD.DATA_SEQ_NUM));
    c.setCreationDate(r.getValue(OFC_RECORD.DATE_CREATED));
    c.setModifiedDate(r.getValue(OFC_RECORD.DATE_MODIFIED));
    c.setCreatedBy(createDetachedUser(r.getValue(OFC_RECORD.CREATED_BY_ID)));
    c.setModifiedBy(createDetachedUser(r.getValue(OFC_RECORD.MODIFIED_BY_ID)));
    c.setWarnings(r.getValue(OFC_RECORD.WARNINGS));
    c.setErrors(r.getValue(OFC_RECORD.ERRORS));
    c.setSkipped(r.getValue(OFC_RECORD.SKIPPED));
    c.setMissing(r.getValue(OFC_RECORD.MISSING));
    c.setOwner(createDetachedUser(r.getValue(OFC_RECORD.OWNER_ID)));
    c.setDataStep(Step.valueOf(r.getValue(OFC_RECORD_DATA.STEP)));
    c.setDataWorkflowSequenceNumber(r.getValue(OFC_RECORD_DATA.SEQ_NUM));
    c.setDataCreationDate(r.getValue(OFC_RECORD_DATA.DATE_CREATED));
    c.setDataModifiedDate(r.getValue(OFC_RECORD_DATA.DATE_MODIFIED));
    c.setDataCreatedBy(createDetachedUser(r.getValue(OFC_RECORD_DATA.CREATED_BY)));
    c.setDataModifiedBy(createDetachedUser(r.getValue(OFC_RECORD_DATA.MODIFIED_BY)));
    c.setDataRootEntityKeyValues(getFieldValues(r, rootEntityDefn.getKeyAttributeDefinitions(), RECORD_DATA_KEY_FIELDS, String.class));
    c.setDataEntityCounts(getFieldValues(r, schema.getCountableEntitiesInRecordList(rootEntityDefn), RECORD_DATA_COUNT_FIELDS, Integer.class));
    c.setDataQualifierValues(getFieldValues(r, schema.getQualifierAttributeDefinitions(rootEntityDefn), RECORD_DATA_QUALIFIER_FIELDS, String.class));
    c.setDataSummaryValues(getFieldValues(r, schema.getSummaryAttributeDefinitions(rootEntityDefn), RECORD_DATA_SUMMARY_FIELDS, String.class));
    String state = r.getValue(OFC_RECORD.STATE);
    c.setState(state == null ? null : State.fromCode(state));
    c.setApplicationVersion(new Version(r.getValue(OFC_RECORD_DATA.APP_VERSION)));
    byte[] data = r.getValue(OFC_RECORD_DATA.DATA);
    ModelSerializer modelSerializer = new ModelSerializer(SERIALIZATION_BUFFER_SIZE);
    Entity rootEntity = c.getRootEntity();
    modelSerializer.mergeFrom(data, rootEntity);
    c.setRootEntityKeyValues(getFieldValues(r, rootEntityDefn.getKeyAttributeDefinitions(), RECORD_KEY_FIELDS, String.class));
    c.setEntityCounts(getFieldValues(r, schema.getCountableEntitiesInRecordList(rootEntityDefn), RECORD_COUNT_FIELDS, Integer.class));
    c.setQualifierValues(getFieldValues(r, schema.getQualifierAttributeDefinitions(rootEntityDefn), RECORD_QUALIFIER_FIELDS, String.class));
    c.setSummaryValues(getFieldValues(r, schema.getSummaryAttributeDefinitions(rootEntityDefn), RECORD_SUMMARY_FIELDS, String.class));
    return c;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CollectRecord(org.openforis.collect.model.CollectRecord) Entity(org.openforis.idm.model.Entity) Version(org.openforis.commons.versioning.Version) ModelVersion(org.openforis.idm.metamodel.ModelVersion) Schema(org.openforis.idm.metamodel.Schema) ModelSerializer(org.openforis.idm.model.ModelSerializer)

Example 3 with ModelSerializer

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

the class ModelSerializationTest method testProto.

@Test
public void testProto() throws Exception {
    // Create
    CollectSurvey survey = loadSurvey();
    CollectRecord record = createTestRecord(survey);
    Entity entity = record.getRootEntity();
    // Save
    ModelSerializer ms = new ModelSerializer(3000);
    byte[] data = ms.toByteArray(entity);
    // Load
    CollectRecord record2 = new CollectRecord(survey, "2.0", "cluster");
    Entity reloadedEntity = record2.getRootEntity();
    ms.mergeFrom(data, reloadedEntity);
    Assert.assertEquals(entity, reloadedEntity);
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Entity(org.openforis.idm.model.Entity) CollectSurvey(org.openforis.collect.model.CollectSurvey) ModelSerializer(org.openforis.idm.model.ModelSerializer) CollectIntegrationTest(org.openforis.collect.CollectIntegrationTest) Test(org.junit.Test)

Aggregations

Entity (org.openforis.idm.model.Entity)3 ModelSerializer (org.openforis.idm.model.ModelSerializer)3 CollectRecord (org.openforis.collect.model.CollectRecord)2 HashMap (java.util.HashMap)1 Field (org.jooq.Field)1 TableField (org.jooq.TableField)1 Test (org.junit.Test)1 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 RecordSummarySortField (org.openforis.collect.model.RecordSummarySortField)1 Version (org.openforis.commons.versioning.Version)1 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)1 ModelVersion (org.openforis.idm.metamodel.ModelVersion)1 Schema (org.openforis.idm.metamodel.Schema)1