2017-01-26 18:21:33 +01:00
|
|
|
package org.gcube.datacatalogue.grsf_manage_widget.client.view;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
2017-02-20 12:21:52 +01:00
|
|
|
import java.util.Iterator;
|
2017-01-26 18:21:33 +01:00
|
|
|
import java.util.List;
|
2017-02-20 12:21:52 +01:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2017-01-26 18:21:33 +01:00
|
|
|
|
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.client.GRSFManageWidgetService;
|
2017-01-27 16:14:14 +01:00
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.client.GRSFManageWidgetServiceAsync;
|
2017-01-26 18:21:33 +01:00
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.shared.GRSFStatus;
|
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.shared.ManageProductBean;
|
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.shared.ex.NoGRSFRecordException;
|
|
|
|
|
|
|
|
import com.github.gwtbootstrap.client.ui.AlertBlock;
|
|
|
|
import com.github.gwtbootstrap.client.ui.Button;
|
2017-08-01 18:18:29 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.CheckBox;
|
2017-01-26 18:21:33 +01:00
|
|
|
import com.github.gwtbootstrap.client.ui.ControlGroup;
|
|
|
|
import com.github.gwtbootstrap.client.ui.Form;
|
|
|
|
import com.github.gwtbootstrap.client.ui.Icon;
|
|
|
|
import com.github.gwtbootstrap.client.ui.Image;
|
|
|
|
import com.github.gwtbootstrap.client.ui.ListBox;
|
|
|
|
import com.github.gwtbootstrap.client.ui.Modal;
|
|
|
|
import com.github.gwtbootstrap.client.ui.TextArea;
|
|
|
|
import com.github.gwtbootstrap.client.ui.TextBox;
|
|
|
|
import com.github.gwtbootstrap.client.ui.constants.AlertType;
|
|
|
|
import com.github.gwtbootstrap.client.ui.constants.ControlGroupType;
|
|
|
|
import com.google.gwt.core.client.GWT;
|
|
|
|
import com.google.gwt.dom.client.SelectElement;
|
|
|
|
import com.google.gwt.event.dom.client.ClickEvent;
|
|
|
|
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;
|
|
|
|
import com.google.gwt.user.client.ui.HTML;
|
|
|
|
import com.google.gwt.user.client.ui.Widget;
|
|
|
|
|
|
|
|
public class ManageProductWidget extends Composite{
|
|
|
|
|
2017-01-27 16:14:14 +01:00
|
|
|
private static GRSFManageWidgetServiceAsync service = GWT.create(GRSFManageWidgetService.class);
|
2017-01-26 18:21:33 +01:00
|
|
|
|
|
|
|
private static ManageProductWidgetUiBinder uiBinder = GWT
|
|
|
|
.create(ManageProductWidgetUiBinder.class);
|
|
|
|
|
|
|
|
interface ManageProductWidgetUiBinder extends
|
|
|
|
UiBinder<Widget, ManageProductWidget> {
|
|
|
|
}
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
Modal manageProductModal;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
AlertBlock infoBlock;
|
|
|
|
|
|
|
|
@UiField
|
2017-02-19 16:41:36 +01:00
|
|
|
TextArea titleTextArea;
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-08-01 18:00:19 +02:00
|
|
|
@UiField
|
|
|
|
TextBox shortNameTextBox;
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-19 16:41:36 +01:00
|
|
|
@UiField
|
|
|
|
TextBox productGrsfType;
|
2017-08-01 18:00:19 +02:00
|
|
|
|
|
|
|
@UiField
|
|
|
|
TextArea descriptionTextArea;
|
|
|
|
|
2017-08-01 18:18:29 +02:00
|
|
|
@UiField
|
|
|
|
CheckBox traceabilityFlag;
|
|
|
|
|
2017-08-01 18:00:19 +02:00
|
|
|
@UiField
|
|
|
|
TextBox GRSFNameTexBox;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
TextBox semanticIdentifierTextBox;
|
2017-02-19 16:41:36 +01:00
|
|
|
|
2017-08-01 18:00:19 +02:00
|
|
|
@UiField
|
|
|
|
TextBox sources;
|
2017-01-26 18:21:33 +01:00
|
|
|
|
|
|
|
@UiField
|
|
|
|
TextBox currentStatus;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
ListBox listBoxStatus;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
TextArea annotationArea;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
Button cancelButton;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
Button confirmButton;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
Icon loaderIcon;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
ControlGroup listBoxStatusGroup;
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-20 12:21:52 +01:00
|
|
|
@UiField
|
|
|
|
ControlGroup annotationAreaGroup;
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-20 12:21:52 +01:00
|
|
|
@UiField
|
2017-02-20 16:16:57 +01:00
|
|
|
ControlGroup productGrsfTypeGroup;
|
2017-01-26 18:21:33 +01:00
|
|
|
|
|
|
|
@UiField
|
|
|
|
Form formUpdate;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
Image loadingImage;
|
|
|
|
|
|
|
|
public static final String LOADING_IMAGE_URL = GWT.getModuleBaseURL() + "../images/loader.gif";
|
|
|
|
private final static List<GRSFStatus> STATUS = new ArrayList<GRSFStatus>(Arrays.asList(GRSFStatus.values()));
|
2017-02-16 18:32:19 +01:00
|
|
|
private final static String STATUS_UPDATE_SUCCESS = "The item has been correctly updated. Thanks for your collaboration!";
|
|
|
|
private final static String STATUS_UPDATE_ERROR = "Sorry, there was a problem while trying to update the status of this item";
|
2017-01-26 18:21:33 +01:00
|
|
|
protected static final String ERROR_ON_RETRIEVING_BEAN = "It seems there was a problem while contacting the service...";
|
2017-02-16 18:32:19 +01:00
|
|
|
protected static final String NO_GRSF_RECORD_BEAN = "This item is not a GRSF record, thus it cannot be managed";
|
|
|
|
|
2017-02-16 18:38:37 +01:00
|
|
|
protected static final String NO_ADMIN_ROLE = "Sorry but it seems you do not have the rights to manage items."
|
2017-08-01 18:00:19 +02:00
|
|
|
+ " You are suggested to contact the VRE Manager if something is wrong with this";
|
2017-01-26 18:21:33 +01:00
|
|
|
private ManageProductBean bean;
|
|
|
|
|
|
|
|
public ManageProductWidget(String productIdentifier) {
|
|
|
|
initWidget(uiBinder.createAndBindUi(this));
|
|
|
|
|
|
|
|
if(productIdentifier == null || productIdentifier.isEmpty()){
|
2017-02-16 18:32:19 +01:00
|
|
|
GWT.log("The received item identifier is null..");
|
2017-01-26 18:21:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-16 18:32:19 +01:00
|
|
|
GWT.log("item identifier is " + productIdentifier);
|
2017-01-26 18:21:33 +01:00
|
|
|
|
|
|
|
// start loader service
|
|
|
|
loadingImage.setUrl(LOADING_IMAGE_URL);
|
|
|
|
loadingImage.setVisible(true);
|
|
|
|
|
2017-02-16 18:32:19 +01:00
|
|
|
manageProductModal.show();
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-01-26 18:21:33 +01:00
|
|
|
// async request to fetch the product
|
|
|
|
retrieveProductBean(productIdentifier);
|
|
|
|
}
|
|
|
|
|
2017-02-16 18:32:19 +01:00
|
|
|
private void retrieveProductBean(final String productIdentifier) {
|
2017-01-26 18:21:33 +01:00
|
|
|
|
2017-02-16 18:32:19 +01:00
|
|
|
// check if it is an administrator
|
|
|
|
service.isAdminUser(new AsyncCallback<Boolean>() {
|
2017-01-26 18:21:33 +01:00
|
|
|
|
|
|
|
@Override
|
2017-02-16 18:32:19 +01:00
|
|
|
public void onSuccess(Boolean result) {
|
|
|
|
|
|
|
|
if(!result){
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-16 18:38:37 +01:00
|
|
|
showInfo(NO_ADMIN_ROLE, AlertType.ERROR);
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-16 18:32:19 +01:00
|
|
|
// hide the form and disable the send button
|
2017-01-26 18:21:33 +01:00
|
|
|
formUpdate.setVisible(false);
|
|
|
|
confirmButton.setEnabled(false);
|
2017-02-16 18:32:19 +01:00
|
|
|
loadingImage.setVisible(false);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
service.getProductBeanById(productIdentifier, new AsyncCallback<ManageProductBean>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSuccess(ManageProductBean result) {
|
|
|
|
|
|
|
|
if(result != null){
|
|
|
|
bean = result;
|
|
|
|
annotationArea.setText("");
|
|
|
|
infoBlock.setVisible(false);
|
2017-08-01 18:00:19 +02:00
|
|
|
|
2017-02-19 16:41:36 +01:00
|
|
|
titleTextArea.setText(bean.getItemTitle());
|
2017-08-01 18:00:19 +02:00
|
|
|
shortNameTextBox.setText(bean.getShortTitle());
|
2017-02-16 18:32:19 +01:00
|
|
|
currentStatus.setText(bean.getCurrentStatus().toString());
|
2017-02-19 16:41:36 +01:00
|
|
|
productGrsfType.setText(bean.getGrsfType());
|
2017-08-01 18:00:19 +02:00
|
|
|
GRSFNameTexBox.setText(bean.getGrsfName());
|
|
|
|
semanticIdentifierTextBox.setText(bean.getSemanticId());
|
|
|
|
sources.setText(bean.getSources());
|
2017-02-22 10:03:47 +01:00
|
|
|
descriptionTextArea.setText(bean.getDescription());
|
2017-08-01 18:18:29 +02:00
|
|
|
traceabilityFlag.setValue(bean.isTraceabilityFlag());
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-20 12:21:52 +01:00
|
|
|
// check if we need to show more
|
|
|
|
if(bean.getExtrasIfAvailable() != null && !bean.getExtrasIfAvailable().isEmpty())
|
2017-02-20 16:16:57 +01:00
|
|
|
addExtrasAfter(bean, productGrsfTypeGroup);
|
|
|
|
|
2017-02-16 18:32:19 +01:00
|
|
|
List<GRSFStatus> statusToShow = new ArrayList<GRSFStatus>(STATUS);
|
|
|
|
statusToShow.remove(bean.getCurrentStatus());
|
|
|
|
listBoxStatus.addItem("Select the new status");
|
|
|
|
listBoxStatus.getElement().<SelectElement>cast().getOptions().getItem(0).setDisabled(true);
|
|
|
|
for (GRSFStatus availableStatus : statusToShow) {
|
|
|
|
listBoxStatus.addItem(availableStatus.toString());
|
|
|
|
}
|
|
|
|
listBoxStatus.setSelectedIndex(0);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
showInfo(ERROR_ON_RETRIEVING_BEAN, AlertType.ERROR);
|
|
|
|
formUpdate.setVisible(false);
|
|
|
|
confirmButton.setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadingImage.setVisible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Throwable caught) {
|
|
|
|
|
|
|
|
if(caught instanceof NoGRSFRecordException)
|
|
|
|
showInfo(NO_GRSF_RECORD_BEAN, AlertType.WARNING);
|
|
|
|
else
|
|
|
|
showInfo(caught.getMessage(), AlertType.ERROR);
|
|
|
|
|
|
|
|
// hide the form and disable the send button
|
|
|
|
formUpdate.setVisible(false);
|
|
|
|
confirmButton.setEnabled(false);
|
|
|
|
loadingImage.setVisible(false);
|
|
|
|
}
|
|
|
|
});
|
2017-01-26 18:21:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Throwable caught) {
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-16 18:38:37 +01:00
|
|
|
showInfo(NO_ADMIN_ROLE, AlertType.ERROR);
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-01-26 18:21:33 +01:00
|
|
|
// hide the form and disable the send button
|
|
|
|
formUpdate.setVisible(false);
|
|
|
|
confirmButton.setEnabled(false);
|
|
|
|
loadingImage.setVisible(false);
|
2017-02-16 18:32:19 +01:00
|
|
|
|
2017-01-26 18:21:33 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-20 12:21:52 +01:00
|
|
|
/**
|
|
|
|
* Add extras if available after controlGroupBefore
|
|
|
|
* @param bean
|
|
|
|
* @param productTypeGroup
|
|
|
|
*/
|
|
|
|
private void addExtrasAfter(ManageProductBean bean,
|
|
|
|
ControlGroup controlGroupBefore) {
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-20 12:21:52 +01:00
|
|
|
int index = formUpdate.getWidgetIndex(controlGroupBefore);
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-20 12:21:52 +01:00
|
|
|
Map<String, String> extras = bean.getExtrasIfAvailable();
|
|
|
|
Iterator<Entry<String, String>> iterator = extras.entrySet().iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
Map.Entry<java.lang.String, java.lang.String> entry = (Map.Entry<java.lang.String, java.lang.String>) iterator
|
|
|
|
.next();
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-20 12:21:52 +01:00
|
|
|
formUpdate.insert(new FormEntryModel(entry.getKey(), entry.getValue()), index);
|
|
|
|
index++;
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-20 12:21:52 +01:00
|
|
|
}
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-20 12:21:52 +01:00
|
|
|
}
|
2017-01-26 18:21:33 +01:00
|
|
|
|
|
|
|
@UiHandler("cancelButton")
|
|
|
|
void onCancelButton(ClickEvent ce){
|
|
|
|
manageProductModal.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
@UiHandler("confirmButton")
|
|
|
|
void onSaveButton(ClickEvent ce){
|
|
|
|
|
|
|
|
if(bean == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
listBoxStatusGroup.setType(ControlGroupType.NONE);
|
|
|
|
|
|
|
|
if(listBoxStatus.getSelectedIndex() <= 0){
|
|
|
|
listBoxStatusGroup.setType(ControlGroupType.ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-20 12:21:52 +01:00
|
|
|
annotationAreaGroup.setType(ControlGroupType.NONE);
|
2017-02-20 16:16:57 +01:00
|
|
|
|
2017-02-20 12:21:52 +01:00
|
|
|
if(annotationArea.getText() == null || annotationArea.getText().isEmpty()){
|
|
|
|
annotationArea.setPlaceholder("An annotation message to send along the update (mandatory)");
|
|
|
|
annotationAreaGroup.setType(ControlGroupType.ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-26 18:21:33 +01:00
|
|
|
|
|
|
|
manageProductModal.setCloseVisible(false);
|
|
|
|
cancelButton.setEnabled(false);
|
|
|
|
confirmButton.setEnabled(false);
|
|
|
|
loaderIcon.setVisible(true);
|
|
|
|
|
|
|
|
// set new values
|
|
|
|
bean.setAnnotation(new HTML(annotationArea.getText().trim()).getText());
|
|
|
|
bean.setNewStatus(GRSFStatus.fromString(listBoxStatus.getSelectedItemText()));
|
|
|
|
|
|
|
|
service.notifyProductUpdate(bean, new AsyncCallback<String>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSuccess(String result) {
|
|
|
|
|
|
|
|
if(result == null){
|
|
|
|
showInfo(STATUS_UPDATE_SUCCESS, AlertType.SUCCESS);
|
|
|
|
confirmButton.removeFromParent();
|
|
|
|
formUpdate.setVisible(false);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
showInfo(STATUS_UPDATE_ERROR + "(" + result + ")", AlertType.ERROR);
|
|
|
|
confirmButton.setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
manageProductModal.setCloseVisible(true);
|
|
|
|
cancelButton.setEnabled(true);
|
|
|
|
loaderIcon.setVisible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Throwable caught) {
|
|
|
|
|
|
|
|
manageProductModal.setCloseVisible(true);
|
|
|
|
cancelButton.setEnabled(true);
|
|
|
|
confirmButton.setEnabled(true);
|
|
|
|
loaderIcon.setVisible(false);
|
|
|
|
showInfo(STATUS_UPDATE_ERROR, AlertType.ERROR);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show information
|
|
|
|
* @param statusUpdateError
|
|
|
|
*/
|
|
|
|
protected void showInfo(String statusUpdateError, AlertType type) {
|
|
|
|
|
|
|
|
infoBlock.setText(statusUpdateError);
|
|
|
|
infoBlock.setType(type);
|
|
|
|
infoBlock.setVisible(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|