context: using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Net;
  using System.Net.Security;
  using System.Security.Cryptography.X509Certificates;
  using System.Text;
  using System.Windows.Forms;
  using System.Xml;
  using Microsoft.Win32;
  using TCDefectIntegration.Properties;
  using RestProxy;
  using TCDefectIntegration.BrowserWindow;
  using System.IO;
  
  namespace TCDefectIntegration
  {
      public class JiraIntegrator : Integrator
      {
          private const String XmlObjectTagName = "XmlObject";
  
          private const String LogInfoAttributeName = "LogInfo";
  
          private frmLogin frmLogin;
  
          private JiraSoapServiceService jira;
  
          private string jiraToken;
  
          private string password = Settings.Default.Password;
  
          private string proxyPassword = Settings.Default.ProxyPassword;
  
          private string proxyUserName = Settings.Default.ProxyUser;
  
          private string userName = Settings.Default.User;
  
          private JiraRestManager restmanager;
  
          private bool ShowLoginDialog
          {
              get
              {
                  if (Settings.Default.UseProxy)
                  {
                      if (proxyPassword == String.Empty)
                      {
                          if (proxyUserName == String.Empty)
                          {
                              return true;
                          }
                      }
                  }
  
                  if (password == String.Empty)
                  {
                      if (userName == String.Empty)
                      {
                          return true;
                      }
                  }
                  return false;
              }
          }
  
          public override string CreateDefect(Dictionary<string, string> defectInfos)
          {
              string defectId = "";
              try
              {
  
                  Connect();
  
                  // manual creation & restful mode
                  if (Settings.Default.UseManualDefectCreationWindow || Settings.Default.UseRestfulConnection)
                  {
                      string url = GetDefectIntegrationSetting("JiraCreateIssueURL", defectInfos);
                      // open browser
                      BrowserForm browser = new BrowserForm(url,Settings.Default.User,Settings.Default.Password,Settings.Default.AutomateLogin);
                      browser.ShowDialog();
  
                      defectId = browser.issueId;
                      browser.Dispose();
  
                      if (string.IsNullOrEmpty(defectId))
                      {
                          return "";
                      }
                  }
                  else
                  {
                      RemoteIssue issue = new RemoteIssue();
                      RemotePriority[] priorities = jira.getPriorities(jiraToken);
                      RemoteIssueType[] issueTypes = jira.getIssueTypes(jiraToken);
                      jira.getProjectRoles(jiraToken);
  
                      issue.created = DateTime.Now;
                      issue.summary = "TOSCA[" + defectInfos["Workspace-User"] + "]: Error in Testcase \""
                                      + defectInfos["TestCase-Name"] + "\"";
                      string description = defectInfos["Log-Description"];
                      if (!String.IsNullOrEmpty(description))
                      {
                          issue.description = description;
                      }
                      else {
                          String compressedLog = defectInfos["CompressedLogDetails"];
                          issue.description = String.IsNullOrEmpty(compressedLog)
                              ? String.Empty
                              : GetLogInfoFromCompressedLogDetails(compressedLog);
                      }
                      issue.assignee = GetDefectIntegrationSetting("DefectAssignee", defectInfos);
                      if (issue.assignee == string.Empty)
                      {
                          issue.assignee = "-1";
                      }
  
                      issue.project = GetDefectIntegrationSetting("DefectProject", defectInfos);
                      issue.priority = GetDefectIntegrationSetting("DefectPriority", defectInfos);
                      foreach (RemotePriority priority in priorities)
                      {
                          if (priority.name == issue.priority)
                          {
                              issue.priority = priority.id;
                              break;
                          }
                      }
                      issue.status = GetDefectIntegrationSetting("DefectStatus", defectInfos);
                      issue.type = GetDefectIntegrationSetting("DefectType", defectInfos);
                      foreach (RemoteIssueType issueType in issueTypes)
                      {
                          if (issueType.name == issue.type)
                          {
                              issue.type = issueType.id;
                              break;
                          }
                      }
                      issue.components = GetComponents(GetDefectIntegrationSetting("DefectComponents", defectInfos));
  
                      issue.customFieldValues = GetCustomDefectProperties(defectInfos).Select(property => new RemoteCustomFieldValue
                      {
                          customfieldId = "customfield_" + property.id,
                          values = property.value.Split(',')
                      }).ToArray();
                      issue = jira.createIssue(jiraToken, issue);
                      Disconnect();
  
                      defectId = issue != null ? issue.key : string.Empty;
                  }
  
                  if (Settings.Default.UseRestfulConnection)
                  {
                      // attach attachment
                      string key = defectInfos.Keys.Where(k => k.Contains("Attachment")).FirstOrDefault();
                      if (!string.IsNullOrEmpty(key))
                      {
                          string filePath = defectInfos[key];
                          string fileType = getFileEnding(filePath);
                          //get extension
                          restmanager.addAttachment(defectId, filePath, "Attachment from Tosca." + fileType, "image/" + fileType);
                      }
                  }
              }
              catch (Exception e)
              {
                  MessageBox.Show(
                          "Jira submit failed with the following reason: " + "\r\n" + e.Message + e.StackTrace,
                          "Error occured",
                          MessageBoxButtons.OK,
                          MessageBoxIcon.Error);
              }
              return defectId;
          }
  
  
  
          public override void OpenDefect(string defectId)
          {
              string url = Settings.Default.URL;
              if (!url.EndsWith("/"))
              {
                  url += "/";
              }
  
              string defaultBrowserPath = GetDefaultBrowserPath();
              Process.Start(defaultBrowserPath, url + defectId);
          }
  
          public override Dictionary<string, string> GetStatesForDefects(List<string> defectIds)
          {
              Dictionary<string, string> result = new Dictionary<string, string>();
              List<string> failedIds = new List<string>();
  
              Connect();
  
              RemoteStatus[] remoteStatuses = null;
              Dictionary<string, string> statusNames = new Dictionary<string, string>();
  
              if (Settings.Default.UseRestfulConnection.Equals(false))
              {
                  remoteStatuses = jira.getStatuses(jiraToken);
                  statusNames = remoteStatuses.ToDictionary(remoteStatus => remoteStatus.id, remoteStatus => remoteStatus.name);
              }
  
              foreach (string defectId in defectIds)
              {
                  try
                  {
                      if (Settings.Default.UseRestfulConnection)
                      {
                          result.Add(defectId, restmanager.getIssueStatus(defectId));
                      }
                      else
                      {
                          RemoteIssue issue = jira.getIssue(jiraToken, defectId);
                          if (statusNames.ContainsKey(issue.status))
                          {
                              result.Add(defectId, statusNames[issue.status]);
                          }
                      }
                  }
                  catch
                  {
                      failedIds.Add(defectId);
                  }
              }
  
              Disconnect();
  
              if (failedIds.Count > 0)
              {
                  MessageBox.Show(
                      "The status of the following Defects could not be retrieved:" + Environment.NewLine
  
gt:                     + string.Join(",", failedIds.ToArray()),