Merge branch 'mikhail.golubev/configurable-issues-states'
[idea/community.git] / plugins / tasks / tasks-core / jira / src / com / intellij / tasks / jira / rest / api20alpha1 / JiraRestApi20Alpha1.java
index 7de17945ac318da643b832be0dcfbcd2abcabe4a..bc70d239ce733b6c8daa5de19678336e89ccbb10 100644 (file)
@@ -2,9 +2,10 @@ package com.intellij.tasks.jira.rest.api20alpha1;
 
 import com.google.gson.reflect.TypeToken;
 import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.tasks.CustomTaskState;
 import com.intellij.tasks.LocalTask;
+import com.intellij.tasks.Task;
 import com.intellij.tasks.TaskBundle;
-import com.intellij.tasks.TaskState;
 import com.intellij.tasks.jira.JiraRepository;
 import com.intellij.tasks.jira.rest.JiraRestApi;
 import com.intellij.tasks.jira.rest.JiraRestTask;
@@ -16,20 +17,35 @@ import org.jetbrains.annotations.Nullable;
 
 import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 /**
  * This REST API is used in JIRA versions from 4.2 to 4.4.
+ *
  * @author Mikhail Golubev
  */
 public class JiraRestApi20Alpha1 extends JiraRestApi {
   private static final Logger LOG = Logger.getInstance(JiraRestApi20Alpha1.class);
-  private static final Type ISSUES_WRAPPER_TYPE = new TypeToken<JiraResponseWrapper.Issues<JiraIssueApi20Alpha1>>() { /* empty */ }.getType();
+  private static final Type ISSUES_WRAPPER_TYPE = new TypeToken<JiraResponseWrapper.Issues<JiraIssueApi20Alpha1>>() {/* empty */}.getType();
 
   public JiraRestApi20Alpha1(JiraRepository repository) {
     super(repository);
   }
 
+  @NotNull
+  @Override
+  public Set<CustomTaskState> getAvailableTaskStates(@NotNull Task task) throws Exception {
+    // REST API of JIRA 4.x for retrieving possible transitions is very limited: we can't fetch possible resolutions and
+    // names of transition destinations. So we have no other options than to hardcode them.
+    final HashSet<CustomTaskState> result = new HashSet<CustomTaskState>();
+    result.add(new CustomTaskState("4", "In Progress"));
+    result.add(new CustomTaskState("5", "Resolved (Fixed)"));
+    result.add(new CustomTaskState("3", "Reopened"));
+    return result;
+  }
+
   @Override
   protected JiraIssue parseIssue(String response) {
     return JiraRepository.GSON.fromJson(response, JiraIssueApi20Alpha1.class);
@@ -57,20 +73,24 @@ public class JiraRestApi20Alpha1 extends JiraRestApi {
 
   @Nullable
   @Override
-  protected String getRequestForStateTransition(@NotNull TaskState state) {
-    switch (state) {
-      case IN_PROGRESS:
-        return  "{\"transition\": \"4\"}";
-      case RESOLVED:
-        // 5 for "Resolved", 2 for "Closed"
-        return  "{\"transition\": \"5\", \"resolution\": \"Fixed\"}";
-      case REOPENED:
-        return  "{\"transition\": \"3\"}";
-      default:
-        return null;
+  protected String getRequestForStateTransition(@NotNull CustomTaskState state) {
+    try {
+      switch (Integer.parseInt(state.getId())) {
+        case 4: // In Progress
+          return "{\"transition\": \"4\"}";
+        case 5: // Resolved (2 for "Closed")
+          return "{\"transition\": \"5\", \"resolution\": \"Fixed\"}";
+        case 3: // Reopened
+          return "{\"transition\": \"3\"}";
+      }
+    }
+    catch (NumberFormatException ignored) {
     }
+    LOG.error("Unknown ID of predefined issue state: " + state.getId());
+    return null;
   }
 
+
   @Override
   public void updateTimeSpend(@NotNull LocalTask task, @NotNull String timeSpent, String comment) throws Exception {
     throw new Exception(TaskBundle.message("jira.failure.no.time.spent"));