Search in sources :

Example 1 with Field

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

the class RecordConverter method convertToLatestUnitStorage.

/**
 * This is a workaround: it avoids to refer to the old unit field to get data
 *
 * TODO remove this conversion or apply only for records stored using version prior to 3.0 Alpha 5
 *
 * @param survey
 * @param rootEntity
 */
protected void convertToLatestUnitStorage(CollectRecord record) {
    final CollectSurvey survey = (CollectSurvey) record.getSurvey();
    Entity rootEntity = record.getRootEntity();
    rootEntity.traverse(new NodeVisitor() {

        @Override
        public void visit(Node<? extends NodeDefinition> node, int idx) {
            if (node instanceof NumberAttribute<?, ?> || node instanceof NumericRangeAttribute<?, ?>) {
                Field<String> unitNameField;
                Field<Integer> unitField;
                if (node instanceof NumberAttribute<?, ?>) {
                    unitNameField = ((NumberAttribute<?, ?>) node).getUnitNameField();
                    unitField = ((NumberAttribute<?, ?>) node).getUnitField();
                } else {
                    unitNameField = ((NumericRangeAttribute<?, ?>) node).getUnitNameField();
                    unitField = ((NumericRangeAttribute<?, ?>) node).getUnitField();
                }
                if (unitNameField.hasData()) {
                    moveDataToNewUnitField(survey, unitNameField, unitField);
                }
            }
        }
    });
}
Also used : Entity(org.openforis.idm.model.Entity) Field(org.openforis.idm.model.Field) NumericRangeAttribute(org.openforis.idm.model.NumericRangeAttribute) NumberAttribute(org.openforis.idm.model.NumberAttribute) CollectSurvey(org.openforis.collect.model.CollectSurvey) NodeVisitor(org.openforis.idm.model.NodeVisitor)

Example 2 with Field

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

the class JSONValueFormatter method formatValue.

@SuppressWarnings("unchecked")
public String formatValue(Attribute<?, ?> attr) {
    if (attr.isEmpty()) {
        return null;
    }
    JSONObject jsonObj = new JSONObject();
    List<Field<?>> fields = attr.getFields();
    for (Field<?> field : fields) {
        jsonObj.put(field.getName(), field.getValue());
    }
    return jsonObj.toJSONString();
}
Also used : Field(org.openforis.idm.model.Field) JSONObject(org.json.simple.JSONObject)

Aggregations

Field (org.openforis.idm.model.Field)2 JSONObject (org.json.simple.JSONObject)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 Entity (org.openforis.idm.model.Entity)1 NodeVisitor (org.openforis.idm.model.NodeVisitor)1 NumberAttribute (org.openforis.idm.model.NumberAttribute)1 NumericRangeAttribute (org.openforis.idm.model.NumericRangeAttribute)1