Mylyn/Incubator/Generic SQL Connector/Configuring Industrial Connector using Nothing/persistorsource

From Eclipsepedia

Jump to: navigation, search
/*******************************************************************************
 * Copyright (c) 2008 Industrial TSI and Maarten Meijer.
 * 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:
 *    Industrial TSI - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.industrial.demo.memory;    

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set; 

import org.eclipse.core.runtime.CoreException;
import org.eclipse.mylyn.industrial.core.dto.IndustrialAttachment;
import org.eclipse.mylyn.industrial.core.dto.IndustrialComment;
import org.eclipse.mylyn.industrial.core.dto.IndustrialQueryParams;
import org.eclipse.mylyn.industrial.core.dto.IndustrialTask;
import org.eclipse.mylyn.industrial.core.persistence.IPersistor;
import org.eclipse.mylyn.tasks.core.TaskRepository; 

/**
 * On-The-Fly (Memory) implementation of the interface
 * org.eclipse.mylyn.industrial.core.persistence.IPersistor
 * 
 * @author Wim Jongman
 * @since 0.8.0
 * 
 */ 

// The first part just contains some fields we are going to use as our storage medium and the default constructor.

public class MemoryPersistor implements IPersistor {

	private HashMap<String, IndustrialTask> tasks = new HashMap<String, IndustrialTask>();

	private HashMap<String, ArrayList<IndustrialComment>> comments = new HashMap<String, ArrayList<IndustrialComment>>();

	private Map<String, byte[]> blobs = new HashMap<String, byte[]>();

	private Map<String, List<IndustrialAttachment>> attachements = new HashMap<String, List<IndustrialAttachment>>();

	public MemoryPersistor() {
	}

// Defines if this repository can be intitialized. This means that the class defined in the org.eclipse.mylyn.sql.core.database.dbinitializer may be called to do initialization work. Whatever that may be is up to the implementor of that class.

	public boolean canInitialize(TaskRepository repository) throws SQLException, CoreException {
		return false;
	}

// Fetches additional Mylyn attributes from the repository. Until the Mylyn attribute task.common.user.reporter.name is supported by its own method, it can be supplied by this method. We need this to supply the task reporter name.

	public Map<String, String> fetchAdditional(TaskRepository repository, String... key) throws SQLException,
			CoreException {

		Map<String, String> result = new HashMap<String, String>();
		result.put("task.common.user.reporter.name", System.getProperty("user.name"));
		return result;
	}

// Return the blob content of the attachment with the given key. The passed id of the attachment is read from the task so this method should not return null.

	public byte[] fetchAttachmentBlob(TaskRepository repository, String attachmentId) throws SQLException,
			CoreException {
		return blobs.get(attachmentId);
	}

// Given a repository and a task key, return all attachments belonging to the task with the given key.

	public List<IndustrialAttachment> fetchAttachments(TaskRepository repository, String... taskId)
			throws SQLException, CoreException {

		List<IndustrialAttachment> as = attachements.get(taskId[0]);
		return (as != null) ? as : new ArrayList<IndustrialAttachment>();

	}

// Given a repository and one or more task keys, return all comments belonging to the task with the given keys.

	public List<IndustrialComment> fetchComments(TaskRepository repository, String... taskId) throws SQLException,
			CoreException {
		List<IndustrialComment> cs = comments.get(taskId[0]);
		return (cs != null) ? cs : new ArrayList<IndustrialComment>();
	}

// Fetches a task from the repository

	public IndustrialTask fetchTask(TaskRepository repository, String... taskId) throws SQLException, CoreException {
		return tasks.get(taskId[0]);
	}

// Search for all tasks obeying the given criteria and return their keys. This just returns all tasks, no effort was made to match each task against the criteria

	public List<String> findTasks(TaskRepository repository, IndustrialQueryParams criteria) throws SQLException,
			CoreException {
		Set<String> keys = tasks.keySet();
		if (keys != null)
			return new ArrayList<String>(keys);
		return new ArrayList<String>();
	}

// This list of allowed statusses is also used to populate a Status task attribute and to query the repository.

	public List<String> getLegalIssueStatus(TaskRepository repository) throws SQLException, CoreException {
		return Arrays.asList(new String[] { "OPEN", "CLOSED" });
	}

// This list of owners is also used to populate the Owner task attribute and to query the repository.

	public List<String> getLegalOwners(TaskRepository repository) throws SQLException, CoreException {
		return Arrays.asList(new String[] { System.getProperty("user.name"), "Some othe people" });
	}

// This list of priorities is also used to populate a Priority task attribute and to query the repository.

	public List<String> getLegalPriorities(TaskRepository repository) throws SQLException, CoreException {
		return Arrays.asList(new String[] { "High", "Medium", "Low" });
	}

// This list of products is also used to populate the Product task attribute and to query the repository.

	public List<String> getLegalProducts(TaskRepository repository) throws SQLException, CoreException {
		return Arrays.asList(new String[] { "ECLIPSE", ".NET", "NETBEANS" });
	}


// Initialize this repository configuration.

	public void initialize(TaskRepository repository) throws SQLException, CoreException {
	}

// Persists an attachement

	public void persistAttachment(TaskRepository repository, IndustrialAttachment attachment) throws SQLException,
			CoreException {
		List<IndustrialAttachment> list = attachements.get(attachment.getTaskId());
		if (list == null) {
			list = new ArrayList<IndustrialAttachment>();
		}

		list.add(attachment);
		attachment.setId("" + (list.size() + 1));
		attachements.put(attachment.getTaskId(), list);

	}

// Persists a comment

	public void persistComment(TaskRepository repository, IndustrialComment comment) throws SQLException, CoreException {

		comment.setGroupKey(Calendar.getInstance().getTime().toString());
		ArrayList<IndustrialComment> list = comments.get(comment.getTaskId());
		if (list == null) {
			list = new ArrayList<IndustrialComment>();
		}
		list.add(comment);
		comments.put(comment.getTaskId(), list);

	}

// Persists a task

	public String persistTask(TaskRepository repository, IndustrialTask toPersisTask) throws SQLException,
			CoreException {
		String id = "MemomryTask" + (tasks.size() + 1);
		toPersisTask.setTaskId(id);
		tasks.put(id, toPersisTask);
		return id;
	}

// Updates a task

	public void updateTask(TaskRepository repository, IndustrialTask task) throws SQLException, CoreException {
		tasks.put(task.getTaskId(), task);
	}

// Validates the repository

	public boolean validate(TaskRepository repository) throws SQLException, CoreException {
		return true;
	}

}