Search in sources :

Example 81 with AttributeDefinition

use of org.openforis.idm.metamodel.AttributeDefinition in project collect by openforis.

the class CollectEarthGridTemplateGenerator method validateCsvRows.

private List<CSVRowValidationResult> validateCsvRows(CsvReader csvReader, CollectSurvey survey, boolean firstLineIsHeaders, boolean validateOnlyFirstLines) throws IOException {
    List<CSVRowValidationResult> results = new ArrayList<CSVRowValidationResult>();
    List<CSVRowValidationResult> validateCsvRow = null;
    // Get the list of attribute types expected per row!
    List<AttributeDefinition> attributesPerRow = getAttributesPerRow(survey);
    int rowNumber = 1;
    if (!firstLineIsHeaders) {
        validateCsvRow = validateCsvRow(survey, attributesPerRow, (String[]) csvReader.getColumnNames().toArray(new String[csvReader.getColumnNames().size()]), rowNumber++);
        if (!validateCsvRow.isEmpty()) {
            results.addAll(validateCsvRow);
        }
    }
    CsvLine csvLine;
    while ((csvLine = csvReader.readNextLine()) != null) {
        if (validateOnlyFirstLines && rowNumber < 50) {
            validateCsvRow = validateCsvRow(survey, attributesPerRow, csvLine.getLine(), rowNumber++);
            if (validateCsvRow != null) {
                results.addAll(validateCsvRow);
            }
        }
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition) DateAttributeDefinition(org.openforis.idm.metamodel.DateAttributeDefinition) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition) CsvLine(org.openforis.commons.io.csv.CsvLine)

Example 82 with AttributeDefinition

use of org.openforis.idm.metamodel.AttributeDefinition in project collect by openforis.

the class CollectEarthGridTemplateGenerator method generateTemplateCSVFile.

public File generateTemplateCSVFile(CollectSurvey survey, InputStream streamFileWithCsv) throws IOException {
    // copy the template txt file into a String
    StringWriter writer = new StringWriter();
    IOUtils.copy(streamFileWithCsv, writer, "UTF-8");
    String templateContent = writer.toString();
    // find "fromCSV" attributes
    List<AttributeDefinition> fromCsvAttributes = survey.getExtendedDataFields();
    // write the dynamic content to be replaced into the template
    StringBuffer headerSB = new StringBuffer();
    StringBuffer valuesSB = new StringBuffer();
    for (AttributeDefinition attrDef : fromCsvAttributes) {
        String attrName = attrDef.getName();
        headerSB.append(",\"" + attrName + "\"");
        String value = getDummyValue(attrDef, null);
        valuesSB.append(",\"").append(value).append("\"");
    }
    String content = templateContent.replace(CollectEarthProjectFileCreator.PLACEHOLDER_FOR_EXTRA_COLUMNS_HEADER, headerSB.toString());
    content = content.replace(CollectEarthProjectFileCreator.PLACEHOLDER_FOR_EXTRA_COLUMNS_VALUES, valuesSB.toString());
    List<AttributeDefinition> keyAttributeDefinitions = survey.getSchema().getFirstRootEntityDefinition().getKeyAttributeDefinitions();
    String keyAttributes = "";
    for (AttributeDefinition keyAttributeDefinition : keyAttributeDefinitions) {
        keyAttributes += keyAttributeDefinition.getName() + ",";
    }
    keyAttributes = keyAttributes.substring(0, keyAttributes.lastIndexOf(","));
    content = content.replace(CollectEarthProjectFileCreator.PLACEHOLDER_ID_COLUMNS_HEADER, keyAttributes);
    for (int i = 1; i <= 200; i++) {
        String keyValues = "";
        for (AttributeDefinition keyAttributeDefinition : keyAttributeDefinitions) {
            String value = getDummyValue(keyAttributeDefinition, i);
            keyValues += value + ",";
        }
        String replaceIdsLine = CollectEarthProjectFileCreator.PLACEHOLDER_ID_COLUMNS_VALUES + "_" + i + ",";
        content = content.replace(replaceIdsLine, keyValues);
    }
    return Files.writeToTempFile(content, "collect-earth-project-file-creator", ".ced");
}
Also used : StringWriter(java.io.StringWriter) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition) DateAttributeDefinition(org.openforis.idm.metamodel.DateAttributeDefinition) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition)

Example 83 with AttributeDefinition

use of org.openforis.idm.metamodel.AttributeDefinition in project collect by openforis.

the class AttributeDefinitionPR method onStartDefinition.

@Override
protected void onStartDefinition() throws XmlParseException, XmlPullParserException, IOException {
    super.onStartDefinition();
    Boolean key = getBooleanAttribute(KEY, false);
    AttributeDefinition defn = (AttributeDefinition) getDefinition();
    defn.setKey(key == null ? false : key);
    if (defn instanceof Calculable) {
        boolean calculated = getBooleanAttributeWithDefault(CALCULATED, false);
        ((Calculable) defn).setCalculated(calculated);
    }
    defn.setReferencedAttributeId(getIntegerAttribute(REFERENCED_ATTRIBUTE, false));
}
Also used : Calculable(org.openforis.idm.metamodel.Calculable) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition)

Example 84 with AttributeDefinition

use of org.openforis.idm.metamodel.AttributeDefinition in project collect by openforis.

the class EntitySchema method mergeFrom.

@Override
public void mergeFrom(Input input, Entity entity) throws IOException {
    EntityDefinition entityDef = entity.getDefinition();
    for (int number = input.readFieldNumber(this); number > 0; number = input.readFieldNumber(this)) {
        switch(number) {
            case DEFINITION_ID_FIELD_NUMBER:
                // Definition id
                int definitionId = input.readUInt32();
                NodeDefinition defn = null;
                try {
                    defn = entityDef.getChildDefinition(definitionId);
                } catch (IllegalArgumentException e) {
                // not existing child definition
                }
                if (defn == null || (defn instanceof AttributeDefinition && ((AttributeDefinition) defn).isCalculated())) {
                    skipNode(input);
                } else {
                    Node<?> node = defn.createNode();
                    entity.add(node);
                    // Node
                    readAndCheckFieldNumber(input, NODE_FIELD_NUMBER);
                    input.mergeObject(node, getSchema(node.getClass()));
                }
                break;
            case CHILD_NODE_STATE_FIELD_NUMBER:
                // Node state
                int intState = input.readInt32();
                State state = State.parseState(intState);
                readAndCheckFieldNumber(input, CHILD_DEFINITION_ID_FIELD_NUMBER);
                int childDefnId = input.readInt32();
                NodeDefinition childDefn = null;
                try {
                    childDefn = entityDef.getChildDefinition(childDefnId);
                    entity.setChildState(childDefn, state);
                } catch (IllegalArgumentException e) {
                // not existing child definition
                }
                break;
            default:
                throw new ProtostuffException("Unexpected field number");
        }
    }
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) ProtostuffException(com.dyuproject.protostuff.ProtostuffException)

Example 85 with AttributeDefinition

use of org.openforis.idm.metamodel.AttributeDefinition in project collect by openforis.

the class RelationalSchemaGenerator method addAncestorKeyColumns.

protected void addAncestorKeyColumns(DataTable table) throws CollectRdbException {
    NodeDefinition nodeDefn = table.getNodeDefinition();
    List<EntityDefinition> ancestors = nodeDefn.getAncestorEntityDefinitionsInReverseOrder();
    for (int levelIdx = 0; levelIdx < ancestors.size(); levelIdx++) {
        EntityDefinition ancestor = ancestors.get(levelIdx);
        List<AttributeDefinition> keyAttrDefns = ancestor.getKeyAttributeDefinitions();
        for (AttributeDefinition keyDefn : keyAttrDefns) {
            FieldDefinition<?> fieldDefn = getKeyAttributeValueFieldDefinition(keyDefn);
            Path fieldRelativePath = createAncestorKeyRelativePath(ancestors.size() - levelIdx, fieldDefn);
            String colName = getAncestorKeyColumnName(keyDefn);
            AncestorKeyColumn col = new AncestorKeyColumn(colName, fieldDefn, fieldRelativePath);
            addColumn(table, col);
        }
    }
}
Also used : Path(org.openforis.idm.path.Path) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) CoordinateAttributeDefinition(org.openforis.idm.metamodel.CoordinateAttributeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) TextAttributeDefinition(org.openforis.idm.metamodel.TextAttributeDefinition) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition) DateAttributeDefinition(org.openforis.idm.metamodel.DateAttributeDefinition) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) TimeAttributeDefinition(org.openforis.idm.metamodel.TimeAttributeDefinition) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition)

Aggregations

AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)107 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)47 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)37 ArrayList (java.util.ArrayList)33 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)27 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)21 NumericAttributeDefinition (org.openforis.idm.metamodel.NumericAttributeDefinition)20 DateAttributeDefinition (org.openforis.idm.metamodel.DateAttributeDefinition)18 NumberAttributeDefinition (org.openforis.idm.metamodel.NumberAttributeDefinition)18 CollectSurvey (org.openforis.collect.model.CollectSurvey)17 CoordinateAttributeDefinition (org.openforis.idm.metamodel.CoordinateAttributeDefinition)16 KeyAttributeDefinition (org.openforis.idm.metamodel.KeyAttributeDefinition)16 TextAttributeDefinition (org.openforis.idm.metamodel.TextAttributeDefinition)12 TimeAttributeDefinition (org.openforis.idm.metamodel.TimeAttributeDefinition)12 CollectAnnotations (org.openforis.collect.metamodel.CollectAnnotations)11 Schema (org.openforis.idm.metamodel.Schema)10 UIOptions (org.openforis.collect.metamodel.ui.UIOptions)9 Entity (org.openforis.idm.model.Entity)9 Value (org.openforis.idm.model.Value)9 RangeAttributeDefinition (org.openforis.idm.metamodel.RangeAttributeDefinition)8