statistical-algorithms-impo.../src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/project/ProjectManager.java

696 lines
20 KiB
Java

package org.gcube.portlets.user.statisticalalgorithmsimporter.client.project;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.NewMainCodeEvent;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.ProjectStatusEvent;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.SessionExpiredEvent;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.monitor.StatAlgoImporterMonitor;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.rpc.StatAlgoImporterServiceAsync;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.type.ProjectStatusEventType;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.type.SessionExpiredType;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.utils.UtilsGXT3;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterSessionExpiredException;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.InputData;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.MainCode;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.Project;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.ProjectFolder;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.workspace.ItemDescription;
import org.gcube.portlets.widgets.wsexplorer.client.notification.WorkspaceExplorerSelectNotification.WorskpaceExplorerSelectNotificationListener;
import org.gcube.portlets.widgets.wsexplorer.client.select.WorkspaceExplorerSelectDialog;
import org.gcube.portlets.widgets.wsexplorer.shared.Item;
import org.gcube.portlets.widgets.wsexplorer.shared.ItemType;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.core.client.dom.XDOM;
import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox;
import com.sencha.gxt.widget.core.client.event.DialogHideEvent;
import com.sencha.gxt.widget.core.client.event.DialogHideEvent.DialogHideHandler;
/**
*
* @author Giancarlo Panichi email: <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class ProjectManager {
private EventBus eventBus;
private Project project;
public ProjectManager(EventBus eventBus) {
this.eventBus = eventBus;
}
public void createProject() {
WorkspaceExplorerSelectDialog wselectDialog = new WorkspaceExplorerSelectDialog(
"Select Project Folder", true);
WorskpaceExplorerSelectNotificationListener handler = new WorskpaceExplorerSelectNotificationListener() {
@Override
public void onSelectedItem(Item item) {
if (item.getType() == ItemType.FOLDER) {
createProjectOnServer(item);
} else {
UtilsGXT3.info("Attention",
"Select a valid project folder!");
}
}
@Override
public void onFailed(Throwable throwable) {
Log.error("Error in create project: "
+ throwable.getLocalizedMessage());
UtilsGXT3.alert("Error", throwable.getLocalizedMessage());
throwable.printStackTrace();
}
@Override
public void onAborted() {
}
@Override
public void onNotValidSelection() {
UtilsGXT3.info("Attention", "Select a valid project folder!");
}
};
wselectDialog.addWorkspaceExplorerSelectNotificationListener(handler);
wselectDialog.setZIndex(XDOM.getTopZIndex());
wselectDialog.show();
}
public void openProject() {
WorkspaceExplorerSelectDialog wselectDialog = new WorkspaceExplorerSelectDialog(
"Select Project Folder", true);
WorskpaceExplorerSelectNotificationListener handler = new WorskpaceExplorerSelectNotificationListener() {
@Override
public void onSelectedItem(Item item) {
if (item.getType() == ItemType.FOLDER) {
openProjectOnServer(item);
} else {
UtilsGXT3.info("Attention",
"Select a valid project folder!");
}
}
@Override
public void onFailed(Throwable throwable) {
Log.error("Error in create project: "
+ throwable.getLocalizedMessage());
UtilsGXT3.alert("Error", throwable.getLocalizedMessage());
throwable.printStackTrace();
}
@Override
public void onAborted() {
}
@Override
public void onNotValidSelection() {
UtilsGXT3.info("Attention", "Select a valid project folder!");
}
};
wselectDialog.addWorkspaceExplorerSelectNotificationListener(handler);
wselectDialog.setZIndex(XDOM.getTopZIndex());
wselectDialog.show();
}
public void addResource() {
List<ItemType> selectableTypes = new ArrayList<ItemType>();
selectableTypes.add(ItemType.EXTERNAL_FILE);
selectableTypes.add(ItemType.EXTERNAL_IMAGE);
selectableTypes.add(ItemType.IMAGE_DOCUMENT);
selectableTypes.add(ItemType.DOCUMENT);
selectableTypes.add(ItemType.PDF_DOCUMENT);
List<ItemType> showableTypes = new ArrayList<ItemType>();
showableTypes.addAll(Arrays.asList(ItemType.values()));
WorkspaceExplorerSelectDialog wselectDialog = new WorkspaceExplorerSelectDialog(
"Select a Resource", selectableTypes, showableTypes);
WorskpaceExplorerSelectNotificationListener handler = new WorskpaceExplorerSelectNotificationListener() {
@Override
public void onSelectedItem(Item item) {
if (item.getType() == ItemType.EXTERNAL_FILE
|| item.getType() == ItemType.EXTERNAL_IMAGE
|| item.getType() == ItemType.IMAGE_DOCUMENT
|| item.getType() == ItemType.DOCUMENT
|| item.getType() == ItemType.PDF_DOCUMENT) {
String filename = item.getName();
if (filename != null && !filename.isEmpty()) {
addResourceToProject(item);
} else {
UtilsGXT3.info("Attention", "Select a valid resource!");
}
} else {
UtilsGXT3.info("Attention", "Select a valid resource!");
}
}
@Override
public void onFailed(Throwable throwable) {
Log.error("Error in add resource: "
+ throwable.getLocalizedMessage());
UtilsGXT3.alert("Error", throwable.getLocalizedMessage());
throwable.printStackTrace();
}
@Override
public void onAborted() {
}
@Override
public void onNotValidSelection() {
UtilsGXT3.info("Attention", "Select a valid resource!");
}
};
wselectDialog.addWorkspaceExplorerSelectNotificationListener(handler);
wselectDialog.setZIndex(XDOM.getTopZIndex());
wselectDialog.show();
}
protected void addResourceToProject(Item item) {
Log.debug("Add Resource To Project Item selected: " + item);
ItemDescription itemDescription = new ItemDescription(item.getId(),
item.getName(), item.getOwner(), item.getPath(), item.getType()
.name());
StatAlgoImporterServiceAsync.INSTANCE.addResourceToProject(
itemDescription, new AsyncCallback<Void>() {
@Override
public void onSuccess(Void result) {
fireProjectStatusAddResourceEvent();
}
@Override
public void onFailure(Throwable caught) {
if (caught instanceof StatAlgoImporterSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
Log.error("Error in add resource to project: "
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error",
caught.getLocalizedMessage());
}
caught.printStackTrace();
}
});
}
protected void createProjectOnServer(Item item) {
Log.debug("Create Project Item selected: " + item);
final ItemDescription newProjectFolder = new ItemDescription(
item.getId(), item.getName(), item.getOwner(), item.getPath(),
item.getType().name());
StatAlgoImporterServiceAsync.INSTANCE.createProjectOnWorkspace(
newProjectFolder, new AsyncCallback<Void>() {
@Override
public void onSuccess(Void result) {
project = new Project(new ProjectFolder(
newProjectFolder));
fireProjectStatusOpenEvent();
}
@Override
public void onFailure(Throwable caught) {
if (caught instanceof StatAlgoImporterSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
Log.error("Error in create project: "
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error",
caught.getLocalizedMessage());
}
caught.printStackTrace();
}
});
}
protected void openProjectOnServer(Item item) {
Log.debug("Open Project Item selected: " + item);
final ItemDescription newProjectFolder = new ItemDescription(
item.getId(), item.getName(), item.getOwner(), item.getPath(),
item.getType().name());
StatAlgoImporterServiceAsync.INSTANCE.openProjectOnWorkspace(
newProjectFolder, new AsyncCallback<Project>() {
@Override
public void onSuccess(Project projectOpened) {
Log.debug("Open: " + projectOpened);
project = projectOpened;
fireProjectStatusOpenEvent();
}
@Override
public void onFailure(Throwable caught) {
if (caught instanceof StatAlgoImporterSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
Log.error("Error in open project: "
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error",
caught.getLocalizedMessage());
}
caught.printStackTrace();
}
});
}
public void saveProject(InputData inputData,
final StatAlgoImporterMonitor monitor) {
Log.debug("Save Project: " + inputData);
StatAlgoImporterServiceAsync.INSTANCE.saveProject(inputData,
new AsyncCallback<Void>() {
@Override
public void onSuccess(Void result) {
monitor.hide();
UtilsGXT3.info("Save", "Project saved!");
}
@Override
public void onFailure(Throwable caught) {
monitor.hide();
if (caught instanceof StatAlgoImporterSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
Log.error("Error on save project: "
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error",
caught.getLocalizedMessage());
}
caught.printStackTrace();
}
});
}
public void createSofware(InputData inputData,
final StatAlgoImporterMonitor monitor) {
Log.debug("Create Software: " + inputData);
StatAlgoImporterServiceAsync.INSTANCE.createSoftware(inputData,
new AsyncCallback<Void>() {
@Override
public void onSuccess(Void result) {
monitor.hide();
fireProjectStatusSoftwareCreatedEvent();
UtilsGXT3.info("Create Software", "Software Created!");
}
@Override
public void onFailure(Throwable caught) {
monitor.hide();
if (caught instanceof StatAlgoImporterSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
Log.error("Error in create software: "
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error",
caught.getLocalizedMessage());
}
caught.printStackTrace();
}
});
}
public void setMainCode(final ItemDescription itemDescription) {
if (project != null) {
if (project.getMainCode() != null
&& project.getMainCode().getItemDescription() != null) {
final ConfirmMessageBox mb = new ConfirmMessageBox("Attention",
"All previous configurations will be lost. Would you like to change main code?");
mb.addDialogHideHandler(new DialogHideHandler() {
@Override
public void onDialogHide(DialogHideEvent event) {
switch (event.getHideButton()) {
case NO:
break;
case YES:
setMainCodeOnServer(itemDescription);
break;
default:
break;
}
}
});
mb.setWidth(300);
mb.show();
} else {
setMainCodeOnServer(itemDescription);
}
} else {
}
}
private void setMainCodeOnServer(final ItemDescription itemDescription) {
Log.debug("Set Main Code: " + itemDescription);
StatAlgoImporterServiceAsync.INSTANCE.setMainCode(itemDescription,
new AsyncCallback<Void>() {
@Override
public void onFailure(Throwable caught) {
if (caught instanceof StatAlgoImporterSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
Log.error("Error setting main code: "
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error",
caught.getLocalizedMessage());
}
caught.printStackTrace();
}
@Override
public void onSuccess(Void result) {
project.setMainCode(new MainCode(itemDescription));
fireProjectStatusMainCodeSetEvent();
}
});
}
public void setNewMainCode(NewMainCodeEvent newMainCodeEvent,
final StatAlgoImporterMonitor monitor) {
StatAlgoImporterServiceAsync.INSTANCE.setNewMainCode(
newMainCodeEvent.getFile(), newMainCodeEvent.getCode(),
new AsyncCallback<ItemDescription>() {
@Override
public void onFailure(Throwable caught) {
monitor.hide();
if (caught instanceof StatAlgoImporterSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
Log.error("Error on save new main code: "
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error",
caught.getLocalizedMessage());
}
caught.printStackTrace();
}
@Override
public void onSuccess(ItemDescription itemDescription) {
project.setMainCode(new MainCode(itemDescription));
fireProjectStatusMainCodeSetEvent();
monitor.hide();
}
});
}
public void deleteItem(final ItemDescription itemDescription) {
if (project != null
&& project.getMainCode() != null
&& project.getMainCode().getItemDescription() != null
&& itemDescription.getId().compareTo(
project.getMainCode().getItemDescription().getId()) == 0) {
final ConfirmMessageBox mb = new ConfirmMessageBox("Delete",
"All previous configurations will be lost. Would you like to delete main code?");
mb.addDialogHideHandler(new DialogHideHandler() {
@Override
public void onDialogHide(DialogHideEvent event) {
switch (event.getHideButton()) {
case NO:
break;
case YES:
deleteItemOnServer(itemDescription, true);
break;
default:
break;
}
}
});
mb.setWidth(300);
mb.show();
} else {
final ConfirmMessageBox mb = new ConfirmMessageBox("Delete",
"Would you like to delete this resource?");
mb.addDialogHideHandler(new DialogHideHandler() {
@Override
public void onDialogHide(DialogHideEvent event) {
switch (event.getHideButton()) {
case NO:
break;
case YES:
deleteItemOnServer(itemDescription, false);
break;
default:
break;
}
}
});
mb.setWidth(300);
mb.show();
}
}
private void deleteItemOnServer(ItemDescription itemDescription,
final boolean mainCode) {
final StatAlgoImporterMonitor monitor = new StatAlgoImporterMonitor();
StatAlgoImporterServiceAsync.INSTANCE.deleteResourceOnProject(
itemDescription, new AsyncCallback<Project>() {
@Override
public void onFailure(Throwable caught) {
monitor.hide();
if (caught instanceof StatAlgoImporterSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
Log.error("Error deleting resourse: "
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error",
caught.getLocalizedMessage());
}
caught.printStackTrace();
}
@Override
public void onSuccess(Project result) {
monitor.hide();
project = result;
if (mainCode) {
fireProjectStatusDeleteMainCodeEvent();
} else {
fireProjectStatusDeleteEvent();
}
}
});
}
public void startProjectManager() {
fireProjectStatusStartEvent();
}
protected void fireProjectStatusOpenEvent() {
ProjectStatusEvent projectStatusEvent = new ProjectStatusEvent(
ProjectStatusEventType.OPEN, project);
eventBus.fireEvent(projectStatusEvent);
Log.debug("ProjectStatusEvent fired! " + projectStatusEvent);
}
protected void fireProjectStatusSaveProjectEvent() {
ProjectStatusEvent projectStatusEvent = new ProjectStatusEvent(
ProjectStatusEventType.SAVE, project);
eventBus.fireEvent(projectStatusEvent);
Log.debug("ProjectStatusEvent fired! " + projectStatusEvent);
}
protected void fireProjectStatusAddResourceEvent() {
ProjectStatusEvent projectStatusEvent = new ProjectStatusEvent(
ProjectStatusEventType.ADD_RESOURCE, project);
eventBus.fireEvent(projectStatusEvent);
Log.debug("ProjectStatusEvent fired! " + projectStatusEvent);
}
protected void fireProjectStatusDeleteEvent() {
ProjectStatusEvent projectStatusEvent = new ProjectStatusEvent(
ProjectStatusEventType.DELETE_RESOURCE, project);
eventBus.fireEvent(projectStatusEvent);
Log.debug("ProjectStatusEvent fired! " + projectStatusEvent);
}
protected void fireProjectStatusDeleteMainCodeEvent() {
ProjectStatusEvent projectStatusEvent = new ProjectStatusEvent(
ProjectStatusEventType.DELETE_MAIN_CODE, project);
eventBus.fireEvent(projectStatusEvent);
Log.debug("ProjectStatusEvent fired! " + projectStatusEvent);
}
protected void fireProjectStatusUpdateEvent() {
ProjectStatusEvent projectStatusEvent = new ProjectStatusEvent(
ProjectStatusEventType.UPDATE, project);
eventBus.fireEvent(projectStatusEvent);
Log.debug("ProjectStatusEvent fired! " + projectStatusEvent);
}
protected void fireProjectStatusMainCodeSetEvent() {
ProjectStatusEvent projectStatusEvent = new ProjectStatusEvent(
ProjectStatusEventType.MAIN_CODE_SET, project);
eventBus.fireEvent(projectStatusEvent);
Log.debug("ProjectStatusEvent fired! " + projectStatusEvent);
}
protected void fireProjectStatusSoftwareCreatedEvent() {
ProjectStatusEvent projectStatusEvent = new ProjectStatusEvent(
ProjectStatusEventType.SOFTWARE_CREATED, project);
eventBus.fireEvent(projectStatusEvent);
Log.debug("ProjectStatusEvent fired! " + projectStatusEvent);
}
protected void fireProjectStatusStartEvent() {
ProjectStatusEvent projectStatusEvent = new ProjectStatusEvent(
ProjectStatusEventType.START, project);
eventBus.fireEvent(projectStatusEvent);
Log.debug("ProjectStatusEvent fired! " + projectStatusEvent);
}
public void startProjectManager(String value) {
StatAlgoImporterServiceAsync.INSTANCE.restoreUISession(value,
new AsyncCallback<Project>() {
public void onFailure(Throwable caught) {
if (caught instanceof StatAlgoImporterSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
UtilsGXT3.alert("Error",
caught.getLocalizedMessage());
}
}
public void onSuccess(Project p) {
if (p != null) {
project = p;
fireProjectStatusOpenEvent();
}
}
});
}
public void softwarePublish() {
if (project != null) {
final ConfirmMessageBox mb = new ConfirmMessageBox(
"Attention",
"The compiled software will be notified to the Infrastructure Administrators and will be published in the suggested VREs. The source code will not be shared. Do you confirm the submission?");
mb.addDialogHideHandler(new DialogHideHandler() {
@Override
public void onDialogHide(DialogHideEvent event) {
switch (event.getHideButton()) {
case NO:
break;
case YES:
softwarePublishOnServer();
break;
default:
break;
}
}
});
mb.setWidth(300);
mb.show();
} else {
Log.error("Project not open: " + project);
UtilsGXT3.alert("Error", "Project not open!");
}
}
private void softwarePublishOnServer() {
final StatAlgoImporterMonitor monitor = new StatAlgoImporterMonitor();
StatAlgoImporterServiceAsync.INSTANCE
.publishSoftware(new AsyncCallback<Void>() {
public void onFailure(Throwable caught) {
monitor.hide();
if (caught instanceof StatAlgoImporterSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
UtilsGXT3.alert("Error",
caught.getLocalizedMessage());
}
}
public void onSuccess(Void result) {
monitor.hide();
UtilsGXT3.info("Publish",
"The software has been published!");
}
});
}
}