use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.
the class EffectiveTableModel method addKeyColumns.
private void addKeyColumns(List<EffectiveTableColumn> columns, FormTree.Node node) {
LookupKeySet lookupKeySet = new LookupKeySet(formTree, node.getField());
Map<LookupKey, FormulaNode> formulas = lookupKeySet.getKeyFormulas(node.getFieldId());
for (LookupKey lookupKey : lookupKeySet.getLookupKeys()) {
ImmutableTableColumn tableColumn = ImmutableTableColumn.builder().id(node.getFieldId().asString() + "_k" + lookupKey.getKeyIndex()).formula(formulas.get(lookupKey).toString()).label(lookupKey.getKeyLabel()).build();
columns.add(new EffectiveTableColumn(formTree, tableColumn));
}
}
use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.
the class FormScan method prepare.
/**
* Prepares a column query based on the requested fields and formulas.
*/
public void prepare(ColumnQueryBuilder columnQueryBuilder) {
// check to see if we still need to hit the database after being populated by the cache
if (columnMap.isEmpty() && foreignKeyMap.isEmpty() && rowCount == null) {
return;
}
// Build the query
ExprQueryBuilder queryBuilder = new ExprQueryBuilder(columnFactory, formClass, columnQueryBuilder);
for (Map.Entry<FormulaNode, PendingSlot<ColumnView>> column : columnMap.entrySet()) {
if (column.getKey().equals(PK_COLUMN_KEY)) {
queryBuilder.addResourceId(new IdColumnBuilder(column.getValue()));
} else {
queryBuilder.addExpr(column.getKey(), column.getValue());
}
}
// Only add a row count observer IF it has been requested AND
// it hasn't been loaded from the cache.
RowCountBuilder rowCountBuilder = null;
if (rowCount != null && !rowCount.isSet()) {
rowCountBuilder = new RowCountBuilder(rowCount);
queryBuilder.addResourceId(rowCountBuilder);
}
for (Map.Entry<ForeignKeyId, PendingSlot<ForeignKey>> fk : foreignKeyMap.entrySet()) {
queryBuilder.addField(fk.getKey().getFieldId(), columnFactory.newForeignKeyBuilder(fk.getKey().getRightFormId(), fk.getValue()));
}
}
use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.
the class NodeMatcherTest method resolve.
private Collection<String> resolve(String exprString) {
FormTree tree = tree();
symbolTable = new NodeMatcher(tree);
FormulaNode expr = FormulaParser.parse(exprString);
Collection<NodeMatch> matches;
if (expr instanceof SymbolNode) {
matches = symbolTable.resolveSymbol((SymbolNode) expr);
} else if (expr instanceof CompoundExpr) {
matches = symbolTable.resolveCompoundExpr((CompoundExpr) expr);
} else {
throw new IllegalArgumentException(exprString);
}
// Create a string that we can match against easily
List<String> strings = Lists.newArrayList();
for (NodeMatch match : matches) {
strings.add(match.toDebugString());
}
System.out.println("Resolved " + exprString + " => " + strings);
return strings;
}
use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.
the class Updater method computeSerialNumberPrefix.
private String computeSerialNumberPrefix(FormClass formClass, SerialNumberType type, FormInstance effectiveRecord) {
if (!type.hasPrefix()) {
return null;
}
try {
FormEvalContext evalContext = new FormEvalContext(formClass);
evalContext.setInstance(effectiveRecord);
FormulaNode formula = FormulaParser.parse(type.getPrefixFormula());
FieldValue prefixValue = formula.evaluate(evalContext);
if (prefixValue instanceof TextValue) {
return ((TextValue) prefixValue).asString();
} else {
throw new IllegalStateException("Prefix " + type.getPrefixFormula() + " resolves to type " + prefixValue.getTypeClass().getId());
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to compute prefix for serial number", e);
return null;
}
}
use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.
the class SiteFormStorage method getPermissions.
@Override
public FormPermissions getPermissions(int userId) {
if (activity.getOwnerUserId() == userId) {
return FormPermissions.owner();
} else {
UserPermission databasePermission = permissionsCache.getPermission(userId, activity.getDatabaseId());
FormPermissions.Builder permissions = FormPermissions.builder();
FormulaNode partnerFilter = Formulas.equals(new SymbolNode(CuidAdapter.partnerField(activity.getId())), new ConstantNode(CuidAdapter.partnerRecordId(databasePermission.getPartnerId()).asString()));
if (databasePermission.isViewAll()) {
permissions.allowView();
} else if (databasePermission.isView()) {
permissions.allowFilteredView(partnerFilter.asExpression());
}
if (databasePermission.isEditAll()) {
permissions.allowEdit();
} else if (databasePermission.isEdit()) {
permissions.allowFilteredEdit(partnerFilter.asExpression());
}
if (databasePermission.isDesign()) {
permissions.allowSchemaUpdate();
}
// published property of activity overrides user permissions
if (activity.isPublished()) {
permissions.allowUnfilteredView();
}
return permissions.build();
}
}
Aggregations