ckan-metadata-publisher-widget/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/client/ui/AddResourceToDataset.java

163 lines
4.7 KiB
Java

package org.gcube.portlets.widgets.ckandatapublisherwidget.client.ui;
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.CKanPublisherService;
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.CKanPublisherServiceAsync;
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.Encoder;
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.AddResourceEvent;
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBean;
import com.github.gwtbootstrap.client.ui.AlertBlock;
import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.TextArea;
import com.github.gwtbootstrap.client.ui.TextBox;
import com.github.gwtbootstrap.client.ui.constants.AlertType;
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.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;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
/**
* Form used to add resource(s) to a dataset
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public class AddResourceToDataset extends Composite{
private static AddResourceToDatasetUiBinder uiBinder = GWT
.create(AddResourceToDatasetUiBinder.class);
interface AddResourceToDatasetUiBinder extends
UiBinder<Widget, AddResourceToDataset> {
}
// bus to alert the dataset form about this new resource
private HandlerManager eventBus;
// the dataset id
private String datasetId;
// the owner
private String owner;
//Create a remote service proxy to talk to the server-side ckan service.
private final CKanPublisherServiceAsync ckanServices = GWT.create(CKanPublisherService.class);
@UiField TextBox resourceUrlTextBox;
@UiField TextBox resourceNameTextBox;
@UiField TextArea resourceDescriptionTextArea;
@UiField Button addResourceButton;
@UiField AlertBlock infoBlock;
@UiField Button goToDatasetButton;
public AddResourceToDataset(HandlerManager eventBus, String datasetId, String owner, final String datasetUrl) {
initWidget(uiBinder.createAndBindUi(this));
// save bus
this.eventBus = eventBus;
// save dataset id (it is needed when we will add resources)
this.datasetId = datasetId;
// the owner of the dataset/files
this.owner = owner;
goToDatasetButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
String currentPath = Window.Location.getHref();
int datasetIndex = datasetUrl.indexOf("/dataset");
if(datasetIndex == -1)
return;
String finalPart = datasetUrl.substring(datasetIndex);
String finalPath = currentPath + "?" + Encoder.encode("q") + "=" + Encoder.encode(finalPart);
Window.Location.assign(finalPath);
}
});
}
@UiHandler("addResourceButton")
void onAddButtonClick(ClickEvent e){
infoBlock.setVisible(false);
// validation
if(resourceUrlTextBox.getText().isEmpty() || resourceNameTextBox.getText().isEmpty()){
showAlert("Url and name fields cannot be empty", AlertType.ERROR);
return;
}
// collect data and build up the bean
final ResourceBean resource = new ResourceBean(resourceUrlTextBox.getText(), resourceNameTextBox.getText(), resourceDescriptionTextArea.getText());
// disable add button
addResourceButton.setEnabled(false);
// try to create
ckanServices.addResourceToDataset(resource, datasetId, owner, new AsyncCallback<ResourceBean>() {
@Override
public void onSuccess(ResourceBean result) {
if(result != null){
showAlert("Resource created correctly", AlertType.SUCCESS);
eventBus.fireEvent(new AddResourceEvent(result));
// remove data
resourceUrlTextBox.setText("");
resourceNameTextBox.setText("");
resourceDescriptionTextArea.setText("");
}
else
showAlert("Unable to add this resource. Check that the url is correct", AlertType.ERROR);
}
@Override
public void onFailure(Throwable caught) {
showAlert("Unable to add this resource, sorry", AlertType.ERROR);
}
});
}
/**
* Show error/success after resource creation attempt.
* @param text
* @param type
*/
protected void showAlert(String text, AlertType type) {
infoBlock.setText(text);
infoBlock.setType(type);
infoBlock.setVisible(true);
addResourceButton.setEnabled(true);
// hide after some seconds
Timer t = new Timer() {
@Override
public void run() {
infoBlock.setVisible(false);
}
};
t.schedule(4000);
}
}