2017-04-25 15:52:37 +02:00
|
|
|
package org.gcube.portlets.widgets.ckandatapublisherwidget.client.ui.resources;
|
2016-06-07 17:50:33 +02:00
|
|
|
|
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.CKanPublisherService;
|
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.CKanPublisherServiceAsync;
|
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.AddResourceEvent;
|
2024-02-13 17:18:28 +01:00
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.ReloadDatasetPageEvent;
|
2024-02-16 15:35:09 +01:00
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.WorkspaceItemSelectedEvent;
|
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.WorkspaceItemSelectedEventHandler;
|
2016-11-09 19:13:39 +01:00
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceElementBean;
|
2024-02-02 17:09:09 +01:00
|
|
|
import org.gcube.portlets.widgets.mpformbuilder.client.ui.utils.LoaderIcon;
|
2016-06-07 17:50:33 +02:00
|
|
|
|
2024-02-16 15:35:09 +01:00
|
|
|
import com.github.gwtbootstrap.client.ui.Alert;
|
2016-06-07 17:50:33 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.AlertBlock;
|
|
|
|
import com.github.gwtbootstrap.client.ui.Button;
|
2021-04-09 14:51:55 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.ControlGroup;
|
2016-06-07 17:50:33 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.TextArea;
|
|
|
|
import com.github.gwtbootstrap.client.ui.TextBox;
|
|
|
|
import com.github.gwtbootstrap.client.ui.constants.AlertType;
|
2021-04-09 14:51:55 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.constants.ControlGroupType;
|
2016-06-07 17:50:33 +02:00
|
|
|
import com.google.gwt.core.client.GWT;
|
2024-02-16 15:35:09 +01:00
|
|
|
import com.google.gwt.dom.client.Style.Unit;
|
2016-06-07 17:50:33 +02:00
|
|
|
import com.google.gwt.event.dom.client.ClickEvent;
|
2016-06-10 12:30:54 +02:00
|
|
|
import com.google.gwt.event.dom.client.ClickHandler;
|
2016-06-07 17:50:33 +02:00
|
|
|
import com.google.gwt.event.shared.HandlerManager;
|
|
|
|
import com.google.gwt.uibinder.client.UiBinder;
|
|
|
|
import com.google.gwt.uibinder.client.UiField;
|
|
|
|
import com.google.gwt.uibinder.client.UiHandler;
|
|
|
|
import com.google.gwt.user.client.Timer;
|
2016-06-10 12:30:54 +02:00
|
|
|
import com.google.gwt.user.client.Window;
|
2016-06-07 17:50:33 +02:00
|
|
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
|
|
|
import com.google.gwt.user.client.ui.Composite;
|
2024-02-16 15:35:09 +01:00
|
|
|
import com.google.gwt.user.client.ui.DialogBox;
|
2023-07-25 15:24:37 +02:00
|
|
|
import com.google.gwt.user.client.ui.FlowPanel;
|
2024-02-16 15:35:09 +01:00
|
|
|
import com.google.gwt.user.client.ui.HTML;
|
2016-06-07 17:50:33 +02:00
|
|
|
import com.google.gwt.user.client.ui.Widget;
|
|
|
|
|
|
|
|
/**
|
2024-02-23 12:35:47 +01:00
|
|
|
* The Class AddResourceToDataset.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
2024-02-16 15:35:09 +01:00
|
|
|
*
|
2024-02-23 12:35:47 +01:00
|
|
|
* Feb 23, 2024
|
2016-06-07 17:50:33 +02:00
|
|
|
*/
|
2024-02-16 15:35:09 +01:00
|
|
|
public class AddResourceToDataset extends Composite {
|
2016-06-07 17:50:33 +02:00
|
|
|
|
2024-02-16 15:35:09 +01:00
|
|
|
private static AddResourceToDatasetUiBinder uiBinder = GWT.create(AddResourceToDatasetUiBinder.class);
|
2016-06-07 17:50:33 +02:00
|
|
|
|
2024-02-23 12:35:47 +01:00
|
|
|
/**
|
|
|
|
* The Interface AddResourceToDatasetUiBinder.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
|
|
|
*
|
|
|
|
* Feb 23, 2024
|
|
|
|
*/
|
2024-02-16 15:35:09 +01:00
|
|
|
interface AddResourceToDatasetUiBinder extends UiBinder<Widget, AddResourceToDataset> {
|
2016-06-07 17:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// bus to alert the dataset form about this new resource
|
|
|
|
private HandlerManager eventBus;
|
2016-06-08 18:31:54 +02:00
|
|
|
|
2016-06-07 17:50:33 +02:00
|
|
|
// the dataset id
|
|
|
|
private String datasetId;
|
2016-10-22 17:51:43 +02:00
|
|
|
|
2016-07-20 19:14:20 +02:00
|
|
|
// the dataset organization
|
|
|
|
private String datasetOrg;
|
2016-06-08 18:31:54 +02:00
|
|
|
|
2016-06-07 17:50:33 +02:00
|
|
|
private final CKanPublisherServiceAsync ckanServices = GWT.create(CKanPublisherService.class);
|
|
|
|
|
2024-02-16 15:35:09 +01:00
|
|
|
@UiField
|
|
|
|
TextBox resourceUrlTextBox;
|
|
|
|
@UiField
|
|
|
|
TextBox resourceNameTextBox;
|
|
|
|
@UiField
|
|
|
|
TextArea resourceDescriptionTextArea;
|
|
|
|
@UiField
|
|
|
|
Button addResourceButton;
|
|
|
|
@UiField
|
|
|
|
AlertBlock infoBlock;
|
|
|
|
@UiField
|
|
|
|
Button goToDatasetButton;
|
|
|
|
@UiField
|
|
|
|
ControlGroup urlControlGroup;
|
|
|
|
@UiField
|
|
|
|
ControlGroup nameControlGroup;
|
|
|
|
@UiField
|
|
|
|
FlowPanel infoPanel;
|
|
|
|
@UiField
|
|
|
|
Button buttoSelectFromWorkspace;
|
|
|
|
@UiField
|
|
|
|
Alert alertInfoURL;
|
|
|
|
|
|
|
|
FlowPanel alertPanel = new FlowPanel();
|
|
|
|
|
2024-02-23 12:35:47 +01:00
|
|
|
/**
|
|
|
|
* Instantiates a new adds the resource to dataset.
|
|
|
|
*
|
|
|
|
* @param eventBus the event bus
|
|
|
|
* @param datasetId the dataset id
|
|
|
|
* @param datasetTitle the dataset title
|
|
|
|
* @param datasetOrg the dataset org
|
|
|
|
* @param datasetUrl the dataset url
|
|
|
|
*/
|
2024-02-16 15:35:09 +01:00
|
|
|
public AddResourceToDataset(HandlerManager eventBus, final String datasetId, String datasetTitle,
|
|
|
|
final String datasetOrg, final String datasetUrl) {
|
2016-06-07 17:50:33 +02:00
|
|
|
initWidget(uiBinder.createAndBindUi(this));
|
|
|
|
// save bus
|
|
|
|
this.eventBus = eventBus;
|
|
|
|
// save dataset id (it is needed when we will add resources)
|
|
|
|
this.datasetId = datasetId;
|
2016-10-22 17:51:43 +02:00
|
|
|
this.datasetOrg = datasetOrg;
|
2024-02-16 15:35:09 +01:00
|
|
|
|
2022-06-14 12:28:03 +02:00
|
|
|
String title = datasetTitle;
|
|
|
|
String link = "";
|
|
|
|
link += title.length() > 90 ? title.substring(0, 90) + "..." : title;
|
2024-02-16 15:35:09 +01:00
|
|
|
goToDatasetButton.setTitle("Go to the item: " + title);
|
2022-06-14 12:28:03 +02:00
|
|
|
goToDatasetButton.setText(link);
|
2024-02-16 15:35:09 +01:00
|
|
|
// goToDatasetButton.setHref(datasetUrl);
|
2016-06-10 12:30:54 +02:00
|
|
|
goToDatasetButton.addClickHandler(new ClickHandler() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(ClickEvent event) {
|
2016-10-22 17:51:43 +02:00
|
|
|
Window.open(datasetUrl, "_blank", "");
|
2016-06-10 12:30:54 +02:00
|
|
|
}
|
|
|
|
});
|
2024-02-16 15:35:09 +01:00
|
|
|
|
|
|
|
alertInfoURL.setType(AlertType.WARNING);
|
|
|
|
|
|
|
|
infoBlock.add(alertPanel);
|
|
|
|
|
|
|
|
bind();
|
|
|
|
|
|
|
|
addResourceButton.setEnabled(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-23 12:35:47 +01:00
|
|
|
/**
|
|
|
|
* Bind.
|
|
|
|
*/
|
2024-02-16 15:35:09 +01:00
|
|
|
private void bind() {
|
|
|
|
|
|
|
|
eventBus.addHandler(WorkspaceItemSelectedEvent.TYPE, new WorkspaceItemSelectedEventHandler() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSelectedItem(final WorkspaceItemSelectedEvent workspaceItemSelectedEvent) {
|
|
|
|
|
|
|
|
if (workspaceItemSelectedEvent.getItem() != null) {
|
|
|
|
LoaderIcon loader = new LoaderIcon("Getting public link...");
|
|
|
|
showAlert(null, loader, AlertType.INFO, false, true);
|
|
|
|
|
|
|
|
ckanServices.getPublicLinkForFileItemId(workspaceItemSelectedEvent.getItem().getId(), true,
|
|
|
|
new AsyncCallback<String>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Throwable caught) {
|
|
|
|
showAlert(caught.getMessage(), null, AlertType.ERROR, false, true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSuccess(String result) {
|
|
|
|
showAlert("", null, null, false, false);
|
|
|
|
resourceUrlTextBox.setText(result);
|
|
|
|
resourceNameTextBox.setText(workspaceItemSelectedEvent.getItem().getName());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-23 12:35:47 +01:00
|
|
|
/**
|
|
|
|
* On select from workspace click.
|
|
|
|
*
|
|
|
|
* @param e the e
|
|
|
|
*/
|
2024-02-16 15:35:09 +01:00
|
|
|
@UiHandler("buttoSelectFromWorkspace")
|
|
|
|
void onSelectFromWorkspaceClick(ClickEvent e) {
|
|
|
|
|
|
|
|
final DialogBox dialog = new DialogBox(false);
|
|
|
|
dialog.setText("Select file from Workspace...");
|
2024-02-16 16:05:33 +01:00
|
|
|
dialog.getElement().setId("selectWorkspaceDialog");
|
2024-02-16 15:35:09 +01:00
|
|
|
|
|
|
|
dialog.getElement().getStyle().setWidth(700, Unit.PX);
|
|
|
|
dialog.getElement().getStyle().setZIndex(10000);
|
|
|
|
dialog.setHeight("400px");
|
|
|
|
|
|
|
|
DialogWorkspaceExplorer dbw = new DialogWorkspaceExplorer(dialog, eventBus);
|
|
|
|
dialog.add(dbw);
|
|
|
|
dialog.center();
|
2016-06-07 17:50:33 +02:00
|
|
|
}
|
|
|
|
|
2024-02-23 12:35:47 +01:00
|
|
|
/**
|
|
|
|
* On add button click.
|
|
|
|
*
|
|
|
|
* @param e the e
|
|
|
|
*/
|
2016-06-07 17:50:33 +02:00
|
|
|
@UiHandler("addResourceButton")
|
2024-02-16 15:35:09 +01:00
|
|
|
void onAddButtonClick(ClickEvent e) {
|
|
|
|
|
2016-06-07 17:50:33 +02:00
|
|
|
infoBlock.setVisible(false);
|
2021-04-09 14:51:55 +02:00
|
|
|
urlControlGroup.setType(ControlGroupType.NONE);
|
|
|
|
nameControlGroup.setType(ControlGroupType.NONE);
|
2016-06-07 17:50:33 +02:00
|
|
|
|
|
|
|
// validation
|
2021-04-09 14:51:55 +02:00
|
|
|
if (resourceUrlTextBox.getText().isEmpty()) {
|
2016-06-07 17:50:33 +02:00
|
|
|
|
2024-02-16 15:35:09 +01:00
|
|
|
showAlert("'URL' field cannot be empty", null, AlertType.ERROR, true, true);
|
2021-04-09 14:51:55 +02:00
|
|
|
urlControlGroup.setType(ControlGroupType.ERROR);
|
2016-06-07 17:50:33 +02:00
|
|
|
return;
|
2021-04-09 14:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// validation
|
|
|
|
if (resourceNameTextBox.getText().isEmpty() || resourceNameTextBox.getText().isEmpty()) {
|
2016-06-07 17:50:33 +02:00
|
|
|
|
2024-02-16 15:35:09 +01:00
|
|
|
showAlert("'Name' field cannot be empty", null, AlertType.ERROR, true, true);
|
2021-04-09 14:51:55 +02:00
|
|
|
nameControlGroup.setType(ControlGroupType.ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2024-02-16 15:35:09 +01:00
|
|
|
|
|
|
|
// THE URL must be HTTPS, see #21068
|
|
|
|
if (!(resourceUrlTextBox.getText().toLowerCase().startsWith("https://"))) {
|
|
|
|
showAlert("The URL must be HTTPS, so start with \"https://\" (e.g. https://your-resource.com)", null,
|
|
|
|
AlertType.ERROR, true, true);
|
2021-04-09 14:51:55 +02:00
|
|
|
urlControlGroup.setType(ControlGroupType.ERROR);
|
|
|
|
return;
|
2024-02-16 15:35:09 +01:00
|
|
|
|
2016-06-07 17:50:33 +02:00
|
|
|
}
|
2016-06-10 12:30:54 +02:00
|
|
|
|
2016-06-07 17:50:33 +02:00
|
|
|
// collect data and build up the bean
|
2024-02-16 15:35:09 +01:00
|
|
|
final ResourceElementBean resource = new ResourceElementBean(resourceNameTextBox.getText(), true, false, null,
|
|
|
|
null, null, null, null, resourceUrlTextBox.getText(), resourceDescriptionTextArea.getText(),
|
|
|
|
datasetOrg);
|
2016-06-07 17:50:33 +02:00
|
|
|
|
|
|
|
// disable add button
|
|
|
|
addResourceButton.setEnabled(false);
|
2016-06-08 18:31:54 +02:00
|
|
|
|
2023-07-25 15:24:37 +02:00
|
|
|
LoaderIcon loader = new LoaderIcon("Adding resource, please wait...");
|
|
|
|
infoPanel.add(loader);
|
2024-02-16 15:35:09 +01:00
|
|
|
|
2016-06-07 17:50:33 +02:00
|
|
|
// try to create
|
2024-02-12 16:52:06 +01:00
|
|
|
ckanServices.addResourceToDataset(resource, datasetOrg, datasetId, new AsyncCallback<ResourceElementBean>() {
|
2016-06-08 18:31:54 +02:00
|
|
|
|
2016-06-07 17:50:33 +02:00
|
|
|
@Override
|
2016-11-09 19:13:39 +01:00
|
|
|
public void onSuccess(ResourceElementBean result) {
|
2023-07-25 15:24:37 +02:00
|
|
|
infoPanel.clear();
|
2016-06-08 18:31:54 +02:00
|
|
|
|
2024-02-16 15:35:09 +01:00
|
|
|
if (result != null) {
|
|
|
|
showAlert("Resource created correctly", null, AlertType.SUCCESS, true, true);
|
2016-06-08 18:31:54 +02:00
|
|
|
eventBus.fireEvent(new AddResourceEvent(result));
|
2024-02-13 17:18:28 +01:00
|
|
|
eventBus.fireEvent(new ReloadDatasetPageEvent(datasetId));
|
2016-06-08 18:31:54 +02:00
|
|
|
|
|
|
|
// remove data
|
|
|
|
resourceUrlTextBox.setText("");
|
|
|
|
resourceNameTextBox.setText("");
|
|
|
|
resourceDescriptionTextArea.setText("");
|
|
|
|
|
2024-02-16 15:35:09 +01:00
|
|
|
} else
|
|
|
|
showAlert("Unable to add this resource. Check that the url is correct", null, AlertType.ERROR, true,
|
|
|
|
true);
|
2024-02-23 12:35:47 +01:00
|
|
|
|
2024-02-19 17:40:13 +01:00
|
|
|
addResourceButton.setEnabled(true);
|
2016-06-08 18:31:54 +02:00
|
|
|
|
2016-06-07 17:50:33 +02:00
|
|
|
}
|
2016-06-08 18:31:54 +02:00
|
|
|
|
2016-06-07 17:50:33 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(Throwable caught) {
|
2023-07-25 15:24:37 +02:00
|
|
|
infoPanel.clear();
|
2024-02-16 15:35:09 +01:00
|
|
|
showAlert("Unable to add this resource, sorry. Error is: " + caught.getMessage(), null, AlertType.ERROR,
|
|
|
|
true, true);
|
2024-02-23 12:35:47 +01:00
|
|
|
|
2024-02-19 17:40:13 +01:00
|
|
|
addResourceButton.setEnabled(true);
|
2016-06-08 18:31:54 +02:00
|
|
|
|
2016-06-07 17:50:33 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show error/success after resource creation attempt.
|
2024-02-23 12:35:47 +01:00
|
|
|
*
|
|
|
|
* @param text the text
|
|
|
|
* @param loader the loader
|
|
|
|
* @param type the type
|
|
|
|
* @param scheduleHide the schedule hide
|
|
|
|
* @param setVisible the set visible
|
2016-06-07 17:50:33 +02:00
|
|
|
*/
|
2024-02-16 15:35:09 +01:00
|
|
|
protected void showAlert(String text, LoaderIcon loader, AlertType type, boolean scheduleHide, boolean setVisible) {
|
|
|
|
|
|
|
|
alertPanel.clear();
|
2016-06-08 18:31:54 +02:00
|
|
|
|
2016-06-07 17:50:33 +02:00
|
|
|
infoBlock.setType(type);
|
2024-02-16 15:35:09 +01:00
|
|
|
infoBlock.setVisible(setVisible);
|
2016-06-07 17:50:33 +02:00
|
|
|
|
2024-02-16 15:35:09 +01:00
|
|
|
if (scheduleHide) {
|
|
|
|
// hide after some seconds
|
|
|
|
Timer t = new Timer() {
|
2016-06-07 17:50:33 +02:00
|
|
|
|
2024-02-16 15:35:09 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2016-06-07 17:50:33 +02:00
|
|
|
|
2024-02-16 15:35:09 +01:00
|
|
|
infoBlock.setVisible(false);
|
2016-06-07 17:50:33 +02:00
|
|
|
|
2024-02-16 15:35:09 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
t.schedule(8000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text != null) {
|
|
|
|
alertPanel.add(new HTML(text));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loader != null) {
|
|
|
|
alertPanel.add(loader);
|
|
|
|
}
|
2016-06-07 17:50:33 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|