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