org.eclipse.mylyn.trac.core_3.7.1.v20120425-0100

gleSelect".equals(tracAttribute.getType()))
      {
        attr.getMetaData().setReadOnly(true);
      }
      if (field != null)
      {
        String defaultValue = field.getDefaultValue();
        if ((defaultValue != null) && (defaultValue.length() > 0)) {
          attr.setValue(defaultValue);
        }
      }
    }
    return attr;
  }
  
  public RepositoryResponse postTaskData(TaskRepository repository, TaskData taskData, Set<TaskAttribute> oldAttributes, IProgressMonitor monitor)
    throws CoreException
  {
    try
    {
      TracTicket ticket = getTracTicket(repository, taskData);
      ITracClient server = connector.getClientManager().getTracClient(repository);
      if (taskData.isNew())
      {
        int id = server.createTicket(ticket, monitor);
        return new RepositoryResponse(RepositoryResponse.ResponseKind.TASK_CREATED, id);
      }
      String newComment = "";
      TaskAttribute newCommentAttribute = taskData.getRoot().getMappedAttribute("task.common.comment.new");
      if (newCommentAttribute != null) {
        newComment = newCommentAttribute.getValue();
      }
      server.updateTicket(ticket, newComment, monitor);
      return new RepositoryResponse(RepositoryResponse.ResponseKind.TASK_UPDATED, ticket.getId());
    }
    catch (OperationCanceledException e)
    {
      throw e;
    }
    catch (Exception e)
    {
      throw new CoreException(TracCorePlugin.toStatus(e, repository));
    }
  }
  
  public boolean initializeTaskData(TaskRepository repository, TaskData data, ITaskMapping initializationData, IProgressMonitor monitor)
    throws CoreException
  {
    try
    {
      ITracClient client = connector.getClientManager().getTracClient(repository);
      client.updateAttributes(monitor, false);
      createDefaultAttributes(data, client, false);
      removeEmptySingleSelectAttributes(data);
      return true;
    }
    catch (OperationCanceledException e)
    {
      throw e;
    }
    catch (Exception e)
    {
      throw new CoreException(TracCorePlugin.toStatus(e, repository));
    }
  }
  
  public boolean initializeSubTaskData(TaskRepository repository, TaskData taskData, TaskData parentTaskData, IProgressMonitor monitor)
    throws CoreException
  {
    initializeTaskData(repository, taskData, null, monitor);
    TaskAttribute blockingAttribute = taskData.getRoot().getMappedAttribute("blocking");
    if (blockingAttribute == null) {
      throw new CoreException(new RepositoryStatus(repository, 4, "org.eclipse.mylyn.trac.core", 
        1, "The repository does not support subtasks"));
    }
    TaskMapper mapper = new TaskMapper(taskData);
    mapper.merge(new TaskMapper(parentTaskData));
    mapper.setDescription("");
    mapper.setSummary("");
    blockingAttribute.setValue(parentTaskData.getTaskId());
    TaskAttribute blockedByAttribute = taskData.getRoot().getMappedAttribute("blockedby");
    if (blockedByAttribute != null) {
      blockedByAttribute.clearValues();
    }
    TaskAttribute typeAttribute = taskData.getRoot().getAttribute(TracAttribute.TYPE.getTracKey());
    if ((typeAttribute != null) && ("story".equals(typeAttribute.getValue())) && 
      (typeAttribute.getOptions().containsKey("task"))) {
      typeAttribute.setValue("task");
    }
    return true;
  }
  
  public boolean canInitializeSubTaskData(TaskRepository taskRepository, ITask task)
  {
    return Boolean.parseBoolean(task.getAttribute("SupportsSubtasks"));
  }
  
  public TaskAttributeMapper getAttributeMapper(TaskRepository repository)
  {
    ITracClient client = connector.getClientManager().getTracClient(repository);
    return new TracAttributeMapper(repository, client);
  }
  
  public boolean supportsSubtasks(TaskData taskData)
  {
    return taskData.getRoot().getAttribute("blockedby") != null;
  }
  
  public static TracTicket getTracTicket(TaskRepository repository, TaskData data)
    throws InvalidTicketException, CoreException
  {
    TracTicket ticket = data.isNew() ? new TracTicket() : new TracTicket(
      TracRepositoryConnector.getTicketId(data.getTaskId()));
    
    Collection<TaskAttribute> attributes = data.getRoot().getAttributes().values();
    for (TaskAttribute attribute : attributes) {
      if ((!TracAttributeMapper.isInternalAttribute(attribute)) && 
        (!TracAttribute.RESOLUTION.getTracKey().equals(attribute.getId()))) {
        if ((!attribute.getMetaData().isReadOnly()) || (TracTicket.Key.TOKEN.getKey().equals(attribute.getId()))) {
          ticket.putValue(attribute.getId(), attribute.getValue());
        }
      }
    }
    StringBuilder sb = new StringBuilder();
    Object removeValues = TracRepositoryConnector.getAttributeValues(data, "task.common.removecc");
    List<String> values = TracRepositoryConnector.getAttributeValues(data, "task.common.user.cc");
    for (String user : values) {
      if (!((List)removeValues).contains(user))
      {
        if (sb.length() > 0) {
          sb.append(",");
        }
        sb.append(user);
      }
    }
    if (TracRepositoryConnector.getAttributeValue(data, "task.common.newcc").length() > 0)
    {
      if (sb.length() > 0) {
        sb.append(",");
      }
      sb.append(TracRepositoryConnector.getAttributeValue(data, "task.common.newcc"));
    }
    if (Boolean.TRUE.equals(TracRepositoryConnector.getAttributeValue(data, "task.common.addselfcc")))
    {
      if (sb.length() > 0) {
        sb.append(",");
      }
      sb.append(repository.getUserName());
    }
    ticket.putBuiltinValue(TracTicket.Key.CC, sb.toString());
    
    ticket.putValue("owner", TracRepositoryConnector.getAttributeValue(data, "task.common.user.assigned"));
    
    TaskAttribute operationAttribute = data.getRoot().getMappedAttribute("task.common.operation");
    if (operationAttribute != null)
    {
      TaskOperation operation = TaskOperation.createFrom(operationAttribute);
      String action = operation.getOperationId();
      if (!"leave".equals(action)) {
        if ("accept".equals(action))
        {
          ticket.putValue("status", TracRepositoryConnector.TaskStatus.ASSIGNED.toStatusString());
        }
        else if ("resolve".equals(action))
        {
          ticket.putValue("status", TracRepositoryConnector.TaskStatus.CLOSED.toStatusString());
          ticket.putValue("resolution", TracRepositoryConnector.getAttributeValue(data, 
            "task.common.resolution"));
        }
        else if ("reopen".equals(action))
        {
          ticket.putValue("status", TracRepositoryConnector.TaskStatus.REOPENED.toStatusString());
          ticket.putValue("resolution", "");
        }
        else if ("reassign".equals(action))
        {
          ticket.putValue("status", TracRepositoryConnector.TaskStatus.NEW.toStatusString());
        }
      }
      ticket.putValue("action", action);
    }
    Date lastChanged = TracUtil.parseDate(TracRepositoryConnector.getAttributeValue(data, 
      TracAttribute.CHANGE_TIME.getTracKey()));
    ticket.setLastChanged(lastChanged);
    
    return ticket;
  }
  
  public void migrateTaskData(TaskRepository taskRepository, TaskData taskData)
  {
    int version = 0;
    if (taskData.getVersion() != null) {
      try
      {
        version = Integer.parseInt(taskData.getVersion());
      }
      catch (NumberFormatException localNumberFormatException) {}
    }
    TaskAttribute attribute;
    if (version < 1)
    {
      TaskAttribute root = taskData.getRoot();
      List<TaskAttribute> attributes = new ArrayList(root.getAttributes().values());
      for (Iterator localIterator = attributes.iterator(); localIterator.hasNext();)
      {
        attribute = (TaskAttribute)localIterator.next();
        if (("operation".equals(attribute.getMetaData().getType())) && 
          ("reassign".equals(attribute.getValue())))
        {
          root.removeAttribute(attribute.getId());
        }
        else if ("task.common.operation".equals(attribute.getId()))
        {
          attribute.getMetaData().setType("operation");
        }
        else if ("task.common.newcc".equals(attribute.getId()))
        {
          attribute.getMetaData().setType("shortText").setReadOnly(false);
        }
        else
        {
          TracAttribute tracAttribute = TracAttribute.getByTracKey(attribute.getId());
          if (tracAttribute != null)
          {
            attribute.getMetaData().setType(tracAttribute.getType());
            attribute.getMetaData().setKind(tracAttribute.getKind());
            attribute.getMetaData().setReadOnly(tracAttribute.isReadOnly());
          }
        }
      }
      if (root.getAttribute("task.common.removecc") == null) {
        root.createAttribute("task.common.removecc");
      }
      if (root.getAttribute("task.common.comment.new") == null) {
        root.createAttribute("task.common.comment.new").getMetaData().setType("longRichText").setReadOnly(false);
      }
    }
    if (version < 2)
    {
      List<TaskAttribute> attributes = new ArrayList(taskData.getRoot().getAttributes().values());
      for (TaskAttribute attribute : attributes) {
        if (!TracAttributeMapper.isInternalAttribute(attribute))
        {
          TaskAttributeMetaData metaData = attribute.getMetaData();
          metaData.putValue("tracKey", attribute.getId());
          if (metaData.getType() == null) {
            metaData.setType("shortText");
          }
        }
      }
      taskData.setVersion("2");
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.internal.trac.core.TracTaskDataHandler
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.internal.trac.core;

import java.io.InputStream;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.mylyn.commons.net.Policy;
import org.eclipse.mylyn.internal.trac.core.client.ITracClient;
import org.eclipse.mylyn.internal.trac.core.model.TracTicket;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentHandler;
import org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentSource;
import org.eclipse.mylyn.tasks.core.data.TaskAttachmentMapper;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.UnsubmittedTaskAttachment;

public class TracAttachmentHandler
  extends AbstractTaskAttachmentHandler
{
  private final TracRepositoryConnector connector;
  
  public TracAttachmentHandler(TracRepositoryConnector connector)
  {
    this.connector = connector;
  }
  
  public InputStream getContent(TaskRepository repository, ITask task, TaskAttribute attachmentAttribute, IProgressMonitor monitor)
    throws CoreException
  {
    TaskAttachmentMapper mapper = TaskAttachmentMapper.createFrom(attachmentAttribute);
    String filename = mapper.getFileName();
    if ((filename == null) || (filename.length() == 0)) {
      throw new CoreException(new RepositoryStatus(repository.getRepositoryUrl(), 4, 
        "org.eclipse.mylyn.trac.core", 1, "Attachment download from " + 
        repository.getRepositoryUrl() + " failed, missing attachment filename."));
    }
    try
    {
      ITracClient client = connector.getClientManager().getTracClient(repository);
      int id = Integer.parseInt(task.getTaskId());
      return client.getAttachmentData(id, filename, monitor);
    }
    catch (OperationCanceledException e)
    {
      throw e;
    }
    catch (Exception e)
    {
      throw new CoreException(TracCorePlugin.toStatus(e, repository));
    }
  }
  
  public void postContent(TaskRepository repository, ITask task, AbstractTaskAttachmentSource source, String comment, TaskAttribute attachmentAttribute, IProgressMonitor monitor)
    throws CoreException
  {
    if (!TracRepositoryConnector.hasAttachmentSupport(repository, task)) {
      throw new CoreException(new RepositoryStatus(repository.getRepositoryUrl(), 1, 
        "org.eclipse.mylyn.trac.core", 1, 
        "Attachments are not supported by this repository access type"));
    }
    UnsubmittedTaskAttachment attachment = new UnsubmittedTaskAttachment(source, attachmentAttribute);
    monitor = Policy.monitorFor(monitor);
    try
    {
      monitor.beginTask(Messages.TracAttachmentHandler_Uploading_attachment, -1);
      try
      {
        ITracClient client = connector.getClientManager().getTracClient(repository);
        int id = Integer.parseInt(task.getTaskId());
        client.putAttachmentData(id, attachment.getFileName(), attachment.getDescription(), 
          attachment.createInputStream(monitor), monitor, attachment.getReplaceExisting());
        if ((comment != null) && (comment.length() > 0))
        {
          TracTicket ticket = new TracTicket(id);
          client.updateTicket(ticket, comment, monitor);
        }
      }
      catch (OperationCanceledException e)
      {
        throw e;
      }
      catch (Exception e)
      {
        throw new CoreException(TracCorePlugin.toStatus(e, repository));
      }
    }
    finally
    {
      monitor.done();
    }
    monitor.done();
  }
  
  public boolean canGetContent(TaskRepository repository, ITask task)
  {
    return TracRepositoryConnector.hasAttachmentSupport(repository, task);
  }
  
  public boolean canPostContent(TaskRepository repository, ITask task)
  {
    return TracRepositoryConnector.hasAttachmentSupport(repository, task);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.internal.trac.core.TracAttachmentHandler
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.internal.trac.core;

import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.mylyn.internal.trac.core.client.AbstractWikiHandler;
import org.eclipse.mylyn.internal.trac.core.client.ITracClient;
import org.eclipse.mylyn.internal.trac.core.client.ITracWikiClient;
import org.eclipse.mylyn.internal.trac.core.client.TracException;
import org.eclipse.mylyn.internal.trac.core.model.TracWikiPage;
import org.eclipse.mylyn.internal.trac.core.model.TracWikiPageInfo;
import org.eclipse.mylyn.tasks.core.TaskRepository;

public class TracWikiHandler
  extends AbstractWikiHandler
{
  private final TracRepositoryConnector connector;
  
  public TracWikiHandler(TracRepositoryConnector connector)
  {
    this.connector = connector;
  }
  
  public String[] downloadAllPageNames(TaskRepository repository, IProgressMonitor monitor)
    throws CoreException
  {
    monitor.beginTask(Messages.TracWikiHandler_Download_Wiki_Page_Names, -1);
    try
    {
      String[] names = getTracWikiClient(repository).getAllWikiPageNames(monitor);
      return names;
    }
    catch (TracException e)
    {
      throw new CoreException(TracCorePlugin.toStatus(e, repository));
    }
    finally
    {
      monitor.done();
    }
  }
  
  public TracWikiPage getWikiPage(TaskRepository repository, String pageName, IProgressMonitor monitor)
    throws CoreException
  {
    monitor.beginTask(Messages.TracWikiHandler_Download_Wiki_Page, -1);
    try
    {
      TracWikiPage page = getTracWikiClient(repository).getWikiPage(pageName, monitor);
      return page;
    }
    catch (TracException e)
    {
      throw new CoreException(TracCorePlugin.toStatus(e, repository));
    }
    finally
    {
      monitor.done();
    }
  }
  
  public void postWikiPage(TaskRepository repository, TracWikiPage newPage, IProgressMonitor monitor)
    throws CoreException
  {
    monitor.beginTask(Messages.TracWikiHandler_Upload_Wiki_Page, -1);
    try
    {
      String pageName = newPage.getPageInfo().getPageName();
      String content = newPage.getContent();
      Map<String, Object> attributes = new HashMap();
      attributes.put("comment", newPage.getPageInfo().getComment());
      attributes.put("author", newPage.getPageInfo().getAuthor());
      boolean success = getTracWikiClient(repository).putWikipage(pageName, content, attributes, monitor);
      if (success) {
        return;
      }
      throw new CoreException(TracCorePlugin.toStatus(new TracException(
        "Failed to upload wiki page. No further information available."), repository));
    }
    catch (TracException e)
    {
      throw new CoreException(TracCorePlugin.toStatus(e, repository));
    }
    finally
    {
      monitor.done();
    }
  }
  
  public TracWikiPageInfo[] getPageHistory(TaskRepository repository, String pageName, IProgressMonitor monitor)
    throws CoreException
  {
    monitor.beginTask(Messages.TracWikiHandler_Retrieve_Wiki_Page_History, -1);
    try
    {
      TracWikiPageInfo[] versions = getTracWikiClient(repository).getWikiPageInfoAllVersions(pageName, monitor);
      return versions;
    }
    catch (TracException e)
    {
      throw new CoreException(TracCorePlugin.toStatus(e, repository));
    }
    finally
    {
      monitor.done();
    }
  }
  
  private ITracWikiClient getTracWikiClient(TaskRepository repository)
    throws TracException
  {
    ITracClient client = connector.getClientManager().getTracClient(repository);
    if ((client instanceof ITracWikiClient)) {
      return (ITracWikiClient)client;
    }
    throw new TracException("The access mode of " + repository.toString() + 
      " does not support Wiki page editting.");
  }
  
  public String getWikiUrl(TaskRepository repository)
  {
    return repository.getRepositoryUrl() + "/wiki/";
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.internal.trac.core.TracWikiHandler
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.internal.trac.core;

import java.net.MalformedURLException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.mylyn.internal.trac.core.client.InvalidTicketException;
import org.eclipse.mylyn.internal.trac.core.client.TracException;
import org.eclipse.mylyn.internal.trac.core.client.TracLoginException;
import org.eclipse.mylyn.internal.trac.core.client.TracMidAirCollisionException;
import org.eclipse.mylyn.internal.trac.core.client.TracPermissionDeniedException;
import org.eclipse.mylyn.internal.trac.core.util.TracUtil;
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.osgi.framework.BundleContext;

public class TracCorePlugin
  extends Plugin
{
  public static final String ID_PLUGIN = "org.eclipse.mylyn.trac.core";
  public static final String ENCODING_UTF_8 = "UTF-8";
  private static TracCorePlugin plugin;
  public static final String CONNECTOR_KIND = "trac";
  private TracRepositoryConnector connector;
  
  public static TracCorePlugin getDefault()
  {
    return plugin;
  }
  
  public void start(BundleContext context)
    throws Exception
  {
    super.start(context);
    plugin = this;
  }
  
  public void stop(BundleContext context)
    throws Exception
  {
    if (connector != null)
    {
      connector.stop();
      connector = null;
    }
    plugin = null;
    super.stop(context);
  }
  
  public TracRepositoryConnector getConnector()
  {
    return connector;
  }
  
  void setConnector(TracRepositoryConnector connector)
  {
    this.connector = connector;
  }
  
  protected IPath getRepostioryAttributeCachePath()
  {
    IPath stateLocation = Platform.getStateLocation(getBundle());
    IPath cacheFile = stateLocation.append("repositoryConfigurations");
    return cacheFile;
  }
  
  public static IStatus toStatus(Throwable e, TaskRepository repository)
  {
    if ((e instanceof TracLoginException)) {
      return RepositoryStatus.createLoginError(repository.getRepositoryUrl(), "org.eclipse.mylyn.trac.core");
    }
    if ((e instanceof TracPermissionDeniedException)) {
      return TracUtil.createPermissionDeniedError(repository.getRepositoryUrl(), "org.eclipse.mylyn.trac.core");
    }
    if ((e instanceof InvalidTicketException)) {
      return new RepositoryStatus(repository.getRepositoryUrl(), 4, "org.eclipse.mylyn.trac.core", 
        5, Messages.TracCorePlugin_the_SERVER_RETURNED_an_UNEXPECTED_RESOPNSE, e);
    }
    if ((e instanceof TracMidAirCollisionException)) {
      return RepositoryStatus.createCollisionError(repository.getUrl(), "org.eclipse.mylyn.trac.core");
    }
    if ((e instanceof TracException))
    {
      String message = e.getMessage();
      if (message == null) {
        message = Messages.TracCorePlugin_I_O_error_has_occured;
      }
      return new RepositoryStatus(repository.getRepositoryUrl(), 4, "org.eclipse.mylyn.trac.core", 
        5, message, e);
    }
    if ((e instanceof ClassCastException)) {
      return new RepositoryStatus(4, "org.eclipse.mylyn.trac.core", 5, 
        Messages.TracCorePlugin_Unexpected_server_response_ + e.getMessage(), e);
    }
    if ((e instanceof MalformedURLException)) {
      return new RepositoryStatus(4, "org.eclipse.mylyn.trac.core", 5, 
        Messages.TracCorePlugin_Repository_URL_is_invalid, e);
    }
    return new RepositoryStatus(4, "org.eclipse.mylyn.trac.core", 7, 
      Messages.TracCorePlugin_Unexpected_error, e);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.internal.trac.core.TracCorePlugin
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.internal.trac.core;

import org.eclipse.mylyn.tasks.core.ITask.PriorityLevel;

public enum TracRepositoryConnector$TracPriorityLevel
{
  BLOCKER,  CRITICAL,  MAJOR,  MINOR,  TRIVIAL;
  
  public static TracPriorityLevel fromPriority(String priority)
  {
    if (priority == null) {
      return null;
    }
    if (priority.equals("blocker")) {
      return BLOCKER;
    }
    if (priority.equals("critical")) {
      return CRITICAL;
    }
    if (priority.equals("major")) {
      return MAJOR;
    }
    if (priority.equals("minor")) {
      return MINOR;
    }
    if (priority.equals("trivial")) {
      return TRIVIAL;
    }
    return null;
  }
  
  public ITask.PriorityLevel toPriorityLevel()
  {
    switch (this)
    {
    case BLOCKER: 
      return ITask.PriorityLevel.P1;
    case CRITICAL: 
      return ITask.PriorityLevel.P2;
    case MAJOR: 
      return ITask.PriorityLevel.P3;
    case MINOR: 
      return ITask.PriorityLevel.P4;
    case TRIVIAL: 
      return ITask.PriorityLevel.P5;
    }
    return null;
  }
  
  public String toString()
  {
    switch (this)
    {
    case BLOCKER: 
      return "blocker";
    case CRITICAL: 
      return "critical";
    case MAJOR: 
      return "major";
    case MINOR: 
      return "minor";
    case TRIVIAL: 
      return "trivial";
    }
    return null;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.internal.trac.core.TracRepositoryConnector.TracPriorityLevel
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.internal.trac.core;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
import org.eclipse.mylyn.commons.net.AuthenticationType;
import org.eclipse.mylyn.commons.net.Policy;
import org.eclipse.mylyn.internal.trac.core.client.AbstractWikiHandler;
import org.eclipse.mylyn.internal.trac.core.client.ITracClient;
import org.eclipse.mylyn.internal.trac.core.client.ITracClient.Version;
import org.eclipse.mylyn.internal.trac.core.client.ITracWikiClient;
import org.eclipse.mylyn.internal.trac.core.model.TracComment;
import org.eclipse.mylyn.internal.trac.core.model.TracPriority;
import org.eclipse.mylyn.internal.trac.core.model.TracSearch;
import org.eclipse.mylyn.internal.trac.core.model.TracTicket;
import org.eclipse.mylyn.internal.trac.core.util.TracUtil;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.IRepositoryQuery;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITask.PriorityLevel;
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.TaskRepositoryLocationFactory;
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;
import org.eclipse.mylyn.tasks.core.data.TaskHistory;
import org.eclipse.mylyn.tasks.core.data.TaskMapper;
import org.eclipse.mylyn.tasks.core.data.TaskRelation;
import org.eclipse.mylyn.tasks.core.data.TaskRevision;
import org.eclipse.mylyn.tasks.core.data.TaskRevision.Change;
import org.eclipse.mylyn.tasks.core.sync.ISynchronizationSession;

public class TracRepositoryConnector
  extends AbstractRepositoryConnector
{
  public static enum TaskKind
  {
    DEFECT,  ENHANCEMENT,  TASK,  STORY;
    
    public static TaskKind fromString(String type)
    {
      if (type == null) {
        return null;
      }
      if (type.equals("Defect")) {
        return DEFECT;
      }
      if (type.equals("Enhancement")) {
        return ENHANCEMENT;
      }
      if (type.equals("Task")) {
        return TASK;
      }
      if (type.equals("Story")) {
        return STORY;
      }
      return null;
    }
    
    public static TaskKind fromType(String type)
    {
      if (type == null) {
        return null;
      }
      if ((type.equals("defect")) || (type.equals("error"))) {
        return DEFECT;
      }
      if (type.equals("enhancement")) {
        return ENHANCEMENT;
      }
      if (type.equals("task")) {
        return TASK;
      }
      if (type.equals("story")) {
        return STORY;
      }
      return null;
    }
    
    public String toString()
    {
      switch (this)
      {
      case DEFECT: 
        return "Defect";
      case ENHANCEMENT: 
        return "Enhancement";
      case STORY: 
        return "Task";
      case TASK: 
        return "Story";
      }
      return "";
    }
  }
  
  public static enum TaskStatus
  {
    ASSIGNED,  CLOSED,  NEW,  REOPENED;
    
    public static TaskStatus fromStatus(String status)
    {
      if (status == null) {
        return null;
      }
      if (status.equals("new")) {
        return NEW;
      }
      if (status.equals("assigned")) {
        return ASSIGNED;
      }
      if (status.equals("reopened")) {
        return REOPENED;
      }
      if (status.equals("closed")) {
        return CLOSED;
      }
      return null;
    }
    
    public String toStatusString()
    {
      switch (this)
      {
      case NEW: 
        return "new";
      case ASSIGNED: 
        return "assigned";
      case REOPENED: 
        return "reopened";
      case CLOSED: 
        return "closed";
      }
      return "";
    }
    
    public String toString()
    {
      switch (this)
      {
      case NEW: 
        return "New";
      case ASSIGNED: 
        return "Assigned";
      case REOPENED: 
        return "Reopened";
      case CLOSED: 
        return "Closed";
      }
      return "";
    }
  }
  
  public static enum TracPriorityLevel
  {
    BLOCKER,  CRITICAL,  MAJOR,  MINOR,  TRIVIAL;
    
    public static TracPriorityLevel fromPriority(String priority)
    {
      if (priority == null) {
        return null;
      }
      if (priority.equals("blocker")) {
        return BLOCKER;
      }
      if (priority.equals("critical")) {
        return CRITICAL;
      }
      if (priority.equals("major")) {
        return MAJOR;
      }
      if (priority.equals("minor")) {
        return MINOR;
      }
      if (priority.equals("trivial")) {
        return TRIVIAL;
      }
      return null;
    }
    
    public ITask.PriorityLevel toPriorityLevel()
    {
      switch (this)
      {
      case BLOCKER: 
        return ITask.PriorityLevel.P1;
      case CRITICAL: 
        return ITask.PriorityLevel.P2;
      case MAJOR: 
        return ITask.PriorityLevel.P3;
      case MINOR: 
        return ITask.PriorityLevel.P4;
      case TRIVIAL: 
        return ITask.PriorityLevel.P5;
      }
      return null;
    }
    
    public String toString()
    {
      switch (this)
      {
      case BLOCKER: 
        return "blocker";
      case CRITICAL: 
        return "critical";
      case MAJOR: 
        return "major";
      case MINOR: 
        return "minor";
      case TRIVIAL: 
        return "trivial";
      }
      return null;
    }
  }
  
  private static final Date DEFAULT_COMPLETION_DATE = new Date(0L);
  private static int TASK_PRIORITY_LEVELS = 5;
  public static final String TASK_KEY_SUPPORTS_SUBTASKS = "SupportsSubtasks";
  public static final String TASK_KEY_UPDATE_DATE = "UpdateDate";
  
  public static String getDisplayUsername(TaskRepository repository)
  {
    AuthenticationCredentials credentials = repository.getCredentials(AuthenticationType.REPOSITORY);
    if ((credentials != null) && (credentials.getUserName().length() > 0)) {
      return "anonymous";
    }
    return repository.getUserName();
  }
  
  public static ITask.PriorityLevel getTaskPriority(String tracPriority)
  {
    if (tracPriority != null)
    {
      TracPriorityLevel priority = TracPriorityLevel.fromPriority(tracPriority);
      if (priority != null) {
        return priority.toPriorityLevel();
      }
    }
    return ITask.PriorityLevel.getDefault();
  }
  
  public static ITask.PriorityLevel getTaskPriority(String priority, TracPriority[] tracPriorities)
  {
    if ((priority != null) && (tracPriorities != null) && (tracPriorities.length > 0))
    {
      int minValue = tracPriorities[0].getValue();
      int range = tracPriorities[(tracPriorities.length - 1)].getValue() - minValue;
      TracPriority[] arrayOfTracPriority;
      int j = (arrayOfTracPriority = tracPriorities).length;
      for (int i = 0; i < j; i++)
      {
        TracPriority tracPriority = arrayOfTracPriority[i];
        if (priority.equals(tracPriority.getName()))
        {
          float relativeValue = (tracPriority.getValue() - minValue) / range;
          int value = (int)(relativeValue * TASK_PRIORITY_LEVELS) + 1;
          return ITask.PriorityLevel.fromLevel(value);
        }
      }
    }
    return getTaskPriority(priority);
  }
  
  public static int getTicketId(String taskId)
    throws CoreException
  {
    try
    {
      return Integer.parseInt(taskId);
    }
    catch (NumberFormatException e)
    {
      throw new CoreException(new Status(4, "org.eclipse.mylyn.trac.core", 0, 
        "Invalid ticket id: " + taskId, e));
    }
  }
  
  static List<String> getAttributeValues(TaskData data, String attributeId)
  {
    TaskAttribute attribute = data.getRoot().getMappedAttribute(attributeId);
    if (attribute != null) {
      return attribute.getValues();
    }
    return Collections.emptyList();
  }
  
  static String getAttributeValue(TaskData data, String attributeId)
  {
    TaskAttribute attribute = data.getRoot().getMappedAttribute(attributeId);
    if (attribute != null) {
      return attribute.getValue();
    }
    return "";
  }
  
  public static boolean hasAttachmentSupport(TaskRepository repository, ITask task)
  {
    return ITracClient.Version.XML_RPC.name().equals(repository.getVersion());
  }
  
  public static boolean hasChangedSince(TaskRepository repository)
  {
    return ITracClient.Version.XML_RPC.name().equals(repository.getVersion());
  }
  
  public static boolean hasRichEditor(TaskRepository repository)
  {
    return ITracClient.Version.XML_RPC.name().equals(repository.getVersion());
  }
  
  public static boolean hasRichEditor(TaskRepository repository, ITask task)
  {
    return hasRichEditor(repository);
  }
  
  public static boolean isCompleted(String tracStatus)
  {
    TaskStatus taskStatus = TaskStatus.fromStatus(tracStatus);
    return taskStatus == TaskStatus.CLOSED;
  }
  
  private final TracAttachmentHandler attachmentHandler = new TracAttachmentHandler(this);
  private TracClientManager clientManager;
  private File repositoryConfigurationCacheFile;
  private final TracTaskDataHandler taskDataHandler = new TracTaskDataHandler(this);
  private TaskRepositoryLocationFactory taskRepositoryLocationFactory = new TaskRepositoryLocationFactory();
  private final TracWikiHandler wikiHandler = new TracWikiHandler(this);
  
  public TracRepositoryConnector()
  {
    if (TracCorePlugin.getDefault() != null)
    {
      TracCorePlugin.getDefault().setConnector(this);
      IPath path = TracCorePlugin.getDefault().getRepostioryAttributeCachePath();
      repositoryConfigurationCacheFile = path.toFile();
    }
  }
  
  public TracRepositoryConnector(File repositoryConfigurationCacheFile)
  {
    this.repositoryConfigurationCacheFile = repositoryConfigurationCacheFile;
  }
  
  public boolean canCreateNewTask(TaskRepository repository)
  {
    return true;
  }
  
  public boolean canCreateTaskFromKey(TaskRepository repository)
  {
    return true;
  }
  
  public boolean canSynchronizeTask(TaskRepository taskRepository, ITask task)
  {
    return hasRichEditor(taskRepository, task);
  }
  
  public TracAttachmentHandler getTaskAttachmentHandler()
  {
    return attachmentHandler;
  }
  
  public synchronized TracClientManager getClientManager()
  {
    if (clientManager == null) {
      clientManager = new TracClientManager(repositoryConfigurationCacheFile, taskRepositoryLocationFactory);
    }
    return clientManager;
  }
  
  public String getConnectorKind()
  {
    return "trac";
  }
  
  public String getLabel()
  {
    return Messages.TracRepositoryConnector_Trac_Client_Label;
  }
  
  public TaskData getTaskData(TaskRepository repository, String taskId, IProgressMonitor monitor)
    throws CoreException
  {
    return taskDataHandler.getTaskData(repository, taskId, monitor);
  }
  
  public TracTaskDataHandler getTaskDataHandler()
  {
    return taskDataHandler;
  }
  
  public String getRepositoryUrlFromTaskUrl(String url)
  {
    if (url == null) {
      return null;
    }
    int index = url.lastIndexOf("/ticket/");
    return index == -1 ? null : url.substring(0, index);
  }
  
  public String getTaskIdFromTaskUrl(String url)
  {
    if (url == null) {
      return null;
    }
    int index = url.lastIndexOf("/ticket/");
    return index == -1 ? null : url.substring(index + "/ticket/".length());
  }
  
  public String getTaskIdPrefix()
  {
    return "#";
  }
  
  public TaskRepositoryLocationFactory getTaskRepositoryLocationFactory()
  {
    return taskRepositoryLocationFactory;
  }
  
  public String getTaskUrl(String repositoryUrl, String taskId)
  {
    return repositoryUrl + "/ticket/" + taskId;
  }
  
  public AbstractWikiHandler getWikiHandler()
  {
    return wikiHandler;
  }
  
  public boolean hasWiki(TaskRepository repository)
  {
    ITracClient client = getClientManager().getTracClient(repository);
    if ((client instanceof ITracWikiClient)) {
      return true;
    }
    return false;
  }
  
  public IStatus performQuery(TaskRepository repository, IRepositoryQuery query, TaskDataCollector resultCollector, ISynchronizationSession session, IProgressMonitor monitor)
  {
    try
    {
      monitor.beginTask(Messages.TracRepositoryConnector_Querying_repository, -1);
      
      TracSearch search = TracUtil.toTracSearch(query);
      Object localObject2;
      if (search == null) {
        return new RepositoryStatus(repository.getRepositoryUrl(), 4, "org.eclipse.mylyn.trac.core", 
          1, "The query is invalid: \"" + query.getUrl() + "\"");
      }
      search.setMax(5000);
      try
      {
        Map<String, ITask> taskById = null;
        
        ITracClient client = getClientManager().getTracClient(repository);
        client.updateAttributes(monitor, false);
        if ((session != null) && (session.isFullSynchronization()) && (hasRichEditor(repository)) && 
          (!session.getTasks().isEmpty()))
        {
          List<Integer> ticketIds = new ArrayList();
          client.searchForTicketIds(search, ticketIds, monitor);
          for (Integer id : ticketIds)
          {
            if (taskById == null)
            {
              taskById = new HashMap();
              for (ITask task : session.getTasks()) {
                taskById.put(task.getTaskId(), task);
              }
            }
            TaskData taskData = new TaskData(taskDataHandler.getAttributeMapper(repository), 
              "trac", repository.getRepositoryUrl(), id);
            taskData.setPartial(true);
            TaskAttribute attribute = TracTaskDataHandler.createAttribute(taskData, TracAttribute.ID);
            attribute.setValue(id);
            resultCollector.accept(taskData);
          }
        }
        else
        {
          List<TracTicket> tickets = new ArrayList();
          client.search(search, tickets, monitor);
          for (TracTicket ticket : tickets)
          {
            TaskData taskData = taskDataHandler.createTaskDataFromTicket(client, repository, ticket, 
              monitor);
            taskData.setPartial(true);
            if ((session != null) && (!session.isFullSynchronization()) && (hasRichEditor(repository)))
            {
              if (taskById == null)
              {
                taskById = new HashMap();
                for (ITask task : session.getTasks()) {
                  taskById.put(task.getTaskId(), task);
                }
              }
              ITask task = (ITask)taskById.get(ticket.getId());
              if ((task != null) && (hasTaskChanged(repository, task, taskData))) {
                session.markStale(task);
              }
            }
            resultCollector.accept(taskData);
          }
        }
      }
      catch (OperationCanceledException e)
      {
        throw e;
      }
      catch (Throwable e)
      {
        return TracCorePlugin.toStatus(e, repository);
      }
      ITracClient client;
      return Status.OK_STATUS;
    }
    finally
    {
      monitor.done();
    }
  }
  
  /* Error */
  public void postSynchronization(ISynchronizationSession event, IProgressMonitor monitor)
    throws CoreException
  {
    // Byte code:
    //   0: aload_2
    //   1: ldc 1
    //   3: iconst_1
    //   4: invokeinterface 823 3 0
    //   9: aload_1
    //   10: invokeinterface 837 1 0
    //   15: ifeq +62 -> 77
    //   18: aload_1
    //   19: invokeinterface 841 1 0
    //   24: ifnonnull +53 -> 77
    //   27: aload_0
    //   28: aload_1
    //   29: invokespecial 744	org/eclipse/mylyn/internal/trac/core/TracRepositoryConnector:getSynchronizationTimestamp	(Lorg/eclipse/mylyn/tasks/core/sync/ISynchronizationSession;)Ljava/util/Date;
    //   32: astore_3
    //   33: aload_3
    //   34: ifnull +43 -> 77
    //   37: aload_1
    //   38: invokeinterface 843 1 0
    //   43: new 315	java/lang/StringBuilder
    //   46: dup
    //   47: aload_3
    //   48: invokestatic 771	org/eclipse/mylyn/internal/trac/core/util/TracUtil:toTracTime	(Ljava/util/Date;)J
    //   51: invokestatic 701	java/lang/String:valueOf	(J)Ljava/lang/String;
    //   54: invokespecial 707	java/lang/StringBuilder:<init>	(Ljava/lang/String;)V
    //   57: invokevirtual 706	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   60: invokevirtual 783	org/eclipse/mylyn/tasks/core/TaskRepository:setSynchronizati
1 2 3 4 5 6 7 8 9 10

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd