387 lines
11 KiB
Java
387 lines
11 KiB
Java
package org.gcube.portlets.widgets.workspaceuploader.client.uploader;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import org.gcube.portlets.widgets.workspaceuploader.client.ClosableDialog;
|
|
import org.gcube.portlets.widgets.workspaceuploader.client.ConstantsWorkspaceUploader;
|
|
import org.gcube.portlets.widgets.workspaceuploader.client.DialogResult;
|
|
import org.gcube.portlets.widgets.workspaceuploader.client.WorkspaceUploaderServiceAsync;
|
|
import org.gcube.portlets.widgets.workspaceuploader.shared.HandlerResultMessage;
|
|
import org.gcube.portlets.widgets.workspaceuploader.shared.WorkspaceUploaderItem;
|
|
|
|
import com.google.gwt.core.client.GWT;
|
|
import com.google.gwt.dom.client.Style.Unit;
|
|
import com.google.gwt.event.dom.client.ClickEvent;
|
|
import com.google.gwt.event.dom.client.ClickHandler;
|
|
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.Anchor;
|
|
import com.google.gwt.user.client.ui.Button;
|
|
import com.google.gwt.user.client.ui.FileUpload;
|
|
import com.google.gwt.user.client.ui.FormPanel;
|
|
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
|
|
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
|
|
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
|
|
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
|
|
import com.google.gwt.user.client.ui.HTML;
|
|
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
|
|
import com.google.gwt.user.client.ui.HasVerticalAlignment;
|
|
import com.google.gwt.user.client.ui.Hidden;
|
|
import com.google.gwt.user.client.ui.HorizontalPanel;
|
|
import com.google.gwt.user.client.ui.VerticalPanel;
|
|
|
|
|
|
/**
|
|
* The Class DialogUploadStream.
|
|
*
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
* Sep 24, 2015
|
|
*/
|
|
public class DialogUploadStream extends ClosableDialog {
|
|
|
|
/**
|
|
*
|
|
*/
|
|
|
|
private DialogUploadStream INSTANCE = this;
|
|
// private boolean isStatusCompleted = false;
|
|
protected Button btnUpload = new Button("Upload");
|
|
// protected Button btnCancel = new Button("Cancel");
|
|
private Hidden hiddenOverwrite = new Hidden(ConstantsWorkspaceUploader.IS_OVERWRITE,"false");
|
|
protected final FormPanel formPanel = new FormPanel();
|
|
protected FileUpload fileUpload;
|
|
protected Hidden jsonClientKeys;
|
|
protected List<WorkspaceUploaderItem> fakeUploaders = new ArrayList<WorkspaceUploaderItem>();
|
|
|
|
/**
|
|
* The Enum UPLOAD_TYPE.
|
|
*
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
* Sep 11, 2015
|
|
*/
|
|
public static enum UPLOAD_TYPE {File, Archive};
|
|
private HTML LOADING = new HTML("Upload starting...");
|
|
|
|
protected String parentIdentifier = "";
|
|
protected VerticalPanel panel;
|
|
private HorizontalPanel hpButtons;
|
|
// private MonitorPanel monitorPanel;
|
|
protected String clientUploadKey;
|
|
private Anchor anchorMyUploads;
|
|
|
|
/**
|
|
* Instantiates a new dialog upload stream.
|
|
*
|
|
* @param headerTitle the header title
|
|
* @param parentId the parent id
|
|
* @param uploadType the upload type
|
|
*/
|
|
public DialogUploadStream(String headerTitle, final String parentId, UPLOAD_TYPE uploadType){
|
|
super(false, true, headerTitle);
|
|
// setText(headerTitle);
|
|
this.parentIdentifier = parentId;
|
|
this.addStyleName("fileInputUploader");
|
|
formPanel.setAction(ConstantsWorkspaceUploader.WORKSPACE_UPLOADER_SERVLET_STREAM);
|
|
formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
|
|
formPanel.setMethod(FormPanel.METHOD_POST);
|
|
formPanel.setWidth("auto");
|
|
|
|
// Create a panel to hold all of the form widgets.
|
|
panel = new VerticalPanel();
|
|
formPanel.setWidget(panel);
|
|
setModal(false);
|
|
|
|
fileUpload = new FileUpload();
|
|
fileUpload.setName(ConstantsWorkspaceUploader.UPLOAD_FORM_ELEMENT);
|
|
|
|
// Add hidden parameters
|
|
panel.add(new Hidden(ConstantsWorkspaceUploader.ID_FOLDER,parentId));
|
|
panel.add(new Hidden(ConstantsWorkspaceUploader.UPLOAD_TYPE,uploadType.toString()));
|
|
panel.add(hiddenOverwrite);
|
|
|
|
// clientUploadKey = GenerateUUID.get(15, 16);
|
|
initJsonClientKeys();
|
|
panel.add(jsonClientKeys);
|
|
|
|
panel.add(fileUpload);
|
|
hpButtons = new HorizontalPanel();
|
|
hpButtons.getElement().getStyle().setMarginTop(10, Unit.PX);
|
|
// hpButtons.getElement().getStyle().setMarginBottom(10, Unit.PX);
|
|
// btnCancel.getElement().getStyle().setMarginLeft(10, Unit.PX);
|
|
hpButtons.add(btnUpload);
|
|
// hpButtons.add(btnCancel);
|
|
panel.add(hpButtons);
|
|
|
|
HorizontalPanel hp = new HorizontalPanel();
|
|
hp.getElement().setId("hpID");
|
|
hp.getElement().getStyle().setWidth(100, Unit.PCT);
|
|
hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
|
|
hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
|
|
|
|
// vp.add(anchorMyUploads);
|
|
|
|
anchorMyUploads = new Anchor(ConstantsWorkspaceUploader.MY_UPLOADS);
|
|
anchorMyUploads.addClickHandler(new ClickHandler() {
|
|
|
|
@Override
|
|
public void onClick(ClickEvent event) {
|
|
UploaderMonitor.getInstance().showMonitor();
|
|
}
|
|
});
|
|
hp.add(anchorMyUploads);
|
|
panel.add(hp);
|
|
add(formPanel);
|
|
}
|
|
|
|
protected void initJsonClientKeys(){
|
|
jsonClientKeys = new Hidden(ConstantsWorkspaceUploader.CLIENT_UPLOAD_KEYS,"");
|
|
}
|
|
|
|
/**
|
|
* Bind events.
|
|
*/
|
|
public void bindEvents(){
|
|
this.addHandlers();
|
|
this.addListeners();
|
|
}
|
|
|
|
/**
|
|
* Adds the handlers.
|
|
*/
|
|
protected void addHandlers() {
|
|
|
|
// handle the post
|
|
formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {
|
|
|
|
@Override
|
|
public void onSubmitComplete(SubmitCompleteEvent event) {
|
|
GWT.log("onSubmitComplete");
|
|
hide();
|
|
// isStatusCompleted = true;
|
|
String result = event.getResults();
|
|
|
|
if (result == null) {
|
|
removeLoading();
|
|
new DialogResult(null, "Error during upload", "An error occurred during file upload.").center();
|
|
return;
|
|
}
|
|
String strippedResult = new HTML(result).getText();
|
|
final HandlerResultMessage resultMessage = HandlerResultMessage.parseResult(strippedResult);
|
|
|
|
switch (resultMessage.getStatus()) {
|
|
case ERROR:
|
|
removeLoading();
|
|
GWT.log("Error during upload "+resultMessage.getMessage());
|
|
break;
|
|
case UNKNOWN:
|
|
removeLoading();
|
|
GWT.log("Error during upload "+resultMessage.getMessage());
|
|
break;
|
|
case WARN: {
|
|
GWT.log("Upload completed with warnings "+resultMessage.getMessage());
|
|
removeLoading();
|
|
break;
|
|
}
|
|
case OK: {
|
|
// removeLoading();
|
|
// UploaderMonitor.getInstance().pollWorkspaceUploaderId(resultMessage.getMessage());
|
|
|
|
/*Timer t = new Timer() {
|
|
public void run() {
|
|
GWT.log("Upload started with id: "+resultMessage.getMessage());
|
|
UploaderMonitor.getInstance().pollWorkspaceUploaderId(resultMessage.getMessage());
|
|
}
|
|
};
|
|
|
|
t.schedule(250);*/
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
formPanel.addSubmitHandler(new SubmitHandler() {
|
|
|
|
@Override
|
|
public void onSubmit(SubmitEvent event) {
|
|
GWT.log("SubmitEvent");
|
|
addLoading();
|
|
enableButtons(false);
|
|
WorkspaceUploaderItem fakeUploader = new WorkspaceUploaderItem();
|
|
fakeUploader.setClientUploadKey(clientUploadKey);
|
|
UploaderMonitor.getInstance().addNewSubmit(fakeUploader, fileUpload.getFilename());
|
|
setVisible(false);
|
|
removeLoading();
|
|
UploaderMonitor.getInstance().pollWorkspaceUploaderId(clientUploadKey);
|
|
|
|
//WAITING 0.5 SEC TO FIRST UPDATES
|
|
new Timer() {
|
|
|
|
@Override
|
|
public void run() {
|
|
removeLoading();
|
|
UploaderMonitor.getInstance().pollWorkspaceUploaderId(clientUploadKey);
|
|
|
|
}
|
|
}.schedule(500);
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Adds the loading.
|
|
*/
|
|
protected void addLoading(){
|
|
panel.add(LOADING);
|
|
}
|
|
|
|
/**
|
|
* Removes the loading.
|
|
*/
|
|
protected void removeLoading(){
|
|
try{
|
|
panel.remove(LOADING);
|
|
}catch(Exception e){
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Enable buttons.
|
|
*
|
|
* @param bool the bool
|
|
*/
|
|
protected void enableButtons(boolean bool){
|
|
btnUpload.setEnabled(bool);
|
|
// btnCancel.setEnabled(bool);
|
|
// upload.setEnabled(bool);
|
|
}
|
|
|
|
/**
|
|
* Adds the listeners.
|
|
*/
|
|
protected void addListeners() {
|
|
|
|
btnUpload.addClickHandler(new ClickHandler() {
|
|
|
|
@Override
|
|
public void onClick(ClickEvent event) {
|
|
GWT.log("btnSubmit click");
|
|
if (fileUpload.getFilename()==null || !(fileUpload.getFilename().length()>2)) {
|
|
GWT.log("No file specified ");
|
|
return;
|
|
}
|
|
|
|
GWT.log("fileUpload.getFilename() "+fileUpload.getFilename());
|
|
/*
|
|
* TODO: recall: Some browser would write in fileUploadField.getValue() C:\fakepath\$fileName
|
|
*/
|
|
String normalizedFileName = fileUpload.getFilename();
|
|
if (normalizedFileName.contains("\\")) {
|
|
normalizedFileName = normalizedFileName.substring(normalizedFileName.lastIndexOf("\\")+1); //remove C:\fakepath\ if exists
|
|
}
|
|
final String label = normalizedFileName;
|
|
WorkspaceUploaderServiceAsync.Util.getInstance().itemExistsInWorkpaceFolder(parentIdentifier, normalizedFileName, new AsyncCallback<String>() {
|
|
|
|
@Override
|
|
public void onSuccess(final String itemId) {
|
|
GWT.log("itemExistsInWorkpaceFolder: "+itemId);
|
|
if(itemId!=null){
|
|
// HANDLE OWERWRITE
|
|
if(Window.confirm(label + " exists in folder. Overwrite?")){
|
|
removeItemAndSubmitForm(itemId);
|
|
updateItemSubmitForm(itemId);
|
|
}
|
|
}else
|
|
submitForm(); //ITEM does NOT EXIST SO SUBMIT FORM;
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(Throwable caught) {
|
|
GWT.log("Sorry an error occurred on the server "+caught.getLocalizedMessage() + ". Please try again later");
|
|
Window.alert(caught.getMessage());
|
|
}
|
|
|
|
});
|
|
}
|
|
});
|
|
|
|
// btnCancel.addClickHandler(new ClickHandler() {
|
|
//
|
|
// @Override
|
|
// public void onClick(ClickEvent event) {
|
|
// hide();
|
|
//
|
|
// }
|
|
// });
|
|
}
|
|
|
|
|
|
/**
|
|
* Submit form.
|
|
*/
|
|
public void submitForm(){
|
|
|
|
/*messageBoxWait = new MessageBoxWait(ConstantsExplorer.PROGRESS, ConstantsExplorer.SAVINGYOURFILE, fileUploadField.getValue());
|
|
|
|
//Progress bar for upload
|
|
final Timer t = new Timer()
|
|
{
|
|
public void run()
|
|
{
|
|
if (isStatusCompleted)
|
|
{
|
|
cancel();
|
|
messageBoxWait.getMessageBoxWait().close();
|
|
}
|
|
}
|
|
};
|
|
t.scheduleRepeating(500);*/
|
|
formPanel.submit();
|
|
}
|
|
|
|
|
|
/**
|
|
* Removes the item and submit form.
|
|
*
|
|
* @param itemId the item id
|
|
*/
|
|
protected void removeItemAndSubmitForm(String itemId){
|
|
/*
|
|
AppControllerExplorer.rpcWorkspaceService.removeItem(itemId, new AsyncCallback<Boolean>() {
|
|
|
|
@Override
|
|
public void onFailure(Throwable caught) {
|
|
Info.display("Error", caught.getMessage());
|
|
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onSuccess(Boolean result) {
|
|
if(result){
|
|
hiddenOverwrite.setValue("true");
|
|
submitForm();
|
|
}
|
|
|
|
}
|
|
|
|
});*/
|
|
}
|
|
|
|
/**
|
|
* Update item submit form.
|
|
*
|
|
* @param itemId the item id
|
|
*/
|
|
protected void updateItemSubmitForm(String itemId){
|
|
|
|
hiddenOverwrite.setValue("true");
|
|
submitForm();
|
|
}
|
|
}
|
|
|