Search in sources :

Example 76 with AttributeDefinition

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

the class SchemaUpdater method generateAlias.

public EntityDefinition generateAlias(EntityDefinition sourceDef, String sourceFilterAttributeName, EntityDefinition targetParentDef, String targetFilterAttributeName) {
    EntityDefinition aliasDef = survey.getSchema().cloneDefinition(sourceDef, targetFilterAttributeName);
    // add "Alias" suffix to labels
    for (NodeLabel nodeLabel : aliasDef.getLabels()) {
        aliasDef.setLabel(nodeLabel.getType(), nodeLabel.getLanguage(), nodeLabel.getText() + " Alias");
    }
    aliasDef.traverse(new NodeDefinitionVisitor() {

        public void visit(NodeDefinition def) {
            if (def instanceof AttributeDefinition) {
                AttributeDefinition attrDef = (AttributeDefinition) def;
                attrDef.setCalculated(false);
                attrDef.removeAllChecks();
                attrDef.removeAllAttributeDefaults();
            }
            def.setRelevantExpression(null);
            def.setRequiredExpression(null);
        }
    });
    targetParentDef.addChildDefinition(aliasDef);
    aliasDef.setVirtual(true);
    aliasDef.setGeneratorExpression(generateAliasGeneratorExpression(sourceDef, sourceFilterAttributeName, targetParentDef, targetFilterAttributeName));
    UIOptions uiOptions = survey.getUIOptions();
    // prevent layout errors
    uiOptions.setLayout(aliasDef, Layout.FORM);
    uiOptions.setHidden(aliasDef, true);
    return aliasDef;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) NodeLabel(org.openforis.idm.metamodel.NodeLabel) UIOptions(org.openforis.collect.metamodel.ui.UIOptions) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) NodeDefinitionVisitor(org.openforis.idm.metamodel.NodeDefinitionVisitor)

Example 77 with AttributeDefinition

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

the class CalculatedAttributeFormulaFormValidator method validateExpression.

private void validateExpression(ValidationContext ctx) {
    if (validateRequired(ctx, EXPRESSION_FIELD)) {
        AttributeDefinition attrDefn = getAttributeDefinition(ctx);
        EntityDefinition parentEntityDefn = getParentEntityDefinition(ctx);
        validateValueExpression(ctx, attrDefn, parentEntityDefn, EXPRESSION_FIELD);
    }
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition)

Example 78 with AttributeDefinition

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

the class RecordIndexManager method search.

public List<String> search(SearchType searchType, Survey survey, int attributeDefnId, int fieldIndex, String queryText, int maxResults) throws RecordIndexException {
    Schema schema = survey.getSchema();
    AttributeDefinition defn = (AttributeDefinition) schema.getDefinitionById(attributeDefnId);
    String indexName = defn.getAnnotation(Annotation.AUTOCOMPLETE.getQName());
    if (StringUtils.isNotBlank(indexName)) {
        IndexSearcher indexSearcher = null;
        try {
            indexSearcher = createIndexSearcher();
            Set<String> result = search(indexName, indexSearcher, searchType, queryText, fieldIndex, maxResults);
            List<String> sortedList = getSortedList(result);
            return sortedList;
        } catch (Exception e) {
            throw new RecordIndexException(e);
        } finally {
            close(indexSearcher);
        }
    } else {
        throw new RecordIndexException("Index name is not defined for attribute with id: " + attributeDefnId);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Schema(org.openforis.idm.metamodel.Schema) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) ParseException(org.apache.lucene.queryParser.ParseException) IOException(java.io.IOException)

Example 79 with AttributeDefinition

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

the class RecordManager method checkAllKeysSpecified.

// END OF RECORD UPDATE METHODS
private void checkAllKeysSpecified(CollectRecord record) throws MissingRecordKeyException {
    Entity rootEntity = record.getRootEntity();
    List<String> rootEntityKeyValues = record.getRootEntityKeyValues();
    List<AttributeDefinition> keyDefns = rootEntity.getDefinition().getKeyAttributeDefinitions();
    for (int i = 0; i < keyDefns.size(); i++) {
        AttributeDefinition keyDefn = keyDefns.get(i);
        EntityDefinition keyParentDefn = keyDefn.getParentEntityDefinition();
        Entity keyParent = record.findNodeByPath(keyParentDefn.getPath());
        if (keyParent == null) {
            throw new MissingRecordKeyException();
        }
        boolean required = keyParent.isRequired(keyDefn);
        if (required) {
            Node<?> keyNode = keyParent.getChild(keyDefn);
            if (keyNode == null) {
                throw new MissingRecordKeyException();
            } else {
                String keyValue = rootEntityKeyValues.get(i);
                if (StringUtils.isBlank(keyValue)) {
                    throw new MissingRecordKeyException();
                }
            }
        }
    }
}
Also used : Entity(org.openforis.idm.model.Entity) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) MissingRecordKeyException(org.openforis.collect.persistence.MissingRecordKeyException) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition)

Example 80 with AttributeDefinition

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

the class CollectEarthGridTemplateGenerator method validate.

public CSVFileValidationResult validate(File file, CollectSurvey survey, ValidationParameters validationParameters) {
    CsvReader csvReader = null;
    CSVFileValidationResult validationResults = null;
    List<CSVRowValidationResult> rowValidations = new ArrayList<CSVRowValidationResult>();
    try {
        csvReader = new CsvReader(file);
        boolean headersFound = false;
        if (csvReader.size() == 1) {
            headersFound = true;
        } else {
            try {
                csvReader.readHeaders();
                List<String> firstLineValues = csvReader.getColumnNames();
                headersFound = lineContainsHeaders(survey, firstLineValues);
                if (headersFound) {
                    validationResults = validateCSVHeaders(firstLineValues, survey);
                } else {
                    // Check that the number of columns coincide with the number of attributes expected
                    // Get the list of attribute types expected per row!
                    List<AttributeDefinition> attributesPerRow = getAttributesPerRow(survey);
                    // Validate that the number of columns in the CSV and the expected number of columns match!!!!
                    if (firstLineValues.size() != attributesPerRow.size()) {
                        // The expected number of columns and the actual columns do not fit!!
                        // Break the operation and return a validation error!
                        validationResults = new CSVFileValidationResult(ErrorType.INVALID_HEADERS, getExpectedHeaders(survey), firstLineValues);
                    }
                }
            } catch (Exception e) {
                // this may happen when there are duplicate values in the first row
                headersFound = false;
                csvReader.setHeadersRead(true);
            }
        }
        if (validationResults == null) {
            rowValidations.addAll(validateCsvRows(csvReader, survey, headersFound, validationParameters.isValidateOnlyFirstLines()));
            long linesRead = csvReader.getLinesRead();
            if (linesRead > CSV_LENGTH_ERROR) {
                validationResults = new CSVFileValidationResult(ErrorType.INVALID_NUMBER_OF_PLOTS_TOO_LARGE);
                validationResults.setNumberOfRows((int) linesRead);
            } else if (csvReader.getLinesRead() > CSV_LENGTH_WARNING) {
                validationResults = new CSVFileValidationResult(ErrorType.INVALID_NUMBER_OF_PLOTS_WARNING);
                validationResults.setNumberOfRows((int) linesRead);
            }
        }
    } catch (Exception e) {
        logger.error("Error reading CSV file ", e);
        validationResults = new CSVFileValidationResult(ErrorType.INVALID_FILE_TYPE);
    } finally {
        IOUtils.closeQuietly(csvReader);
    }
    if (validationResults == null) {
        validationResults = new CSVFileValidationResult();
    }
    validationResults.setRowValidations(rowValidations);
    return validationResults;
}
Also used : CsvReader(org.openforis.commons.io.csv.CsvReader) 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) IOException(java.io.IOException)

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