Search in sources :

Example 36 with Schema

use of org.openforis.idm.metamodel.Schema in project collect by openforis.

the class CSVDataImportProcess method getOrCreateParentEntity.

// @SuppressWarnings("unchecked")
// private <T extends Value> void setValueInAttribute(Attribute<?, T> attr, String strVal) {
// T val;
// if ( attr instanceof DateAttribute ) {
// val = (T) Date.parseDate(strVal);
// } else if ( attr instanceof CodeAttribute ) {
// val = (T) new Code(strVal);
// } else if ( attr instanceof CoordinateAttribute ) {
// val = (T) Coordinate.parseCoordinate(strVal);
// } else if ( attr instanceof TextAttribute ) {
// val = (T) new TextValue(strVal);
// } else {
// throw new UnsupportedOperationException("Attribute type not supported: " + attr.getClass().getName());
// }
// attr.setValue(val);
// }
private Entity getOrCreateParentEntity(CollectRecord record, DataLine line) {
    Survey survey = record.getSurvey();
    Schema schema = survey.getSchema();
    EntityDefinition parentEntityDefn = (EntityDefinition) schema.getDefinitionById(parentEntityDefinitionId);
    Entity rootEntity = record.getRootEntity();
    Entity currentParent = rootEntity;
    List<EntityDefinition> ancestorEntityDefns = parentEntityDefn.getAncestorEntityDefinitionsInReverseOrder();
    ancestorEntityDefns.add(parentEntityDefn);
    // skip the root entity
    for (int i = 1; i < ancestorEntityDefns.size(); i++) {
        EntityDefinition ancestorDefn = ancestorEntityDefns.get(i);
        String ancestorName = ancestorDefn.getName();
        EntityIdentifier<?> identifier = line.getAncestorIdentifier(ancestorDefn.getId());
        Entity childEntity;
        if (ancestorDefn.isMultiple()) {
            List<Entity> childEntities = findChildEntities(currentParent, ancestorName, identifier);
            switch(childEntities.size()) {
                case 0:
                    if (settings.isCreateAncestorEntities() || ancestorDefn == parentEntityDefn) {
                        childEntity = createChildEntity(currentParent, ancestorName, identifier, line.getColumnNamesByField(), line.getLineNumber());
                    } else {
                        status.addParsingError(createParentEntitySearchError(record, line, identifier, PARENT_ENTITY_NOT_FOUND_MESSAGE_KEY));
                        return null;
                    }
                    break;
                case 1:
                    childEntity = childEntities.get(0);
                    break;
                default:
                    status.addParsingError(createParentEntitySearchError(record, line, identifier, MULTIPLE_PARENT_ENTITY_FOUND_MESSAGE_KEY));
                    return null;
            }
        } else {
            if (currentParent.getCount(ancestorDefn) == 0) {
                Node<?> newNode = ancestorDefn.createNode();
                currentParent.add(newNode);
            }
            childEntity = (Entity) currentParent.getChild(ancestorDefn);
        }
        currentParent = childEntity;
    }
    return currentParent;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entity(org.openforis.idm.model.Entity) CollectSurvey(org.openforis.collect.model.CollectSurvey) Survey(org.openforis.idm.metamodel.Survey) Schema(org.openforis.idm.metamodel.Schema)

Example 37 with Schema

use of org.openforis.idm.metamodel.Schema in project collect by openforis.

the class ValidationMessageBuilder method getComparisonCheckMessageArg.

protected String getComparisonCheckMessageArg(Attribute<?, ?> attribute, String expression, Locale locale) {
    if (StringUtils.isNotBlank(expression)) {
        String result = expression;
        Survey survey = attribute.getSurvey();
        Schema schema = survey.getSchema();
        SurveyContext surveyContext = survey.getContext();
        ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
        try {
            Entity parentEntity = attribute.getParent();
            EntityDefinition parentDefinition = parentEntity.getDefinition();
            Set<String> referencedPaths = expressionEvaluator.determineReferencedPaths(expression);
            for (String path : referencedPaths) {
                String absolutePath = parentDefinition.getPath() + PATH_SEPARATOR + path;
                NodeDefinition nodeDefinition = schema.getDefinitionByPath(absolutePath);
                String label = getPrettyLabelText(nodeDefinition, locale);
                result = result.replaceAll(nodeDefinition.getName(), label);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    } else {
        return expression;
    }
}
Also used : Entity(org.openforis.idm.model.Entity) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Survey(org.openforis.idm.metamodel.Survey) Schema(org.openforis.idm.metamodel.Schema) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 38 with Schema

use of org.openforis.idm.metamodel.Schema 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 39 with Schema

use of org.openforis.idm.metamodel.Schema in project collect by openforis.

the class SchemaPropertyHandler method getProperty.

@Override
public Object getProperty(Object object, String propertyName) {
    if (object instanceof Schema) {
        Schema schema = (Schema) object;
        List<EntityDefinition> entityDefinitions = schema.getRootEntityDefinitions();
        for (EntityDefinition entityDefinition : entityDefinitions) {
            if (propertyName.equals(entityDefinition.getName())) {
                return entityDefinition;
            }
        }
    }
    return null;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Schema(org.openforis.idm.metamodel.Schema)

Example 40 with Schema

use of org.openforis.idm.metamodel.Schema in project collect by openforis.

the class SurveyManagerIntegrationTest method before.

// @Before
public void before() throws SurveyImportException, IOException, InvalidIdmlException {
    if (survey == null) {
        survey = importModel();
    }
    Schema schema = survey.getSchema();
    clusterEntityDefinition = schema.getRootEntityDefinition("cluster");
    CollectRecord record = new CollectRecord(recordManager, survey, "2.0");
    cluster = record.createRootEntity("cluster");
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Schema(org.openforis.idm.metamodel.Schema)

Aggregations

Schema (org.openforis.idm.metamodel.Schema)65 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)46 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)23 CollectSurvey (org.openforis.collect.model.CollectSurvey)19 ArrayList (java.util.ArrayList)10 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)10 Survey (org.openforis.idm.metamodel.Survey)10 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)6 UIOptions (org.openforis.collect.metamodel.ui.UIOptions)5 NumericAttributeDefinition (org.openforis.idm.metamodel.NumericAttributeDefinition)5 Test (org.junit.Test)4 UITab (org.openforis.collect.metamodel.ui.UITab)4 UITabSet (org.openforis.collect.metamodel.ui.UITabSet)4 Step (org.openforis.collect.model.CollectRecord.Step)4 CollectRecordSummary (org.openforis.collect.model.CollectRecordSummary)4 RecordFilter (org.openforis.collect.model.RecordFilter)4 NodeDefinitionVisitor (org.openforis.idm.metamodel.NodeDefinitionVisitor)4 NumberAttributeDefinition (org.openforis.idm.metamodel.NumberAttributeDefinition)4 Entity (org.openforis.idm.model.Entity)4 List (java.util.List)3