<commit_before>/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers and others.
 * 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
 *******************************************************************************/

package org.eclipse.mylyn.internal.bugzilla.core;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITaskMapping;
import org.eclipse.mylyn.tasks.core.RepositoryResponse;
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.core.data.TaskDataCollector;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class BugzillaTaskDataHandler extends AbstractTaskDataHandler {

	private static final String TASK_DATA_VERSION_1_0 = "1";

	private static final String TASK_DATA_VERSION_2_0 = "2";

	private static final String TASK_DATA_VERSION_CURRENT = "3";

	private final BugzillaRepositoryConnector connector;

	public BugzillaTaskDataHandler(BugzillaRepositoryConnector connector) {
		this.connector = connector;
	}

	public TaskData getTaskData(TaskRepository repository, String taskId, IProgressMonitor monitor)
			throws CoreException {
		try {
			BugzillaClient client = connector.getClientManager().getClient(repository,
					new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN));
			int bugId = BugzillaRepositoryConnector.getBugId(taskId);
			TaskData taskData;
			taskData = client.getTaskData(bugId, getAttributeMapper(repository), monitor);
			return taskData;

		} catch (IOException e) {
			throw new CoreException(new BugzillaStatus(IStatus.ERROR, BugzillaCorePlugin.PLUGIN_ID,
					RepositoryStatus.ERROR_IO, repository.getRepositoryUrl(), e));
		}
	}

	@Override
	public void getMultiTaskData(TaskRepository repository, Set<String> taskIds, TaskDataCollector collector,
			IProgressMonitor monitor) throws CoreException {
		try {
			monitor.beginTask("Receiving tasks", taskIds.size());
			BugzillaClient client = connector.getClientManager().getClient(repository,
					new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN));

			client.getTaskData(taskIds, collector, getAttributeMapper(repository), monitor);
		} catch (IOException e) {
			throw new CoreException(new BugzillaStatus(IStatus.ERROR, BugzillaCorePlugin.PLUGIN_ID,
					RepositoryStatus.ERROR_IO, repository.getRepositoryUrl(), e));
		} finally {
			monitor.done();
		}
	}

	@Override
	public void migrateTaskData(TaskRepository taskRepository, TaskData taskData) {
		String taskDataVersion = taskData.getVersion();
		if (taskDataVersion == null) {
			taskDataVersion = "0";
		}
		// 1: the value was stored in the attribute rather than the key
		if (taskDataVersion.compareTo(TASK_DATA_VERSION_1_0) <= 0) {
			for (TaskAttribute attribute : new ArrayList<TaskAttribute>(taskData.getRoot().getAttributes().values())) {
				if (attribute.getId().equals(BugzillaAttribute.DESC.getKey())) {
					TaskAttribute attrLongDesc = createAttribute(taskData, BugzillaAttribute.LONG_DESC);
					attrLongDesc.setValue(attribute.getValue());
					taskData.getRoot().removeAttribute(BugzillaAttribute.DESC.getKey());
				}
			}
			// Old actions not saved so recreate them upon migration
			RepositoryConfiguration configuration = BugzillaCorePlugin.getRepositoryConfiguration(taskRepository.getRepositoryUrl());
			if (configuration != null) {
				configuration.addValidOperations(taskData);
			}
		}

		// 2: the description type was wrong
		if (taskDataVersion.compareTo(TASK_DATA_VERSION_2_0) <= 0) {
			TaskAttribute attrDescription = taskData.getRoot().getMappedAttribute(BugzillaAttribute.LONG_DESC.getKey());
			if (attrDescription != null) {
				attrDescription.getMetaData().setType(TaskAttribute.TYPE_LONG_RICH_TEXT);
			}
		}

		if (taskDataVersion.compareTo(TASK_DATA_VERSION_CURRENT) < 0) {
			taskData.setVersion(TASK_DATA_VERSION_CURRENT);
		}<commit_msg>NEW - bug 229655: port Bugzilla connector to new 3.0 api https://bugs.eclipse.org/bugs/show_bug.cgi?id=229655<commit_after>/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers and others.
 * 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
 *******************************************************************************/

package org.eclipse.mylyn.internal.bugzilla.core;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITaskMapping;
import org.eclipse.mylyn.tasks.core.RepositoryResponse;
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.core.data.TaskDataCollector;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class BugzillaTaskDataHandler extends AbstractTaskDataHandler {

	private static final String TASK_DATA_VERSION_1_0 = "1";

	private static final String TASK_DATA_VERSION_2_0 = "2";

	private static final String TASK_DATA_VERSION_3_0 = "3";

	private static final String TASK_DATA_VERSION_CURRENT = "4";

	private final BugzillaRepositoryConnector connector;

	public BugzillaTaskDataHandler(BugzillaRepositoryConnector connector) {
		this.connector = connector;
	}

	public TaskData getTaskData(TaskRepository repository, String taskId, IProgressMonitor monitor)
			throws CoreException {
		try {
			BugzillaClient client = connector.getClientManager().getClient(repository,
					new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN));
			int bugId = BugzillaRepositoryConnector.getBugId(taskId);
			TaskData taskData;
			taskData = client.getTaskData(bugId, getAttributeMapper(repository), monitor);
			return taskData;

		} catch (IOException e) {
			throw new CoreException(new BugzillaStatus(IStatus.ERROR, BugzillaCorePlugin.PLUGIN_ID,
					RepositoryStatus.ERROR_IO, repository.getRepositoryUrl(), e));
		}
	}

	@Override
	public void getMultiTaskData(TaskRepository repository, Set<String> taskIds, TaskDataCollector collector,
			IProgressMonitor monitor) throws CoreException {
		try {
			monitor.beginTask("Receiving tasks", taskIds.size());
			BugzillaClient client = connector.getClientManager().getClient(repository,
					new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN));

			client.getTaskData(taskIds, collector, getAttributeMapper(repository), monitor);
		} catch (IOException e) {
			throw new CoreException(new BugzillaStatus(IStatus.ERROR, BugzillaCorePlugin.PLUGIN_ID,
					RepositoryStatus.ERROR_IO, repository.getRepositoryUrl(), e));
		} finally {
			monitor.done();
		}
	}

	@Override
	public void migrateTaskData(TaskRepository taskRepository, TaskData taskData) {
		String taskDataVersion = taskData.getVersion();
		if (taskDataVersion == null) {
			taskDataVersion = "0";
		}
		// 1: the value was stored in the attribute rather than the key
		if (taskDataVersion.compareTo(TASK_DATA_VERSION_1_0) <= 0) {
			for (TaskAttribute attribute : new ArrayList<TaskAttribute>(taskData.getRoot().getAttributes().values())) {
				if (attribute.getId().equals(BugzillaAttribute.DESC.getKey())) {
					TaskAttribute attrLongDesc = createAttribute(taskData, BugzillaAttribute.LONG_DESC);
					attrLongDesc.setValue(attribute.getValue());
					taskData.getRoot().removeAttribute(BugzillaAttribute.DESC.getKey());
				}
			}
			// Old actions not saved so recreate them upon migration
			RepositoryConfiguration configuration = BugzillaCorePlugin.getRepositoryConfiguration(taskRepository.getRepositoryUrl());
			if (configuration != null) {
				configuration.addValidOperations(taskData);
			}
		}

		// 2: the description type was wrong
		if (taskDataVersion.compareTo(TASK_DATA_VERSION_2_0) <= 0) {
			TaskAttribute attrDescription = taskData.getRoot().getMappedAttribute(BugzillaAttribute.LONG_DESC.getKey());
			if (attrDescription != null) {
				attrDescription.getMetaData().setType(TaskAttribute.TYPE_LONG_RICH_TEXT);
			}
		}

		// 3: the comment type was wrong
		if (taskDataVersion.compareTo(TASK_DATA_VERSION_3_0) <= 0) {
			TaskAttribute attrNewComment = taskData.getRoot()
					.getMappedAttribute(BugzillaAttribute.NEW_COMMENT.getKey());
			if (attrNewComment != null) {
				attrNewComment.getMetaData().setType(TaskAttribute.TYPE_LONG_RICH_TEXT);
			}
		}

		if (taskDataVersion.compareTo(TASK_DATA_VERSION_CURRENT) < 0) {
			taskData.setVersion(TASK_DATA_VERSION_CURRENT);
		}
{'fraction_non_alphanumeric': 0.12825955124317767, 'fraction_numerical': 0.007984637153830604, 'mean_word_length': 5.676788124156545, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 11, 'https://': 1, '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': '2567721', 'n_tokens_mistral': 3126, 'n_tokens_neox': 2758, 'n_words': 581}