2017-11-09 22:11:10 +01:00
|
|
|
package org.gcube.datacatalogue.grsf_manage_widget.client.view.subwidgets;
|
|
|
|
|
2017-11-23 20:04:47 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-12-07 12:37:21 +01:00
|
|
|
import org.gcube.datacatalogue.common.enums.Product_Type;
|
2018-01-23 18:25:16 +01:00
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.client.GRSFManageWidgetServiceAsync;
|
2017-12-12 18:54:45 +01:00
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.shared.ConnectedBean;
|
2017-11-23 20:04:47 +01:00
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.shared.ManageProductBean;
|
|
|
|
|
|
|
|
import com.github.gwtbootstrap.client.ui.Button;
|
2017-12-13 18:34:45 +01:00
|
|
|
import com.github.gwtbootstrap.client.ui.CheckBox;
|
2018-01-23 18:25:16 +01:00
|
|
|
import com.github.gwtbootstrap.client.ui.Icon;
|
2017-11-23 20:04:47 +01:00
|
|
|
import com.github.gwtbootstrap.client.ui.Paragraph;
|
|
|
|
import com.github.gwtbootstrap.client.ui.TextBox;
|
|
|
|
import com.github.gwtbootstrap.client.ui.constants.IconType;
|
2017-11-09 22:11:10 +01:00
|
|
|
import com.google.gwt.core.client.GWT;
|
2017-11-23 20:04:47 +01:00
|
|
|
import com.google.gwt.dom.client.Style.Float;
|
2017-12-13 18:34:45 +01:00
|
|
|
import com.google.gwt.dom.client.Style.FontWeight;
|
2017-11-23 20:04:47 +01:00
|
|
|
import com.google.gwt.dom.client.Style.Unit;
|
|
|
|
import com.google.gwt.event.dom.client.ChangeEvent;
|
|
|
|
import com.google.gwt.event.dom.client.ChangeHandler;
|
|
|
|
import com.google.gwt.event.dom.client.ClickEvent;
|
|
|
|
import com.google.gwt.event.dom.client.ClickHandler;
|
2018-01-23 18:25:16 +01:00
|
|
|
import com.google.gwt.event.dom.client.KeyCodes;
|
2017-11-23 20:04:47 +01:00
|
|
|
import com.google.gwt.event.dom.client.KeyPressEvent;
|
|
|
|
import com.google.gwt.event.dom.client.KeyPressHandler;
|
2017-11-09 22:11:10 +01:00
|
|
|
import com.google.gwt.uibinder.client.UiBinder;
|
2017-11-23 20:04:47 +01:00
|
|
|
import com.google.gwt.uibinder.client.UiField;
|
2018-01-23 18:25:16 +01:00
|
|
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
2017-12-13 18:34:45 +01:00
|
|
|
import com.google.gwt.user.client.ui.Anchor;
|
2017-11-09 22:11:10 +01:00
|
|
|
import com.google.gwt.user.client.ui.Composite;
|
2017-11-23 20:04:47 +01:00
|
|
|
import com.google.gwt.user.client.ui.HTML;
|
|
|
|
import com.google.gwt.user.client.ui.HorizontalPanel;
|
|
|
|
import com.google.gwt.user.client.ui.VerticalPanel;
|
2017-11-09 22:11:10 +01:00
|
|
|
import com.google.gwt.user.client.ui.Widget;
|
|
|
|
|
|
|
|
public class ConnectToWidget extends Composite{
|
|
|
|
|
|
|
|
private static ConnectToWidgetUiBinder uiBinder = GWT
|
|
|
|
.create(ConnectToWidgetUiBinder.class);
|
|
|
|
|
2018-01-23 18:25:16 +01:00
|
|
|
private static final String REGEX_UUID = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
|
|
|
|
|
2017-11-09 22:11:10 +01:00
|
|
|
interface ConnectToWidgetUiBinder extends UiBinder<Widget, ConnectToWidget> {
|
|
|
|
}
|
|
|
|
|
2017-11-23 20:04:47 +01:00
|
|
|
@UiField
|
|
|
|
VerticalPanel connectPanel;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
Button suggestRecord;
|
|
|
|
|
2017-12-07 12:37:21 +01:00
|
|
|
//private GRSFManageWidgetServiceAsync service;
|
|
|
|
private List<Tuple> connectList = new ArrayList<Tuple>(0);
|
2017-11-23 20:04:47 +01:00
|
|
|
|
2017-12-13 18:34:45 +01:00
|
|
|
private List<ConnectedBean> connectedRecords = null;
|
|
|
|
|
2018-01-23 18:25:16 +01:00
|
|
|
private GRSFManageWidgetServiceAsync service;
|
|
|
|
|
|
|
|
public ConnectToWidget(final ManageProductBean bean, GRSFManageWidgetServiceAsync service) {
|
2017-11-09 22:11:10 +01:00
|
|
|
initWidget(uiBinder.createAndBindUi(this));
|
2018-01-23 18:25:16 +01:00
|
|
|
|
|
|
|
this.service = service;
|
2017-12-07 12:37:21 +01:00
|
|
|
|
2017-12-13 18:34:45 +01:00
|
|
|
// get already connected beans, if any
|
|
|
|
connectedRecords = bean.getConnectTo();
|
|
|
|
|
|
|
|
if(connectedRecords != null){
|
|
|
|
connectPanel.add(new HTML("<hr style=\"width:100%;\"/>"));
|
|
|
|
for (ConnectedBean connected : connectedRecords) {
|
|
|
|
Widget widget = buildWidgetForConnectedRecords(connected);
|
|
|
|
connectPanel.add(widget);
|
|
|
|
connectPanel.add(new HTML("<hr style=\"width:100%;\"/>"));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-12-07 12:37:21 +01:00
|
|
|
//this.service = service;
|
|
|
|
String acceptedDomain = bean.getGrsfDomain().equalsIgnoreCase(Product_Type.STOCK.getOrigName()) ? Product_Type.FISHERY.getOrigName() : Product_Type.STOCK.getOrigName(); // inverted
|
2017-11-23 20:04:47 +01:00
|
|
|
suggestRecord.setTitle("Connect this " + bean.getGrsfDomain() + " record to a " + acceptedDomain + " record ");
|
|
|
|
suggestRecord.setIcon(IconType.PLUS_SIGN);
|
|
|
|
suggestRecord.getElement().getStyle().setFloat(Float.RIGHT);
|
|
|
|
|
|
|
|
// add handler
|
|
|
|
suggestRecord.addClickHandler(new ClickHandler() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(ClickEvent arg0) {
|
|
|
|
|
2017-12-12 18:54:45 +01:00
|
|
|
ConnectedBean cb = new ConnectedBean();
|
2018-01-23 18:25:16 +01:00
|
|
|
// cb.setExtra(true);
|
2017-11-23 20:04:47 +01:00
|
|
|
cb.setSourceDomain(bean.getGrsfDomain());
|
2017-12-07 12:37:21 +01:00
|
|
|
Tuple t = buildWidgetConnect(cb);
|
|
|
|
connectList.add(t);
|
|
|
|
connectPanel.add(t.getW());
|
2017-11-23 20:04:47 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-11-09 22:11:10 +01:00
|
|
|
}
|
2017-12-07 12:37:21 +01:00
|
|
|
|
2017-12-13 18:34:45 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param similarGRSFRecord
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static Widget buildWidgetForConnectedRecords(final ConnectedBean connected){
|
|
|
|
|
|
|
|
HorizontalPanel hp = new HorizontalPanel();
|
|
|
|
hp.setWidth("95%");
|
|
|
|
VerticalPanel leftPanel = new VerticalPanel();
|
|
|
|
leftPanel.setWidth("80%");
|
|
|
|
leftPanel.getElement().getStyle().setMarginLeft(20, Unit.PX);
|
|
|
|
Paragraph name = new Paragraph("Record name: " + (connected.getDestShortName() != null? connected.getDestShortName() : "Unavailable"));
|
|
|
|
leftPanel.add(name);
|
|
|
|
Paragraph semanticIdentifier = new Paragraph("Semantic Identifier: " +
|
|
|
|
connected.getDestSemanticIdentifier());
|
|
|
|
leftPanel.add(semanticIdentifier);
|
|
|
|
Paragraph identifier = new Paragraph("Record UUID: " +
|
|
|
|
connected.getDestKnowledgeBaseId());
|
|
|
|
leftPanel.add(identifier);
|
|
|
|
|
|
|
|
Anchor view = new Anchor();
|
|
|
|
view.setHref(connected.getUrl());
|
|
|
|
view.setText("View");
|
|
|
|
view.setTitle("Click to view the similar record");
|
|
|
|
view.setTarget("_blank");
|
|
|
|
view.getElement().getStyle().setFontWeight(FontWeight.BOLD);
|
|
|
|
leftPanel.add(view);
|
|
|
|
|
|
|
|
VerticalPanel rightPanel = new VerticalPanel();
|
|
|
|
rightPanel.setWidth("20%");
|
|
|
|
|
|
|
|
final CheckBox removeExtra = new CheckBox("Remove");
|
2018-01-23 18:25:16 +01:00
|
|
|
removeExtra.setTitle("Remove this record among the connected ones");
|
2017-12-13 18:34:45 +01:00
|
|
|
removeExtra.addClickHandler(new ClickHandler() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(ClickEvent arg0) {
|
|
|
|
connected.setToBeKept(!removeExtra.getValue());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
rightPanel.getElement().getStyle().setFloat(Float.RIGHT);
|
|
|
|
rightPanel.add(removeExtra);
|
|
|
|
hp.add(leftPanel);
|
|
|
|
hp.add(rightPanel);
|
|
|
|
hp.getElement().getStyle().setPadding(10, Unit.PX);
|
|
|
|
hp.getElement().getStyle().setMarginBottom(10, Unit.PX);
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
2017-11-23 20:04:47 +01:00
|
|
|
/**
|
|
|
|
* Builds up a widget for connecting records.
|
|
|
|
* @param w the widget
|
|
|
|
* @param cb the connectBean.
|
|
|
|
*/
|
2017-12-12 18:54:45 +01:00
|
|
|
private Tuple buildWidgetConnect(final ConnectedBean cb){
|
2017-11-09 22:11:10 +01:00
|
|
|
|
2017-11-23 20:04:47 +01:00
|
|
|
VerticalPanel main = new VerticalPanel();
|
2018-01-23 18:25:16 +01:00
|
|
|
main.setWidth("100%");
|
2017-11-23 20:04:47 +01:00
|
|
|
HorizontalPanel hp = new HorizontalPanel();
|
|
|
|
hp.setWidth("100%");
|
|
|
|
|
|
|
|
VerticalPanel vpLeft = new VerticalPanel();
|
|
|
|
vpLeft.getElement().getStyle().setMarginLeft(15, Unit.PX);
|
|
|
|
vpLeft.setWidth("80%");
|
2017-12-13 18:34:45 +01:00
|
|
|
Paragraph semanticIdentifier = new Paragraph("Identifier (UUID):");
|
2017-11-23 20:04:47 +01:00
|
|
|
final TextBox box = new TextBox();
|
2018-01-23 18:25:16 +01:00
|
|
|
final Icon icon = new Icon(IconType.OK_SIGN);
|
|
|
|
final Anchor view = new Anchor();
|
|
|
|
view.setText("View");
|
|
|
|
view.setTitle("Click to inspect the record");
|
|
|
|
view.setTarget("_blank");
|
|
|
|
view.getElement().getStyle().setFontWeight(FontWeight.BOLD);
|
|
|
|
view.setVisible(false);
|
|
|
|
icon.setVisible(false);
|
2017-11-23 20:04:47 +01:00
|
|
|
box.addKeyPressHandler(new KeyPressHandler() {
|
2017-12-07 12:37:21 +01:00
|
|
|
|
2017-11-23 20:04:47 +01:00
|
|
|
@Override
|
|
|
|
public void onKeyPress(KeyPressEvent event) {
|
2018-01-23 18:25:16 +01:00
|
|
|
GWT.log("onKeyPress " + event.getNativeEvent().getKeyCode());
|
|
|
|
if(!(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_BACKSPACE || event.getNativeEvent().getKeyCode() == KeyCodes.KEY_DELETE))
|
|
|
|
validateUUID(box, cb, icon, view);
|
2017-11-23 20:04:47 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
box.addChangeHandler(new ChangeHandler() {
|
2017-12-07 12:37:21 +01:00
|
|
|
|
2017-11-23 20:04:47 +01:00
|
|
|
@Override
|
|
|
|
public void onChange(ChangeEvent event) {
|
2018-01-23 18:25:16 +01:00
|
|
|
GWT.log("onChange");
|
|
|
|
validateUUID(box, cb, icon, view);
|
2017-11-23 20:04:47 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
box.setWidth("512px");
|
2018-01-23 18:25:16 +01:00
|
|
|
box.setPlaceholder("Insert the Identifier (UUID) of the record to connect and press ENTER");
|
2017-11-23 20:04:47 +01:00
|
|
|
|
|
|
|
vpLeft.add(semanticIdentifier);
|
|
|
|
vpLeft.add(box);
|
|
|
|
|
|
|
|
VerticalPanel vpRight = new VerticalPanel();
|
|
|
|
vpRight.setWidth("20%");
|
|
|
|
|
|
|
|
Button removeExtra = new Button();
|
|
|
|
removeExtra.setIcon(IconType.MINUS);
|
|
|
|
removeExtra.setTitle("Remove this connection");
|
|
|
|
removeExtra.addClickHandler(new ClickHandler() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(ClickEvent arg0) {
|
|
|
|
|
|
|
|
// remove this object from the pairs list
|
2017-12-07 12:37:21 +01:00
|
|
|
Iterator<Tuple> iterator = connectList.iterator();
|
2017-11-23 20:04:47 +01:00
|
|
|
while (iterator.hasNext()) {
|
2017-12-07 12:37:21 +01:00
|
|
|
Tuple pair = (Tuple) iterator
|
2017-11-23 20:04:47 +01:00
|
|
|
.next();
|
|
|
|
if(pair.getO().equals(cb)){
|
|
|
|
pair.getW().removeFromParent();
|
|
|
|
iterator.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
vpRight.getElement().getStyle().setFloat(Float.RIGHT);
|
|
|
|
vpRight.add(removeExtra);
|
|
|
|
hp.add(vpLeft);
|
|
|
|
hp.add(vpRight);
|
|
|
|
HTML separator = new HTML("<hr style=\"width:100%;\"/>");
|
|
|
|
connectPanel.add(separator);
|
|
|
|
main.add(hp);
|
|
|
|
main.add(separator);
|
2017-12-07 12:37:21 +01:00
|
|
|
return new Tuple(cb, main, box);
|
2017-11-23 20:04:47 +01:00
|
|
|
}
|
2018-01-23 18:25:16 +01:00
|
|
|
|
|
|
|
protected void validateUUID(final TextBox box, final ConnectedBean c, final Icon icon, final Anchor view) {
|
|
|
|
|
|
|
|
final String currentText = box.getText().trim();
|
|
|
|
|
|
|
|
if(currentText == null || currentText.isEmpty()){
|
|
|
|
icon.setVisible(false);
|
|
|
|
view.setVisible(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!currentText.matches(REGEX_UUID)){
|
|
|
|
icon.setType(IconType.BAN_CIRCLE);
|
|
|
|
icon.setTitle("Not a valid UUID");
|
|
|
|
icon.setVisible(true);
|
|
|
|
view.setVisible(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// else check at server side if it exists
|
|
|
|
GWT.log("Text changed to " + currentText);
|
|
|
|
box.setEnabled(false);
|
|
|
|
icon.setIcon(IconType.ROTATE_RIGHT);
|
|
|
|
icon.setSpin(true);
|
|
|
|
|
|
|
|
service.checkIdentifierExists(currentText, new AsyncCallback<String>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSuccess(String result) {
|
|
|
|
|
|
|
|
if(result != null){
|
|
|
|
c.setDestKnowledgeBaseId(currentText);
|
|
|
|
icon.setType(IconType.OK_CIRCLE);
|
|
|
|
icon.setSpin(false);
|
|
|
|
icon.setTitle("");
|
|
|
|
icon.setVisible(true);
|
|
|
|
view.setHref(result);
|
|
|
|
view.setVisible(true);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
icon.setType(IconType.BAN_CIRCLE);
|
|
|
|
icon.setTitle("Not a valid UUID");
|
|
|
|
icon.setSpin(false);
|
|
|
|
icon.setVisible(true);
|
|
|
|
view.setVisible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
box.setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Throwable caught) {
|
|
|
|
box.setEnabled(true);
|
|
|
|
icon.setSpin(false);
|
|
|
|
view.setVisible(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2017-11-23 20:04:47 +01:00
|
|
|
|
2017-12-07 12:37:21 +01:00
|
|
|
/**
|
|
|
|
* Get the list of records to connect with this one
|
|
|
|
* @return
|
|
|
|
*/
|
2017-12-12 18:54:45 +01:00
|
|
|
public List<ConnectedBean> getConnectList() {
|
2017-12-07 12:37:21 +01:00
|
|
|
|
2017-12-13 18:34:45 +01:00
|
|
|
if(connectedRecords == null)
|
|
|
|
connectedRecords = new ArrayList<ConnectedBean>(0);
|
2017-12-07 12:37:21 +01:00
|
|
|
|
|
|
|
for (Tuple p : connectList) {
|
2017-12-13 18:34:45 +01:00
|
|
|
String suggestedIdentifier = ((ConnectedBean)p.getO()).getDestKnowledgeBaseId();
|
2017-12-07 12:37:21 +01:00
|
|
|
if(suggestedIdentifier == null || suggestedIdentifier.isEmpty())
|
|
|
|
continue;
|
2018-01-23 18:25:16 +01:00
|
|
|
else{
|
|
|
|
ConnectedBean connectedRecord = (ConnectedBean) p.getO();
|
|
|
|
connectedRecord.setToBeKept(true);
|
2017-12-13 18:34:45 +01:00
|
|
|
connectedRecords.add((ConnectedBean) p.getO());
|
2018-01-23 18:25:16 +01:00
|
|
|
}
|
2017-11-23 20:04:47 +01:00
|
|
|
}
|
2017-12-07 12:37:21 +01:00
|
|
|
|
2017-12-13 18:34:45 +01:00
|
|
|
return connectedRecords;
|
2017-11-23 20:04:47 +01:00
|
|
|
}
|
2017-11-09 22:11:10 +01:00
|
|
|
|
|
|
|
}
|