use of org.openforis.idm.metamodel.Unit 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());
}
}
}
}
use of org.openforis.idm.metamodel.Unit in project collect by openforis.
the class NumberColumnProvider method extractValue.
@Override
protected String extractValue(Attribute<?, ?> attr, String fieldName) {
if (NumberAttributeDefinition.UNIT_NAME_FIELD.equals(fieldName)) {
NumberAttribute<?, ?> numAttr = (NumberAttribute<?, ?>) attr;
Unit unit = numAttr.getUnit();
if (unit == null) {
return "";
} else {
return unit.getName();
}
} else {
return super.extractValue(attr, fieldName);
}
}
use of org.openforis.idm.metamodel.Unit in project collect by openforis.
the class JSONValueParser method parseRealRange.
public RealRange parseRealRange(RangeAttributeDefinition attrDef, String value) {
Map<String, Object> map = parseJSONToMap(value);
if (map == null) {
return null;
}
Double from = getDouble(map, RealRange.FROM_FIELD);
Double to = getDouble(map, RealRange.TO_FIELD);
if (to == null) {
to = from;
}
Integer unitId = getInteger(map, RealRange.UNIT_ID_FIELD);
Unit unit = attrDef.getActualUnit(unitId);
return new RealRange(from, to, unit);
}
use of org.openforis.idm.metamodel.Unit in project collect by openforis.
the class JSONValueParser method parseReal.
public RealValue parseReal(NumberAttributeDefinition attrDef, String value) {
Map<String, Object> map = parseJSONToMap(value, RealValue.VALUE_FIELD);
if (map == null) {
return null;
}
Integer unitId = getInteger(map, RealValue.UNIT_ID_FIELD);
Unit unit = attrDef.getActualUnit(unitId);
return new RealValue(getDouble(map, RealValue.VALUE_FIELD), unit);
}
use of org.openforis.idm.metamodel.Unit 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