package org.gcube.portlets.widgets.wstaskexecutor.client; import java.util.ArrayList; import java.util.List; import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskConfiguration; import org.gcube.common.workspacetaskexecutor.shared.exception.ItemNotConfiguredException; import org.gcube.common.workspacetaskexecutor.shared.exception.WorkspaceFolderLocked; import org.gcube.portlets.widgets.wstaskexecutor.client.TaskCompletedNotification.TaskCompletedNotificationListner; import org.gcube.portlets.widgets.wstaskexecutor.client.dialog.DialogConfirm; import org.gcube.portlets.widgets.wstaskexecutor.client.dialog.DialogResult; import org.gcube.portlets.widgets.wstaskexecutor.client.event.CreateTaskConfigurationEvent; import org.gcube.portlets.widgets.wstaskexecutor.client.event.CreateTaskConfigurationEventHandler; import org.gcube.portlets.widgets.wstaskexecutor.client.event.DeleteConfigurationEvent; import org.gcube.portlets.widgets.wstaskexecutor.client.event.DeleteConfigurationEventHandler; import org.gcube.portlets.widgets.wstaskexecutor.client.event.PerformRunTaskEvent; import org.gcube.portlets.widgets.wstaskexecutor.client.event.PerformRunTaskEventHandler; import org.gcube.portlets.widgets.wstaskexecutor.client.event.ShowCreateTaskConfigurationEvent; import org.gcube.portlets.widgets.wstaskexecutor.client.event.ShowCreateTaskConfigurationEventHandler; import org.gcube.portlets.widgets.wstaskexecutor.client.rpc.WsTaskExecutorWidgetServiceAsync; import org.gcube.portlets.widgets.wstaskexecutor.client.view.LoaderIcon; import org.gcube.portlets.widgets.wstaskexecutor.client.view.WsTaskExecutorWidgetViewManager; import org.gcube.portlets.widgets.wstaskexecutor.client.view.binder.MonitorFolderTaskExecutionStatusView; import org.gcube.portlets.widgets.wstaskexecutor.shared.WSItem; import com.github.gwtbootstrap.client.ui.Alert; import com.github.gwtbootstrap.client.ui.Modal; import com.github.gwtbootstrap.client.ui.constants.AlertType; 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.shared.HandlerManager; 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.VerticalPanel; /** * Entry point classes define onModuleLoad(). * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * May 4, 2018 */ public class WsTaskExecutorWidget { /** * The message displayed to the user when the server cannot be reached or * returns an error. */ private static final String SERVER_ERROR = "An error occurred while " + "attempting to contact the server. Please check your network " + "connection and try again."; /** * Create a remote service proxy to talk to the server-side Greeting * service. */ public static final WsTaskExecutorWidgetServiceAsync wsTaskService = WsTaskExecutorWidgetServiceAsync.Util.getInstance(); /** The Constant eventBus. */ public final static HandlerManager eventBus = new HandlerManager(null); private WsTaskExecutorWidgetViewManager viewManager = new WsTaskExecutorWidgetViewManager(); private final List taskEventsListeners = new ArrayList(); /** * Instantiates a new ws task executor widget. */ public WsTaskExecutorWidget() { bindEvents(); // TODO Auto-generated constructor stub } /** * Bind events. */ private void bindEvents() { eventBus.addHandler(PerformRunTaskEvent.TYPE, new PerformRunTaskEventHandler() { @Override public void onPerformRunTask(PerformRunTaskEvent perforRunTaskEvent) { try { performRunTask(perforRunTaskEvent.getFolder(), perforRunTaskEvent.getConf()); } catch (Exception e) { Window.alert(e.getMessage()); } } }); eventBus.addHandler(DeleteConfigurationEvent.TYPE, new DeleteConfigurationEventHandler() { @Override public void onRemoveConfiguration(final DeleteConfigurationEvent dcEvent) { GWT.log("qui remove"); if(dcEvent.getTaskConf()!=null){ DialogConfirm confirm = new DialogConfirm(null, "Delete Confirm?", "Deleting the configuration: "+dcEvent.getTaskConf().getTaskId()+". Confirm?"); confirm.getYesButton().addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { WsTaskExecutorWidget.wsTaskService.removeTaskConfiguration(dcEvent.getTaskConf(), new AsyncCallback() { @Override public void onFailure(Throwable caught) { new DialogResult(null, "Alert!!!", caught.getMessage()).center(); } @Override public void onSuccess(Boolean result) { DialogResult dg = new DialogResult(null, "Configuration removed", "The configuration "+dcEvent.getTaskConf().getConfigurationKey() +" has been removed correctly"); dg.center(); try { viewManager.showCreateTaskConfigurationForFolder(dcEvent.getWsItem(), null); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } }); confirm.getElement().getStyle().setZIndex(Integer.MAX_VALUE-1000); confirm.center(); } } }); eventBus.addHandler(ShowCreateTaskConfigurationEvent.TYPE, new ShowCreateTaskConfigurationEventHandler() { @Override public void onShowCreateConfiguration( ShowCreateTaskConfigurationEvent showCreateTaskConfigurationEvent) { if(showCreateTaskConfigurationEvent.getWsItem()!=null) viewManager.showCreateTaskConfigurationForFolder(showCreateTaskConfigurationEvent.getWsItem(), null); } }); eventBus.addHandler(CreateTaskConfigurationEvent.TYPE, new CreateTaskConfigurationEventHandler() { @Override public void onCreateConfiguration( final CreateTaskConfigurationEvent createTCE) { if(createTCE.getWsItem()!=null && createTCE.getConf()!=null){ GWT.log("Creating the configuration: "+createTCE.getConf()); WsTaskExecutorWidget.wsTaskService.createTaskConfiguration(createTCE.getWsItem().getItemId(), createTCE.getConf(), createTCE.isUpdate(), new AsyncCallback() { @Override public void onFailure(Throwable caught) { Window.alert(caught.getMessage()); } @Override public void onSuccess(Boolean result) { try { showTaskConfigurationsForFolder(createTCE.getWsItem()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } } }); } /** * Perform run task. * * @param wsItem the folder * @param conf the conf * @throws Exception the exception */ public void performRunTask(WSItem wsItem, TaskConfiguration conf) throws Exception { if(wsItem==null || wsItem.getItemId()==null) throw new Exception("Invalid parameter workpace item or its id is null"); if(conf==null) throw new Exception("Invalid parameter the configuration is null"); //WsTaskExecutorWidget.wsTaskService.runTask() } /** * Show task configurations for folder. * * @param wsItem the folder * @throws Exception the exception */ public void showTaskConfigurationsForFolder(final WSItem wsItem) throws Exception { if(wsItem==null || wsItem.getItemId()==null) throw new Exception("Invalid parameter workpace item or its id is null"); MonitorFolderTaskExecutionStatusView monitor = viewManager.getMonitor(wsItem); //SHOWING CURRENT ACTIVE MONITOR if(monitor!=null) { GWT.log("Monitor for workpace item: "+wsItem.getItemId() +" exists showing it.."); viewManager.showMonitorTaskStatusFor(wsItem, monitor.getTaskConfiguration(), monitor.getTaskComputation(), taskEventsListeners); return; } final Modal box = new Modal(true); box.setTitle("Checking configurations..."); LoaderIcon loader = new LoaderIcon("Checking folder configurations..."); box.add(loader); GWT.log("Performing checkItemTaskConfigurations: "+wsItem.getItemId()); WsTaskExecutorWidget.wsTaskService.checkItemTaskConfigurations(wsItem.getItemId(), new AsyncCallback>() { @Override public void onSuccess(List result) { box.hide(); viewManager.showTaskConfigurationsFolderInfo(wsItem, result, taskEventsListeners); } @Override public void onFailure(Throwable caught) { box.hide(); if(caught instanceof ItemNotConfiguredException){ viewManager.showCreateTaskConfigurationForFolder(wsItem, null); }else if(caught instanceof WorkspaceFolderLocked){ VerticalPanel v = new VerticalPanel(); Alert alert = new Alert("Current Folder is locked by another proccess. Do you want see Task status?"); alert.setClose(true); alert.setType(AlertType.INFO); Button butt = new Button("Show Status"); butt.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { //WsThreddsWidget.eventBus.fireEvent(new ShowMonitorSyncStatusEvent(folder)); } }); v.add(alert); v.add(butt); box.add(v); } } }); box.show(); } }