Search in sources :

Example 1 with IntegerRange

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

the class IntegerRangeValidator method evaluate.

@Override
public ValidationResultFlag evaluate(IntegerRangeAttribute node) {
    IntegerRange range = node.getValue();
    if (range != null) {
        Integer from = range.getFrom();
        Integer to = range.getTo();
        if (!(from == null || to == null)) {
            boolean valid = to >= from;
            return ValidationResultFlag.valueOf(valid);
        }
    }
    return ValidationResultFlag.OK;
}
Also used : IntegerRange(org.openforis.idm.model.IntegerRange)

Example 2 with IntegerRange

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

the class JSONValueParser method parseIntegerRange.

public IntegerRange parseIntegerRange(RangeAttributeDefinition attrDef, String value) {
    Map<String, Object> map = parseJSONToMap(value);
    if (map == null) {
        return null;
    }
    Integer from = getInteger(map, IntegerRange.FROM_FIELD);
    Integer to = getInteger(map, IntegerRange.TO_FIELD);
    if (to == null) {
        to = from;
    }
    Integer unitId = getInteger(map, NumberValue.UNIT_ID_FIELD);
    Unit unit = attrDef.getActualUnit(unitId);
    return new IntegerRange(from, to, unit);
}
Also used : IntegerRange(org.openforis.idm.model.IntegerRange) Unit(org.openforis.idm.metamodel.Unit)

Example 3 with IntegerRange

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

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

the class AttributeValueUtils method extractNormalizedValue.

private static Object extractNormalizedValue(NumericRangeAttribute<?, ?> attr) {
    NumericRange<?> value = attr.getValue();
    RangeAttributeDefinition defn = attr.getDefinition();
    List<Unit> units = defn.getUnits();
    if (units != null && units.size() > 1) {
        Unit unit = attr.getUnit();
        Unit defaultUnit = defn.getDefaultUnit();
        if (unit != null && defaultUnit != null && unit != defaultUnit) {
            Number from = value.getFrom();
            Number to = value.getTo();
            NumericRange<?> normalizedValue;
            if (attr instanceof IntegerRangeAttribute) {
                int normalizedFrom = getNormalizedValue(from, unit, defaultUnit).intValue();
                int normalizedTo = getNormalizedValue(to, unit, defaultUnit).intValue();
                normalizedValue = new IntegerRange(normalizedFrom, normalizedTo, defaultUnit);
            } else {
                double normalizedFrom = getNormalizedValue(from, unit, defaultUnit).doubleValue();
                double normalizedTo = getNormalizedValue(to, unit, defaultUnit).doubleValue();
                normalizedValue = new RealRange(normalizedFrom, normalizedTo, defaultUnit);
            }
            return normalizedValue;
        } else {
            return value;
        }
    } else {
        return value;
    }
}
Also used : IntegerRange(org.openforis.idm.model.IntegerRange) RealRange(org.openforis.idm.model.RealRange) Unit(org.openforis.idm.metamodel.Unit) IntegerRangeAttribute(org.openforis.idm.model.IntegerRangeAttribute) RangeAttributeDefinition(org.openforis.idm.metamodel.RangeAttributeDefinition)

Aggregations

IntegerRange (org.openforis.idm.model.IntegerRange)4 Unit (org.openforis.idm.metamodel.Unit)2 NumberFormat (java.text.NumberFormat)1 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)1 CodeList (org.openforis.idm.metamodel.CodeList)1 CodeListService (org.openforis.idm.metamodel.CodeListService)1 RangeAttributeDefinition (org.openforis.idm.metamodel.RangeAttributeDefinition)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 IntegerRangeAttribute (org.openforis.idm.model.IntegerRangeAttribute)1 NumberValue (org.openforis.idm.model.NumberValue)1 NumericRange (org.openforis.idm.model.NumericRange)1 RealRange (org.openforis.idm.model.RealRange)1 TextValue (org.openforis.idm.model.TextValue)1 Time (org.openforis.idm.model.Time)1