Search in sources :

Example 1 with ColumnSet

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);
        }
    }
}
Also used : ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) File(java.io.File) QueryModel(org.activityinfo.model.query.QueryModel) PrintWriter(java.io.PrintWriter)

Example 2 with ColumnSet

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;
                }
            });
        }
    };
}
Also used : SymbolNode(org.activityinfo.model.formula.SymbolNode) ColumnView(org.activityinfo.model.query.ColumnView) RecordRef(org.activityinfo.model.type.RecordRef) Supplier(com.google.common.base.Supplier) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Nullable(javax.annotation.Nullable)

Example 3 with ColumnSet

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;
}
Also used : ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet)

Example 4 with ColumnSet

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;
}
Also used : ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) ResourceId(org.activityinfo.model.resource.ResourceId) ColumnView(org.activityinfo.model.query.ColumnView) RecordRef(org.activityinfo.model.type.RecordRef) ColumnSet(org.activityinfo.model.query.ColumnSet) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) QueryModel(org.activityinfo.model.query.QueryModel)

Example 5 with ColumnSet

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());
}
Also used : ColumnView(org.activityinfo.model.query.ColumnView) ColumnJsonWriter(org.activityinfo.store.query.output.ColumnJsonWriter) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) JsonFormTreeBuilder(org.activityinfo.model.formTree.JsonFormTreeBuilder) GeoAreaType(org.activityinfo.model.type.geo.GeoAreaType) ResourceId.valueOf(org.activityinfo.model.resource.ResourceId.valueOf) UserDatabaseMeta(org.activityinfo.model.database.UserDatabaseMeta) WKBReader(com.vividsolutions.jts.io.WKBReader) PermissionsEnforcer(org.activityinfo.store.query.server.PermissionsEnforcer) org.activityinfo.model.form(org.activityinfo.model.form) NotFoundException(com.sun.jersey.api.NotFoundException) Level(java.util.logging.Level) HashSet(java.util.HashSet) Operation(io.swagger.v3.oas.annotations.Operation) SQLException(java.sql.SQLException) ResourceId(org.activityinfo.model.resource.ResourceId) Optional(com.google.common.base.Optional) Geometry(com.vividsolutions.jts.geom.Geometry) RecordRef(org.activityinfo.model.type.RecordRef) JsonValue(org.activityinfo.json.JsonValue) FormTreePrettyPrinter(org.activityinfo.model.formTree.FormTreePrettyPrinter) VersionedFormStorage(org.activityinfo.store.spi.VersionedFormStorage) RecordLockSet(org.activityinfo.model.database.RecordLockSet) InvalidUpdateException(org.activityinfo.store.query.server.InvalidUpdateException) PrintWriter(java.io.PrintWriter) Charsets(com.google.common.base.Charsets) ParseException(com.vividsolutions.jts.io.ParseException) StringWriter(java.io.StringWriter) Set(java.util.Set) javax.ws.rs.core(javax.ws.rs.core) Logger(java.util.logging.Logger) String.format(java.lang.String.format) FormStorage(org.activityinfo.store.spi.FormStorage) RecordLock(org.activityinfo.model.database.RecordLock) List(java.util.List) javax.ws.rs(javax.ws.rs) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Tag(io.swagger.v3.oas.annotations.tags.Tag) XlsFormBuilder(org.activityinfo.io.xlsform.XlsFormBuilder) AuthenticatedUser(org.activityinfo.legacy.shared.AuthenticatedUser) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) InjectParam(com.sun.jersey.api.core.InjectParam) FormTree(org.activityinfo.model.formTree.FormTree) RowBasedJsonWriter(org.activityinfo.store.query.output.RowBasedJsonWriter) ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) HashSet(java.util.HashSet)

Aggregations

ColumnSet (org.activityinfo.model.query.ColumnSet)50 QueryModel (org.activityinfo.model.query.QueryModel)42 Test (org.junit.Test)28 ColumnView (org.activityinfo.model.query.ColumnView)24 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)14 FormTree (org.activityinfo.model.formTree.FormTree)12 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)12 ResourceId (org.activityinfo.model.resource.ResourceId)10 FormClass (org.activityinfo.model.form.FormClass)7 NullFormScanCache (org.activityinfo.store.query.shared.NullFormScanCache)7 Nullable (javax.annotation.Nullable)6 Charsets (com.google.common.base.Charsets)5 Operation (io.swagger.v3.oas.annotations.Operation)5 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)5 ColumnModel (org.activityinfo.model.query.ColumnModel)5 RowBasedJsonWriter (org.activityinfo.store.query.output.RowBasedJsonWriter)5 PrintWriter (java.io.PrintWriter)4 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)4 FormField (org.activityinfo.model.form.FormField)4 RecordRef (org.activityinfo.model.type.RecordRef)4