use of org.openforis.idm.model.expression.InvalidExpressionException in project collect by openforis.
the class MinCountValidatorTest method updateMinCount.
// private boolean containsMinCountError(List<ValidationResult> errors, String name) {
// for (ValidationResult result : errors) {
// ValidationRule<?> validator = result.getValidator();
// if (validator instanceof MinCountValidator) {
// MinCountValidator v = (MinCountValidator) validator;
// NodeDefinition nodeDefinition = v.getNodeDefinition();
// if (nodeDefinition.getName().equals(name)) {
// return true;
// }
// }
// }
// return false;
// }
private void updateMinCount(Entity entity, String childName) {
NodeDefinition childDef = entity.getDefinition().getChildDefinition(childName);
try {
String expr = childDef.getMinCountExpression();
int count;
if (StringUtils.isNotBlank(expr)) {
Number val = expressionEvaluator.evaluateNumericValue(entity, null, expr);
count = val.intValue();
} else {
count = 0;
}
entity.setMinCount(childDef, count);
} catch (InvalidExpressionException e) {
throw new RuntimeException(e);
}
}
use of org.openforis.idm.model.expression.InvalidExpressionException in project collect by openforis.
the class RecordUpdater method calculateMaxCount.
private int calculateMaxCount(Entity entity, NodeDefinition defn) {
String expression = defn.getMaxCountExpression();
if (!entity.isRelevant(defn) || StringUtils.isBlank(expression)) {
return defn.isMultiple() ? Integer.MAX_VALUE : 1;
}
if (defn.getFixedMaxCount() != null) {
return defn.getFixedMaxCount();
}
try {
SurveyContext surveyContext = defn.getSurvey().getContext();
ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
Number value = expressionEvaluator.evaluateNumericValue(entity, null, expression);
return value == null ? Integer.MAX_VALUE : value.intValue();
} catch (InvalidExpressionException e) {
throw new IdmInterpretationError("Error evaluating required expression", e);
}
}
use of org.openforis.idm.model.expression.InvalidExpressionException in project collect by openforis.
the class RecordUpdater method calculateMinCount.
private int calculateMinCount(Entity entity, NodeDefinition defn) {
String expression = defn.getMinCountExpression();
if (!entity.isRelevant(defn) || StringUtils.isBlank(expression)) {
return 0;
}
if (defn.getFixedMinCount() != null) {
return defn.getFixedMinCount();
}
try {
SurveyContext surveyContext = defn.getSurvey().getContext();
ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
Number value = expressionEvaluator.evaluateNumericValue(entity, null, expression);
return value == null ? 0 : value.intValue();
} catch (InvalidExpressionException e) {
throw new IdmInterpretationError("Error evaluating required expression", e);
}
}
use of org.openforis.idm.model.expression.InvalidExpressionException in project collect by openforis.
the class CSVDataExportProcess method exportData.
// private String calculateOutputFileName() {
// return "data.zip";
// /*
// StringBuilder sb = new StringBuilder();
// sb.append(survey.getName());
// sb.append("_");
// sb.append(rootEntityName);
// sb.append("_");
// sb.append("csv_data");
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
// String today = formatter.format(new Date());
// sb.append(today);
// sb.append(".zip");
// return sb.toString();
// */
// }
private void exportData(OutputStream outputStream, int entityDefId) throws InvalidExpressionException, IOException, RecordPersistenceException {
Writer outputWriter = new OutputStreamWriter(outputStream, OpenForisIOUtils.UTF_8);
CSVDataExportColumnProviderGenerator csvDataExportColumnProviderGenerator = new CSVDataExportColumnProviderGenerator(recordFilter.getSurvey(), configuration);
DataTransformation transform = csvDataExportColumnProviderGenerator.generateDataTransformation(entityDefId);
@SuppressWarnings("resource") ModelCsvWriter // closing modelWriter will close referenced output stream
modelWriter = new ModelCsvWriter(outputWriter, transform);
modelWriter.printColumnHeadings();
CollectSurvey survey = recordFilter.getSurvey();
Step step = recordFilter.getStepGreaterOrEqual();
List<CollectRecordSummary> summaries = recordManager.loadSummaries(recordFilter);
for (CollectRecordSummary s : summaries) {
if (status.isRunning()) {
try {
CollectRecord record = recordManager.load(survey, s.getId(), step, false);
modelWriter.printData(record);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
status.incrementProcessed();
} else {
break;
}
}
modelWriter.flush();
}
use of org.openforis.idm.model.expression.InvalidExpressionException in project collect by openforis.
the class DescendantNodeFilter method accept.
@Override
public boolean accept(Node<?> node) {
NodeDefinition nodeDef = node.getDefinition();
if (!(nodeDef instanceof EntityDefinition) || !descendantAttributeDefinition.isDescendantOf((EntityDefinition) nodeDef)) {
return false;
}
Record record = node.getRecord();
SurveyContext surveyContext = record.getSurveyContext();
ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
List<Node<?>> attributes = record.findNodesByPath(descendantAttributeDefinition.getPath());
for (Node<?> attribute : attributes) {
try {
Entity parentEntity = attribute.getParent();
if (parentEntity == node && expressionEvaluator.evaluateBoolean(parentEntity, attribute, descendantAttributeCondition)) {
return true;
}
} catch (InvalidExpressionException e) {
throw new RuntimeException(e);
}
}
return false;
}
Aggregations