Search in sources :

Example 1 with NumberValue

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

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

the class CollectEarthGridTemplateGenerator method validateCell.

/**
 * Checks if a value can be used as the input for an attribute
 * @param attributeDefinition The attribute that we want to check the value against
 * @param value The value that should be checked
 * @return True if the value can be used in the attribute. False otherwise (for instance trying to use a string "abc" as the input for a Number attribute
 */
private String validateCell(CollectSurvey survey, AttributeDefinition attributeDefinition, String value) {
    try {
        // By creating the value using hte attribute definitoon a validation is performed
        Value valueCreated = attributeDefinition.createValue(value);
        if (attributeDefinition.isAlwaysRequired() && StringUtils.isBlank(value)) {
            return String.format("The attribute %s is marekd as \"always required\". The value in the cell is empty!", attributeDefinition.getLabel(NodeLabel.Type.INSTANCE));
        }
        if (isEpsg4326SRS(survey) && attributeDefinition.getName().equals(LAT_COORDINATE)) {
            double lat = ((NumberValue<Number>) valueCreated).getValue().doubleValue();
            if (lat < -90 || lat > 90) {
                return "The latitude of a plot must be between -90 and 90 degrees!";
            }
        }
        if (isEpsg4326SRS(survey) && attributeDefinition.getName().equals(LONG_COORDINATE)) {
            double longitude = ((NumberValue<Number>) valueCreated).getValue().doubleValue();
            if (longitude < -180 || longitude > 180) {
                return "The latitude of a plot must be between -180 and 180 degrees!";
            }
        }
        // Make sure that the code used in a code-attribute is actually present on the codelist
        if (attributeDefinition instanceof CodeAttributeDefinition) {
            CodeAttributeDefinition cad = (CodeAttributeDefinition) attributeDefinition;
            // IF IT IS A STRICT CODE LIST
            if (!cad.isAllowUnlisted()) {
                // Check that the code exists in the codelist
                // Gets the level (in case of hierarchical codelists) that the attribute refers to. If it is a flat codelist then it is alway 0
                int levelIndex = cad.getLevelIndex();
                CodeListService codeListService = attributeDefinition.getSurvey().getContext().getCodeListService();
                List<CodeListItem> items = codeListService.loadItems(cad.getList(), levelIndex + 1);
                // Check one by one in the codes of the codelist assigned to the attribute if the value is present as a code!
                for (CodeListItem codeListItem : items) {
                    if (codeListItem.getCode().equals(value)) {
                        // FOUND! All good, return null
                        return null;
                    }
                }
                return String.format("The code with value \"%s\" is not part of the codelist used by code %s ", value, attributeDefinition.getLabel(NodeLabel.Type.INSTANCE));
            }
        } else if (attributeDefinition instanceof CodeAttributeDefinition) {
        }
    } catch (Exception e) {
        return String.format("The value \"%s\" cannot be used as a value for the attribute %s", value, attributeDefinition.getLabel(NodeLabel.Type.INSTANCE));
    }
    return null;
}
Also used : CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) NumberValue(org.openforis.idm.model.NumberValue) Value(org.openforis.idm.model.Value) CodeListService(org.openforis.idm.metamodel.CodeListService) CodeListItem(org.openforis.idm.metamodel.CodeListItem) IOException(java.io.IOException)

Aggregations

CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)2 CodeListService (org.openforis.idm.metamodel.CodeListService)2 NumberValue (org.openforis.idm.model.NumberValue)2 IOException (java.io.IOException)1 NumberFormat (java.text.NumberFormat)1 CodeList (org.openforis.idm.metamodel.CodeList)1 CodeListItem (org.openforis.idm.metamodel.CodeListItem)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 NumericRange (org.openforis.idm.model.NumericRange)1 TextValue (org.openforis.idm.model.TextValue)1 Time (org.openforis.idm.model.Time)1 Value (org.openforis.idm.model.Value)1