<commit_before>/*
 * 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.kie.workbench.common.screens.projectimportsscreen.client.forms;

import javax.enterprise.event.Event;
import javax.enterprise.inject.New;
import javax.inject.Inject;

import com.google.gwt.user.client.ui.Widget;
import org.guvnor.common.services.project.model.ProjectImports;
import org.guvnor.common.services.project.service.ProjectService;
import org.guvnor.common.services.shared.metadata.MetadataService;
import org.guvnor.common.services.shared.metadata.model.Metadata;
import org.jboss.errai.common.client.api.Caller;
import org.jboss.errai.common.client.api.RemoteCallback;
import org.kie.workbench.common.screens.projectimportsscreen.client.resources.i18n.ProjectConfigScreenConstants;
import org.kie.workbench.common.screens.projectimportsscreen.client.type.ProjectImportsResourceType;
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.resources.i18n.CommonConstants;
import org.kie.workbench.common.widgets.client.widget.BusyIndicatorView;
import org.kie.workbench.common.widgets.configresource.client.widget.unbound.ImportsWidgetPresenter;
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.lifecycle.OnClose;
import org.uberfire.lifecycle.OnStartup;
import org.uberfire.mvp.Command;
import org.uberfire.mvp.PlaceRequest;
import org.uberfire.workbench.events.NotificationEvent;
import org.uberfire.workbench.model.menu.Menus;

import static org.uberfire.commons.validation.PortablePreconditions.*;

@WorkbenchEditor(identifier = "projectConfigScreen", supportedTypes = { ProjectImportsResourceType.class })
public class ProjectImportsScreenPresenter
        implements ProjectImportsScreenView.Presenter {

    private BusyIndicatorView busyIndicatorView;
    private Event<NotificationEvent> notification;

    private ProjectImportsScreenView view;

    private Caller<ProjectService> projectService;
    private Caller<MetadataService> metadataService;

    private FileMenuBuilder menuBuilder;
    private Menus menus;

    private Path path;
    private ProjectImports content;
    private ImportsWidgetPresenter importsWidget;

    private boolean isReadOnly;

    public ProjectImportsScreenPresenter() {
    }

    @Inject
    public ProjectImportsScreenPresenter( @New final ProjectImportsScreenView view,
                                          @New final FileMenuBuilder menuBuilder,
                                          @New final ImportsWidgetPresenter importsWidget,
                                          final Event<NotificationEvent> notification,
                                          final BusyIndicatorView busyIndicatorView,
                                          final Caller<ProjectService> projectService,
                                          final Caller<MetadataService> metadataService ) {
        this.view = view;
        this.menuBuilder = menuBuilder;
        this.importsWidget = importsWidget;
        this.projectService = projectService;
        this.metadataService = metadataService;
        this.busyIndicatorView = busyIndicatorView;
        this.notification = notification;

        view.setImports( importsWidget );

        view.setPresenter( this );
    }

    @OnStartup
    public void init( final Path path,
                      final PlaceRequest place ) {
        this.path = checkNotNull( "path",
                                  path );
        this.isReadOnly = place.getParameter( "readOnly", null ) != null;

        makeMenuBar();

        view.showBusyIndicator( CommonConstants.INSTANCE.Loading() );

        projectService.call( getModelSuccessCallback(),
                             new HasBusyIndicatorDefaultErrorCallback( view ) ).load( path );
    }

    private RemoteCallback<ProjectImports> getModelSuccessCallback() {
        return new RemoteCallback<ProjectImports>() {

            @Override
            public void callback( final ProjectImports projectImports ) {
                content = projectImports;
                importsWidget.setContent( content, isReadOnly );
            }
        };
    }

    private void makeMenuBar() {
        if ( isReadOnly ) {
            menus = menuBuilder.addRestoreVersion( path ).build();
        } else {
            menus = menuBuilder.addSave( new Command() {
                @Override
                public void execute() {
                    onSave();
                }
            } ).build();
        }
    }

    private void onSave() {
        new SaveOperationService().save( path,
                                         new CommandWithCommitMessage() {
                                             @Override
                                             public void execute( final String commitMessage ) {
                                                 busyIndicatorView.showBusyIndicator( CommonConstants.INSTANCE.Saving() );
                                                 projectService.call( getSaveSuccessCallback(),
                                                                      new HasBusyIndicatorDefaultErrorCallback( busyIndicatorView ) ).save( path,
                                                                                                                                            content,
                                                                                                                                            view.getMetadata(),
                                                                                                                                            commitMessage );
                                             }<commit_msg>BZ-1039916: aligned imports editor with others by including delete, rename and copy operations.<commit_after>/*
 * 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.kie.workbench.common.screens.projectimportsscreen.client.forms;

import javax.enterprise.event.Event;
import javax.enterprise.inject.New;
import javax.inject.Inject;

import com.google.gwt.user.client.ui.Widget;
import org.guvnor.common.services.project.model.ProjectImports;
import org.guvnor.common.services.project.service.ProjectService;
import org.guvnor.common.services.shared.metadata.MetadataService;
import org.guvnor.common.services.shared.metadata.model.Metadata;
import org.jboss.errai.common.client.api.Caller;
import org.jboss.errai.common.client.api.RemoteCallback;
import org.kie.workbench.common.screens.projectimportsscreen.client.resources.i18n.ProjectConfigScreenConstants;
import org.kie.workbench.common.screens.projectimportsscreen.client.type.ProjectImportsResourceType;
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.resources.i18n.CommonConstants;
import org.kie.workbench.common.widgets.client.widget.BusyIndicatorView;
import org.kie.workbench.common.widgets.configresource.client.widget.unbound.ImportsWidgetPresenter;
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.lifecycle.OnClose;
import org.uberfire.lifecycle.OnStartup;
import org.uberfire.mvp.Command;
import org.uberfire.mvp.PlaceRequest;
import org.uberfire.workbench.events.NotificationEvent;
import org.uberfire.workbench.model.menu.Menus;

import static org.uberfire.commons.validation.PortablePreconditions.*;

@WorkbenchEditor(identifier = "projectConfigScreen", supportedTypes = { ProjectImportsResourceType.class })
public class ProjectImportsScreenPresenter
        implements ProjectImportsScreenView.Presenter {

    private BusyIndicatorView busyIndicatorView;
    private Event<NotificationEvent> notification;

    private ProjectImportsScreenView view;

    private Caller<ProjectService> projectService;
    private Caller<MetadataService> metadataService;

    private FileMenuBuilder menuBuilder;
    private Menus menus;

    private Path path;
    private ProjectImports content;
    private ImportsWidgetPresenter importsWidget;

    private boolean isReadOnly;

    public ProjectImportsScreenPresenter() {
    }

    @Inject
    public ProjectImportsScreenPresenter( @New final ProjectImportsScreenView view,
                                          @New final FileMenuBuilder menuBuilder,
                                          @New final ImportsWidgetPresenter importsWidget,
                                          final Event<NotificationEvent> notification,
                                          final BusyIndicatorView busyIndicatorView,
                                          final Caller<ProjectService> projectService,
                                          final Caller<MetadataService> metadataService ) {
        this.view = view;
        this.menuBuilder = menuBuilder;
        this.importsWidget = importsWidget;
        this.projectService = projectService;
        this.metadataService = metadataService;
        this.busyIndicatorView = busyIndicatorView;
        this.notification = notification;

        view.setImports( importsWidget );

        view.setPresenter( this );
    }

    @OnStartup
    public void init( final Path path,
                      final PlaceRequest place ) {
        this.path = checkNotNull( "path",
                                  path );
        this.isReadOnly = place.getParameter( "readOnly", null ) != null;

        makeMenuBar();

        view.showBusyIndicator( CommonConstants.INSTANCE.Loading() );

        projectService.call( getModelSuccessCallback(),
                             new HasBusyIndicatorDefaultErrorCallback( view ) ).load( path );
    }

    private RemoteCallback<ProjectImports> getModelSuccessCallback() {
        return new RemoteCallback<ProjectImports>() {

            @Override
            public void callback( final ProjectImports projectImports ) {
                content = projectImports;
                importsWidget.setContent( content, isReadOnly );
            }
        };
    }

    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 )
                    .build();
        }
    }

    private void onSave() {
        new SaveOperationService().save( path,
                                         new CommandWithCommitMessage() {
                                             @Override
                                             public void execute( final String commitMessage ) {
                                                 busyIndicatorView.showBusyIndicator( CommonConstants.INSTANCE.Saving() );
                                                 projectService.call( getSaveSuccessCallback(),
                                                                      new HasBusyIndicatorDefaultErrorCallback( busyIndicatorView ) ).save( path,
                                                                                                                                            content,
                                                                                                                                            view.getMetadata(),
                                                                                                                                            commitMessage );
                                             }
{'fraction_non_alphanumeric': 0.0718488921176898, 'fraction_numerical': 0.0022520886305848167, 'mean_word_length': 2.006333260537235, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 94, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6750171', 'n_tokens_mistral': 3109, 'n_tokens_neox': 3064, 'n_words': 681}