Search in sources :

Example 11 with NumericAttributeDefinition

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

the class CSVDataImportProcess method setUnitField.

private void setUnitField(Attribute<?, ?> attr, String value, long row, String colName) {
    if (StringUtils.isBlank(value)) {
        ((NumberAttribute<?, ?>) attr).setUnit(null);
    } else {
        Survey survey = attr.getSurvey();
        Unit unit = survey.getUnit(value);
        NumericAttributeDefinition defn = (NumericAttributeDefinition) attr.getDefinition();
        if (unit == null || !defn.getUnits().contains(unit)) {
            ParsingError parsingError = new ParsingError(ErrorType.INVALID_VALUE, row, colName, UNIT_NOT_FOUND_MESSAGE_KEY);
            parsingError.setMessageArgs(new String[] { value });
            status.addParsingError(parsingError);
        } else {
            Field<Integer> field = ((NumberAttribute<?, ?>) attr).getUnitField();
            NodeChangeSet changes = recordUpdater.updateField(field, unit.getId());
            if (nodeChangeBatchProcessor != null) {
                nodeChangeBatchProcessor.add(changes, adminUser.getUsername());
            }
        }
    }
}
Also used : NodeChangeSet(org.openforis.collect.model.NodeChangeSet) CollectSurvey(org.openforis.collect.model.CollectSurvey) Survey(org.openforis.idm.metamodel.Survey) ParsingError(org.openforis.collect.io.metadata.parsing.ParsingError) NumberAttribute(org.openforis.idm.model.NumberAttribute) Unit(org.openforis.idm.metamodel.Unit) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition)

Example 12 with NumericAttributeDefinition

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

the class SurveyValidator method validateDataTypeNotChanged.

protected List<SurveyValidationResult> validateDataTypeNotChanged(CollectSurvey oldPublishedSurvey, CollectSurvey newSurvey) {
    final Schema oldSchema = oldPublishedSurvey.getSchema();
    SurveyValidationNodeDefinitionVisitor visitor = new SurveyValidationNodeDefinitionVisitor() {

        @Override
        public void visit(NodeDefinition nodeDefn) {
            NodeDefinition oldDefn = oldSchema.getDefinitionById(nodeDefn.getId());
            if (oldDefn != null && (oldDefn.getClass() != nodeDefn.getClass() || oldDefn instanceof NumericAttributeDefinition && ((NumericAttributeDefinition) oldDefn).getType() != ((NumericAttributeDefinition) nodeDefn).getType())) {
                String messageKey = "survey.validation.error.data_type_changed";
                String path = nodeDefn.getPath();
                SurveyValidationResult result = new SurveyValidationResult(path, messageKey);
                addResult(result);
            }
        }
    };
    visitNodeDefinitions(newSurvey, visitor);
    return visitor.getResults();
}
Also used : Schema(org.openforis.idm.metamodel.Schema) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition)

Example 13 with NumericAttributeDefinition

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

the class CalculatedAttributeTest method addItem.

protected Entity addItem(Entity parentEntity, Integer qtyValue, Double priceValue) {
    EntityDefinition rootEntityDefn = parentEntity.getDefinition();
    EntityDefinition itemDefn = (EntityDefinition) rootEntityDefn.getChildDefinition("item");
    Entity item = (Entity) itemDefn.createNode();
    if (qtyValue != null) {
        NodeDefinition qtyDefn = itemDefn.getChildDefinition("qty");
        IntegerAttribute qty = (IntegerAttribute) qtyDefn.createNode();
        qty.setValue(new IntegerValue(qtyValue, null));
        item.add(qty);
    }
    if (priceValue != null) {
        NumericAttributeDefinition priceDefn = (NumericAttributeDefinition) itemDefn.getChildDefinition("price");
        RealAttribute price = (RealAttribute) priceDefn.createNode();
        price.setValue(new RealValue(priceValue, null));
        item.add(price);
    }
    NumberAttributeDefinition totalDefn = (NumberAttributeDefinition) itemDefn.getChildDefinition("total");
    RealAttribute total = (RealAttribute) totalDefn.createNode();
    item.add(total);
    NumberAttributeDefinition discountDefn = (NumberAttributeDefinition) itemDefn.getChildDefinition("discount_percent");
    IntegerAttribute discount = (IntegerAttribute) discountDefn.createNode();
    item.add(discount);
    EntityBuilder.addValue(item, "time", new Time(110, 5));
    parentEntity.add(item);
    recordUpdater.initializeRecord(record);
    return item;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition)

Aggregations

NumericAttributeDefinition (org.openforis.idm.metamodel.NumericAttributeDefinition)13 Test (org.junit.Test)4 AttributeDefault (org.openforis.idm.metamodel.AttributeDefault)4 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)4 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)3 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)3 ArrayList (java.util.ArrayList)2 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)2 Schema (org.openforis.idm.metamodel.Schema)2 List (java.util.List)1 Stack (java.util.Stack)1 ParsingError (org.openforis.collect.io.metadata.parsing.ParsingError)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 NodeChangeSet (org.openforis.collect.model.NodeChangeSet)1 CodeTable (org.openforis.collect.relational.model.CodeTable)1 DataColumn (org.openforis.collect.relational.model.DataColumn)1 DataTable (org.openforis.collect.relational.model.DataTable)1 CodeListItem (org.openforis.idm.metamodel.CodeListItem)1 CodeListLevel (org.openforis.idm.metamodel.CodeListLevel)1 DateAttributeDefinition (org.openforis.idm.metamodel.DateAttributeDefinition)1