348 lines
9.3 KiB
Java
348 lines
9.3 KiB
Java
package org.gcube.portlets.widgets.workspaceuploader.client.uploader;
|
|
|
|
|
|
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.shared.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.Window;
|
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
|
import com.google.gwt.user.client.ui.Button;
|
|
import com.google.gwt.user.client.ui.DialogBox;
|
|
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.Hidden;
|
|
import com.google.gwt.user.client.ui.HorizontalPanel;
|
|
import com.google.gwt.user.client.ui.VerticalPanel;
|
|
|
|
|
|
/**
|
|
* The Class DialogUpload.
|
|
*
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
* Sep 24, 2015
|
|
*/
|
|
public class DialogUpload extends DialogBox {
|
|
|
|
private final FormPanel formPanel = new FormPanel();
|
|
private boolean isStatusCompleted = false;
|
|
private Button btnSubmit = new Button("Upload");
|
|
private Button btnCancel = new Button("Cancel");
|
|
private Hidden hiddenOverwrite = new Hidden(ConstantsWorkspaceUploader.IS_OVERWRITE,"false");
|
|
private FileUpload upload;
|
|
|
|
/**
|
|
* The Enum UPLOAD_TYPE.
|
|
*
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
* Sep 24, 2015
|
|
*/
|
|
public static enum UPLOAD_TYPE {File, Archive};
|
|
private HTML LOADING = new HTML("Upload starting...");
|
|
|
|
private String parentIdentifier = "";
|
|
private VerticalPanel panel;
|
|
private HorizontalPanel hpButtons;
|
|
private String clientUploadKey;
|
|
|
|
/**
|
|
* Instantiates a new dialog upload.
|
|
*
|
|
* @param headerTitle the header title
|
|
* @param parentId the parent id
|
|
* @param uploadType the upload type
|
|
*/
|
|
public DialogUpload(String headerTitle, final String parentId, UPLOAD_TYPE uploadType){
|
|
|
|
setText(headerTitle);
|
|
this.parentIdentifier = parentId;
|
|
formPanel.setAction(ConstantsWorkspaceUploader.WORKSPACE_UPLOADER_SERVLET);
|
|
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);
|
|
|
|
upload = new FileUpload();
|
|
upload.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);
|
|
panel.add(new Hidden(ConstantsWorkspaceUploader.CLIENT_UPLOAD_KEYS,clientUploadKey));
|
|
panel.add(upload);
|
|
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(btnSubmit);
|
|
hpButtons.add(btnCancel);
|
|
panel.add(hpButtons);
|
|
|
|
// 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(clientUploadKey);
|
|
fakeUploader.setClientUploadKey(clientUploadKey);
|
|
UploaderMonitor.getInstance().addNewSubmit(fakeUploader, upload.getFilename());
|
|
setVisible(false);
|
|
}
|
|
});
|
|
|
|
add(formPanel);
|
|
|
|
this.addListeners();
|
|
}
|
|
|
|
/**
|
|
* Adds the loading.
|
|
*/
|
|
private void addLoading(){
|
|
panel.add(LOADING);
|
|
}
|
|
|
|
/**
|
|
* Removes the loading.
|
|
*/
|
|
private void removeLoading(){
|
|
try{
|
|
panel.remove(LOADING);
|
|
}catch(Exception e){
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Enable buttons.
|
|
*
|
|
* @param bool the bool
|
|
*/
|
|
private void enableButtons(boolean bool){
|
|
btnSubmit.setEnabled(bool);
|
|
btnCancel.setEnabled(bool);
|
|
}
|
|
|
|
/**
|
|
* Adds the listeners.
|
|
*/
|
|
private void addListeners() {
|
|
|
|
btnSubmit.addClickHandler(new ClickHandler() {
|
|
|
|
@Override
|
|
public void onClick(ClickEvent event) {
|
|
GWT.log("btnSubmit click");
|
|
if (upload.getFilename()==null || !(upload.getFilename().length()>2)) {
|
|
GWT.log("No file specified ");
|
|
return;
|
|
}
|
|
/*
|
|
* TODO: recall: Some browser would write in fileUploadField.getValue() C:\fakepath\$fileName
|
|
*/
|
|
String normalizedFileName = upload.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);
|
|
//TODO HANDLE OWERWRITE
|
|
|
|
// if(itemId!=null){
|
|
//
|
|
// MessageBoxConfirm msg = new MessageBoxConfirm("Replace "+label+"?", label + " exists in folder "+parentName + ". Overwrite?");
|
|
//
|
|
//
|
|
// msg.getMessageBoxConfirm().addCallback(new Listener<MessageBoxEvent>() {
|
|
//
|
|
// public void handleEvent(MessageBoxEvent be) {
|
|
//
|
|
// //IF NOT CANCELLED
|
|
// String clickedButton = be.getButtonClicked().getItemId();
|
|
//
|
|
// if(clickedButton.equals(Dialog.YES)){
|
|
//
|
|
//// 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);*/
|
|
GWT.log("Form submit: "+formPanel.getAction());
|
|
formPanel.submit();
|
|
}
|
|
|
|
|
|
/**
|
|
* Removes the item and submit form.
|
|
*
|
|
* @param itemId the item id
|
|
*/
|
|
private 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
|
|
*/
|
|
private void updateItemSubmitForm(String itemId){
|
|
|
|
hiddenOverwrite.setValue("true");
|
|
submitForm();
|
|
}
|
|
|
|
/**
|
|
* Checks if is status completed.
|
|
*
|
|
* @return the isStatusCompleted
|
|
*/
|
|
public boolean isStatusCompleted() {
|
|
return isStatusCompleted;
|
|
}
|
|
|
|
|
|
}
|
|
|