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;
}
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());
}
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");
}
}
Aggregations