2018-03-02 17:03:57 +01:00
package org.gcube.portlets.widgets.wsthreddssync.client ;
2018-03-14 15:37:08 +01:00
import java.util.ArrayList ;
import java.util.List ;
2021-03-04 16:24:54 +01:00
import java.util.Map ;
2018-03-14 15:37:08 +01:00
2018-03-13 15:09:36 +01:00
import org.gcube.portal.wssynclibrary.shared.WorkspaceFolderLocked ;
2018-03-02 17:03:57 +01:00
import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncStatus ;
2018-03-13 15:09:36 +01:00
import org.gcube.portlets.widgets.wsthreddssync.client.SyncCompletedNotification.HasWsSyncNotificationListner ;
import org.gcube.portlets.widgets.wsthreddssync.client.SyncCompletedNotification.SyncCompletedNotificationListner ;
2018-03-13 17:39:09 +01:00
import org.gcube.portlets.widgets.wsthreddssync.client.dialog.DialogConfirm ;
import org.gcube.portlets.widgets.wsthreddssync.client.dialog.DialogResult ;
2018-03-02 17:03:57 +01:00
import org.gcube.portlets.widgets.wsthreddssync.client.event.PerformDoSyncEvent ;
import org.gcube.portlets.widgets.wsthreddssync.client.event.PerformDoSyncEventHandler ;
2018-03-13 17:39:09 +01:00
import org.gcube.portlets.widgets.wsthreddssync.client.event.PerformDoUnSyncEvent ;
import org.gcube.portlets.widgets.wsthreddssync.client.event.PerformDoUnSyncEventHandler ;
2018-03-02 17:03:57 +01:00
import org.gcube.portlets.widgets.wsthreddssync.client.event.ShowMonitorSyncStatusEvent ;
import org.gcube.portlets.widgets.wsthreddssync.client.event.ShowMonitorSyncStatusEventHandler ;
import org.gcube.portlets.widgets.wsthreddssync.client.rpc.ThreddsWorkspaceSyncServiceAsync ;
import org.gcube.portlets.widgets.wsthreddssync.client.view.LoaderIcon ;
import org.gcube.portlets.widgets.wsthreddssync.client.view.WsThreddsWidgetViewManager ;
import org.gcube.portlets.widgets.wsthreddssync.client.view.binder.MonitorFolderSyncStatusView ;
2021-03-04 16:24:54 +01:00
import org.gcube.portlets.widgets.wsthreddssync.shared.GatewayRolesThredds ;
2018-03-02 17:03:57 +01:00
import org.gcube.portlets.widgets.wsthreddssync.shared.WsFolder ;
import org.gcube.portlets.widgets.wsthreddssync.shared.WsThreddsSynchFolderConfiguration ;
import org.gcube.portlets.widgets.wsthreddssync.shared.WsThreddsSynchFolderDescriptor ;
2021-03-04 16:24:54 +01:00
import com.github.gwtbootstrap.client.ui.Alert ;
2018-03-02 17:03:57 +01:00
import com.github.gwtbootstrap.client.ui.Modal ;
2021-03-04 16:24:54 +01:00
import com.github.gwtbootstrap.client.ui.constants.AlertType ;
2018-03-02 17:03:57 +01:00
import com.google.gwt.core.client.GWT ;
2018-03-13 17:39:09 +01:00
import com.google.gwt.event.dom.client.ClickEvent ;
import com.google.gwt.event.dom.client.ClickHandler ;
2018-03-02 17:03:57 +01:00
import com.google.gwt.event.shared.HandlerManager ;
import com.google.gwt.user.client.Window ;
import com.google.gwt.user.client.rpc.AsyncCallback ;
2021-03-04 16:24:54 +01:00
import com.google.gwt.user.client.ui.HorizontalPanel ;
2018-03-02 17:03:57 +01:00
// TODO: Auto-generated Javadoc
/ * *
* The Class WsThreddsWidget .
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
* Feb 14 , 2018
* /
2018-03-13 15:09:36 +01:00
public class WsThreddsWidget implements HasWsSyncNotificationListner {
2018-03-02 17:03:57 +01:00
/** The ws thredds sync service. */
public static final ThreddsWorkspaceSyncServiceAsync wsThreddsSyncService = ThreddsWorkspaceSyncServiceAsync . Util . getInstance ( ) ;
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
/** The Constant eventBus. */
public final static HandlerManager eventBus = new HandlerManager ( null ) ;
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
private WsThreddsWidgetViewManager viewManager = new WsThreddsWidgetViewManager ( ) ;
2018-03-14 15:37:08 +01:00
private final List < SyncCompletedNotificationListner > syncEventsListeners = new ArrayList < SyncCompletedNotificationListner > ( ) ;
2021-03-03 18:14:53 +01:00
2021-03-04 16:24:54 +01:00
public static final String MISSING_THREDDS_ADMIN_RIGHTS = " It seems you are not authorized to create a configuration towards THREDDS Catalogue. Request it to the VRE manager or the portal administrator. " ;
public static final String MISSING_THREDDS_PUBLISHER_RIGHTS = " It seems you are not authorized to run a syncronization task towards THREDDS Catalogue. Request it to the VRE manager or the portal administrator. " ;
public static final String MISSING_THREDDS_RIGHTS = " It seems you are not authorized to interact with the facility 'Sync with THREDDS'. Request authorization to the VRE manager or the portal administrator. " ;
2021-03-03 18:14:53 +01:00
2018-03-02 17:03:57 +01:00
/ * *
* This is the entry point method .
* /
public WsThreddsWidget ( ) {
bindEvents ( ) ;
}
/ * *
* Bind events .
* /
private void bindEvents ( ) {
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
eventBus . addHandler ( PerformDoSyncEvent . TYPE , new PerformDoSyncEventHandler ( ) {
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
@Override
public void onPerformDoSync ( PerformDoSyncEvent performDoSyncEvent ) {
2018-03-05 15:44:51 +01:00
//GWT.log("One PerformDoSyncEvent "+Random.nextDouble());
2018-03-02 17:03:57 +01:00
if ( performDoSyncEvent . getFolder ( ) ! = null )
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
performFolderSync ( performDoSyncEvent . getFolder ( ) , performDoSyncEvent . getConf ( ) ) ;
}
} ) ;
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
eventBus . addHandler ( ShowMonitorSyncStatusEvent . TYPE , new ShowMonitorSyncStatusEventHandler ( ) {
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
@Override
public void onShowMonitorSyncStatus ( ShowMonitorSyncStatusEvent showSyncStatusEvent ) {
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
if ( showSyncStatusEvent . getFolder ( ) ! = null )
2018-03-05 15:44:51 +01:00
2018-03-14 15:37:08 +01:00
viewManager . showMonitorSyncToFolder ( showSyncStatusEvent . getFolder ( ) , syncEventsListeners ) ;
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
}
} ) ;
2018-03-05 15:44:51 +01:00
2018-03-13 17:39:09 +01:00
eventBus . addHandler ( PerformDoUnSyncEvent . TYPE , new PerformDoUnSyncEventHandler ( ) {
@Override
public void onPerformDoUnSync ( final PerformDoUnSyncEvent performDoUnSyncEvent ) {
2018-03-14 15:37:08 +01:00
GWT . log ( " DO UnSync performed on: " + performDoUnSyncEvent . getFolder ( ) ) ;
2018-03-13 17:39:09 +01:00
if ( performDoUnSyncEvent . getFolder ( ) ! = null ) {
2018-03-14 15:37:08 +01:00
final DialogConfirm confirm = new DialogConfirm ( null , " Unsync confirm? " , " Deleting sync configurations to the folder ' " + performDoUnSyncEvent . getFolder ( ) . getFoderName ( ) + " '</b><br>Confirm? " ) ;
2018-03-13 17:39:09 +01:00
confirm . getYesButton ( ) . addClickHandler ( new ClickHandler ( ) {
@Override
public void onClick ( ClickEvent event ) {
2018-03-14 15:37:08 +01:00
confirm . hide ( ) ;
2018-03-13 17:39:09 +01:00
performFolderUnSync ( performDoUnSyncEvent . getFolder ( ) ) ;
}
} ) ;
2018-03-14 15:37:08 +01:00
confirm . getElement ( ) . getStyle ( ) . setZIndex ( Integer . MAX_VALUE - 1000 ) ;
confirm . center ( ) ;
2018-03-13 17:39:09 +01:00
}
}
} ) ;
2018-03-02 17:03:57 +01:00
}
/ * *
* Show sync folder info .
*
* @param folder the folder
* @throws Exception the exception
* /
public void showSyncFolderInfo ( final WsFolder folder ) throws Exception {
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
if ( folder = = null | | folder . getFolderId ( ) = = null )
throw new Exception ( " Invalid parameter folder null " ) ;
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
MonitorFolderSyncStatusView monitor = viewManager . getMonitor ( folder ) ;
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
//SHOWING CURRENT ACTIVE MONITOR
if ( monitor ! = null ) {
GWT . log ( " Monitor for folder: " + folder . getFolderId ( ) + " exists showing it.. " ) ;
2018-03-14 15:37:08 +01:00
viewManager . showMonitorSyncToFolder ( folder , syncEventsListeners ) ;
2018-03-02 17:03:57 +01:00
return ;
}
2018-03-05 10:59:30 +01:00
2018-03-02 17:03:57 +01:00
final Modal box = new Modal ( true ) ;
box . setTitle ( " Checking configurations... " ) ;
2021-03-04 16:24:54 +01:00
final HorizontalPanel hpLoader = new HorizontalPanel ( ) ;
LoaderIcon loader = new LoaderIcon ( " Checking authorizations and folder configurations... " ) ;
hpLoader . add ( loader ) ;
box . add ( hpLoader ) ;
//LAOADING ROLES
WsThreddsWidget . wsThreddsSyncService . getScopesWithThreddsRolesForLoggedUser ( new AsyncCallback < Map < String , GatewayRolesThredds > > ( ) {
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
@Override
2021-03-04 16:24:54 +01:00
public void onFailure ( Throwable caught ) {
hpLoader . clear ( ) ;
hpLoader . setVisible ( false ) ;
Alert alert = new Alert ( ) ;
alert . setType ( AlertType . INFO ) ;
alert . setText ( caught . getMessage ( ) ) ;
box . add ( alert ) ;
2018-03-02 17:03:57 +01:00
}
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
@Override
2021-03-04 16:24:54 +01:00
public void onSuccess ( Map < String , GatewayRolesThredds > result ) {
try {
hpLoader . clear ( ) ;
hpLoader . setVisible ( false ) ;
} catch ( Exception e ) {
//Silent
}
if ( result ! = null ) {
GWT . log ( " Returned Map(Scope,Role): " + result ) ;
2021-03-08 10:55:48 +01:00
final boolean isThreddsPubliser = result . containsValue ( GatewayRolesThredds . DATA_EDITOR ) ;
final boolean isThreddsAdmin = result . containsValue ( GatewayRolesThredds . DATA_MANAGER ) ;
2021-03-04 16:24:54 +01:00
//no rights assigned
if ( ! isThreddsAdmin & & ! isThreddsPubliser ) {
showMissingRights ( box ) ;
return ;
}
//at least one THREDDS rights is assigned
GWT . log ( " Performing isItemSynched: " + folder . getFolderId ( ) ) ;
WsThreddsWidget . wsThreddsSyncService . isItemSynched ( folder . getFolderId ( ) , new AsyncCallback < WsThreddsSynchFolderDescriptor > ( ) {
@Override
public void onSuccess ( WsThreddsSynchFolderDescriptor result ) {
box . hide ( ) ;
//GWT.log("WsThreddsSynchFolderDescriptor result: "+result);
viewManager . showThreddsFolderInfo ( folder , result , isThreddsAdmin , isThreddsPubliser ) ;
}
@Override
public void onFailure ( Throwable caught ) {
box . hide ( ) ;
if ( caught instanceof WorkspaceFolderLocked ) {
viewManager . showMonitorSyncToFolder ( folder , syncEventsListeners ) ;
return ;
}
viewManager . cancelMonitor ( folder ) ;
Window . alert ( caught . getMessage ( ) ) ;
}
} ) ;
2018-03-13 15:09:36 +01:00
2021-03-04 16:24:54 +01:00
} else {
//no rights detected
showMissingRights ( box ) ;
2018-03-13 15:09:36 +01:00
return ;
}
2021-03-04 16:24:54 +01:00
2018-03-02 17:03:57 +01:00
}
2021-03-04 16:24:54 +01:00
2018-03-02 17:03:57 +01:00
} ) ;
2018-03-05 15:44:51 +01:00
2021-03-04 16:24:54 +01:00
2018-03-02 17:03:57 +01:00
box . show ( ) ;
}
2021-03-04 16:24:54 +01:00
private void showMissingRights ( Modal box ) {
Alert alert = new Alert ( ) ;
alert . setType ( AlertType . WARNING ) ;
alert . setText ( MISSING_THREDDS_RIGHTS ) ;
alert . setClose ( false ) ;
box . add ( alert ) ;
}
2018-03-05 15:44:51 +01:00
2018-03-14 15:37:08 +01:00
private void performFolderUnSync ( final WsFolder folder ) {
2018-03-13 17:39:09 +01:00
GWT . log ( " Performing doSyncFolder on: " + folder ) ;
final Modal box = new Modal ( true ) ;
box . setTitle ( " Deleting... " ) ;
box . hide ( false ) ;
LoaderIcon loader = new LoaderIcon ( " Deleting sync configurations to the folder: " + folder . getFoderName ( ) ) ;
box . add ( loader ) ;
wsThreddsSyncService . doUnSyncFolder ( folder . getFolderId ( ) , new AsyncCallback < Boolean > ( ) {
@Override
public void onFailure ( Throwable caught ) {
Window . alert ( caught . getMessage ( ) ) ;
if ( box ! = null )
box . hide ( ) ;
2018-03-14 15:37:08 +01:00
//CALLING METHOD ON SYNC ERROR TO THE LISTENERS
for ( SyncCompletedNotificationListner myListener : syncEventsListeners ) {
myListener . onSyncError ( folder ) ;
}
2018-03-13 17:39:09 +01:00
}
@Override
public void onSuccess ( Boolean result ) {
box . hide ( ) ;
if ( result ) {
DialogResult dlg = new DialogResult ( null , " Unsync performed " , " Unsync was performed correctly " ) ;
2018-03-14 15:37:08 +01:00
dlg . center ( ) ;
//CALLING METHOD UNSYNC PERFORMED COMPLETED TO THE LISTENERS
for ( SyncCompletedNotificationListner myListener : syncEventsListeners ) {
myListener . onUnSyncPerformed ( folder ) ;
}
2018-03-13 17:39:09 +01:00
}
}
} ) ;
box . show ( ) ;
}
2018-03-02 17:03:57 +01:00
/ * *
* Perform folder sync .
*
* @param folder the folder
* @param config the config
* /
private void performFolderSync ( final WsFolder folder , WsThreddsSynchFolderConfiguration config ) {
GWT . log ( " Performing doSyncFolder on: " + folder ) ;
final Modal box = new Modal ( true ) ;
box . setTitle ( " Starting synchronization... " ) ;
2018-03-05 11:54:33 +01:00
box . hide ( false ) ;
2018-03-02 17:03:57 +01:00
LoaderIcon loader = new LoaderIcon ( " Inizializiting synchronization to the folder: " + folder . getFoderName ( ) ) ;
box . add ( loader ) ;
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
wsThreddsSyncService . doSyncFolder ( folder . getFolderId ( ) , config , new AsyncCallback < ThSyncStatus > ( ) {
@Override
public void onFailure ( Throwable caught ) {
Window . alert ( caught . getMessage ( ) ) ;
viewManager . cancelMonitor ( folder ) ;
2018-03-05 15:44:51 +01:00
if ( box ! = null )
box . hide ( ) ;
2018-03-02 17:03:57 +01:00
}
@Override
public void onSuccess ( ThSyncStatus result ) {
2018-03-05 15:44:51 +01:00
if ( box ! = null )
box . hide ( ) ;
GWT . log ( " doSyncFolder Updating sync status: " + result ) ;
2018-03-14 15:37:08 +01:00
viewManager . showMonitorSyncToFolder ( folder , syncEventsListeners ) ;
2018-03-05 15:44:51 +01:00
2018-03-02 17:03:57 +01:00
}
} ) ;
box . show ( ) ;
}
2018-03-13 15:09:36 +01:00
/ * ( non - Javadoc )
* @see org . gcube . portlets . widgets . wsthreddssync . client . SyncCompletedNotification . HasWsSyncNotificationListner # addSyncCompletedListner ( org . gcube . portlets . widgets . wsthreddssync . client . SyncCompletedNotification . SyncCompletedNotificationListner )
* /
@Override
public void addSyncCompletedListner ( SyncCompletedNotificationListner listner ) {
2018-03-14 15:37:08 +01:00
syncEventsListeners . add ( listner ) ;
2018-03-13 15:09:36 +01:00
}
2018-03-02 17:03:57 +01:00
}