/*
* Copyright 2012 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.drools.workbench.screens.dtablexls.client.editor;
import java.util.List;
import javax.enterprise.context.Dependent;
import javax.enterprise.event.Event;
import javax.enterprise.inject.New;
import javax.inject.Inject;
import com.google.gwt.user.client.ui.IsWidget;
import org.drools.workbench.models.guided.dtable.shared.conversion.ConversionMessage;
import org.drools.workbench.models.guided.dtable.shared.conversion.ConversionResult;
import org.drools.workbench.screens.dtablexls.client.resources.i18n.DecisionTableXLSEditorConstants;
import org.drools.workbench.screens.dtablexls.client.type.DecisionTableXLSResourceType;
import org.drools.workbench.screens.dtablexls.client.widgets.ConversionMessageWidget;
import org.drools.workbench.screens.dtablexls.client.widgets.PopupListWidget;
import org.drools.workbench.screens.dtablexls.service.DecisionTableXLSService;
import org.guvnor.common.services.shared.metadata.MetadataService;
import org.guvnor.common.services.shared.validation.model.ValidationMessage;
import org.jboss.errai.common.client.api.RemoteCallback;
import org.jboss.errai.common.client.api.Caller;
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.validation.ValidationPopup;
import org.kie.workbench.common.widgets.client.resources.i18n.CommonConstants;
import org.kie.workbench.common.widgets.client.widget.BusyIndicatorView;
import org.kie.workbench.common.widgets.metadata.client.callbacks.MetadataSuccessCallback;
import org.kie.workbench.common.widgets.metadata.client.widget.MetadataWidget;
import org.uberfire.backend.vfs.Path;
import org.uberfire.lifecycle.OnClose;
import org.uberfire.lifecycle.OnStartup;
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.mvp.Command;
import org.uberfire.mvp.PlaceRequest;
import org.uberfire.workbench.events.NotificationEvent;
import org.uberfire.workbench.model.menu.Menus;
@Dependent
@WorkbenchEditor(identifier = "DecisionTableXLSEditor", supportedTypes = { DecisionTableXLSResourceType.class })
public class DecisionTableXLSEditorPresenter {
@Inject
private Caller<DecisionTableXLSService> decisionTableXLSService;
@Inject
private Caller<MetadataService> metadataService;
@Inject
private Event<NotificationEvent> notification;
@Inject
private PlaceManager placeManager;
@Inject
private DecisionTableXLSEditorView view;
@Inject
private MetadataWidget metadataWidget;
@Inject
private MultiPageEditor multiPage;
@Inject
private BusyIndicatorView busyIndicatorView;
@Inject
@New
private FileMenuBuilder menuBuilder;
private Menus menus;
private Path path;
private PlaceRequest place;
private boolean isReadOnly;
@OnStartup
public void onStartup( final Path path,
final PlaceRequest place ) {
this.path = path;
this.place = place;
this.isReadOnly = place.getParameter( "readOnly", null ) == null ? false : true;
makeMenuBar();
multiPage.addWidget( view,
DecisionTableXLSEditorConstants.INSTANCE.DecisionTable() );
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
}
} );
view.setPath( path );
view.setReadOnly( isReadOnly );
}
private void makeMenuBar() {
if ( isReadOnly ) {
menus = menuBuilder.addRestoreVersion( path ).build();
} else {
menus = menuBuilder
.addCopy( path )
.addRename( path )
.addDelete( path )
.addValidate( onValidate() )
.addCommand( DecisionTableXLSEditorConstants.INSTANCE.Convert(),
new Command() {
@Override
public void execute() {
convert();
}
} )
.build();
}
}
private Command onValidate() {
return new Command() {
@Override
public void execute() {
decisionTableXLSService.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,
path );
}
};
}
@OnClose
public void onClose() {
this.path = null;
}
@WorkbenchPartTitle
public String getTitle() {
return "XLS Decision Table Editor [" + path.getFileName() + "]";
}
@WorkbenchPartView
public IsWidget getWidget() {
return multiPage;
}
@WorkbenchMenu
public Menus getMenus() {
return menus;
}
private void convert() {
busyIndicatorView.showBusyIndicator( DecisionTableXLSEditorConstants.INSTANCE.Converting() );
decisionTableXLSService.call( new RemoteCallback<ConversionResult>() {
@Override
public void callback( final ConversionResult response ) {
busyIndicatorView.hideBusyIndicator();
if ( response.getMessages().size() > 0 ) {
final PopupListWidget popup = new PopupListWidget();
for ( ConversionMessage message : response.getMessages() ) {
popup.addListItem( new ConversionMessageWidget( message ) );
}
popup.show();
}
}
} ).convert( path );
}
}