package org.drools.workbench.screens.globals.client.editor;
import java.util.List;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.New;
import javax.inject.Inject;
import com.google.gwt.user.client.ui.IsWidget;
import org.drools.workbench.screens.globals.client.resources.i18n.GlobalsEditorConstants;
import org.drools.workbench.screens.globals.client.type.GlobalResourceType;
import org.drools.workbench.screens.globals.model.GlobalsEditorContent;
import org.drools.workbench.screens.globals.model.GlobalsModel;
import org.drools.workbench.screens.globals.service.GlobalsEditorService;
import org.guvnor.common.services.shared.metadata.MetadataService;
import org.guvnor.common.services.shared.validation.model.ValidationMessage;
import org.guvnor.common.services.shared.version.VersionService;
import org.guvnor.common.services.shared.version.events.RestoreEvent;
import org.jboss.errai.common.client.api.Caller;
import org.jboss.errai.common.client.api.RemoteCallback;
import org.kie.workbench.common.widgets.client.callbacks.HasBusyIndicatorDefaultErrorCallback;
import org.kie.workbench.common.widgets.client.menu.FileMenuBuilder;
import org.kie.workbench.common.widgets.client.popups.file.CommandWithCommitMessage;
import org.kie.workbench.common.widgets.client.popups.file.SaveOperationService;
import org.kie.workbench.common.widgets.client.popups.validation.ValidationPopup;
import org.kie.workbench.common.widgets.client.resources.i18n.CommonConstants;
import org.kie.workbench.common.widgets.metadata.client.callbacks.MetadataSuccessCallback;
import org.kie.workbench.common.widgets.metadata.client.widget.MetadataWidget;
import org.kie.workbench.common.widgets.viewsource.client.callbacks.ViewSourceSuccessCallback;
import org.kie.workbench.common.widgets.viewsource.client.screen.ViewSourceView;
import org.uberfire.backend.vfs.ObservablePath;
import org.uberfire.backend.vfs.Path;
import org.uberfire.client.annotations.WorkbenchEditor;
import org.uberfire.client.annotations.WorkbenchMenu;
import org.uberfire.client.annotations.WorkbenchPartTitle;
import org.uberfire.client.annotations.WorkbenchPartView;
import org.uberfire.client.common.MultiPageEditor;
import org.uberfire.client.common.Page;
import org.uberfire.client.mvp.PlaceManager;
import org.uberfire.client.workbench.events.ChangeTitleWidgetEvent;
import org.uberfire.lifecycle.IsDirty;
import org.uberfire.lifecycle.OnClose;
import org.uberfire.lifecycle.OnMayClose;
import org.uberfire.lifecycle.OnSave;
import org.uberfire.lifecycle.OnStartup;
import org.uberfire.mvp.Command;
import org.uberfire.mvp.ParameterizedCommand;
import org.uberfire.mvp.PlaceRequest;
import org.uberfire.workbench.events.NotificationEvent;
import org.uberfire.workbench.model.menu.Menus;
import org.uberfire.workbench.type.FileNameUtil;
import static org.uberfire.client.common.ConcurrentChangePopup.*;
/**
* Globals Editor Presenter
*/
@WorkbenchEditor(identifier = "org.kie.guvnor.globals", supportedTypes = { GlobalResourceType.class }, priority = 101)
public class GlobalsEditorPresenter {
@Inject
private Caller<GlobalsEditorService> globalsEditorService;
@Inject
private Caller<MetadataService> metadataService;
@Inject
private Caller<VersionService> versionService;
@Inject
private GlobalsEditorView view;
@Inject
private ViewSourceView viewSource;
@Inject
private MetadataWidget metadataWidget;
@Inject
private MultiPageEditor multiPage;
@Inject
private Event<NotificationEvent> notification;
@Inject
private Event<ChangeTitleWidgetEvent> changeTitleNotification;
@Inject
private Event<RestoreEvent> restoreEvent;
@Inject
private PlaceManager placeManager;
@Inject
private GlobalResourceType type;
@Inject
@New
private FileMenuBuilder menuBuilder;
private Menus menus;
private ObservablePath path;
private PlaceRequest place;
private boolean isReadOnly;
private ObservablePath.OnConcurrentUpdateEvent concurrentUpdateSessionInfo = null;
private GlobalsModel model;
@OnStartup
public void onStartup( final ObservablePath path,
final PlaceRequest place ) {
this.path = path;
this.place = place;
this.isReadOnly = place.getParameter( "readOnly", null ) == null ? false : true;
this.path.onRename( new Command() {
@Override
public void execute() {
changeTitleNotification.fire( new ChangeTitleWidgetEvent( place, getTitle(), null ) );
}
} );
this.path.onConcurrentUpdate( new ParameterizedCommand<ObservablePath.OnConcurrentUpdateEvent>() {
@Override
public void execute( final ObservablePath.OnConcurrentUpdateEvent eventInfo ) {
concurrentUpdateSessionInfo = eventInfo;
}
} );
this.path.onConcurrentRename( new ParameterizedCommand<ObservablePath.OnConcurrentRenameEvent>() {
@Override
public void execute( final ObservablePath.OnConcurrentRenameEvent info ) {
newConcurrentRename( info.getSource(),
info.getTarget(),
info.getIdentity(),
new Command() {
@Override
public void execute() {
disableMenus();
}
},
new Command() {
@Override
public void execute() {
reload();
}
}
).show();
}
} );
this.path.onConcurrentDelete( new ParameterizedCommand<ObservablePath.OnConcurrentDelete>() {
@Override
public void execute( final ObservablePath.OnConcurrentDelete info ) {
newConcurrentDelete( info.getPath(),
info.getIdentity(),
new Command() {
@Override
public void execute() {
disableMenus();
}
},
new Command() {
@Override
public void execute() {
placeManager.closePlace( place );
}
}
).show();
}
} );
makeMenuBar();
view.showBusyIndicator( CommonConstants.INSTANCE.Loading() );
multiPage.addWidget( view,
CommonConstants.INSTANCE.EditTabTitle() );
multiPage.addPage( new Page( viewSource,
CommonConstants.INSTANCE.SourceTabTitle() ) {
@Override
public void onFocus() {
viewSource.showBusyIndicator( CommonConstants.INSTANCE.Loading() );
globalsEditorService.call( new ViewSourceSuccessCallback( viewSource ),
new HasBusyIndicatorDefaultErrorCallback( viewSource ) ).toSource( path,
model );
}
@Override
public void onLostFocus() {
viewSource.clear();
}
} );
multiPage.addPage( new Page( metadataWidget,
CommonConstants.INSTANCE.MetadataTabTitle() ) {
@Override
public void onFocus() {
metadataWidget.showBusyIndicator( CommonConstants.INSTANCE.Loading() );
metadataService.call( new MetadataSuccessCallback( metadataWidget,
isReadOnly ),
new HasBusyIndicatorDefaultErrorCallback( metadataWidget ) ).getMetadata( path );
}
@Override
public void onLostFocus() {
//Nothing to do
}
} );
loadContent();
}
private void reload() {
changeTitleNotification.fire( new ChangeTitleWidgetEvent( place, getTitle(), null ) );
view.showBusyIndicator( CommonConstants.INSTANCE.Loading() );
loadContent();
}
private void disableMenus() {
menus.getItemsMap().get( FileMenuBuilder.MenuItems.COPY ).setEnabled( false );
menus.getItemsMap().get( FileMenuBuilder.MenuItems.RENAME ).setEnabled( false );
menus.getItemsMap().get( FileMenuBuilder.MenuItems.DELETE ).setEnabled( false );
menus.getItemsMap().get( FileMenuBuilder.MenuItems.VALIDATE ).setEnabled(false);
}
private void loadContent() {
globalsEditorService.call( getModelSuccessCallback(),
new HasBusyIndicatorDefaultErrorCallback( view ) ).loadContent( path );
}
private RemoteCallback<GlobalsEditorContent> getModelSuccessCallback() {
return new RemoteCallback<GlobalsEditorContent>() {
@Override
public void callback( final GlobalsEditorContent content ) {
model = content.getModel();
final List<String> fullyQualifiedClassNames = content.getFullyQualifiedClassNames();
view.setContent( content.getModel().getGlobals(),
fullyQualifiedClassNames,
isReadOnly );
view.hideBusyIndicator();
}
};
}
private void makeMenuBar() {
if ( isReadOnly ) {
menus = menuBuilder.addRestoreVersion( path ).build();
} else {
menus = menuBuilder
.addSave( new Command() {
@Override
public void execute() {
onSave();
}
} )
.addCopy( path )
.addRename( path )
.addDelete( path )
.addValidate( onValidate() )
.build();
}
}
private Command onValidate() {
return new Command() {
@Override
public void execute() {
globalsEditorService.call( new RemoteCallback<List<ValidationMessage>>() {
@Override
public void callback( final List<ValidationMessage> results ) {
if ( results == null || results.isEmpty() ) {
notification.fire( new NotificationEvent( CommonConstants.INSTANCE.ItemValidatedSuccessfully(),
NotificationEvent.NotificationType.SUCCESS ) );
} else {
ValidationPopup.showMessages( results );
}
}
} ).validate( path,
model );
}
};
}
@OnSave
public void onSave() {
if ( isReadOnly ) {
view.alertReadOnly();
return;
}
if ( concurrentUpdateSessionInfo != null ) {
newConcurrentUpdate( concurrentUpdateSessionInfo.getPath(),
concurrentUpdateSessionInfo.getIdentity(),
new Command() {
@Override
public void execute() {
save();
}
},
new Command() {
@Override
public void execute() {
//cancel?
}
},
new Command() {
@Override
public void execute() {
reload();
}
}
).show();
} else {
save();
}
}
private void save() {
new SaveOperationService().save( path,
new CommandWithCommitMessage() {
@Override
public void execute( final String comment ) {
view.showBusyIndicator( CommonConstants.INSTANCE.Saving() );
globalsEditorService.call( getSaveSuccessCallback(),
new HasBusyIndicatorDefaultErrorCallback( view ) ).save( path,
model,
metadataWidget.getContent(),
comment );
}
} );
}
private RemoteCallback<Path> getSaveSuccessCallback() {
return new RemoteCallback<Path>() {
@Override
public void callback( final Path path ) {
view.setNotDirty();
view.hideBusyIndicator();
metadataWidget.resetDirty();
notification.fire( new NotificationEvent( CommonConstants.INSTANCE.ItemSavedSuccessfully() ) );
}
};
}
@WorkbenchPartView
public IsWidget getWidget() {
return multiPage;
}
@OnClose
public void onClose() {
this.path = null;
}
@IsDirty
public boolean isDirty() {
if ( isReadOnly ) {
return false;
}
return ( view.isDirty() || metadataWidget.isDirty() );
}
@OnMayClose
public boolean checkIfDirty() {
if ( isDirty() ) {
return view.confirmClose();
}
return true;
}
@WorkbenchPartTitle
public String getTitle() {
final String fileName = FileNameUtil.removeExtension( path,
type );
if ( isReadOnly ) {
return GlobalsEditorConstants.INSTANCE.globalsEditorReadOnlyTitle0( fileName );
}
return GlobalsEditorConstants.INSTANCE.globalsEditorTitle0( fileName );
}
@WorkbenchMenu
public Menus getMenus() {
return menus;
}
public void onRestore( @Observes RestoreEvent restore ) {
if ( path == null || restore == null || restore.getPath() == null ) {
return;
}
if ( path.equals( restore.getPath() ) ) {
loadContent();
notification.fire( new NotificationEvent( CommonConstants.INSTANCE.ItemRestored() ) );
}
}
}