Search in sources :

Example 6 with Bucket

use of org.activityinfo.shared.command.result.Bucket in project activityinfo by bedatadriven.

the class PivotSitesHandlerTest method execute.

private void execute() {
    setUser(OWNER_USER_ID);
    try {
        PivotSites pivot = new PivotSites(dimensions, filter);
        pivot.setValueType(valueType);
        pivot.setPointRequested(pointsRequested);
        buckets = execute(pivot).getBuckets();
    } catch (CommandException e) {
        throw new RuntimeException(e);
    }
    System.out.println("Buckets = [");
    for (Bucket bucket : buckets) {
        System.out.print("  { Value: " + bucket.doubleValue());
        for (Dimension dim : bucket.dimensions()) {
            DimensionCategory cat = bucket.getCategory(dim);
            System.out.print("\n    " + dim.toString() + ": ");
            System.out.print(cat.toString());
        }
        System.out.println("\n  }");
    }
    System.out.print("]\n");
}
Also used : DimensionCategory(org.activityinfo.shared.report.content.DimensionCategory) Bucket(org.activityinfo.shared.command.result.Bucket) CommandException(org.activityinfo.shared.exception.CommandException) Dimension(org.activityinfo.shared.report.model.Dimension) AttributeGroupDimension(org.activityinfo.shared.report.model.AttributeGroupDimension) AdminDimension(org.activityinfo.shared.report.model.AdminDimension) DateDimension(org.activityinfo.shared.report.model.DateDimension)

Example 7 with Bucket

use of org.activityinfo.shared.command.result.Bucket in project activityinfo by bedatadriven.

the class PivotSitesHandlerLocalTest method execute.

private void execute() {
    setUser(OWNER_USER_ID);
    try {
        PivotSites pivot = new PivotSites(dimensions, filter);
        pivot.setValueType(valueType);
        buckets = executeLocally(pivot).getBuckets();
    } catch (CommandException e) {
        throw new RuntimeException(e);
    }
    System.out.println("Buckets = [");
    for (Bucket bucket : buckets) {
        System.out.print(bucket.toString());
    }
    System.out.print("]\n");
}
Also used : Bucket(org.activityinfo.shared.command.result.Bucket) CommandException(org.activityinfo.shared.exception.CommandException)

Example 8 with Bucket

use of org.activityinfo.shared.command.result.Bucket in project activityinfo by bedatadriven.

the class GetPartnersDimensionHandler method execute.

@Override
public void execute(GetPartnersDimension cmd, ExecutionContext context, final AsyncCallback<PartnerResult> callback) {
    // if the filter doesn't contain any activity, database or indicator values, just return an empty list
    if (!cmd.getFilter().isRestricted(DimensionType.Database) && !cmd.getFilter().isRestricted(DimensionType.Activity) && !cmd.getFilter().isRestricted(DimensionType.Indicator)) {
        callback.onSuccess(new PartnerResult());
        return;
    }
    final Dimension dimension = new Dimension(DimensionType.Partner);
    PivotSites query = new PivotSites();
    query.setFilter(cmd.getFilter());
    query.setDimensions(dimension);
    query.setValueType(ValueType.DIMENSION);
    context.execute(query, new AsyncCallback<PivotSites.PivotResult>() {

        @Override
        public void onSuccess(PivotSites.PivotResult result) {
            Set<PartnerDTO> partners = new HashSet<PartnerDTO>();
            for (Bucket bucket : result.getBuckets()) {
                EntityCategory category = (EntityCategory) bucket.getCategory(dimension);
                if (category == null) {
                    Log.debug("Partner is null: " + bucket.toString());
                } else {
                    PartnerDTO partner = new PartnerDTO();
                    partner.setId(category.getId());
                    partner.setName(category.getLabel());
                    partners.add(partner);
                }
            }
            // sort partners by name (fallback on id)
            List<PartnerDTO> list = new ArrayList<PartnerDTO>(partners);
            Collections.sort(list, new Comparator<PartnerDTO>() {

                @Override
                public int compare(PartnerDTO p1, PartnerDTO p2) {
                    int result = p1.getName().compareToIgnoreCase(p2.getName());
                    if (result != 0) {
                        return result;
                    } else {
                        return ((Integer) p1.getId()).compareTo(p2.getId());
                    }
                }
            });
            callback.onSuccess(new PartnerResult(list));
        }

        @Override
        public void onFailure(Throwable caught) {
            callback.onFailure(caught);
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) Dimension(org.activityinfo.shared.report.model.Dimension) GetPartnersDimension(org.activityinfo.shared.command.GetPartnersDimension) Comparator(java.util.Comparator) PivotSites(org.activityinfo.shared.command.PivotSites) Bucket(org.activityinfo.shared.command.result.Bucket) ArrayList(java.util.ArrayList) List(java.util.List) EntityCategory(org.activityinfo.shared.report.content.EntityCategory) PartnerResult(org.activityinfo.shared.command.result.PartnerResult)

Example 9 with Bucket

use of org.activityinfo.shared.command.result.Bucket in project activityinfo by bedatadriven.

the class PivotSitesHandler method execute.

@Override
public void execute(PivotSites command, ExecutionContext context, final AsyncCallback<PivotResult> callback) {
    LOGGER.fine("Pivoting: " + command);
    if (command.getValueType() == ValueType.INDICATOR) {
        if (command.getFilter() == null || command.getFilter().getRestrictions(DimensionType.Indicator).isEmpty()) {
            Log.error("No indicator filter provided to pivot query");
        }
    }
    final PivotQueryContext queryContext = new PivotQueryContext(command, context, dialect);
    final List<PivotQuery> queries = Lists.newArrayList();
    for (BaseTable baseTable : baseTables) {
        if (baseTable.accept(command)) {
            queries.add(new PivotQuery(queryContext, baseTable));
        }
    }
    final List<Bucket> buckets = Lists.newArrayList();
    if (queries.isEmpty()) {
        callback.onSuccess(new PivotResult(buckets));
    }
    final Set<PivotQuery> remaining = Sets.newHashSet(queries);
    final List<Throwable> errors = Lists.newArrayList();
    for (final PivotQuery query : queries) {
        query.execute(new AsyncCallback<Void>() {

            @Override
            public void onSuccess(Void voidResult) {
                if (errors.isEmpty()) {
                    remaining.remove(query);
                    if (remaining.isEmpty()) {
                        callback.onSuccess(new PivotResult(queryContext.getBuckets()));
                    }
                }
            }

            @Override
            public void onFailure(Throwable caught) {
                if (errors.isEmpty()) {
                    callback.onFailure(caught);
                }
                errors.add(caught);
            }
        });
    }
}
Also used : PivotQuery(org.activityinfo.shared.command.handler.pivot.PivotQuery) PivotQueryContext(org.activityinfo.shared.command.handler.pivot.PivotQueryContext) BaseTable(org.activityinfo.shared.command.handler.pivot.BaseTable) Bucket(org.activityinfo.shared.command.result.Bucket) PivotResult(org.activityinfo.shared.command.PivotSites.PivotResult)

Example 10 with Bucket

use of org.activityinfo.shared.command.result.Bucket in project activityinfo by bedatadriven.

the class PivotTableDataBuilder method build.

public PivotTableData build(PivotReportElement<?> element, List<Dimension> rowDims, List<Dimension> colDims, List<Bucket> buckets) {
    PivotTableData table = new PivotTableData();
    Map<Dimension, Comparator<PivotTableData.Axis>> comparators = createComparators(element.allDimensions());
    for (Bucket bucket : buckets) {
        PivotTableData.Axis column = colDims.isEmpty() ? table.getRootColumn() : find(table.getRootColumn(), colDims.iterator(), comparators, bucket);
        PivotTableData.Axis row = rowDims.isEmpty() ? table.getRootRow() : find(table.getRootRow(), rowDims.iterator(), comparators, bucket);
        row.setValue(column, bucket.doubleValue());
    }
    return table;
}
Also used : PivotTableData(org.activityinfo.shared.report.content.PivotTableData) Bucket(org.activityinfo.shared.command.result.Bucket) Dimension(org.activityinfo.shared.report.model.Dimension) CategoryComparator(org.activityinfo.shared.command.handler.pivot.order.CategoryComparator) Comparator(java.util.Comparator) DefinedCategoryComparator(org.activityinfo.shared.command.handler.pivot.order.DefinedCategoryComparator)

Aggregations

Bucket (org.activityinfo.shared.command.result.Bucket)16 Dimension (org.activityinfo.shared.report.model.Dimension)11 EntityCategory (org.activityinfo.shared.report.content.EntityCategory)9 AdminDimension (org.activityinfo.shared.report.model.AdminDimension)9 Test (org.junit.Test)8 AttributeGroupDimension (org.activityinfo.shared.report.model.AttributeGroupDimension)6 DateDimension (org.activityinfo.shared.report.model.DateDimension)6 PivotSites (org.activityinfo.shared.command.PivotSites)5 ArrayList (java.util.ArrayList)4 OnDataSet (org.activityinfo.server.database.OnDataSet)4 Comparator (java.util.Comparator)3 List (java.util.List)2 DispatcherSync (org.activityinfo.server.command.DispatcherSync)2 User (org.activityinfo.server.database.hibernate.entity.User)2 CommandException (org.activityinfo.shared.exception.CommandException)2 PivotTableData (org.activityinfo.shared.report.content.PivotTableData)2 PivotTableReportElement (org.activityinfo.shared.report.model.PivotTableReportElement)2 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)1 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)1 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)1