<commit_before>/*
 *
 * *********************************************************************
 * fsdevtools
 * %%
 * Copyright (C) 2016 e-Spirit AG
 * %%
 * 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 com.espirit.moddev.cli;

import com.espirit.moddev.cli.exception.CliError;
import com.espirit.moddev.cli.exception.CliException;
import com.espirit.moddev.cli.api.configuration.Config;
import com.espirit.moddev.cli.api.configuration.ImportConfig;
import com.espirit.moddev.cli.api.CliContext;
import de.espirit.firstspirit.access.AdminService;
import de.espirit.firstspirit.access.Connection;
import de.espirit.firstspirit.access.ConnectionManager;
import de.espirit.firstspirit.access.UserService;
import de.espirit.firstspirit.access.admin.ProjectStorage;
import de.espirit.firstspirit.access.project.Project;
import de.espirit.firstspirit.agency.BrokerAgent;
import de.espirit.firstspirit.agency.SpecialistType;
import de.espirit.firstspirit.agency.SpecialistsBroker;
import de.espirit.firstspirit.common.IOError;
import de.espirit.firstspirit.common.MaximumNumberOfSessionsExceededException;
import de.espirit.firstspirit.server.authentication.AuthenticationException;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Default implementation of {@link com.espirit.moddev.cli.api.CliContext}.
 *
 * @author <EMAIL>
 */
public class CliContextImpl implements CliContext {

    private static final Logger LOGGER = LoggerFactory.getLogger(CliContextImpl.class);

    private final Map<String, Object> properties;
    private final Config clientConfig;
    private Connection connection;
    private SpecialistsBroker projectBroker;

    /**
     * Create a new instance that uses the given {@link com.espirit.moddev.cli.api.configuration.Config}.
     *
     * @param clientConfig the configuration to be used
     * @throws java.lang.IllegalArgumentException if clientConfig is null
     */
    public CliContextImpl(final Config clientConfig) {
        if (clientConfig == null) {
            throw new IllegalArgumentException("Config is null!");
        }
        this.clientConfig = clientConfig;
        properties = new HashMap<>();
        initializeFirstSpiritConnection();
    }

    private void initializeFirstSpiritConnection() {
        openConnection();
        requireProjectSpecificBroker();
    }

    private void openConnection() {
        try {
            connection = obtainConnection();
            Object[] args = {clientConfig.getHost(), clientConfig.getPort(), clientConfig.getUser()};
            LOGGER
                .debug("Connect to fs-server '{}:{}' with user '{}'...", args);
            connection.connect();
        } catch (MaximumNumberOfSessionsExceededException e) {
            throw new CliException(CliError.SESSIONS, clientConfig, e);
        } catch (AuthenticationException e) {
            throw new CliException(CliError.AUTHENTICATION, clientConfig, e);
        } catch (IOException e) {
            throw new CliException(CliError.GENERAL_IO, clientConfig, e);
        } catch (IOError e) {
            throw new CliException(e);
        } catch (Exception e) { //NOSONAR
            throw new CliException(CliError.UNEXPECTED, clientConfig, e);
        }
    }

    /**
     * Connect to the FirstSpirit server using the configuration of this instance.
     *
     * @return a connection to a FirstSpirit server
     */
    protected Connection obtainConnection() {
        return ConnectionManager
            .getConnection(clientConfig.getHost(), clientConfig.getPort(), clientConfig.getConnectionMode().getCode(), clientConfig.getUser(),
                           clientConfig.getPassword());
    }

    private void requireProjectSpecificBroker() {
        LOGGER.debug("Require project specific specialist broker for project '{}'...", clientConfig.getProject());

        String name;
        try {
            final Project project = getProject();
            name = project != null ? project.getName() : null;
        } catch (Exception e) { //NOSONAR
            throw new IllegalStateException(
                "Project '" + clientConfig.getProject() + "' not found on server. Correct project name or omit --dont-create-project option.", e);
        }

        if (StringUtils.isNotBlank(name)) {
            final SpecialistsBroker broker = connection.getBroker();
            final BrokerAgent brokerAgent = broker.requireSpecialist(BrokerAgent.TYPE);
            projectBroker = brokerAgent.getBrokerByProjectName(name);
        }
        if(projectBroker == null) {
            throw new IllegalStateException("ProjectBroker cannot be retrieved for project " + name);
        }
    }

    @Override
    public UserService getUserService() {
        return getProject().getUserService();
    }

    @Override
    public Project getProject() {
        final String projectName = clientConfig.getProject();
        Project project = connection.getProjectByName(projectName);
        if (project == null && clientConfig.isCreatingProjectIfMissing()) {
            project = createProject(projectName);
        }
        LOGGER.debug("activate project if deactivated: " + clientConfig.isActivateProjectIfDeactivated(), projectName);
        if (clientConfig.isActivateProjectIfDeactivated()) {
            activateProject(projectName, project);
        }
        LOGGER.debug("project is '{}'", project);
        return project;
    }

    private void activateProject(String projectName, Project project) {
        if(project != null) {
            throw new IllegalArgumentException("Project for activation is null");
        }
        if(!project.isActive()) {
            LOGGER.warn("Project '{}' is not active! Try to activate...", projectName);
            UserService userService = project.getUserService();
            AdminService adminService = userService.getConnection().getService(AdminService.class);
            adminService.getProjectStorage().activateProject(project);
        } else {
            LOGGER.debug("Project '{}' is already active! No need to activate...", projectName);
        }
    }<commit_msg>fix sonar issues<commit_after>/*
 *
 * *********************************************************************
 * fsdevtools
 * %%
 * Copyright (C) 2016 e-Spirit AG
 * %%
 * 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 com.espirit.moddev.cli;

import com.espirit.moddev.cli.exception.CliError;
import com.espirit.moddev.cli.exception.CliException;
import com.espirit.moddev.cli.api.configuration.Config;
import com.espirit.moddev.cli.api.configuration.ImportConfig;
import com.espirit.moddev.cli.api.CliContext;
import de.espirit.firstspirit.access.AdminService;
import de.espirit.firstspirit.access.Connection;
import de.espirit.firstspirit.access.ConnectionManager;
import de.espirit.firstspirit.access.UserService;
import de.espirit.firstspirit.access.admin.ProjectStorage;
import de.espirit.firstspirit.access.project.Project;
import de.espirit.firstspirit.agency.BrokerAgent;
import de.espirit.firstspirit.agency.SpecialistType;
import de.espirit.firstspirit.agency.SpecialistsBroker;
import de.espirit.firstspirit.common.IOError;
import de.espirit.firstspirit.common.MaximumNumberOfSessionsExceededException;
import de.espirit.firstspirit.server.authentication.AuthenticationException;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Default implementation of {@link com.espirit.moddev.cli.api.CliContext}.
 *
 * @author e-Spirit AG
 */
public class CliContextImpl implements CliContext {

    private static final Logger LOGGER = LoggerFactory.getLogger(CliContextImpl.class);

    private final Map<String, Object> properties;
    private final Config clientConfig;
    private Connection connection;
    private SpecialistsBroker projectBroker;

    /**
     * Create a new instance that uses the given {@link com.espirit.moddev.cli.api.configuration.Config}.
     *
     * @param clientConfig the configuration to be used
     * @throws java.lang.IllegalArgumentException if clientConfig is null
     */
    public CliContextImpl(final Config clientConfig) {
        if (clientConfig == null) {
            throw new IllegalArgumentException("Config is null!");
        }
        this.clientConfig = clientConfig;
        properties = new HashMap<>();
        initializeFirstSpiritConnection();
    }

    private void initializeFirstSpiritConnection() {
        openConnection();
        requireProjectSpecificBroker();
    }

    private void openConnection() {
        try {
            connection = obtainConnection();
            Object[] args = {clientConfig.getHost(), clientConfig.getPort(), clientConfig.getUser()};
            LOGGER
                .debug("Connect to fs-server '{}:{}' with user '{}'...", args);
            connection.connect();
        } catch (MaximumNumberOfSessionsExceededException e) {
            throw new CliException(CliError.SESSIONS, clientConfig, e);
        } catch (AuthenticationException e) {
            throw new CliException(CliError.AUTHENTICATION, clientConfig, e);
        } catch (IOException e) {
            throw new CliException(CliError.GENERAL_IO, clientConfig, e);
        } catch (IOError e) {
            throw new CliException(e);
        } catch (Exception e) { //NOSONAR
            throw new CliException(CliError.UNEXPECTED, clientConfig, e);
        }
    }

    /**
     * Connect to the FirstSpirit server using the configuration of this instance.
     *
     * @return a connection to a FirstSpirit server
     */
    protected Connection obtainConnection() {
        return ConnectionManager
            .getConnection(clientConfig.getHost(), clientConfig.getPort(), clientConfig.getConnectionMode().getCode(), clientConfig.getUser(),
                           clientConfig.getPassword());
    }

    private void requireProjectSpecificBroker() {
        LOGGER.debug("Require project specific specialist broker for project '{}'...", clientConfig.getProject());

        String name;
        try {
            final Project project = getProject();
            name = project != null ? project.getName() : null;
        } catch (Exception e) { //NOSONAR
            throw new IllegalStateException(
                "Project '" + clientConfig.getProject() + "' not found on server. Correct project name or omit --dont-create-project option.", e);
        }

        if (StringUtils.isNotBlank(name)) {
            final SpecialistsBroker broker = connection.getBroker();
            final BrokerAgent brokerAgent = broker.requireSpecialist(BrokerAgent.TYPE);
            projectBroker = brokerAgent.getBrokerByProjectName(name);
        }
        if(projectBroker == null) {
            throw new IllegalStateException("ProjectBroker cannot be retrieved for project " + name);
        }
    }

    @Override
    public UserService getUserService() {
        return getProject().getUserService();
    }

    @Override
    public Project getProject() {
        final String projectName = clientConfig.getProject();
        Project project = connection.getProjectByName(projectName);
        if (project == null && clientConfig.isCreatingProjectIfMissing()) {
            project = createProject(projectName);
        }
        LOGGER.debug("activate project if deactivated: " + clientConfig.isActivateProjectIfDeactivated(), projectName);
        if (clientConfig.isActivateProjectIfDeactivated()) {
            activateProject(projectName, project);
        }
        LOGGER.debug("project is '{}'", project);
        return project;
    }

    private void activateProject(String projectName, Project project) {
        if(project == null) {
            throw new IllegalArgumentException("Project for activation is null");
        }
        if(!project.isActive()) {
            LOGGER.warn("Project '{}' is not active! Try to activate...", projectName);
            UserService userService = project.getUserService();
            AdminService adminService = userService.getConnection().getService(AdminService.class);
            adminService.getProjectStorage().activateProject(project);
        } else {
            LOGGER.debug("Project '{}' is already active! No need to activate...", projectName);
        }
    }
{'fraction_non_alphanumeric': 0.11747054087682061, 'fraction_numerical': 0.0014638073629510357, 'mean_word_length': 3.151929504709815, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4825234', 'n_tokens_mistral': 3551, 'n_tokens_neox': 3419, 'n_words': 893}