use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.
the class AnalysisViewModelTest method dumpQuery.
private void dumpQuery(ResourceId formId, String... columns) {
if (DUMP_RAW_DATA) {
try {
File tempFile = File.createTempFile("query", ".csv");
try (PrintWriter writer = new PrintWriter(tempFile)) {
QueryModel model = new QueryModel(formId);
for (int i = 0; i < columns.length; i++) {
model.selectExpr(columns[i]).as("c" + i);
}
ColumnSet columnSet = assertLoads(formStore.query(model));
for (int i = 0; i < columns.length; i++) {
if (i > 0) {
writer.print(",");
}
writer.print(columns[i]);
}
writer.println();
for (int i = 0; i < columnSet.getNumRows(); i++) {
for (int j = 0; j < columns.length; j++) {
if (j > 0) {
writer.print(",");
}
ColumnView columnView = columnSet.getColumnView("c" + j);
Object cell = columnView.get(i);
String cells = "";
if (cell != null) {
cells = cell.toString();
}
writer.print(cells);
}
writer.println();
}
}
System.out.println("Dumped data to " + tempFile);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.
the class Presenter method choices.
private Supplier<Promise<List<Choice>>> choices(final Level level) {
final QueryModel queryModel = new QueryModel(level.getFormId());
queryModel.selectResourceId().as("id");
queryModel.selectExpr("label").as("label");
if (!level.isRoot()) {
Choice selectedParent = getSelection(level.getParent());
queryModel.selectExpr("parent").as("parent");
queryModel.setFilter(Formulas.equals(new SymbolNode("parent"), Formulas.idConstant(selectedParent.getRef().getRecordId())));
}
return new Supplier<Promise<List<Choice>>>() {
@Override
public Promise<List<Choice>> get() {
return locator.queryTable(queryModel).then(new Function<ColumnSet, List<Choice>>() {
@Nullable
@Override
public List<Choice> apply(ColumnSet input) {
ColumnView id = input.getColumnView("id");
ColumnView label = input.getColumnView("label");
ColumnView parent = input.getColumnView("parent");
List<Choice> choices = new ArrayList<>();
for (int i = 0; i < input.getNumRows(); i++) {
if (parent == null) {
choices.add(new Choice(level.getFormId(), ResourceId.valueOf(id.getString(i)), label.getString(i)));
} else {
choices.add(new Choice(level.getFormId(), ResourceId.valueOf(id.getString(i)), label.getString(i), new RecordRef(level.getParent().getFormId(), ResourceId.valueOf(parent.getString(i)))));
}
}
return choices;
}
});
}
};
}
use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.
the class GetSitesHandler method extractSiteData.
private SiteDTO[] extractSiteData(List<FieldBinding> fieldBindings, ColumnSet columnSet) {
ColumnSet finalColumnSet;
SiteDTO[] sites;
if (offset > 0 || limit > 0) {
Map<String, ColumnView> paginatedColumns = Maps.newHashMap();
int[] index = generatePaginationIndex(columnSet.getNumRows());
sites = initialiseSites(index.length);
for (Map.Entry<String, ColumnView> column : columnSet.getColumns().entrySet()) {
paginatedColumns.put(column.getKey(), column.getValue().select(index));
}
finalColumnSet = new ColumnSet(index.length, paginatedColumns);
} else {
sites = initialiseSites(columnSet.getNumRows());
finalColumnSet = columnSet;
}
for (FieldBinding binding : fieldBindings) {
binding.extractFieldData(sites, finalColumnSet);
}
return sites;
}
use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.
the class ResourceLocatorSyncImpl method getReferenceChoices.
@Override
public List<ReferenceChoice> getReferenceChoices(Collection<ResourceId> range) {
ResourceId formId = Iterables.getOnlyElement(range);
QueryModel queryModel = new QueryModel(formId);
queryModel.selectResourceId().as("id");
queryModel.selectExpr("label").as("label");
ColumnSetBuilder builder = new ColumnSetBuilder(catalog.get(), new NullFormSupervisor());
ColumnSet columnSet = builder.build(queryModel);
ColumnView id = columnSet.getColumnView("id");
ColumnView label = columnSet.getColumnView("label");
List<ReferenceChoice> choices = Lists.newArrayList();
for (int i = 0; i < columnSet.getNumRows(); i++) {
ResourceId choiceId = ResourceId.valueOf(id.getString(i));
String choiceLabel = label.getString(i);
choices.add(new ReferenceChoice(new RecordRef(formId, choiceId), choiceLabel));
}
return choices;
}
use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.
the class FormResource method computeVisibilityPredicate.
/**
* Computes a record-level visibility predicate.
*/
private java.util.function.Predicate<ResourceId> computeVisibilityPredicate() {
FormPermissions formPermissions = backend.getFormSupervisor().getFormPermissions(formId);
if (!formPermissions.hasVisibilityFilter()) {
return resourceId -> true;
}
QueryModel queryModel = new QueryModel(formId);
queryModel.selectResourceId().as("id");
ColumnSet columnSet = executeQuery(queryModel);
ColumnView id = columnSet.getColumnView("id");
final Set<String> idSet = new HashSet<>();
for (int i = 0; i < id.numRows(); i++) {
idSet.add(id.getString(i));
}
return resourceId -> idSet.contains(resourceId.asString());
}
Aggregations