Search in sources :

Example 1 with AttributeGroupResult

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

the class GetAttributeGroupsDimensionHandler method execute.

@Override
public void execute(GetAttributeGroupsDimension cmd, final ExecutionContext context, final AsyncCallback<AttributeGroupResult> 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 AttributeGroupResult());
        return;
    }
    final Dimension dimension = new Dimension(DimensionType.AttributeGroup);
    final 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) {
            final List<AttributeGroupDTO> list = new ArrayList<AttributeGroupDTO>();
            // populate the resultlist
            for (Bucket bucket : result.getBuckets()) {
                EntityCategory category = (EntityCategory) bucket.getCategory(dimension);
                if (category != null) {
                    AttributeGroupDTO attributeGroup = new AttributeGroupDTO();
                    attributeGroup.setId(category.getId());
                    attributeGroup.setName(category.getLabel());
                    list.add(attributeGroup);
                }
            }
            // sort the groups in the list by name (fallback on id)
            Collections.sort(list, new Comparator<AttributeGroupDTO>() {

                @Override
                public int compare(AttributeGroupDTO g1, AttributeGroupDTO g2) {
                    int result = g1.getName().compareToIgnoreCase(g2.getName());
                    if (result != 0) {
                        return result;
                    } else {
                        return ((Integer) g1.getId()).compareTo(g2.getId());
                    }
                }
            });
            callback.onSuccess(new AttributeGroupResult(list));
        }

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

Example 2 with AttributeGroupResult

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

the class GetAttributeGroupsDimensionHandlerTest method testIndicatorLinked1.

@Test
@OnDataSet("/dbunit/sites-linked.db.xml")
public void testIndicatorLinked1() throws CommandException {
    // cause, contenu du kit
    AttributeGroupResult result = execute(DimensionType.Indicator, 1);
    assertThat(result.getData().size(), equalTo(2));
    assertThat(result.getData().get(0).getName(), equalTo("cause"));
    assertThat(result.getData().get(1).getName(), equalTo("contenu du kit"));
}
Also used : AttributeGroupResult(org.activityinfo.shared.command.result.AttributeGroupResult) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 3 with AttributeGroupResult

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

the class GetAttributeGroupsDimensionHandlerTest method testEmptyFilter.

// all
@Test
@OnDataSet("/dbunit/sites-simple1.db.xml")
public void testEmptyFilter() throws CommandException {
    AttributeGroupResult result = this.execute();
    assertThat(result.getData().size(), equalTo(0));
}
Also used : AttributeGroupResult(org.activityinfo.shared.command.result.AttributeGroupResult) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 4 with AttributeGroupResult

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

the class GetAttributeGroupsDimensionHandlerTest method testIndicatorLinked12100.

@Test
@OnDataSet("/dbunit/sites-linked.db.xml")
public void testIndicatorLinked12100() throws CommandException {
    // cause, contenu du kit
    AttributeGroupResult result = execute(DimensionType.Indicator, 1, 2, 100);
    assertThat(result.getData().size(), equalTo(2));
    assertThat(result.getData().get(0).getName(), equalTo("cause"));
    assertThat(result.getData().get(1).getName(), equalTo("contenu du kit"));
}
Also used : AttributeGroupResult(org.activityinfo.shared.command.result.AttributeGroupResult) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 5 with AttributeGroupResult

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

the class AttributeGroupFilterWidgets method draw.

public void draw(final Filter filter) {
    final Filter value = new Filter(filter);
    value.clearRestrictions(DIMENSION_TYPE);
    // prevents executing the same command needlessly
    if (prevFilter == null || !prevFilter.equals(filter)) {
        prevFilter = filter;
        Log.debug("AttributeGroupFilterWidgets called for filter " + filter);
        // retrieve all attributegroups for the current filter
        service.execute(new GetAttributeGroupsDimension(value), new AsyncCallback<AttributeGroupResult>() {

            @Override
            public void onFailure(Throwable caught) {
                GWT.log("Failed to load attributes", caught);
            }

            @Override
            public void onSuccess(final AttributeGroupResult attributeGroupResult) {
                // if the result is indeed different from the last time, (re)draw the widgets
                if (prevResult == null || !prevResult.equals(attributeGroupResult)) {
                    prevResult = attributeGroupResult;
                    Log.debug("AttributeGroupFilterWidgets drawing widgets for result: " + attributeGroupResult);
                    service.execute(new GetSchema(), new AsyncCallback<SchemaDTO>() {

                        @Override
                        public void onFailure(Throwable caught) {
                            GWT.log("Failed to load schema", caught);
                        }

                        @Override
                        public void onSuccess(final SchemaDTO schema) {
                            // clean up old widgets
                            for (AttributeGroupFilterWidget widget : widgets) {
                                panel.remove(widget);
                            }
                            duplicates.clear();
                            // decorate resultlist from schema
                            List<AttributeGroupDTO> pivotData = attributeGroupResult.getData();
                            groups = new ArrayList<AttributeGroupDTO>();
                            if (CollectionUtil.isNotEmpty(pivotData)) {
                                for (AttributeGroupDTO pivotGroup : pivotData) {
                                    AttributeGroupDTO schemaGroup = schema.getAttributeGroupById(pivotGroup.getId());
                                    if (schemaGroup != null) {
                                        groups.add(schemaGroup);
                                    }
                                }
                            }
                            // create new widgets, one for each attributegroup.
                            // remember the old selection
                            List<Integer> selection = getSelectedIds();
                            widgets = new ArrayList<AttributeGroupFilterWidget>();
                            for (AttributeGroupDTO group : groups) {
                                // create
                                AttributeGroupFilterWidget widget = new AttributeGroupFilterWidget(group);
                                // set old selection
                                widget.setSelection(selection);
                                // what to do when value changes
                                if (valueChangeHandler != null) {
                                    widget.addValueChangeHandler(valueChangeHandler);
                                }
                                // already been added
                                if (isNoDuplicate(widget)) {
                                    widgets.add(widget);
                                    panel.add(widget);
                                } else {
                                    // otherwise add to collection of duplicates
                                    duplicates.put(group.getName().toLowerCase(), widget);
                                }
                            }
                            if (drawCallback != null) {
                                drawCallback.onSuccess(null);
                            }
                        }
                    });
                }
            }
        });
    }
}
Also used : AttributeGroupDTO(org.activityinfo.shared.dto.AttributeGroupDTO) AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) SchemaDTO(org.activityinfo.shared.dto.SchemaDTO) AttributeGroupResult(org.activityinfo.shared.command.result.AttributeGroupResult) Filter(org.activityinfo.shared.command.Filter) GetAttributeGroupsDimension(org.activityinfo.shared.command.GetAttributeGroupsDimension) GetSchema(org.activityinfo.shared.command.GetSchema)

Aggregations

AttributeGroupResult (org.activityinfo.shared.command.result.AttributeGroupResult)10 OnDataSet (org.activityinfo.server.database.OnDataSet)8 Test (org.junit.Test)8 GetAttributeGroupsDimension (org.activityinfo.shared.command.GetAttributeGroupsDimension)2 AttributeGroupDTO (org.activityinfo.shared.dto.AttributeGroupDTO)2 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Filter (org.activityinfo.shared.command.Filter)1 GetSchema (org.activityinfo.shared.command.GetSchema)1 PivotSites (org.activityinfo.shared.command.PivotSites)1 Bucket (org.activityinfo.shared.command.result.Bucket)1 SchemaDTO (org.activityinfo.shared.dto.SchemaDTO)1 EntityCategory (org.activityinfo.shared.report.content.EntityCategory)1 Dimension (org.activityinfo.shared.report.model.Dimension)1