<commit_before>/*******************************************************************************
 * Copyright (c) 2012-2015 <NAME>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   <NAME>. - initial API and implementation
 *******************************************************************************/
package org.eclipse.che.ide.extension.maven.client.module;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.eclipse.che.api.project.gwt.client.ProjectServiceClient;
import org.eclipse.che.api.project.shared.dto.GeneratorDescription;
import org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.app.CurrentProject;
import org.eclipse.che.ide.api.project.node.HasStorablePath;
import org.eclipse.che.ide.api.selection.Selection;
import org.eclipse.che.ide.api.selection.SelectionAgent;
import org.eclipse.che.ide.dto.DtoFactory;
import org.eclipse.che.ide.extension.maven.client.MavenArchetype;
import org.eclipse.che.ide.extension.maven.client.MavenExtension;
import org.eclipse.che.ide.extension.maven.client.MavenLocalizationConstant;
import org.eclipse.che.ide.part.explorer.project.ProjectExplorerPresenter;
import org.eclipse.che.ide.project.node.ModuleNode;
import org.eclipse.che.ide.project.node.ProjectNode;
import org.eclipse.che.ide.rest.AsyncRequestCallback;
import org.eclipse.che.ide.rest.DtoUnmarshallerFactory;
import org.eclipse.che.ide.ui.dialogs.DialogFactory;
import org.eclipse.che.ide.util.NameUtils;
import org.eclipse.che.ide.util.loging.Log;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.ARCHETYPE_GENERATION_STRATEGY;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.ARTIFACT_ID;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.GROUP_ID;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.MAVEN_ID;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.PACKAGING;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.PARENT_ARTIFACT_ID;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.PARENT_GROUP_ID;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.PARENT_VERSION;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.VERSION;

/**
 * @author <NAME>
 * @author <NAME>
 */
@Singleton
public class CreateMavenModulePresenter implements CreateMavenModuleView.ActionDelegate {

    private final AppContext                appContext;
    private final CreateMavenModuleView     view;
    private final ProjectServiceClient      projectService;
    private final DtoFactory                dtoFactory;
    private final DialogFactory             dialogFactory;
    private final ProjectExplorerPresenter  projectExplorer;
    private final MavenLocalizationConstant locale;
    private final SelectionAgent            selectionAgent;
    private final DtoUnmarshallerFactory    unmarshallerFactory;

    private String moduleName;

    private String         artifactId;
    private CurrentProject parentProject;

    @Inject
    public CreateMavenModulePresenter(AppContext appContext,
                                      CreateMavenModuleView view,
                                      ProjectServiceClient projectService,
                                      DtoFactory dtoFactory,
                                      DialogFactory dialogFactory,
                                      ProjectExplorerPresenter projectExplorer,
                                      MavenLocalizationConstant locale,
                                      SelectionAgent selectionAgent,
                                      DtoUnmarshallerFactory unmarshallerFactory) {
        this.view = view;
        this.view.setDelegate(this);

        this.appContext = appContext;
        this.projectService = projectService;
        this.dtoFactory = dtoFactory;
        this.dialogFactory = dialogFactory;
        this.projectExplorer = projectExplorer;
        this.locale = locale;
        this.selectionAgent = selectionAgent;
        this.unmarshallerFactory = unmarshallerFactory;
    }

    public void showDialog(@NotNull CurrentProject project) {
        parentProject = project;
        view.setParentArtifactId(project.getAttributeValue(ARTIFACT_ID));
        view.setGroupId(project.getAttributeValue(GROUP_ID));
        view.setVersion(project.getAttributeValue(VERSION));
        view.reset();
        view.show();
        updateViewState();
    }

    @Override
    public void onClose() {
    }

    @Override
    public void create() {
        ProjectConfigDto projectConfig = dtoFactory.createDto(ProjectConfigDto.class);
        projectConfig.setType(MAVEN_ID);

        Map<String, List<String>> attributes = new HashMap<>();
        attributes.put(ARTIFACT_ID, Arrays.asList(artifactId));
        attributes.put(GROUP_ID, Arrays.asList(view.getGroupId()));
        attributes.put(VERSION, Arrays.asList(view.getVersion()));
        attributes.put(PACKAGING, Arrays.asList(view.getPackaging()));
        attributes.put(PARENT_ARTIFACT_ID, Arrays.asList(parentProject.getAttributeValue(ARTIFACT_ID)));
        attributes.put(PARENT_GROUP_ID, Arrays.asList(parentProject.getAttributeValue(GROUP_ID)));
        attributes.put(PARENT_VERSION, Arrays.asList(parentProject.getAttributeValue(VERSION)));

        projectConfig.setAttributes(attributes);
        projectConfig.setName(view.getName());

        view.showButtonLoader(true);

        String pathToSelectedNode = getPathToSelectedNode();

        if (pathToSelectedNode.isEmpty()) {
            showErrorDialog(locale.mavenCreateModuleMultySelectionError());

            return;
        }

        projectService.createModule(pathToSelectedNode,
                                    projectConfig,
                                    new AsyncRequestCallback<ProjectConfigDto>(
                                            unmarshallerFactory.newUnmarshaller(ProjectConfigDto.class)) {
                                        @Override
                                        protected void onSuccess(ProjectConfigDto addedModule) {
                                            view.close();
                                            view.showButtonLoader(false);

                                            Selection<?> selection = selectionAgent.getSelection();

                                            HasStorablePath parentFolder = (HasStorablePath)selection.getHeadElement();

                                            boolean isModule = parentFolder instanceof ModuleNode;
                                            boolean isProject = parentFolder instanceof ProjectNode;

                                            ProjectConfigDto projectConfigDto = appContext.getCurrentProject().getProjectConfig();

                                            ProjectConfigDto parentConfig =
                                                    projectConfigDto.findModule(parentFolder.getStorablePath());

                                            if (parentConfig == null) {
                                                throw new IllegalArgumentException("Parent folder not fount for " + addedModule.getPath());
                                            }

                                            parentConfig.getModules().add(addedModule);

                                            if (isModule) {
                                                projectExplorer.reloadChildren((ModuleNode)parentFolder);
                                            }

                                            if (isProject) {
                                                projectExplorer.reloadChildren((ProjectNode)parentFolder);
                                            }
                                        }

                                        @Override
                                        protected void onFailure(Throwable exception) {
                                            showErrorDialog(exception.getMessage());
                                        }
                                    });
    }

    private String getPathToSelectedNode() {
        Selection<?> selection = projectExplorer.getSelection();

        if (selection.isMultiSelection() || selection.isEmpty()) {
            return "";
        }

        Object selectedElement = selection.getHeadElement();

        if (selectedElement instanceof HasStorablePath) {<commit_msg>Typo fix<commit_after>/*******************************************************************************
 * Copyright (c) 2012-2015 <NAME>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   <NAME>. - initial API and implementation
 *******************************************************************************/
package org.eclipse.che.ide.extension.maven.client.module;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.eclipse.che.api.project.gwt.client.ProjectServiceClient;
import org.eclipse.che.api.project.shared.dto.GeneratorDescription;
import org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.app.CurrentProject;
import org.eclipse.che.ide.api.project.node.HasStorablePath;
import org.eclipse.che.ide.api.selection.Selection;
import org.eclipse.che.ide.api.selection.SelectionAgent;
import org.eclipse.che.ide.dto.DtoFactory;
import org.eclipse.che.ide.extension.maven.client.MavenArchetype;
import org.eclipse.che.ide.extension.maven.client.MavenExtension;
import org.eclipse.che.ide.extension.maven.client.MavenLocalizationConstant;
import org.eclipse.che.ide.part.explorer.project.ProjectExplorerPresenter;
import org.eclipse.che.ide.project.node.ModuleNode;
import org.eclipse.che.ide.project.node.ProjectNode;
import org.eclipse.che.ide.rest.AsyncRequestCallback;
import org.eclipse.che.ide.rest.DtoUnmarshallerFactory;
import org.eclipse.che.ide.ui.dialogs.DialogFactory;
import org.eclipse.che.ide.util.NameUtils;
import org.eclipse.che.ide.util.loging.Log;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.ARCHETYPE_GENERATION_STRATEGY;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.ARTIFACT_ID;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.GROUP_ID;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.MAVEN_ID;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.PACKAGING;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.PARENT_ARTIFACT_ID;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.PARENT_GROUP_ID;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.PARENT_VERSION;
import static org.eclipse.che.ide.extension.maven.shared.MavenAttributes.VERSION;

/**
 * @author <NAME>
 * @author <NAME>
 */
@Singleton
public class CreateMavenModulePresenter implements CreateMavenModuleView.ActionDelegate {

    private final AppContext                appContext;
    private final CreateMavenModuleView     view;
    private final ProjectServiceClient      projectService;
    private final DtoFactory                dtoFactory;
    private final DialogFactory             dialogFactory;
    private final ProjectExplorerPresenter  projectExplorer;
    private final MavenLocalizationConstant locale;
    private final SelectionAgent            selectionAgent;
    private final DtoUnmarshallerFactory    unmarshallerFactory;

    private String moduleName;

    private String         artifactId;
    private CurrentProject parentProject;

    @Inject
    public CreateMavenModulePresenter(AppContext appContext,
                                      CreateMavenModuleView view,
                                      ProjectServiceClient projectService,
                                      DtoFactory dtoFactory,
                                      DialogFactory dialogFactory,
                                      ProjectExplorerPresenter projectExplorer,
                                      MavenLocalizationConstant locale,
                                      SelectionAgent selectionAgent,
                                      DtoUnmarshallerFactory unmarshallerFactory) {
        this.view = view;
        this.view.setDelegate(this);

        this.appContext = appContext;
        this.projectService = projectService;
        this.dtoFactory = dtoFactory;
        this.dialogFactory = dialogFactory;
        this.projectExplorer = projectExplorer;
        this.locale = locale;
        this.selectionAgent = selectionAgent;
        this.unmarshallerFactory = unmarshallerFactory;
    }

    public void showDialog(@NotNull CurrentProject project) {
        parentProject = project;
        view.setParentArtifactId(project.getAttributeValue(ARTIFACT_ID));
        view.setGroupId(project.getAttributeValue(GROUP_ID));
        view.setVersion(project.getAttributeValue(VERSION));
        view.reset();
        view.show();
        updateViewState();
    }

    @Override
    public void onClose() {
    }

    @Override
    public void create() {
        ProjectConfigDto projectConfig = dtoFactory.createDto(ProjectConfigDto.class);
        projectConfig.setType(MAVEN_ID);

        Map<String, List<String>> attributes = new HashMap<>();
        attributes.put(ARTIFACT_ID, Arrays.asList(artifactId));
        attributes.put(GROUP_ID, Arrays.asList(view.getGroupId()));
        attributes.put(VERSION, Arrays.asList(view.getVersion()));
        attributes.put(PACKAGING, Arrays.asList(view.getPackaging()));
        attributes.put(PARENT_ARTIFACT_ID, Arrays.asList(parentProject.getAttributeValue(ARTIFACT_ID)));
        attributes.put(PARENT_GROUP_ID, Arrays.asList(parentProject.getAttributeValue(GROUP_ID)));
        attributes.put(PARENT_VERSION, Arrays.asList(parentProject.getAttributeValue(VERSION)));

        projectConfig.setAttributes(attributes);
        projectConfig.setName(view.getName());

        view.showButtonLoader(true);

        String pathToSelectedNode = getPathToSelectedNode();

        if (pathToSelectedNode.isEmpty()) {
            showErrorDialog(locale.mavenCreateModuleMultySelectionError());

            return;
        }

        projectService.createModule(pathToSelectedNode,
                                    projectConfig,
                                    new AsyncRequestCallback<ProjectConfigDto>(
                                            unmarshallerFactory.newUnmarshaller(ProjectConfigDto.class)) {
                                        @Override
                                        protected void onSuccess(ProjectConfigDto addedModule) {
                                            view.close();
                                            view.showButtonLoader(false);

                                            Selection<?> selection = selectionAgent.getSelection();

                                            HasStorablePath parentFolder = (HasStorablePath)selection.getHeadElement();

                                            boolean isModule = parentFolder instanceof ModuleNode;
                                            boolean isProject = parentFolder instanceof ProjectNode;

                                            ProjectConfigDto projectConfigDto = appContext.getCurrentProject().getProjectConfig();

                                            ProjectConfigDto parentConfig =
                                                    projectConfigDto.findModule(parentFolder.getStorablePath());

                                            if (parentConfig == null) {
                                                throw new IllegalArgumentException("Parent folder not found for " + addedModule.getPath());
                                            }

                                            parentConfig.getModules().add(addedModule);

                                            if (isModule) {
                                                projectExplorer.reloadChildren((ModuleNode)parentFolder);
                                            }

                                            if (isProject) {
                                                projectExplorer.reloadChildren((ProjectNode)parentFolder);
                                            }
                                        }

                                        @Override
                                        protected void onFailure(Throwable exception) {
                                            showErrorDialog(exception.getMessage());
                                        }
                                    });
    }

    private String getPathToSelectedNode() {
        Selection<?> selection = projectExplorer.getSelection();

        if (selection.isMultiSelection() || selection.isEmpty()) {
            return "";
        }

        Object selectedElement = selection.getHeadElement();

        if (selectedElement instanceof HasStorablePath) {
{'fraction_non_alphanumeric': 0.09517957884153494, 'fraction_numerical': 0.0013405574484723231, 'mean_word_length': 2.104022191400832, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 118, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2504254', 'n_tokens_mistral': 4140, 'n_tokens_neox': 4009, 'n_words': 695}