workspace-tree-widget/src/main/java/org/gcube/portlets/user/workspace/client/view/windows/DialogShareableLink.java

468 lines
13 KiB
Java

package org.gcube.portlets.user.workspace.client.view.windows;
import org.gcube.portlets.user.workspace.client.AppControllerExplorer;
import org.gcube.portlets.user.workspace.client.ConstantsExplorer;
import org.gcube.portlets.user.workspace.client.ConstantsExplorer.WsPortletInitOperation;
import org.gcube.portlets.user.workspace.client.event.RefreshFolderEvent;
import org.gcube.portlets.user.workspace.client.event.SessionExpiredEvent;
import org.gcube.portlets.user.workspace.client.model.FileModel;
import org.gcube.portlets.user.workspace.shared.PublicLink;
import org.gcube.portlets.user.workspace.shared.SessionExpiredException;
import org.gcube.portlets.widgets.switchbutton.client.SwitchButton;
import org.gcube.portlets.widgets.workspacesharingwidget.client.rpc.WorkspaceSharingServiceAsync;
import org.gcube.portlets.widgets.workspacesharingwidget.shared.AllowAccess;
import com.extjs.gxt.ui.client.util.Format;
import com.github.gwtbootstrap.client.ui.Alert;
import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.ControlGroup;
import com.github.gwtbootstrap.client.ui.Fieldset;
import com.github.gwtbootstrap.client.ui.Modal;
import com.github.gwtbootstrap.client.ui.ModalFooter;
import com.github.gwtbootstrap.client.ui.Row;
import com.github.gwtbootstrap.client.ui.TextBox;
import com.github.gwtbootstrap.client.ui.Well;
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.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
/**
* The Class DialogShareableLink.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Sep 19, 2019
*/
public class DialogShareableLink extends Composite {
private static DialogShareableLinkUiBinder uiBinder = GWT.create(DialogShareableLinkUiBinder.class);
private Modal modalBox = new Modal();
@UiField
ControlGroup cgPublicLink;
// @UiField
// ControlGroup cgRemovePublicLink;
@UiField
ControlGroup cgPrivateLink;
@UiField
TextBox textPublicLink;
@UiField
TextBox textPrivateLink;
// @UiField
// Button removePublicLink;
@UiField
Alert errorAlert;
@UiField
Alert actionAlert;
@UiField
Well alertFilePublicLink;
@UiField
Well alertFolderPublicLink;
@UiField
Fieldset fieldSet;
@UiField
SwitchButton switchButton;
@UiField
Row filedEnableDisableSharing;
@UiField
Label labelLinkSharing;
@UiField
HTMLPanel panelFieldsContainer;
private FileModel fileItem;
private String fileVersion;
private boolean itemIsPublicStatus;
/**
* The Interface DialogShareableLinkUiBinder.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Sep 19, 2019
*/
interface DialogShareableLinkUiBinder extends UiBinder<Widget, DialogShareableLink> {
}
/**
* Instantiates a new dialog shareable link.
*
* @param item the item
* @param version the version
*/
public DialogShareableLink(FileModel item, String version) {
initWidget(uiBinder.createAndBindUi(this));
this.fileItem = item;
this.fileVersion = version;
this.actionAlert.setAnimation(true);
showShareableLinkOptions(item, version);
//getElement().setClassName("gwt-DialogBoxNew");
modalBox.setTitle("Get Shareable Link to: "+Format.ellipse(item.getName(), 15));
ModalFooter modalFooter = new ModalFooter();
final Button buttClose = new Button("Close");
modalFooter.add(buttClose);
switchButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
@Override
public void onValueChange(ValueChangeEvent<Boolean> event) {
GWT.log("Switch to: "+event.getValue());
acessToFolderLink(fileItem, event.getValue());
}
});
buttClose.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
modalBox.hide();
}
});
modalBox.add(this);
modalBox.add(modalFooter);
modalBox.show();
}
/**
* Show shareable link options.
*
* @param item the item
* @param version the version
*/
public void showShareableLinkOptions(FileModel item, String version) {
//cgRemovePublicLink.setVisible(false);
cgPrivateLink.setVisible(false);
cgPublicLink.setVisible(true);
alertFilePublicLink.setVisible(false);
alertFolderPublicLink.setVisible(false);
filedEnableDisableSharing.setVisible(false);
panelFieldsContainer.setVisible(false);
showMessage("", false);
this.itemIsPublicStatus = item.isPublic();
if(item.isDirectory()) { //CASE FOLDER - MANAGING AS PUBLIC LINK TO FOLDER
switchButton.setValue(item.isPublic());
//IT IS PRIVATE LINK
if(item.isPublic()) { //CASE PUBLIC FOLDER
alertFolderPublicLink.setVisible(true);
//cgRemovePublicLink.setVisible(true);
filedEnableDisableSharing.setVisible(true);
labelLinkSharing.setText("Anyone with the Public Link can access to the content");
panelFieldsContainer.setVisible(true);
acessToFolderLink(item, true);
}
if(item.isShared()) { //CASE SHARED FOLDER
panelFieldsContainer.setVisible(true);
cgPrivateLink.setVisible(true);
loadAndShowPrivateLink(item, textPrivateLink);
if(!item.isPublic()) { //THE FOLDER IS NOT PUBLIC
cgPublicLink.setVisible(false);
filedEnableDisableSharing.setVisible(true);
labelLinkSharing.setText("");
//labelLinkSharing.setText("Only your cowokers (sharing members) can access to the content");
}
}
//THE FOLDER IS PRIVATE - NO SHARED AND NO PUBLIC
if(!item.isPublic() && !item.isShared()) {
filedEnableDisableSharing.setVisible(true);
alertFolderPublicLink.setVisible(true);
labelLinkSharing.setText("The folder is private, not shared");
}
}else { //CASE FILE - MANAGING AS PUBLIC LINK TO FILE
panelFieldsContainer.setVisible(true);
alertFilePublicLink.setVisible(true);
if(!item.isPublic()) { //THE FILE IS PRIVATE
if(version!=null && !version.isEmpty()) {
showPublicLinkForFileItemIdToVersion(item, textPublicLink, version);
}else {
loadAndShowPublicLinkForItem(item, textPublicLink);
}
}else {
//???
}
}
}
/**
* Load and show private link.
*
* @param item the item
* @param toTextBox the to text box
*/
private void loadAndShowPrivateLink(FileModel item, TextBox toTextBox) {
String currentUrl = portalURL();
int lastChar = currentUrl.lastIndexOf("?");
currentUrl = lastChar > -1 ? currentUrl.substring(0, lastChar) : currentUrl;
String shareLinkUrl = currentUrl + "?" + ConstantsExplorer.GET_ITEMID_PARAMETER + "=" + item.getIdentifier();
shareLinkUrl += "&" + ConstantsExplorer.GET_OPERATION_PARAMETER + "=" + WsPortletInitOperation.gotofolder;
toTextBox.setText(shareLinkUrl);
}
/**
* Portal url.
*
* @return the string
*/
public static native String portalURL()/*-{
return $wnd.location.href;
}-*/;
/**
* Load and show public link for item.
*
* @param item the item
* @param toTextBox the to text box
*/
private void loadAndShowPublicLinkForItem(FileModel item, final TextBox toTextBox) {
AppControllerExplorer.rpcWorkspaceService.getPublicLinkForFileItemId(item.getIdentifier(), true,
new AsyncCallback<PublicLink>() {
@Override
public void onSuccess(PublicLink publicLink) {
String toURL = publicLink.getShortURL() != null && !publicLink.getShortURL().isEmpty()
? publicLink.getShortURL()
: publicLink.getCompleteURL();
toTextBox.setValue(toURL);
}
@Override
public void onFailure(Throwable caught) {
if (caught instanceof SessionExpiredException) {
GWT.log("Session expired");
AppControllerExplorer.getEventBus().fireEvent(new SessionExpiredEvent());
return;
}
//new MessageBoxAlert("Error", caught.getMessage(), null);
//toTextBox.setText(caught.getMessage());
showError(caught.getMessage());
}
});
}
/**
* Show public link for file item id to version.
*
* @param item the item
* @param toTextBox the to text box
* @param version the version
*/
private void showPublicLinkForFileItemIdToVersion(FileModel item, final TextBox toTextBox, final String version) {
AppControllerExplorer.rpcWorkspaceService.getPublicLinkForFileItemIdToVersion(item.getIdentifier(),
version, true, new AsyncCallback<PublicLink>() {
@Override
public void onSuccess(PublicLink publicLink) {
String toURL = getValidURL(publicLink);
toTextBox.setValue(toURL);
}
@Override
public void onFailure(Throwable caught) {
if (caught instanceof SessionExpiredException) {
GWT.log("Session expired");
AppControllerExplorer.getEventBus().fireEvent(new SessionExpiredEvent());
return;
}
//new MessageBoxAlert("Error", caught.getMessage(), null);
showError(caught.getMessage());
}
});
}
/**
* Show error.
*
* @param msg the msg
*/
private void showError(String msg) {
errorAlert.setVisible(true);
errorAlert.setText(msg);
}
/**
* Gets the valid URL.
*
* @param publicLink the public link
* @return the valid URL
*/
private String getValidURL(PublicLink publicLink) {
if(publicLink==null)
return "Error on getting a valid shareble link";
return publicLink.getShortURL() != null && !publicLink.getShortURL().isEmpty()
? publicLink.getShortURL()
: publicLink.getCompleteURL();
}
/**
* Acess to folder link.
*
* @param item the item
* @param setAsPublic the set as public
*/
private void acessToFolderLink(final FileModel item, final boolean setAsPublic) {
panelFieldsContainer.setVisible(false);
WorkspaceSharingServiceAsync.INSTANCE.accessToFolderLink(item.getIdentifier(),
new AsyncCallback<AllowAccess>() {
@Override
public void onFailure(Throwable caught) {
panelFieldsContainer.setVisible(false);
if (caught instanceof SessionExpiredException) {
GWT.log("Session expired");
AppControllerExplorer.getEventBus().fireEvent(new SessionExpiredEvent());
return;
}
showError(caught.getMessage());
}
@Override
public void onSuccess(AllowAccess result) {
panelFieldsContainer.setVisible(true);
GWT.log("AllowAccess? " + result);
if (result.getAccessGranted()) {
String msg = setAsPublic ? "Getting" : "Removing";
msg = msg + " Public Link... permissions granted";
showMessage(msg, true);
allowAccessToFolderLink(item.getIdentifier(), setAsPublic);
} else {
showError("Permission Denied!" + " "+result.getAccessAllowDenyMotivation());
}
}
});
}
/**
* Show message.
*
* @param msg the msg
* @param visible the visible
*/
private void showMessage(String msg, boolean visible) {
actionAlert.setVisible(visible);
actionAlert.setText(msg==null?"":msg);
}
/**
* Allow access to folder link.
*
* @param folderId
* the folder id
* @param setIsPublic
* the set is public
*/
protected void allowAccessToFolderLink(String folderId, final boolean setIsPublic) {
AppControllerExplorer.rpcWorkspaceService.markFolderAsPublicForFolderItemId(folderId, setIsPublic,
new AsyncCallback<PublicLink>() {
@Override
public void onSuccess(PublicLink publicLink) {
if (!setIsPublic && publicLink == null) {
String msg = "Public Link to the folder '" + fileItem.getName() + "' removed correctly";
showMessage(msg, true);
AppControllerExplorer.getEventBus().fireEvent(new RefreshFolderEvent(fileItem.getParentFileModel(), true, false, false));
//REFRESHING ONLY ON CHANGING STATUS
if(itemIsPublicStatus) {
//HERE THE PREVIOUS STATUS WAS ISPUBLIC = TRUE
fileItem.setIsPublic(false);
showShareableLinkOptions(fileItem,fileVersion);
}
return;
}
textPublicLink.setValue(publicLink.getCompleteURL());
// txtShortURL.setValue(publicLink.getShortURL());
// selectTxt();
Timer t = new Timer() {
@Override
public void run() {
GWT.log("Runing refresh after wait: " + AppControllerExplorer.delayTime);
AppControllerExplorer.getEventBus()
.fireEvent(new RefreshFolderEvent(fileItem.getParentFileModel(), true, false, false));
}
};
t.schedule(AppControllerExplorer.delayTime);
showMessage("", false);
//REFRESHING ONLY ON CHANGING STATUS
if(!itemIsPublicStatus) {
//HERE THE PREVIOUS STATUS WAS ISPUBLIC = FALSE
fileItem.setIsPublic(true);
showShareableLinkOptions(fileItem,fileVersion);
}
}
@Override
public void onFailure(Throwable caught) {
panelFieldsContainer.setVisible(false);
if (caught instanceof SessionExpiredException) {
GWT.log("Session expired");
AppControllerExplorer.getEventBus().fireEvent(new SessionExpiredEvent());
return;
}
showError(caught.getMessage());
}
});
}
}