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;
}
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);
}
}
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);
}
}
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();
}
}
}
}
}
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;
}
Aggregations