<commit_before>/*
 * Copyright (c) 2005-2013, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 * 
 * WSO2 Inc. licenses this file to you 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.wso2.carbon.rssmanager.core.environment;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.rssmanager.common.RSSManagerConstants;
import org.wso2.carbon.rssmanager.core.authorize.RSSAuthorizer;
import org.wso2.carbon.rssmanager.core.config.RSSManagementRepository;
import org.wso2.carbon.rssmanager.core.dao.exception.RSSDAOException;
import org.wso2.carbon.rssmanager.core.dao.exception.RSSDatabaseConnectionException;
import org.wso2.carbon.rssmanager.core.dto.common.DatabasePrivilegeSet;
import org.wso2.carbon.rssmanager.core.dto.common.DatabasePrivilegeTemplate;
import org.wso2.carbon.rssmanager.core.dto.common.DatabasePrivilegeTemplateEntry;
import org.wso2.carbon.rssmanager.core.dto.restricted.RSSInstance;
import org.wso2.carbon.rssmanager.core.environment.dao.DatabasePrivilegeTemplateDAO;
import org.wso2.carbon.rssmanager.core.environment.dao.EnvironmentDAO;
import org.wso2.carbon.rssmanager.core.environment.dao.EnvironmentManagementDAOFactory;
import org.wso2.carbon.rssmanager.core.environment.dao.RSSInstanceDAO;
import org.wso2.carbon.rssmanager.core.exception.RSSManagerException;
import org.wso2.carbon.rssmanager.core.manager.adaptor.RSSManagerAdaptor;
import org.wso2.carbon.rssmanager.core.manager.adaptor.RSSManagerAdaptorFactory;
import org.wso2.carbon.rssmanager.core.util.RSSManagerUtil;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class EnvironmentManager {

	private Environment[] environments;
	private String[] environmentNames;
	private EnvironmentDAO environmentDAO;
	private RSSInstanceDAO rssInstanceDAO;
	private DatabasePrivilegeTemplateDAO privilegeTemplateDAO;
	private DatabasePrivilegeTemplateEntryDAO privilegeTemplateEntryDAO;
	private static final Log log = LogFactory.getLog(EnvironmentManager.class);

	public EnvironmentManager(Environment[] environments) {
		this.environments = environments;
		this.environmentNames = this.processEnvironments();
		environmentDAO = EnvironmentManagementDAOFactory.getEnvironmentManagementDAO().getEnvironmentDAO();
		rssInstanceDAO = EnvironmentManagementDAOFactory.getEnvironmentManagementDAO().getRSSInstanceDAO();
		privilegeTemplateDAO = EnvironmentManagementDAOFactory.getEnvironmentManagementDAO().getDatabasePrivilegeTemplateDAO();
		privilegeTemplateEntryDAO = EnvironmentManagementDAOFactory.getEnvironmentManagementDAO().getDatabasePrivilegeTemplateEntryDAO();
	}

	/**
	 * Validate existence of environment in meta repository
	 * @param environmentName name of the environment
	 * @return valid environment
	 * @throws RSSManagerException if error occurred when checking environment validity
	 */
	private Environment validateEnvironment(String environmentName)
			throws RSSManagerException, RSSDatabaseConnectionException {
		if (environmentName == null || environmentName.trim().length() == 0) {
			throw new RSSManagerException(" Environment name is null ");
		}
		Environment environment;
		try {
			environment = environmentDAO.getEnvironment(environmentName);
			if (environment == null) {
				throw new RSSManagerException(" Environment doesn't exist ");
			}
		} catch (RSSDAOException e) {
			throw new RSSManagerException("Error while getting environment " + environmentName, e);
		}
		return environment;
	}

	/**
	 * Remove environment from meta repository
	 *
	 * @param environmentName name of the environment
	 * @throws RSSManagerException if error occurred when removing environment
	 */
	@Deprecated
	public void removeEnvironment(String environmentName)
			throws RSSManagerException, RSSDatabaseConnectionException {
		try {
			environmentDAO.removeEnvironment(environmentName);
		} catch (RSSDAOException e) {
			String msg = "Error occurred while removing RSS environment '" + environmentName + "'";
			handleException(msg, e);
		}
	}

	/**
	 * Add rss instance to meta repository
	 *
	 * @param rssInstance rss instance configuration
	 * @return rss instance
	 * @throws RSSManagerException if error occured when adding rss instance
	 */
	public RSSInstance addRSSInstance(RSSInstance rssInstance)
			throws RSSManagerException, RSSDatabaseConnectionException {
		try {
			int tenantId = RSSManagerUtil.getTenantId();
			Environment environment = validateEnvironment(rssInstance.getEnvironmentName());
			RSSInstance existingInstance = rssInstanceDAO.getRSSInstance(rssInstance.getEnvironmentName(), rssInstance.getName(), tenantId);
			if (existingInstance != null) {
				throw new RSSManagerException("RSSInstance "+rssInstance.getName()+" already exist");
			}
			rssInstance.setEnvironment(environment);
			rssInstance.setTenantId((long) tenantId);
			rssInstanceDAO.addRSSInstance(environment.getName(), rssInstance, tenantId);
			return rssInstance;
		} catch (RSSDAOException e) {
			throw new RSSManagerException(
					"Error occurred while adding RSS instance '" + rssInstance.getName() + "' to environment '" +
					rssInstance.getEnvironmentName() + "'" + " Reason : " + e.getMessage());
		}
	}

	/**
	 * Remove rss instance from the database
	 *
	 * @param environmentName name of the environment
	 * @param rssInstanceName name if the rss instance
	 * @throws RSSManagerException
	 */
	public void removeRSSInstance(String environmentName, String rssInstanceName) throws RSSManagerException {
		try {
			final int tenantId = RSSManagerUtil.getTenantId();
			rssInstanceDAO.removeRSSInstance(environmentName, rssInstanceName, tenantId);
		} catch (RSSDAOException e) {
			String msg = "Error occurred while removing metadata related to " + "RSS instance '" + rssInstanceName + "' " +
			             "from RSS metadata repository : " + e.getMessage();
			handleException(msg, e);
		}
	}

	/**
	 * Update the rss instance meta repository
	 *
	 * @param environmentName name of the environment
	 * @param rssInstance rss instance configuration
	 * @throws RSSManagerException if error occur when updating rss instance
	 */
	public void updateRSSInstance(String environmentName, RSSInstance rssInstance)
			throws RSSManagerException, RSSDatabaseConnectionException {
		try {
			final int tenantId = RSSManagerUtil.getTenantId();
			Environment env = validateEnvironment(environmentName);
			RSSInstance entity = rssInstanceDAO.getRSSInstance(environmentName, rssInstance.getName(), tenantId);
			entity.setServerURL(rssInstance.getDataSourceConfig() == null ? rssInstance.getServerURL() : rssInstance.getDataSourceConfig()
					.getRdbmsConfiguration()
					.getUrl());
			entity.setServerCategory(rssInstance.getServerCategory());
			entity.setAdminUserName(rssInstance.getDataSourceConfig() == null ? rssInstance.getAdminUserName() : rssInstance.getDataSourceConfig()
					.getRdbmsConfiguration()
					.getUsername());
			entity.setAdminPassword(rssInstance.getDataSourceConfig() == null ? rssInstance.getAdminPassword() : rssInstance.getDataSourceConfig()
					.getRdbmsConfiguration()
					.getPassword());
			entity.setTenantId((long) tenantId);
			entity.setDbmsType(rssInstance.getInstanceType());
			entity.setEnvironment(env);
			entity.setSshInformationConfig(rssInstance.getSshInformationConfig());
			entity.setSnapshotConfig(rssInstance.getSnapshotConfig());
			rssInstanceDAO.updateRSSInstance(environmentName, entity, tenantId);
		} catch (RSSDAOException e) {
			String msg = "Error occurred while updating metadata related to " + "RSS instance '" + rssInstance.getName() +
			             "' in RSS metadata repository : " + e.getMessage();
			handleException(msg, e);
		}<commit_msg>fixing type getting reset when edit Database SS-486<commit_after>/*
 * Copyright (c) 2005-2013, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 * 
 * WSO2 Inc. licenses this file to you 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.wso2.carbon.rssmanager.core.environment;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.rssmanager.common.RSSManagerConstants;
import org.wso2.carbon.rssmanager.core.authorize.RSSAuthorizer;
import org.wso2.carbon.rssmanager.core.config.RSSManagementRepository;
import org.wso2.carbon.rssmanager.core.dao.exception.RSSDAOException;
import org.wso2.carbon.rssmanager.core.dao.exception.RSSDatabaseConnectionException;
import org.wso2.carbon.rssmanager.core.dto.common.DatabasePrivilegeSet;
import org.wso2.carbon.rssmanager.core.dto.common.DatabasePrivilegeTemplate;
import org.wso2.carbon.rssmanager.core.dto.common.DatabasePrivilegeTemplateEntry;
import org.wso2.carbon.rssmanager.core.dto.restricted.RSSInstance;
import org.wso2.carbon.rssmanager.core.environment.dao.DatabasePrivilegeTemplateDAO;
import org.wso2.carbon.rssmanager.core.environment.dao.EnvironmentDAO;
import org.wso2.carbon.rssmanager.core.environment.dao.EnvironmentManagementDAOFactory;
import org.wso2.carbon.rssmanager.core.environment.dao.RSSInstanceDAO;
import org.wso2.carbon.rssmanager.core.exception.RSSManagerException;
import org.wso2.carbon.rssmanager.core.manager.adaptor.RSSManagerAdaptor;
import org.wso2.carbon.rssmanager.core.manager.adaptor.RSSManagerAdaptorFactory;
import org.wso2.carbon.rssmanager.core.util.RSSManagerUtil;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class EnvironmentManager {

	private Environment[] environments;
	private String[] environmentNames;
	private EnvironmentDAO environmentDAO;
	private RSSInstanceDAO rssInstanceDAO;
	private DatabasePrivilegeTemplateDAO privilegeTemplateDAO;
	private DatabasePrivilegeTemplateEntryDAO privilegeTemplateEntryDAO;
	private static final Log log = LogFactory.getLog(EnvironmentManager.class);

	public EnvironmentManager(Environment[] environments) {
		this.environments = environments;
		this.environmentNames = this.processEnvironments();
		environmentDAO = EnvironmentManagementDAOFactory.getEnvironmentManagementDAO().getEnvironmentDAO();
		rssInstanceDAO = EnvironmentManagementDAOFactory.getEnvironmentManagementDAO().getRSSInstanceDAO();
		privilegeTemplateDAO = EnvironmentManagementDAOFactory.getEnvironmentManagementDAO().getDatabasePrivilegeTemplateDAO();
		privilegeTemplateEntryDAO = EnvironmentManagementDAOFactory.getEnvironmentManagementDAO().getDatabasePrivilegeTemplateEntryDAO();
	}

	/**
	 * Validate existence of environment in meta repository
	 * @param environmentName name of the environment
	 * @return valid environment
	 * @throws RSSManagerException if error occurred when checking environment validity
	 */
	private Environment validateEnvironment(String environmentName)
			throws RSSManagerException, RSSDatabaseConnectionException {
		if (environmentName == null || environmentName.trim().length() == 0) {
			throw new RSSManagerException(" Environment name is null ");
		}
		Environment environment;
		try {
			environment = environmentDAO.getEnvironment(environmentName);
			if (environment == null) {
				throw new RSSManagerException(" Environment doesn't exist ");
			}
		} catch (RSSDAOException e) {
			throw new RSSManagerException("Error while getting environment " + environmentName, e);
		}
		return environment;
	}

	/**
	 * Remove environment from meta repository
	 *
	 * @param environmentName name of the environment
	 * @throws RSSManagerException if error occurred when removing environment
	 */
	@Deprecated
	public void removeEnvironment(String environmentName)
			throws RSSManagerException, RSSDatabaseConnectionException {
		try {
			environmentDAO.removeEnvironment(environmentName);
		} catch (RSSDAOException e) {
			String msg = "Error occurred while removing RSS environment '" + environmentName + "'";
			handleException(msg, e);
		}
	}

	/**
	 * Add rss instance to meta repository
	 *
	 * @param rssInstance rss instance configuration
	 * @return rss instance
	 * @throws RSSManagerException if error occured when adding rss instance
	 */
	public RSSInstance addRSSInstance(RSSInstance rssInstance)
			throws RSSManagerException, RSSDatabaseConnectionException {
		try {
			int tenantId = RSSManagerUtil.getTenantId();
			Environment environment = validateEnvironment(rssInstance.getEnvironmentName());
			RSSInstance existingInstance = rssInstanceDAO.getRSSInstance(rssInstance.getEnvironmentName(), rssInstance.getName(), tenantId);
			if (existingInstance != null) {
				throw new RSSManagerException("RSSInstance "+rssInstance.getName()+" already exist");
			}
			rssInstance.setEnvironment(environment);
			rssInstance.setTenantId((long) tenantId);
			rssInstanceDAO.addRSSInstance(environment.getName(), rssInstance, tenantId);
			return rssInstance;
		} catch (RSSDAOException e) {
			throw new RSSManagerException(
					"Error occurred while adding RSS instance '" + rssInstance.getName() + "' to environment '" +
					rssInstance.getEnvironmentName() + "'" + " Reason : " + e.getMessage());
		}
	}

	/**
	 * Remove rss instance from the database
	 *
	 * @param environmentName name of the environment
	 * @param rssInstanceName name if the rss instance
	 * @throws RSSManagerException
	 */
	public void removeRSSInstance(String environmentName, String rssInstanceName) throws RSSManagerException {
		try {
			final int tenantId = RSSManagerUtil.getTenantId();
			rssInstanceDAO.removeRSSInstance(environmentName, rssInstanceName, tenantId);
		} catch (RSSDAOException e) {
			String msg = "Error occurred while removing metadata related to " + "RSS instance '" + rssInstanceName + "' " +
			             "from RSS metadata repository : " + e.getMessage();
			handleException(msg, e);
		}
	}

	/**
	 * Update the rss instance meta repository
	 *
	 * @param environmentName name of the environment
	 * @param rssInstance rss instance configuration
	 * @throws RSSManagerException if error occur when updating rss instance
	 */
	public void updateRSSInstance(String environmentName, RSSInstance rssInstance)
			throws RSSManagerException, RSSDatabaseConnectionException {
		try {
			final int tenantId = RSSManagerUtil.getTenantId();
			Environment env = validateEnvironment(environmentName);
			RSSInstance entity = rssInstanceDAO.getRSSInstance(environmentName, rssInstance.getName(), tenantId);
			entity.setServerURL(rssInstance.getDataSourceConfig() == null ? rssInstance.getServerURL() : rssInstance.getDataSourceConfig()
					.getRdbmsConfiguration()
					.getUrl());
			entity.setServerCategory(rssInstance.getServerCategory());
			entity.setAdminUserName(rssInstance.getDataSourceConfig() == null ? rssInstance.getAdminUserName() : rssInstance.getDataSourceConfig()
					.getRdbmsConfiguration()
					.getUsername());
			entity.setAdminPassword(rssInstance.getDataSourceConfig() == null ? rssInstance.getAdminPassword() : rssInstance.getDataSourceConfig()
					.getRdbmsConfiguration()
					.getPassword());
			entity.setTenantId((long) tenantId);
			entity.setDbmsType(rssInstance.getDbmsType());
			entity.setEnvironment(env);
			entity.setSshInformationConfig(rssInstance.getSshInformationConfig());
			entity.setSnapshotConfig(rssInstance.getSnapshotConfig());
			rssInstanceDAO.updateRSSInstance(environmentName, entity, tenantId);
		} catch (RSSDAOException e) {
			String msg = "Error occurred while updating metadata related to " + "RSS instance '" + rssInstance.getName() +
			             "' in RSS metadata repository : " + e.getMessage();
			handleException(msg, e);
		}
{'fraction_non_alphanumeric': 0.09126271693596649, 'fraction_numerical': 0.004248952722920407, 'mean_word_length': 5.4051360674587965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5634744', 'n_tokens_mistral': 4989, 'n_tokens_neox': 4501, 'n_words': 1083}