2016-05-26 18:12:53 +02:00
|
|
|
package org.gcube.portlets.widgets.ckandatapublisherwidget.client.ui;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2016-05-27 19:08:03 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
2016-05-26 18:12:53 +02:00
|
|
|
import java.util.List;
|
2016-05-27 19:08:03 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2016-05-26 18:12:53 +02:00
|
|
|
|
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.CKanPublisherService;
|
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.CKanPublisherServiceAsync;
|
2016-05-27 19:08:03 +02:00
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.DeleteCustomFieldEvent;
|
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.DeleteCustomFieldEventHandler;
|
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.DatasetMetadataBean;
|
2016-05-26 18:12:53 +02:00
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.LicensesBean;
|
2016-05-27 19:08:03 +02:00
|
|
|
import org.gcube.portlets.widgets.workspacesharingwidget.client.view.sharing.multisuggest.Span;
|
2016-05-26 18:12:53 +02:00
|
|
|
|
2016-05-27 19:08:03 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.AlertBlock;
|
2016-05-26 18:12:53 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.Button;
|
|
|
|
import com.github.gwtbootstrap.client.ui.ControlGroup;
|
2016-05-27 19:08:03 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.Label;
|
2016-05-26 18:12:53 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.ListBox;
|
|
|
|
import com.github.gwtbootstrap.client.ui.TextArea;
|
|
|
|
import com.github.gwtbootstrap.client.ui.TextBox;
|
2016-05-27 19:08:03 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.base.ListItem;
|
|
|
|
import com.github.gwtbootstrap.client.ui.constants.AlertType;
|
2016-05-26 18:12:53 +02:00
|
|
|
import com.google.gwt.core.client.GWT;
|
|
|
|
import com.google.gwt.event.dom.client.ClickEvent;
|
2016-05-27 19:08:03 +02:00
|
|
|
import com.google.gwt.event.dom.client.ClickHandler;
|
|
|
|
import com.google.gwt.event.dom.client.KeyCodes;
|
|
|
|
import com.google.gwt.event.dom.client.KeyDownEvent;
|
|
|
|
import com.google.gwt.event.shared.HandlerManager;
|
2016-05-26 18:12:53 +02:00
|
|
|
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.rpc.AsyncCallback;
|
|
|
|
import com.google.gwt.user.client.ui.Composite;
|
2016-05-27 19:08:03 +02:00
|
|
|
import com.google.gwt.user.client.ui.FlowPanel;
|
2016-05-26 18:12:53 +02:00
|
|
|
import com.google.gwt.user.client.ui.Widget;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Edit metadata form for ckan dataset.
|
|
|
|
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
|
|
|
*/
|
|
|
|
public class EditMetadataForm extends Composite{
|
2016-05-27 19:08:03 +02:00
|
|
|
|
2016-05-26 18:12:53 +02:00
|
|
|
/**
|
|
|
|
* Create a remote service proxy to talk to the server-side ckan service.
|
|
|
|
*/
|
|
|
|
private final CKanPublisherServiceAsync ckanServices = GWT.create(CKanPublisherService.class);
|
|
|
|
|
|
|
|
private static EditMetadataFormUiBinder uiBinder = GWT
|
|
|
|
.create(EditMetadataFormUiBinder.class);
|
|
|
|
|
|
|
|
interface EditMetadataFormUiBinder extends
|
|
|
|
UiBinder<Widget, EditMetadataForm> {
|
|
|
|
}
|
|
|
|
|
|
|
|
@UiField TextBox titleTextBox;
|
|
|
|
@UiField TextArea descriptionTextarea;
|
2016-05-27 19:08:03 +02:00
|
|
|
@UiField TextBox tagsEnterTextBox;
|
|
|
|
@UiField FlowPanel tagsPanel;
|
2016-05-26 18:12:53 +02:00
|
|
|
@UiField ListBox licenseListbox;
|
|
|
|
@UiField ListBox visibilityListbox;
|
|
|
|
@UiField ListBox searchableListbox;
|
|
|
|
@UiField TextBox versionTextbox;
|
|
|
|
@UiField TextBox authorTextbox;
|
|
|
|
@UiField TextBox authorEmailTextbox;
|
|
|
|
@UiField TextBox maintainerTextbox;
|
|
|
|
@UiField TextBox maintainerEmailTextbox;
|
|
|
|
@UiField ControlGroup customFields;
|
|
|
|
@UiField Button addCustomFieldButton;
|
|
|
|
@UiField Button createButton;
|
|
|
|
@UiField Button resetButton;
|
2016-05-27 19:08:03 +02:00
|
|
|
@UiField AlertBlock infoBlock;
|
|
|
|
|
|
|
|
// tags list
|
|
|
|
private List<String> tagsList = new ArrayList<String>();
|
|
|
|
|
2016-05-26 18:12:53 +02:00
|
|
|
// the licenses
|
|
|
|
private LicensesBean licenseBean;
|
|
|
|
|
2016-05-27 19:08:03 +02:00
|
|
|
// event bus
|
|
|
|
private final HandlerManager eventBus = new HandlerManager(null);
|
|
|
|
|
2016-05-26 18:12:53 +02:00
|
|
|
// added custom field entries
|
|
|
|
List<CustomFieldEntry> customFieldEntries = new ArrayList<CustomFieldEntry>();
|
2016-05-27 19:08:03 +02:00
|
|
|
|
|
|
|
// dataset metadata bean
|
|
|
|
private DatasetMetadataBean receivedBean;
|
2016-05-26 18:12:53 +02:00
|
|
|
|
2016-05-27 19:08:03 +02:00
|
|
|
public EditMetadataForm(String idFolderWorkspace, String owner) {
|
2016-05-26 18:12:53 +02:00
|
|
|
initWidget(uiBinder.createAndBindUi(this));
|
2016-05-27 19:08:03 +02:00
|
|
|
|
|
|
|
// bind on events
|
|
|
|
bind();
|
2016-05-26 18:12:53 +02:00
|
|
|
|
2016-05-27 19:08:03 +02:00
|
|
|
// set info block
|
|
|
|
setAlertBlock("Retrieving information, please wait...", AlertType.INFO, true);
|
|
|
|
|
|
|
|
// disable create button
|
|
|
|
createButton.setEnabled(false);
|
|
|
|
|
|
|
|
// get back the licenses and the metadata information from the workspace
|
|
|
|
ckanServices.getDatasetBean(idFolderWorkspace, owner, new AsyncCallback<DatasetMetadataBean>() {
|
|
|
|
|
2016-05-26 18:12:53 +02:00
|
|
|
@Override
|
2016-05-27 19:08:03 +02:00
|
|
|
public void onSuccess(DatasetMetadataBean bean) {
|
|
|
|
|
|
|
|
if(bean != null){
|
2016-05-26 18:12:53 +02:00
|
|
|
|
2016-05-27 19:08:03 +02:00
|
|
|
// save it
|
|
|
|
receivedBean = bean;
|
|
|
|
|
|
|
|
// fill the form
|
|
|
|
titleTextBox.setText(bean.getTitle());
|
|
|
|
descriptionTextarea.setText(bean.getDescription());
|
|
|
|
versionTextbox.setText(String.valueOf(bean.getVersion()));
|
|
|
|
authorTextbox.setText(bean.getAuthor());
|
|
|
|
authorEmailTextbox.setText(bean.getAuthorEmail());
|
|
|
|
maintainerTextbox.setText(bean.getMaintainer());
|
|
|
|
maintainerEmailTextbox.setText(bean.getMaintainerEmail());
|
|
|
|
|
|
|
|
// retrieve custom fields
|
|
|
|
Map<String, String> customFieldsMap = bean.getCustomFields();
|
|
|
|
|
|
|
|
if(customFields != null){
|
|
|
|
|
|
|
|
// get the keys and put them as tags
|
|
|
|
Iterator<Entry<String, String>> iteratorOverCustomField = customFieldsMap.entrySet().iterator();
|
|
|
|
|
|
|
|
while (iteratorOverCustomField.hasNext()) {
|
|
|
|
Map.Entry<String, String> entry = (Map.Entry<String, String>) iteratorOverCustomField
|
|
|
|
.next();
|
|
|
|
|
|
|
|
// these are fixed key, variable value custom fields
|
|
|
|
CustomFieldEntry toAdd = new CustomFieldEntry(eventBus);
|
|
|
|
|
|
|
|
// remove the first appendbox
|
|
|
|
toAdd.valueFieldPrepend.removeFromParent();
|
|
|
|
toAdd.keyFieldPrepend.setPrependText(entry.getKey() + ":");
|
|
|
|
((TextBox)toAdd.keyFieldPrepend.getWidget(1)).setText(entry.getValue());
|
|
|
|
|
|
|
|
customFieldEntries.add(toAdd);
|
|
|
|
customFields.add(toAdd);
|
|
|
|
|
|
|
|
final ListItem item = new ListItem();
|
|
|
|
Label label = new Label(entry.getKey());
|
|
|
|
item.add(label);
|
|
|
|
|
|
|
|
}
|
2016-05-26 18:12:53 +02:00
|
|
|
}
|
2016-05-27 19:08:03 +02:00
|
|
|
|
|
|
|
// try to retrieve the licenses
|
|
|
|
setAlertBlock("Retrieving licenses, please wait...", AlertType.INFO, true);
|
|
|
|
ckanServices.getLicenses(new AsyncCallback<LicensesBean>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSuccess(LicensesBean lBean) {
|
|
|
|
|
|
|
|
if(lBean != null && !lBean.getLicenses().isEmpty()){
|
|
|
|
|
|
|
|
licenseBean = lBean;
|
|
|
|
|
|
|
|
// fill the listbox
|
|
|
|
for(String license: licenseBean.getLicenses()){
|
|
|
|
|
|
|
|
licenseListbox.addItem(license);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// everything went ok
|
|
|
|
setAlertBlock("", AlertType.ERROR, false);
|
|
|
|
createButton.setEnabled(true);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
setAlertBlock("Error while retrieving licenses, try to refresh the page", AlertType.ERROR, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Throwable caught) {
|
|
|
|
|
|
|
|
setAlertBlock("Error while retrieving licenses, try to refresh the page", AlertType.ERROR, true);
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
setAlertBlock("Error while retrieving information, try to refresh the page", AlertType.ERROR, true);
|
2016-05-26 18:12:53 +02:00
|
|
|
}
|
2016-05-27 19:08:03 +02:00
|
|
|
|
2016-05-26 18:12:53 +02:00
|
|
|
}
|
2016-05-27 19:08:03 +02:00
|
|
|
|
2016-05-26 18:12:53 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(Throwable caught) {
|
2016-05-27 19:08:03 +02:00
|
|
|
|
|
|
|
setAlertBlock("Error while retrieving information, try to refresh the page", AlertType.ERROR, true);
|
|
|
|
|
2016-05-26 18:12:53 +02:00
|
|
|
}
|
|
|
|
});
|
2016-05-27 19:08:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind on events
|
|
|
|
*/
|
|
|
|
private void bind() {
|
|
|
|
|
|
|
|
// when a custom field is removed, remove it from the list
|
|
|
|
eventBus.addHandler(DeleteCustomFieldEvent.TYPE, new DeleteCustomFieldEventHandler() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRemoveEntry(DeleteCustomFieldEvent event) {
|
|
|
|
|
|
|
|
customFieldEntries.remove(event.getRemovedEntry());
|
|
|
|
customFields.remove(event.getRemovedEntry());
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-05-26 18:12:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@UiHandler("addCustomFieldButton")
|
|
|
|
void addCustomFieldEvent(ClickEvent e){
|
|
|
|
|
2016-05-27 19:08:03 +02:00
|
|
|
CustomFieldEntry toAdd = new CustomFieldEntry(eventBus);
|
2016-05-26 18:12:53 +02:00
|
|
|
customFieldEntries.add(toAdd);
|
|
|
|
customFields.add(toAdd);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@UiHandler("createButton")
|
|
|
|
void createDatasetEvent(ClickEvent e){
|
2016-05-27 19:08:03 +02:00
|
|
|
|
|
|
|
// validate data TODO
|
|
|
|
boolean areDataValid = validateData();
|
|
|
|
|
|
|
|
// Collect current data and send them to the server
|
|
|
|
if(areDataValid){
|
|
|
|
|
|
|
|
String title = titleTextBox.getValue();
|
|
|
|
String description = descriptionTextarea.getText();
|
|
|
|
String selectedLicense = licenseListbox.getSelectedItemText();
|
|
|
|
String visibility = visibilityListbox.getValue();
|
|
|
|
String searchable = searchableListbox.getValue();
|
|
|
|
long version = Long.valueOf(versionTextbox.getValue());
|
|
|
|
String author = authorTextbox.getValue();
|
|
|
|
String authorEmail = authorEmailTextbox.getValue();
|
|
|
|
String maintainer = maintainerTextbox.getValue();
|
|
|
|
String maintainerEmail = maintainerEmailTextbox.getValue();
|
|
|
|
|
|
|
|
// fill the bean
|
|
|
|
receivedBean.setAuthor(author);
|
|
|
|
receivedBean.setAuthorEmail(authorEmail);
|
|
|
|
receivedBean.setDescription(description);
|
|
|
|
receivedBean.setLicense(selectedLicense);
|
|
|
|
receivedBean.setMaintainer(maintainer);
|
|
|
|
receivedBean.setMaintainerEmail(maintainerEmail);
|
|
|
|
receivedBean.setVersion(version);
|
|
|
|
receivedBean.setVisibility(visibility.equals("Public"));
|
|
|
|
receivedBean.setTitle(title);
|
|
|
|
receivedBean.setSearchable(searchable.equals("Yes"));
|
|
|
|
receivedBean.setTags(tagsList);
|
|
|
|
|
|
|
|
Map<String, String> customFieldsMap = new HashMap<String, String>();
|
|
|
|
|
|
|
|
// prepare custom fields
|
|
|
|
for(CustomFieldEntry customEntry : customFieldEntries){
|
|
|
|
|
|
|
|
String key = customEntry.getKey();
|
|
|
|
String value = customEntry.getValue();
|
|
|
|
customFieldsMap.put(key, value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
receivedBean.setCustomFields(customFieldsMap);
|
|
|
|
|
|
|
|
// TODO invoke remote service
|
|
|
|
}
|
|
|
|
|
2016-05-26 18:12:53 +02:00
|
|
|
|
2016-05-27 19:08:03 +02:00
|
|
|
}
|
2016-05-26 18:12:53 +02:00
|
|
|
|
2016-05-27 19:08:03 +02:00
|
|
|
/**
|
|
|
|
* Validate data
|
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
private boolean validateData() {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return true;
|
2016-05-26 18:12:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@UiHandler("resetButton")
|
|
|
|
void resetFormEvent(ClickEvent e){
|
|
|
|
|
|
|
|
// reset main fields
|
|
|
|
titleTextBox.setText("");
|
|
|
|
descriptionTextarea.setText("");
|
|
|
|
versionTextbox.setText("");
|
|
|
|
authorTextbox.setText("");
|
|
|
|
authorEmailTextbox.setText("");
|
|
|
|
maintainerTextbox.setText("");
|
|
|
|
maintainerEmailTextbox.setText("");
|
2016-05-27 19:08:03 +02:00
|
|
|
removeTags();
|
2016-05-26 18:12:53 +02:00
|
|
|
|
|
|
|
// delete custom fields
|
|
|
|
for (CustomFieldEntry customField : customFieldEntries) {
|
|
|
|
customField.removeFromParent();
|
|
|
|
}
|
|
|
|
customFieldEntries.clear();
|
2016-05-27 19:08:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* change alert block behavior.
|
|
|
|
* @param textToShow
|
|
|
|
* @param type
|
|
|
|
* @param visible
|
|
|
|
*/
|
|
|
|
private void setAlertBlock(String textToShow, AlertType type, boolean visible){
|
|
|
|
|
|
|
|
infoBlock.setText(textToShow);
|
|
|
|
infoBlock.setType(type);
|
|
|
|
infoBlock.setVisible(visible);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@UiHandler("tagsEnterTextBox")
|
|
|
|
void onAddTag(KeyDownEvent event){
|
|
|
|
|
|
|
|
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
|
|
|
|
if (!"".equals(tagsEnterTextBox.getValue().trim())) {
|
|
|
|
|
|
|
|
addTagElement(tagsEnterTextBox);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the tag as an element
|
|
|
|
*/
|
|
|
|
private void addTagElement(TextBox itemBox){
|
|
|
|
|
|
|
|
if (itemBox.getValue() != null && !"".equals(itemBox.getValue().trim())) {
|
|
|
|
|
|
|
|
if(tagsList.contains(itemBox.getValue()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
final String value = itemBox.getValue();
|
|
|
|
final ListItem displayItem = new ListItem();
|
|
|
|
displayItem.setStyleName("tag-style");
|
|
|
|
Span p = new Span(itemBox.getValue());
|
|
|
|
|
|
|
|
Span span = new Span("x");
|
|
|
|
span.setTitle("Remove this tag");
|
|
|
|
span.addClickHandler(new ClickHandler() {
|
|
|
|
public void onClick(ClickEvent clickEvent) {
|
|
|
|
removeListItem(displayItem, value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
span.setStyleName("tag-style-x");
|
|
|
|
displayItem.add(p);
|
|
|
|
displayItem.add(span);
|
|
|
|
itemBox.setValue("");
|
|
|
|
itemBox.setFocus(true);
|
|
|
|
tagsPanel.add(displayItem);
|
|
|
|
tagsList.add(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a tag from the list
|
|
|
|
* @param displayItem
|
|
|
|
*/
|
|
|
|
private void removeListItem(ListItem displayItem, String value) {
|
|
|
|
GWT.log("Removing: " + displayItem.getWidget(0).getElement().getInnerHTML(), null);
|
|
|
|
tagsList.remove(value);
|
|
|
|
tagsPanel.remove(displayItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all inserted tags
|
|
|
|
*/
|
|
|
|
private void removeTags(){
|
|
|
|
|
|
|
|
tagsList.clear();
|
|
|
|
tagsPanel.clear();
|
|
|
|
|
2016-05-26 18:12:53 +02:00
|
|
|
}
|
|
|
|
}
|