tabular-data-manager/src/main/java/org/gcube/portlets/user/td/client/rule/RuleApplyPanel.java

647 lines
22 KiB
Java

package org.gcube.portlets.user.td.client.rule;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.gcube.portlets.user.td.client.resource.TabularDataResources;
import org.gcube.portlets.user.td.columnwidget.client.properties.ColumnDataPropertiesCombo;
import org.gcube.portlets.user.td.expressionwidget.client.rpc.ExpressionServiceAsync;
import org.gcube.portlets.user.td.expressionwidget.shared.rule.RuleDescriptionDataProperties;
import org.gcube.portlets.user.td.gwtservice.client.rpc.TDGWTServiceAsync;
import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTIsFinalException;
import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTIsLockedException;
import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTSessionExpiredException;
import org.gcube.portlets.user.td.gwtservice.shared.rule.AppliedRulesResponseData;
import org.gcube.portlets.user.td.gwtservice.shared.rule.ApplyColumnRulesSession;
import org.gcube.portlets.user.td.gwtservice.shared.rule.RuleDescriptionData;
import org.gcube.portlets.user.td.tablewidget.client.util.UtilsGXT3;
import org.gcube.portlets.user.td.widgetcommonevent.client.event.SessionExpiredEvent;
import org.gcube.portlets.user.td.widgetcommonevent.client.type.SessionExpiredType;
import org.gcube.portlets.user.td.widgetcommonevent.shared.TRId;
import org.gcube.portlets.user.td.widgetcommonevent.shared.tr.column.ColumnData;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.web.bindery.event.shared.EventBus;
import com.sencha.gxt.cell.core.client.ButtonCell.IconAlign;
import com.sencha.gxt.cell.core.client.form.ComboBoxCell.TriggerAction;
import com.sencha.gxt.core.client.XTemplates;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.data.shared.LabelProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.dnd.core.client.GridDragSource;
import com.sencha.gxt.dnd.core.client.GridDropTarget;
import com.sencha.gxt.widget.core.client.Dialog;
import com.sencha.gxt.widget.core.client.Dialog.PredefinedButton;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutData;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer;
import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer.HBoxLayoutAlign;
import com.sencha.gxt.widget.core.client.container.MarginData;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.ComboBox;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.FieldSet;
import com.sencha.gxt.widget.core.client.form.TextArea;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.filters.GridFilters;
import com.sencha.gxt.widget.core.client.grid.filters.StringFilter;
import com.sencha.gxt.widget.core.client.info.Info;
import com.sencha.gxt.widget.core.client.menu.Item;
import com.sencha.gxt.widget.core.client.menu.Menu;
import com.sencha.gxt.widget.core.client.menu.MenuItem;
/**
*
* @author giancarlo email: <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class RuleApplyPanel extends FramedPanel {
private static final String RULES_GRID_HEIGHT = "184px";
private static final String SET_RULES = "SetRules";
interface RuleApplyTemplates extends XTemplates {
@XTemplate("<span title=\"{value}\">{value}</span>")
SafeHtml format(String value);
}
private static final String WIDTH = "630px";
private static final String HEIGHT = "520px";
private EventBus eventBus;
private RuleApplyDialog parent;
private TRId trId;
private ArrayList<ColumnData> columns;
private ColumnData column;
private AppliedRulesResponseData appliedRuleResponseData;
private ArrayList<RuleDescriptionData> applicableRules;
private ArrayList<RuleDescriptionData> appliedRules;
private TextButton btnApply;
private TextButton btnClose;
private ComboBox<ColumnData> comboColumns;
private ListStore<RuleDescriptionData> applicableRulesStore;
private Grid<RuleDescriptionData> gridApplicableRules;
private ListStore<RuleDescriptionData> selectedRulesStore;
private Grid<RuleDescriptionData> gridSelectedRules;
public RuleApplyPanel(RuleApplyDialog parent, TRId trId, EventBus eventBus) {
this.parent = parent;
this.trId = trId;
applicableRules = new ArrayList<RuleDescriptionData>();
Log.debug("RuleApplyPanel");
setWidth(WIDTH);
setHeight(HEIGHT);
setHeaderVisible(false);
setBodyBorder(false);
this.eventBus = eventBus;
retrieveColumns();
}
protected void retrieveColumns() {
TDGWTServiceAsync.INSTANCE.getColumns(trId,
new AsyncCallback<ArrayList<ColumnData>>() {
@Override
public void onFailure(Throwable caught) {
if (caught instanceof TDGWTSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
if (caught instanceof TDGWTIsLockedException) {
Log.error(caught.getLocalizedMessage());
UtilsGXT3.alert("Error Locked",
caught.getLocalizedMessage());
} else {
if (caught instanceof TDGWTIsFinalException) {
Log.error(caught.getLocalizedMessage());
UtilsGXT3.alert("Error Final",
caught.getLocalizedMessage());
} else {
Log.debug("Error retrieving columns: "
+ caught.getLocalizedMessage());
UtilsGXT3
.alert("Error retrieving columns",
"Error retrieving column on server!");
}
}
}
}
@Override
public void onSuccess(ArrayList<ColumnData> result) {
Log.debug("Retrieved Columns");
if (result == null) {
UtilsGXT3.alert("Error",
"The requested columns is null");
}
columns = result;
getAppliedBaseColumnRules();
}
});
}
protected void create() {
Log.debug("Create RuleApplyPanel(): " + trId);
FieldSet configurationFieldSet = new FieldSet();
configurationFieldSet.setHeadingText("Configuration");
configurationFieldSet.setCollapsible(false);
VerticalLayoutContainer configurationFieldSetLayout = new VerticalLayoutContainer();
configurationFieldSet.add(configurationFieldSetLayout);
// Column Data
ColumnDataPropertiesCombo propsColumnData = GWT
.create(ColumnDataPropertiesCombo.class);
ListStore<ColumnData> storeCombo = new ListStore<ColumnData>(
propsColumnData.id());
storeCombo.addAll(columns);
Log.trace("StoreCombo created");
comboColumns = new ComboBox<ColumnData>(storeCombo,
propsColumnData.label());
Log.trace("Combo ColumnData created");
addHandlersForComboColumn(propsColumnData.label());
comboColumns.setEmptyText("Select a column...");
comboColumns.setWidth(191);
comboColumns.setTypeAhead(false);
comboColumns.setEditable(false);
comboColumns.setTriggerAction(TriggerAction.ALL);
FieldLabel comboColumnsLabel = new FieldLabel(comboColumns, "Column");
configurationFieldSetLayout.add(comboColumnsLabel,
new VerticalLayoutData(1, -1, new Margins(0)));
// Rules
RuleDescriptionDataProperties propsRules = GWT
.create(RuleDescriptionDataProperties.class);
ColumnConfig<RuleDescriptionData, String> nameCol = new ColumnConfig<RuleDescriptionData, String>(
propsRules.name(), 120, "Name");
nameCol.setCell(new AbstractCell<String>() {
@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
RuleApplyTemplates ruleApplyTemplates = GWT
.create(RuleApplyTemplates.class);
sb.append(ruleApplyTemplates.format(value));
}
});
ColumnConfig<RuleDescriptionData, String> descriptionCol = new ColumnConfig<RuleDescriptionData, String>(
propsRules.description(), 120, "Description");
descriptionCol.setCell(new AbstractCell<String>() {
@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
RuleApplyTemplates ruleApplyTemplates = GWT
.create(RuleApplyTemplates.class);
sb.append(ruleApplyTemplates.format(value));
}
});
ColumnConfig<RuleDescriptionData, String> ownerCol = new ColumnConfig<RuleDescriptionData, String>(
propsRules.ownerLogin(), 70, "Owner");
ownerCol.setCell(new AbstractCell<String>() {
@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
RuleApplyTemplates ruleApplyTemplates = GWT
.create(RuleApplyTemplates.class);
sb.append(ruleApplyTemplates.format(value));
}
});
/*
ColumnConfig<RuleDescriptionData, String> readableExpressionCol = new ColumnConfig<RuleDescriptionData, String>(
propsRules.readableExpression(), 160, "Expression");
readableExpressionCol.setCell(new AbstractCell<String>() {
@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
RuleApplyTemplates ruleApplyTemplates = GWT
.create(RuleApplyTemplates.class);
sb.append(ruleApplyTemplates.format(value));
}
});*/
List<ColumnConfig<RuleDescriptionData, ?>> l = new ArrayList<ColumnConfig<RuleDescriptionData, ?>>();
l.add(nameCol);
l.add(descriptionCol);
l.add(ownerCol);
//l.add(readableExpressionCol);
ColumnModel<RuleDescriptionData> cm = new ColumnModel<RuleDescriptionData>(
l);
StringFilter<RuleDescriptionData> nameFilter = new StringFilter<RuleDescriptionData>(
propsRules.name());
StringFilter<RuleDescriptionData> descriptionFilter = new StringFilter<RuleDescriptionData>(
propsRules.description());
// Applicable Rules
applicableRulesStore = new ListStore<RuleDescriptionData>(
propsRules.id());
gridApplicableRules = new Grid<RuleDescriptionData>(
applicableRulesStore, cm);
gridApplicableRules.setHeight(RULES_GRID_HEIGHT);
gridApplicableRules.getView().setStripeRows(true);
gridApplicableRules.getView().setColumnLines(true);
gridApplicableRules.getView().setAutoFill(true);
gridApplicableRules.setBorders(false);
gridApplicableRules.setLoadMask(true);
gridApplicableRules.setColumnReordering(true);
gridApplicableRules.setColumnResize(true);
gridApplicableRules.getView().setAutoExpandColumn(descriptionCol);
GridFilters<RuleDescriptionData> filtersApplicableRules = new GridFilters<RuleDescriptionData>();
filtersApplicableRules.initPlugin(gridApplicableRules);
filtersApplicableRules.setLocal(true);
filtersApplicableRules.addFilter(nameFilter);
filtersApplicableRules.addFilter(descriptionFilter);
createContextMenu(gridApplicableRules);
// Selected Rules
selectedRulesStore = new ListStore<RuleDescriptionData>(propsRules.id());
gridSelectedRules = new Grid<RuleDescriptionData>(selectedRulesStore, cm);
gridSelectedRules.setHeight(RULES_GRID_HEIGHT);
gridSelectedRules.getView().setStripeRows(true);
gridSelectedRules.getView().setColumnLines(true);
gridSelectedRules.getView().setAutoFill(true);
gridSelectedRules.setBorders(false);
gridSelectedRules.setLoadMask(true);
gridSelectedRules.setColumnReordering(true);
gridSelectedRules.setColumnResize(true);
gridSelectedRules.getView().setAutoExpandColumn(descriptionCol);
GridFilters<RuleDescriptionData> filtersSelectedRules = new GridFilters<RuleDescriptionData>();
filtersSelectedRules.initPlugin(gridSelectedRules);
filtersSelectedRules.setLocal(true);
filtersSelectedRules.addFilter(nameFilter);
filtersSelectedRules.addFilter(descriptionFilter);
createContextMenu(gridSelectedRules);
//
new GridDragSource<RuleDescriptionData>(gridApplicableRules)
.setGroup(SET_RULES);
new GridDragSource<RuleDescriptionData>(gridSelectedRules)
.setGroup(SET_RULES);
new GridDropTarget<RuleDescriptionData>(gridApplicableRules)
.setGroup(SET_RULES);
new GridDropTarget<RuleDescriptionData>(gridSelectedRules)
.setGroup(SET_RULES);
//
FieldLabel rulesApplicableLabel = new FieldLabel(gridApplicableRules,
"Applicable Rules");
configurationFieldSetLayout.add(rulesApplicableLabel,
new VerticalLayoutData(1, -1, new Margins(0)));
FieldLabel rulesSelectedLabel = new FieldLabel(gridSelectedRules,
"Selected Rules");
configurationFieldSetLayout.add(rulesSelectedLabel,
new VerticalLayoutData(1, -1, new Margins(0)));
// Button
btnApply = new TextButton("Apply");
btnApply.setIcon(TabularDataResources.INSTANCE.ruleColumnApply());
btnApply.setIconAlign(IconAlign.RIGHT);
btnApply.setToolTip("Apply Rule");
btnApply.addSelectHandler(new SelectHandler() {
public void onSelect(SelectEvent event) {
Log.debug("Pressed Apply");
apply();
}
});
btnClose = new TextButton("Close");
btnClose.setIcon(TabularDataResources.INSTANCE.close());
btnClose.setIconAlign(IconAlign.RIGHT);
btnClose.setToolTip("Close");
btnClose.addSelectHandler(new SelectHandler() {
public void onSelect(SelectEvent event) {
Log.debug("Pressed Close");
close();
}
});
HBoxLayoutContainer flowButton = new HBoxLayoutContainer();
flowButton.setHBoxLayoutAlign(HBoxLayoutAlign.MIDDLE);
flowButton.setPack(BoxLayoutPack.CENTER);
flowButton.add(btnApply, new BoxLayoutData(new Margins(2, 4, 2, 4)));
flowButton.add(btnClose, new BoxLayoutData(new Margins(2, 4, 2, 4)));
VerticalLayoutContainer v = new VerticalLayoutContainer();
v.add(configurationFieldSet, new VerticalLayoutData(-1, -1,
new Margins(0)));
v.add(flowButton,
new VerticalLayoutData(1, 36, new Margins(5, 2, 5, 2)));
add(v);
}
private void addHandlersForComboColumn(
final LabelProvider<ColumnData> labelProvider) {
comboColumns.addSelectionHandler(new SelectionHandler<ColumnData>() {
public void onSelection(SelectionEvent<ColumnData> event) {
Info.display(
"Column Selected",
"You selected "
+ (event.getSelectedItem() == null ? "nothing"
: labelProvider.getLabel(event
.getSelectedItem()) + "!"));
Log.debug("ComboColumn selected: " + event.getSelectedItem());
ColumnData columnData = event.getSelectedItem();
updateRulesInGrids(columnData);
}
});
}
protected void updateRulesInGrids(ColumnData columnData) {
column = columnData;
applicableRulesStore.clear();
selectedRulesStore.clear();
HashMap<String, ArrayList<RuleDescriptionData>> columnRuleMapping = appliedRuleResponseData
.getColumnRuleMapping();
if (columnRuleMapping != null) {
ArrayList<RuleDescriptionData> applied = columnRuleMapping
.get(columnData.getColumnId());
if (applied != null) {
appliedRules = applied;
selectedRulesStore.addAll(new ArrayList<RuleDescriptionData>(
applied));
}
}
getApplicableBaseColumnRules();
}
protected void getApplicableBaseColumnRules() {
ExpressionServiceAsync.INSTANCE.getApplicableBaseColumnRules(column,
new AsyncCallback<ArrayList<RuleDescriptionData>>() {
@Override
public void onFailure(Throwable caught) {
if (caught instanceof TDGWTSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
Log.error(caught.getLocalizedMessage());
UtilsGXT3.alert(
"Error retrieving applicable rules",
caught.getLocalizedMessage());
}
}
@Override
public void onSuccess(ArrayList<RuleDescriptionData> result) {
Log.trace("loaded " + result.size() + " Rules");
if (appliedRules != null && appliedRules.size() > 0) {
ArrayList<RuleDescriptionData> removableRules = new ArrayList<RuleDescriptionData>();
for (RuleDescriptionData ruleApplied : appliedRules) {
for (RuleDescriptionData ruleApplicable : result) {
if (ruleApplicable.equals(ruleApplied)) {
removableRules.add(ruleApplicable);
break;
}
}
}
result.removeAll(removableRules);
}
applicableRules = result;
applicableRulesStore
.addAll(new ArrayList<RuleDescriptionData>(
applicableRules));
forceLayout();
}
});
}
protected void getAppliedBaseColumnRules() {
ExpressionServiceAsync.INSTANCE.getActiveRulesByTabularResourceId(
trId, new AsyncCallback<AppliedRulesResponseData>() {
@Override
public void onFailure(Throwable caught) {
if (caught instanceof TDGWTSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
Log.error(caught.getLocalizedMessage());
UtilsGXT3.alert("Error retrieving selected rules",
caught.getLocalizedMessage());
}
}
@Override
public void onSuccess(AppliedRulesResponseData result) {
Log.trace("AppliedRuleResponseData: " + result);
appliedRuleResponseData = result;
create();
}
});
}
protected ArrayList<RuleDescriptionData> getSelectedItems() {
List<RuleDescriptionData> selectedItems = selectedRulesStore.getAll();
ArrayList<RuleDescriptionData> rulesSelected = new ArrayList<RuleDescriptionData>(
selectedItems);
return rulesSelected;
}
protected void apply() {
ColumnData column = comboColumns.getCurrentValue();
if (column != null) {
ArrayList<RuleDescriptionData> selectedRules = getSelectedItems();
ArrayList<RuleDescriptionData> rulesToBeApplied = new ArrayList<RuleDescriptionData>();
for (RuleDescriptionData ruleSelected : selectedRules) {
boolean ruleApplied = false;
HashMap<String, ArrayList<RuleDescriptionData>> columnRuleMapping = appliedRuleResponseData
.getColumnRuleMapping();
if (columnRuleMapping != null) {
ArrayList<RuleDescriptionData> applied = columnRuleMapping
.get(column.getColumnId());
if (applied != null) {
for (RuleDescriptionData ruleAlreadyApplied : applied) {
if (ruleSelected.getId() == ruleAlreadyApplied
.getId()) {
ruleApplied = true;
break;
}
}
} else {
}
} else {
}
if (ruleApplied == false) {
rulesToBeApplied.add(ruleSelected);
}
}
ApplyColumnRulesSession applyColumnRulesSession = new ApplyColumnRulesSession(
trId, column, rulesToBeApplied);
parent.applyRules(applyColumnRulesSession);
} else {
Log.error("No column selected");
UtilsGXT3.alert("Attention", "Select a column");
}
}
protected void close() {
parent.close();
}
protected void requestInfo(RuleDescriptionData rule) {
final Dialog infoRuleDialog = new Dialog();
infoRuleDialog.setHeadingText("Info Rule");
infoRuleDialog.getHeader().setIcon(
TabularDataResources.INSTANCE.information());
infoRuleDialog.setPredefinedButtons(PredefinedButton.OK);
FieldSet configurationFieldSet = new FieldSet();
configurationFieldSet.setHeadingText("Configuration");
configurationFieldSet.setCollapsible(false);
configurationFieldSet.setBorders(true);
VerticalLayoutContainer configurationFieldSetLayout = new VerticalLayoutContainer();
configurationFieldSet.add(configurationFieldSetLayout,
new MarginData(0));
TextField ruleName = new TextField();
ruleName.setToolTip("Rule Name");
ruleName.setValue(rule.getName());
ruleName.setReadOnly(true);
FieldLabel ruleNameLabel = new FieldLabel(ruleName, "Name");
configurationFieldSetLayout.add(ruleNameLabel, new VerticalLayoutData(
1, -1, new Margins(0)));
TextField ruleScope = new TextField();
ruleScope.setToolTip("Rule Scope");
ruleScope.setValue(rule.getScopeLabel());
ruleScope.setReadOnly(true);
FieldLabel ruleScopeLabel = new FieldLabel(ruleScope, "Scope");
configurationFieldSetLayout.add(ruleScopeLabel, new VerticalLayoutData(
1, -1, new Margins(0)));
TextArea ruleDescription = new TextArea();
ruleDescription.setToolTip("Rule Description");
ruleDescription.setValue(rule.getDescription());
ruleDescription.setReadOnly(true);
FieldLabel ruleDescriptionLabel = new FieldLabel(ruleDescription,
"Description");
configurationFieldSetLayout.add(ruleDescriptionLabel,
new VerticalLayoutData(1, -1, new Margins(0)));
TextField ruleOwner = new TextField();
ruleOwner.setToolTip("Rule Owner");
ruleOwner.setValue(rule.getOwnerLogin());
ruleOwner.setReadOnly(true);
FieldLabel ruleOwnerLabel = new FieldLabel(ruleOwner, "Owner");
configurationFieldSetLayout.add(ruleOwnerLabel, new VerticalLayoutData(
1, -1, new Margins(0)));
TextArea ruleExpression = new TextArea();
ruleExpression.setToolTip("Rule Expression");
ruleExpression.setValue(rule.getReadableExpression());
ruleExpression.setReadOnly(true);
ruleExpression.setHeight("82px");
FieldLabel ruleExpressionLabel = new FieldLabel(ruleExpression,
"Expression");
configurationFieldSetLayout.add(ruleExpressionLabel,
new VerticalLayoutData(1, -1, new Margins(0)));
infoRuleDialog.add(configurationFieldSet, new MarginData(0));
infoRuleDialog.setHideOnButtonClick(true);
infoRuleDialog.setButtonAlign(BoxLayoutPack.CENTER);
infoRuleDialog.setWidth(500);
infoRuleDialog.show();
}
protected void createContextMenu(final Grid<RuleDescriptionData> grid) {
Menu contextMenu = new Menu();
MenuItem infoItem = new MenuItem();
infoItem.setText("Info");
infoItem.setIcon(TabularDataResources.INSTANCE.information());
infoItem.addSelectionHandler(new SelectionHandler<Item>() {
@Override
public void onSelection(SelectionEvent<Item> event) {
RuleDescriptionData selected = grid.getSelectionModel()
.getSelectedItem();
Log.debug(selected.toString());
requestInfo(selected);
}
});
contextMenu.add(infoItem);
grid.setContextMenu(contextMenu);
}
}