Search in sources :

Example 21 with Value

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

the class BaseAttributeUpdateRequestProxy method parseCompositeAttributeValue.

protected Value parseCompositeAttributeValue(CodeListManager codeListManager, Entity parentEntity, String attributeName, Object value) {
    EntityDefinition parentEntityDefn = parentEntity.getDefinition();
    AttributeDefinition defn = (AttributeDefinition) parentEntityDefn.getChildDefinition(attributeName);
    if (defn instanceof CodeAttributeDefinition) {
        if (value instanceof String) {
            String stringVal = (String) value;
            Value result = parseCode(codeListManager, parentEntity, (CodeAttributeDefinition) defn, stringVal);
            return result;
        } else {
            throw new IllegalArgumentException("Invalid value type: expected String");
        }
    } else if (defn instanceof RangeAttributeDefinition) {
        if (value instanceof String) {
            String stringVal = (String) value;
            RangeAttributeDefinition rangeDef = (RangeAttributeDefinition) defn;
            RangeAttributeDefinition.Type type = rangeDef.getType();
            NumericRange<?> range = null;
            // todo check if unit is required here or is set later by the client
            Unit unit = null;
            switch(type) {
                case INTEGER:
                    range = IntegerRange.parseIntegerRange(stringVal, unit);
                    break;
                case REAL:
                    range = RealRange.parseRealRange(stringVal, unit);
                    break;
            }
            return range;
        } else {
            throw new IllegalArgumentException("Invalid value type: expected String");
        }
    } else {
        throw new IllegalArgumentException("Invalid AttributeDefinition: expected CodeAttributeDefinition or RangeAttributeDefinition");
    }
}
Also used : NumericRange(org.openforis.idm.model.NumericRange) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) Value(org.openforis.idm.model.Value) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) RangeAttributeDefinition(org.openforis.idm.metamodel.RangeAttributeDefinition) Unit(org.openforis.idm.metamodel.Unit) RangeAttributeDefinition(org.openforis.idm.metamodel.RangeAttributeDefinition)

Example 22 with Value

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

the class CreateRecordHandler method execute.

@Override
public List<RecordEvent> execute(CreateRecordCommand command) {
    String username = command.getUsername();
    User user = userManager.loadByUserName(username);
    CollectSurvey survey = surveyManager.getById(command.getSurveyId());
    List<EntityDefinition> rootDefs = survey.getSchema().getRootEntityDefinitions();
    EntityDefinition firstRootEntity = rootDefs.get(0);
    String firstRootEntityName = firstRootEntity.getName();
    CollectRecord record = recordManager.instantiateRecord(survey, firstRootEntityName, user, command.getFormVersion(), Step.ENTRY);
    NodeChangeSet changeSet = recordManager.initializeRecord(record);
    List<String> keyValues = command.getKeyValues();
    Iterator<String> keyValuesIt = keyValues.iterator();
    List<AttributeDefinition> keyAttributeDefinitions = firstRootEntity.getKeyAttributeDefinitions();
    Iterator<AttributeDefinition> keyDefsIt = keyAttributeDefinitions.iterator();
    while (keyDefsIt.hasNext()) {
        AttributeDefinition keyDef = keyDefsIt.next();
        String keyVal = keyValuesIt.next();
        Value keyValue = keyDef.createValue(keyVal);
        Attribute<?, Value> keyAttr = record.findNodeByPath(keyDef.getPath());
        recordUpdater.updateAttribute(keyAttr, keyValue);
    }
    recordManager.save(record);
    List<RecordEvent> events = new EventProducer().produceFor(changeSet, user.getUsername());
    for (RecordEvent recordEvent : events) {
        recordEvent.initializeRecordId(record.getId());
    }
    return events;
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) NodeChangeSet(org.openforis.collect.model.NodeChangeSet) User(org.openforis.collect.model.User) EventProducer(org.openforis.collect.event.EventProducer) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) RecordEvent(org.openforis.collect.event.RecordEvent) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Value(org.openforis.idm.model.Value) CollectSurvey(org.openforis.collect.model.CollectSurvey)

Example 23 with Value

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

the class DeleteNodeCommandHandler method execute.

@Override
public List<RecordEvent> execute(C command) {
    CollectRecord record = findRecord(command);
    Attribute<?, Value> attribute = findAttribute(command, record);
    NodeChangeSet changeSet = recordUpdater.deleteNode(attribute);
    recordManager.save(record);
    List<RecordEvent> events = new EventProducer().produceFor(changeSet, command.getUsername());
    return events;
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) NodeChangeSet(org.openforis.collect.model.NodeChangeSet) Value(org.openforis.idm.model.Value) EventProducer(org.openforis.collect.event.EventProducer) RecordEvent(org.openforis.collect.event.RecordEvent)

Example 24 with Value

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

the class CollectEarthGridTemplateGenerator method validateCell.

/**
 * Checks if a value can be used as the input for an attribute
 * @param attributeDefinition The attribute that we want to check the value against
 * @param value The value that should be checked
 * @return True if the value can be used in the attribute. False otherwise (for instance trying to use a string "abc" as the input for a Number attribute
 */
private String validateCell(CollectSurvey survey, AttributeDefinition attributeDefinition, String value) {
    try {
        // By creating the value using hte attribute definitoon a validation is performed
        Value valueCreated = attributeDefinition.createValue(value);
        if (attributeDefinition.isAlwaysRequired() && StringUtils.isBlank(value)) {
            return String.format("The attribute %s is marekd as \"always required\". The value in the cell is empty!", attributeDefinition.getLabel(NodeLabel.Type.INSTANCE));
        }
        if (isEpsg4326SRS(survey) && attributeDefinition.getName().equals(LAT_COORDINATE)) {
            double lat = ((NumberValue<Number>) valueCreated).getValue().doubleValue();
            if (lat < -90 || lat > 90) {
                return "The latitude of a plot must be between -90 and 90 degrees!";
            }
        }
        if (isEpsg4326SRS(survey) && attributeDefinition.getName().equals(LONG_COORDINATE)) {
            double longitude = ((NumberValue<Number>) valueCreated).getValue().doubleValue();
            if (longitude < -180 || longitude > 180) {
                return "The latitude of a plot must be between -180 and 180 degrees!";
            }
        }
        // Make sure that the code used in a code-attribute is actually present on the codelist
        if (attributeDefinition instanceof CodeAttributeDefinition) {
            CodeAttributeDefinition cad = (CodeAttributeDefinition) attributeDefinition;
            // IF IT IS A STRICT CODE LIST
            if (!cad.isAllowUnlisted()) {
                // Check that the code exists in the codelist
                // Gets the level (in case of hierarchical codelists) that the attribute refers to. If it is a flat codelist then it is alway 0
                int levelIndex = cad.getLevelIndex();
                CodeListService codeListService = attributeDefinition.getSurvey().getContext().getCodeListService();
                List<CodeListItem> items = codeListService.loadItems(cad.getList(), levelIndex + 1);
                // Check one by one in the codes of the codelist assigned to the attribute if the value is present as a code!
                for (CodeListItem codeListItem : items) {
                    if (codeListItem.getCode().equals(value)) {
                        // FOUND! All good, return null
                        return null;
                    }
                }
                return String.format("The code with value \"%s\" is not part of the codelist used by code %s ", value, attributeDefinition.getLabel(NodeLabel.Type.INSTANCE));
            }
        } else if (attributeDefinition instanceof CodeAttributeDefinition) {
        }
    } catch (Exception e) {
        return String.format("The value \"%s\" cannot be used as a value for the attribute %s", value, attributeDefinition.getLabel(NodeLabel.Type.INSTANCE));
    }
    return null;
}
Also used : CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) NumberValue(org.openforis.idm.model.NumberValue) Value(org.openforis.idm.model.Value) CodeListService(org.openforis.idm.metamodel.CodeListService) CodeListItem(org.openforis.idm.metamodel.CodeListItem) IOException(java.io.IOException)

Example 25 with Value

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

the class DataQueryResultItem method extractAttributeValue.

public Value extractAttributeValue() {
    AttributeDefinition def = getAttributeDefinition();
    Value val = new JSONValueParser().parseValue(def, value);
    return val;
}
Also used : JSONValueParser(org.openforis.collect.datacleansing.json.JSONValueParser) Value(org.openforis.idm.model.Value) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition)

Aggregations

Value (org.openforis.idm.model.Value)26 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)9 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)8 AbstractValue (org.openforis.idm.model.AbstractValue)6 Attribute (org.openforis.idm.model.Attribute)6 BooleanValue (org.openforis.idm.model.BooleanValue)6 ArrayList (java.util.ArrayList)5 Entity (org.openforis.idm.model.Entity)5 NodeChangeSet (org.openforis.collect.model.NodeChangeSet)4 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)4 BooleanAttribute (org.openforis.idm.model.BooleanAttribute)4 EventProducer (org.openforis.collect.event.EventProducer)3 RecordEvent (org.openforis.collect.event.RecordEvent)3 CollectRecord (org.openforis.collect.model.CollectRecord)3 CollectSurvey (org.openforis.collect.model.CollectSurvey)3 CodeAttribute (org.openforis.idm.model.CodeAttribute)3 EntityKeysIdentifier (org.openforis.collect.io.data.DataLine.EntityKeysIdentifier)2 EntityPositionIdentifier (org.openforis.collect.io.data.DataLine.EntityPositionIdentifier)2 CollectRecordSummary (org.openforis.collect.model.CollectRecordSummary)2 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)2