tabular-data-information-wi.../src/main/java/org/gcube/portlets/user/td/informationwidget/client/TabularResourceProperties.java

719 lines
23 KiB
Java

package org.gcube.portlets.user.td.informationwidget.client;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
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.licenses.LicenceData;
import org.gcube.portlets.user.td.gwtservice.shared.share.Contacts;
import org.gcube.portlets.user.td.gwtservice.shared.tr.TabResource;
import org.gcube.portlets.user.td.gwtservice.shared.tr.TableData;
import org.gcube.portlets.user.td.informationwidget.client.custom.IconButton;
import org.gcube.portlets.user.td.informationwidget.client.licence.LicenceDataPropertiesCombo;
import org.gcube.portlets.user.td.informationwidget.client.resources.ResourceBundle;
import org.gcube.portlets.user.td.informationwidget.client.util.UtilsGXT3;
import org.gcube.portlets.user.td.metadatawidget.client.TRMetadataAccordionPanel;
import org.gcube.portlets.user.td.metadatawidget.client.TableMetadataAccordionPanel;
import org.gcube.portlets.user.td.widgetcommonevent.client.event.DataViewRequestEvent;
import org.gcube.portlets.user.td.widgetcommonevent.client.event.RibbonEvent;
import org.gcube.portlets.user.td.widgetcommonevent.client.event.SessionExpiredEvent;
import org.gcube.portlets.user.td.widgetcommonevent.client.event.WidgetRequestEvent;
import org.gcube.portlets.user.td.widgetcommonevent.client.type.DataViewRequestType;
import org.gcube.portlets.user.td.widgetcommonevent.client.type.RibbonType;
import org.gcube.portlets.user.td.widgetcommonevent.client.type.SessionExpiredType;
import org.gcube.portlets.user.td.widgetcommonevent.client.type.WidgetRequestType;
import org.gcube.portlets.user.td.widgetcommonevent.shared.TRId;
import org.gcube.portlets.user.td.widgetcommonevent.shared.dataview.TabularResourceDataView;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
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.util.Margins;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.loader.ListLoadConfig;
import com.sencha.gxt.data.shared.loader.ListLoadResult;
import com.sencha.gxt.data.shared.loader.ListLoader;
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.HBoxLayoutContainer;
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.CheckBox;
import com.sencha.gxt.widget.core.client.form.ComboBox;
import com.sencha.gxt.widget.core.client.form.DateField;
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;
/**
*
* @author giancarlo
* email: <a href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class TabularResourceProperties extends FramedPanel {
private static final String DESCRIPTION_FIELD_HEIGHT = "70px";
// private static final String LISTVIEWWIDTH="250px";
private DateTimeFormat sdf = DateTimeFormat.getFormat("yyyy-MM-dd");
protected String headingTitle;
protected HashMap<String, String> tabularResourcePropertiesMap;
protected VerticalLayoutContainer vl;
protected EventBus eventBus;
protected TRId trId;
protected FieldSet trFieldSet;
protected FieldSet tableFieldSet;
protected TabResource tabResource;
protected TextField nameField;
protected TextArea descriptionField;
protected TextField agencyField;
protected TextField dateField;
protected TextArea rightField;
protected DateField validFromField;
protected DateField validUntilToField;
protected TextField ownerField;
protected CheckBox validField;
protected CheckBox finalizedField;
protected ListLoader<ListLoadConfig, ListLoadResult<LicenceData>> loader;
protected ComboBox<LicenceData> comboLicences;
protected ListStore<LicenceData> storeCombo;
protected TextField typeField;
protected TextField tableTypeNameField;
protected IconButton btnShare;
protected FieldLabel shareLabel;
protected TRMetadataAccordionPanel trMetadataPanel;
protected VerticalLayoutContainer layoutTabularResource;
protected TextField tableTypeDefField;
protected TableMetadataAccordionPanel tableMetadataPanel;
protected VerticalLayoutContainer layoutTable;
protected TextButton saveButton;
protected TextButton validationsButton;
protected TextButton resourcesButton;
protected ArrayList<LicenceData> licencesList;
public TabularResourceProperties(String name, EventBus eventBus) {
super();
setId(name);
this.eventBus = eventBus;
forceLayoutOnResize = true;
retrieveLicencesList();
}
public void addTabularResource() {
trFieldSet = new FieldSet();
trFieldSet.setHeadingText("Tabular Resource");
trFieldSet.setCollapsible(true);
trFieldSet.setResize(true);
layoutTabularResource = new VerticalLayoutContainer();
trFieldSet.add(layoutTabularResource);
nameField = new TextField();
// nameField.setReadOnly(true);
nameField.setValue("");
layoutTabularResource.add(new FieldLabel(nameField, "Name"),
new VerticalLayoutData(1, -1));
descriptionField = new TextArea();
descriptionField.setHeight(DESCRIPTION_FIELD_HEIGHT);
// descriptionField.setReadOnly(true);
descriptionField.setValue("");
layoutTabularResource.add(new FieldLabel(descriptionField,
"Description"), new VerticalLayoutData(1, -1));
typeField = new TextField();
typeField.setReadOnly(true);
typeField.setValue("");
layoutTabularResource.add(new FieldLabel(typeField, "Type"),
new VerticalLayoutData(1, -1));
agencyField = new TextField();
// agencyField.setReadOnly(true);
agencyField.setValue("");
layoutTabularResource.add(new FieldLabel(agencyField, "Agency"),
new VerticalLayoutData(1, -1));
dateField = new TextField();
dateField.setReadOnly(true);
dateField.setValue("");
layoutTabularResource.add(new FieldLabel(dateField, "Creation Date"),
new VerticalLayoutData(1, -1));
tableTypeNameField = new TextField();
tableTypeNameField.setReadOnly(true);
tableTypeNameField.setValue("");
layoutTabularResource.add(new FieldLabel(tableTypeNameField, "Table Type"),
new VerticalLayoutData(1, -1));
rightField = new TextArea();
// rightField.setReadOnly(true);
rightField.setValue("");
layoutTabularResource.add(new FieldLabel(rightField, "Rights"),
new VerticalLayoutData(1, -1));
validFromField = new DateField();
layoutTabularResource.add(new FieldLabel(validFromField, "Valid From"),
new VerticalLayoutData(1, -1));
validUntilToField = new DateField();
layoutTabularResource.add(new FieldLabel(validUntilToField,
"Valid Until To"), new VerticalLayoutData(1, -1));
// //
// Combo Licences
LicenceDataPropertiesCombo propsLicenceData = GWT
.create(LicenceDataPropertiesCombo.class);
storeCombo = new ListStore<LicenceData>(propsLicenceData.id());
storeCombo.addAll(licencesList);
comboLicences = new ComboBox<LicenceData>(storeCombo,
propsLicenceData.licence());
comboLicences.setMinListWidth(250);
comboLicences.setEditable(false);
comboLicences.setTypeAhead(false);
comboLicences.setTriggerAction(TriggerAction.ALL);
Log.trace("Combo Licence created");
// /
layoutTabularResource.add(new FieldLabel(comboLicences, "Licence"),
new VerticalLayoutData(1, -1));
// /
ownerField = new TextField();
ownerField.setReadOnly(true);
ownerField.setValue("");
layoutTabularResource.add(new FieldLabel(ownerField, "Owner"),
new VerticalLayoutData(1, -1));
btnShare = new IconButton();
btnShare.setIcon(ResourceBundle.INSTANCE.share());
btnShare.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Log.debug("Clicked btnShare");
eventBus.fireEvent(new RibbonEvent(RibbonType.SHARE));
}
});
shareLabel = new FieldLabel(btnShare, "Share");
shareLabel.setVisible(false);
layoutTabularResource.add(shareLabel, new VerticalLayoutData(1, -1));
validField = new CheckBox();
validField.setValue(false);
validField.setReadOnly(true);
layoutTabularResource.add(new FieldLabel(validField, "Valid"),
new VerticalLayoutData(1, -1));
finalizedField = new CheckBox();
// finalizedField.setBoxLabel("");
finalizedField.setValue(false);
finalizedField.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
@Override
public void onValueChange(ValueChangeEvent<Boolean> event) {
if (event.getValue()) {
UtilsGXT3
.info("Warning",
"A TabularResource set to final can't be modified anymore!");
}
}
});
layoutTabularResource.add(new FieldLabel(finalizedField, "Final"),
new VerticalLayoutData(1, -1));
// Save Button
saveButton = new TextButton("Save");
saveButton.setIcon(ResourceBundle.INSTANCE.save());
saveButton.setIconAlign(IconAlign.RIGHT);
saveButton.setTitle("Save");
SelectHandler saveHandler = new SelectHandler() {
public void onSelect(SelectEvent event) {
onSave();
}
};
saveButton.addSelectHandler(saveHandler);
// Validation Button
validationsButton = new TextButton("Validations");
validationsButton.setIcon(ResourceBundle.INSTANCE.tableValidation());
validationsButton.setIconAlign(IconAlign.RIGHT);
validationsButton.setTitle("Validations");
SelectHandler validationsHandler = new SelectHandler() {
public void onSelect(SelectEvent event) {
openValidations();
}
};
validationsButton.addSelectHandler(validationsHandler);
// Resources Button
resourcesButton = new TextButton("Resources");
resourcesButton.setIcon(ResourceBundle.INSTANCE.resources());
resourcesButton.setIconAlign(IconAlign.RIGHT);
resourcesButton.setTitle("Resources");
SelectHandler resourcesHandler = new SelectHandler() {
public void onSelect(SelectEvent event) {
openResources();
}
};
resourcesButton.addSelectHandler(resourcesHandler);
//
HBoxLayoutContainer hBox = new HBoxLayoutContainer();
hBox.add(saveButton, new BoxLayoutData(new Margins(2, 5, 2, 5)));
hBox.add(validationsButton, new BoxLayoutData(new Margins(2, 5, 2, 5)));
hBox.add(resourcesButton, new BoxLayoutData(new Margins(2, 5, 2, 5)));
layoutTabularResource.add(hBox, new VerticalLayoutData(-1, -1,
new Margins(2)));
vl = new VerticalLayoutContainer();
//vl.setScrollMode(ScrollMode.AUTO); Set In GXT 3.0.1
vl.setAdjustForScroll(true);
vl.add(trFieldSet, new VerticalLayoutData(1, -1,
new Margins(0)));
add(vl);
}
protected void retrieveLicencesList() {
TDGWTServiceAsync.INSTANCE
.getLicences(new AsyncCallback<ArrayList<LicenceData>>() {
public void onFailure(Throwable caught) {
if (caught instanceof TDGWTSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
Log.error("load combo failure:"
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error",
"Error retrieving licences.");
}
}
public void onSuccess(ArrayList<LicenceData> result) {
Log.trace("loaded " + result.size() + " LicenceData");
licencesList = result;
initInformation();
}
});
}
protected void updateTabularResource(TabResource tabResource) {
this.tabResource = tabResource;
Log.debug(tabResource.toString());
nameField.setValue(tabResource.getName());
descriptionField.setValue(tabResource.getDescription());
typeField.setValue(tabResource.getTabResourceType());
agencyField.setValue(tabResource.getAgency());
dateField.setValue(tabResource.getDate());
tableTypeNameField.setValue(tabResource.getTableTypeName());
rightField.setValue(tabResource.getRight());
validFromField.clear();
if (tabResource.getValidFrom() == null
|| tabResource.getValidFrom().isEmpty()) {
Log.debug("ValidFrom null or empty");
} else {
Date vFrom;
try {
vFrom = sdf.parse(tabResource.getValidFrom());
validFromField.setValue(vFrom);
} catch (IllegalArgumentException e) {
Log.error("Error Parsing Valid From for Tabular Resource: "
+ tabResource);
e.printStackTrace();
}
}
validUntilToField.clear();
if (tabResource.getValidUntilTo() == null
|| tabResource.getValidUntilTo().isEmpty()) {
Log.debug("ValidUntilTo null or empty");
} else {
Date vUntilTo;
try {
vUntilTo = sdf.parse(tabResource.getValidUntilTo());
validUntilToField.setValue(vUntilTo);
} catch (IllegalArgumentException e) {
Log.error("Error Parsing Valid Until To for Tabular Resource: "
+ tabResource);
e.printStackTrace();
}
}
comboLicences.clear();
if (tabResource.getLicence() != null
&& !tabResource.getLicence().isEmpty()) {
List<LicenceData> listLicence = storeCombo.getAll();
boolean licenceFound = false;
for (int i = 0; i < listLicence.size(); i++) {
if (tabResource.getLicence().compareTo(
listLicence.get(i).getLicence()) == 0) {
comboLicences.setValue(listLicence.get(i));
licenceFound = true;
break;
}
}
if (licenceFound) {
Log.debug("Licence " + tabResource.getLicence() + " is found");
} else {
Log.debug("Licence " + tabResource.getLicence()
+ " is not found");
}
} else {
Log.debug("Licence is null");
}
ownerField.setValue(tabResource.getOwner().getLogin());
validField.setValue(tabResource.isValid());
if (tabResource.isFinalized()) {
finalizedField.setReadOnly(true);
finalizedField.setValue(true);
saveButton.setEnabled(false);
} else {
finalizedField.setReadOnly(false);
finalizedField.setValue(false);
saveButton.setEnabled(true);
}
ArrayList<Contacts> contacts = tabResource.getContacts();
if (contacts != null && contacts.size() > 0) {
shareLabel.setVisible(true);
} else {
shareLabel.setVisible(false);
}
}
/*
protected void updateShareInfo(ShareInfo result) {
ArrayList<Contacts> contacts = result.getContacts();
if (contacts != null && contacts.size() > 0) {
shareLabel.setVisible(true);
} else {
shareLabel.setVisible(false);
}
}*/
/*
public void addTable() {
tableFieldSet = new FieldSet();
tableFieldSet.setHeadingText("Table");
tableFieldSet.setCollapsible(true);
tableFieldSet.setResize(true);
layoutTable = new VerticalLayoutContainer();
tableFieldSet.add(layoutTable);
tableTypeDefField = new TextField();
tableTypeDefField.setReadOnly(true);
tableTypeDefField.setValue("");
layoutTable.add(new FieldLabel(tableTypeDefField, "Type"),
new VerticalLayoutData(1, -1));
vl.add(tableFieldSet);
}*/
protected void openValidations() {
Log.debug("Request Validations Tab");
if (trId != null) {
WidgetRequestEvent e = new WidgetRequestEvent(
WidgetRequestType.VALIDATIONSTASKSPANEL);
e.setTrId(trId);
eventBus.fireEvent(e);
} else {
Log.error("TRId is null");
}
}
protected void openResources() {
Log.debug("Request Resources Tab");
if (trId != null) {
WidgetRequestEvent e = new WidgetRequestEvent(
WidgetRequestType.RESOURCESPANEL);
e.setTrId(trId);
eventBus.fireEvent(e);
} else {
Log.error("TRId is null");
}
}
protected void updateTable(TableData tableData) {
trId = tableData.getTrId();
/*
* tableTypeNameField.setValue(tableData.getTypeName()); if
* (tableMetadataPanel != null) { tableFieldSet.remove(layoutTable);
* layoutTable.remove(tableMetadataPanel); }
*
*
* tableMetadataPanel = new TableMetadataAccordionPanel(
* "TableInformation", trId, eventBus);
* layoutTable.add(tableMetadataPanel, new VerticalLayoutData(-1, -1));
* tableFieldSet.add(layoutTable);
*/
forceLayout();
}
public void update() {
TDGWTServiceAsync.INSTANCE
.getTabResourceInformation(new AsyncCallback<TabResource>() {
public void onSuccess(TabResource result) {
Log.info("Retrived TR: " + result.getTrId());
updateTabularResource(result);
getLastTable(tabResource.getTrId());
//getShareInformation(result.getTrId());
}
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 {
Log.error("Error retrienving properties: "
+ caught.getLocalizedMessage());
/*UtilsGXT3.alert("Error",
"Error retrienving properties: "
+ caught.getLocalizedMessage());*/
}
}
}
});
}
/*
protected void getShareInformation(TRId trId) {
TDGWTServiceAsync.INSTANCE.getShareInfo(trId,
new AsyncCallback<ShareInfo>() {
public void onSuccess(ShareInfo result) {
Log.debug("Retrived share info:" + result);
updateShareInfo(result);
getLastTable(tabResource.getTrId());
}
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 {
Log.error("Error retrienving Share Informations: "
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error",
"Error retrienving Share Informations: "
+ caught.getLocalizedMessage());
}
}
}
});
}*/
protected void getLastTable(TRId trId) {
TDGWTServiceAsync.INSTANCE.getLastTable(trId,
new AsyncCallback<TableData>() {
public void onSuccess(TableData result) {
//Log.debug("Retrieved LastTable:" + result);
updateTable(result);
}
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 {
Log.error("Error retrienving Last Table: "
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error",
"Error retrienving Last Table: "
+ caught.getLocalizedMessage());
}
}
}
});
}
public void initInformation() {
addTabularResource();
// addTable();
update();
}
protected void onSave() {
if (nameField.getValue() == null || nameField.getValue().isEmpty()) {
Log.debug("Attention Fill name field");
UtilsGXT3.info("Attention", "Fill name field");
return;
}
tabResource.setName(nameField.getValue());
tabResource.setDescription(descriptionField.getValue());
tabResource.setAgency(agencyField.getValue());
tabResource.setRight(rightField.getValue());
Date vFrom = validFromField.getValue();
if (vFrom == null) {
Log.debug("No valid from set");
} else {
try {
String vFr = sdf.format(vFrom);
tabResource.setValidFrom(vFr);
} catch (Throwable e) {
Log.error("No valid from field: " + vFrom);
e.printStackTrace();
}
}
Date vUntilTo = validUntilToField.getValue();
if (vUntilTo == null) {
Log.debug("No valid until to set");
} else {
try {
String vUTo = sdf.format(vUntilTo);
tabResource.setValidUntilTo(vUTo);
} catch (Throwable e) {
Log.error("No valid until to field: " + vUntilTo);
e.printStackTrace();
}
}
if(vFrom!=null && vUntilTo!=null && vFrom.compareTo(vUntilTo)>0){
Log.debug("Attention Valid From field is higher than Valid Until To field");
UtilsGXT3.info("Attention", "Valid From field is higher than Valid Until To field");
return;
}
if (comboLicences.getCurrentValue() != null
&& comboLicences.getCurrentValue().getLicence() != null
&& !comboLicences.getCurrentValue().getLicence().isEmpty()) {
tabResource
.setLicence(comboLicences.getCurrentValue().getLicence());
}
tabResource.setValid(validField.getValue());
tabResource.setFinalized(finalizedField.getValue());
TDGWTServiceAsync.INSTANCE.setTabResourceInformation(tabResource,
new AsyncCallback<Void>() {
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.error("Error Setting Tabular Resoruce Properties: "
+ caught.getLocalizedMessage());
UtilsGXT3
.alert("Error",
"Error setting tabular resource properties: "
+ caught.getLocalizedMessage());
}
}
}
}
public void onSuccess(Void result) {
Log.debug("Tabular Resource properties are set: "
+ tabResource);
UtilsGXT3.info("Proprerties",
"Tabular Resource properties are set");
if (finalizedField.getValue()) {
finalizedField.setReadOnly(true);
finalizedField.setValue(true);
saveButton.setEnabled(false);
} else {
finalizedField.setReadOnly(false);
finalizedField.setValue(false);
saveButton.setEnabled(true);
}
DataViewRequestEvent dataViewRequestEvent=new DataViewRequestEvent();
TabularResourceDataView tabularResouceDataView=new TabularResourceDataView(tabResource.getTrId(),tabResource.getName());
dataViewRequestEvent.setDataView(tabularResouceDataView);
dataViewRequestEvent.setDataViewRequestType(DataViewRequestType.UPDATE_TAB_NAME);
eventBus.fireEvent(dataViewRequestEvent);
}
});
}
}