Search in sources :

Example 1 with NumericRange

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

the class ModelRelationalExpression method getValue.

private Object getValue(Object object) {
    if (object instanceof Number || object instanceof NumericRange || object instanceof String) {
        return object;
    }
    if (object instanceof Boolean) {
        return ((Boolean) object).booleanValue() ? 0.0 : 1.0;
    } else if (object instanceof Value) {
        return AttributeValueUtils.extractMainFieldValue((Value) object, normalizeNumbers);
    }
    if (object instanceof NodePointer) {
        if (object instanceof VariablePointer && normalizeNumbers) {
            ModelNodePointer valuePointer = (ModelNodePointer) ((NodePointer) object).getValuePointer();
            valuePointer.setNormalizeNumbers(true);
        }
        return getValue(((NodePointer) object).getValue());
    }
    if (object instanceof EvalContext) {
        EvalContext ctx = (EvalContext) object;
        Pointer ptr = ctx.getSingleNodePointer();
        return ptr == null ? Double.NaN : getValue(ptr);
    }
    return null;
}
Also used : NumericRange(org.openforis.idm.model.NumericRange) VariablePointer(org.apache.commons.jxpath.ri.model.VariablePointer) Value(org.openforis.idm.model.Value) EvalContext(org.apache.commons.jxpath.ri.EvalContext) NodePointer(org.apache.commons.jxpath.ri.model.NodePointer) Pointer(org.apache.commons.jxpath.Pointer) VariablePointer(org.apache.commons.jxpath.ri.model.VariablePointer) NodePointer(org.apache.commons.jxpath.ri.model.NodePointer)

Example 2 with NumericRange

use of org.openforis.idm.model.NumericRange 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 3 with NumericRange

use of org.openforis.idm.model.NumericRange 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)

Aggregations

NumericRange (org.openforis.idm.model.NumericRange)3 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)2 Value (org.openforis.idm.model.Value)2 NumberFormat (java.text.NumberFormat)1 Pointer (org.apache.commons.jxpath.Pointer)1 EvalContext (org.apache.commons.jxpath.ri.EvalContext)1 NodePointer (org.apache.commons.jxpath.ri.model.NodePointer)1 VariablePointer (org.apache.commons.jxpath.ri.model.VariablePointer)1 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)1 CodeList (org.openforis.idm.metamodel.CodeList)1 CodeListService (org.openforis.idm.metamodel.CodeListService)1 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)1 RangeAttributeDefinition (org.openforis.idm.metamodel.RangeAttributeDefinition)1 Unit (org.openforis.idm.metamodel.Unit)1 BooleanValue (org.openforis.idm.model.BooleanValue)1 Code (org.openforis.idm.model.Code)1 Coordinate (org.openforis.idm.model.Coordinate)1 Date (org.openforis.idm.model.Date)1 File (org.openforis.idm.model.File)1 IntegerRange (org.openforis.idm.model.IntegerRange)1