private void SourceConfigButton_Click(object sender, EventArgs e)
 {
     ConfigTabControl.Hide();
     if (SourceComboBox.Text == "Base de datos")
     {
         connectForm = new ConnectForm();
         if (connectForm.ShowDialog() == DialogResult.OK)
         {
             connection = connectForm.connection;
             config     = connectForm.config;
             ExcelQueryPanel.Hide();
             DataBaseQueryPanel.Show();
         }
         RefreshUI();
     }
     else if (SourceComboBox.Text == "Ficheros Excel" || SourceComboBox.Text == "Ficheros XML" || SourceComboBox.Text == "Ficheros JSON")
     {
         FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
         folderBrowserDialog.ShowNewFolderButton = false;
         if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
         {
             if (!Directory.Exists(folderBrowserDialog.SelectedPath))
             {
                 MessageBox.Show("El directorio seleccionado no existe", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
             else
             {
                 config = new ProcessImporterConfig();
                 if (SourceComboBox.Text == "Ficheros Excel")
                 {
                     config.sourceOptions.Add(new KeyValuePair <string, string>("sourceType", "Ficheros Excel"));
                     config.sourceOptions.Add(new KeyValuePair <string, string>("excelPath", folderBrowserDialog.SelectedPath));
                     ExcelPathLabel.Text = folderBrowserDialog.SelectedPath;
                 }
                 else if (SourceComboBox.Text == "Ficheros XML")
                 {
                     config.sourceOptions.Add(new KeyValuePair <string, string>("sourceType", "Ficheros XML"));
                     config.sourceOptions.Add(new KeyValuePair <string, string>("xmlPath", folderBrowserDialog.SelectedPath));
                     XmlPathLabel.Text = folderBrowserDialog.SelectedPath;
                 }
                 else if (SourceComboBox.Text == "Ficheros JSON")
                 {
                     config.sourceOptions.Add(new KeyValuePair <string, string>("sourceType", "Ficheros JSON"));
                     config.sourceOptions.Add(new KeyValuePair <string, string>("jsonPath", folderBrowserDialog.SelectedPath));
                     XmlPathLabel.Text = folderBrowserDialog.SelectedPath;
                 }
             }
         }
         RefreshUI();
     }
     else
     {
         MessageBox.Show("Debe seleccionar un origen de datos.", "Seleccione un origen", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
        public MainForm()
        {
            InitializeComponent();

            config = new ProcessImporterConfig();

            ConfigTabControl.Hide();

            if (LoadConfig())
            {
                RefreshUI();
                InitApiConfigUI();
            }
        }
 private bool LoadConfig()
 {
     if (File.Exists("ProcessImporter.config"))
     {
         var textFile = File.ReadAllText("ProcessImporter.config");
         try
         {
             config = JsonConvert.DeserializeObject <ProcessImporterConfig>(textFile);
         }
         catch (Exception ex)
         {
             return(false);
         }
         return(true);
     }
     return(false);
 }
        private void SaveConfigButton_Click_1(object sender, EventArgs e)
        {
            ProcessImporterConfig newConfig = new ProcessImporterConfig();

            var sourceType = config.sourceOptions?.FirstOrDefault(p => p.Key == "sourceType").Value;

            if (sourceType == null)
            {
                MessageBox.Show("Configuración incompleta. Debe configurar un origen de datos antes de guardar.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            newConfig.sourceOptions.Add(config.sourceOptions.FirstOrDefault(p => p.Key == "sourceType"));

            if (sourceType == "Base de datos")
            {
                newConfig.sourceOptions.Add(config.sourceOptions.FirstOrDefault(p => p.Key == "connectionString"));

                if (String.IsNullOrWhiteSpace(queryTextBox.Text))
                {
                    MessageBox.Show("Configuración incompleta. La consulta de datos no debe estar en blanco.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                newConfig.sourceOptions.Add(new KeyValuePair <string, string>("query", queryTextBox.Text));
            }

            if (sourceType == "Ficheros Excel")
            {
                newConfig.sourceOptions.Add(config.sourceOptions.FirstOrDefault(p => p.Key == "excelPath"));

                if (String.IsNullOrWhiteSpace(ExcelSheetTextBox.Text))
                {
                    MessageBox.Show("Configuración incompleta. Debe definir un titulo de hoja a procesar.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                newConfig.sourceOptions.Add(new KeyValuePair <string, string>("excelSheet", ExcelSheetTextBox.Text));
            }

            if (sourceType == "Ficheros XML")
            {
                newConfig.sourceOptions.Add(config.sourceOptions.FirstOrDefault(p => p.Key == "xmlPath"));
                newConfig.sourceOptions.Add(new KeyValuePair <string, string>("complexNodes", complexNodesTextBox.Text));
            }

            if (sourceType == "Ficheros JSON")
            {
                newConfig.sourceOptions.Add(config.sourceOptions.FirstOrDefault(p => p.Key == "jsonPath"));
            }

            if (string.IsNullOrWhiteSpace(apiUrlTextBox.Text) ||
                string.IsNullOrWhiteSpace(apiUserTextBox.Text) ||
                string.IsNullOrWhiteSpace(apiPasswordTextBox.Text)
                )
            {
                MessageBox.Show("Hay errores en los datos de configuración de la API. Todos los campos son requeridos.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrWhiteSpace(excelColumnIdTextBox.Text) &&
                string.IsNullOrWhiteSpace(DbColumnIdTextBox.Text) &&
                sourceType != "Ficheros XML" && sourceType != "Ficheros JSON")
            {
                MessageBox.Show("Hay errores en la consulta. La columna identificador no puede estar en blanco.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            if (!string.IsNullOrWhiteSpace(excelColumnIdTextBox.Text))
            {
                newConfig.columnId = excelColumnIdTextBox.Text;
            }

            if (!string.IsNullOrWhiteSpace(DbColumnIdTextBox.Text))
            {
                newConfig.columnId = DbColumnIdTextBox.Text;
            }


            newConfig.apiUrl        = apiUrlTextBox.Text;
            newConfig.apiUser       = apiUserTextBox.Text;
            newConfig.apiPassword   = apiPasswordTextBox.Text;
            newConfig.processName   = processComboBox.Text;
            newConfig.communityName = communityComboBox.Text;
            newConfig.initWF        = initWfCheckBox.Checked;
            newConfig.superAdmin    = superAdminCheckBox.Checked;
            newConfig.maxAttempts   = int.Parse(MaxAttempsNumericUpDown.Value.ToString());


            string json = JsonConvert.SerializeObject(newConfig);

            try
            {
                File.WriteAllText("ProcessImporter.config", json);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ocurrión un error mientras se guardaba la configuración. \n Descripción del error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                var dialogResult = MessageBox.Show("Se guardó correctamente la configuración. ¿Desea cerrar el asistente de configuración? ", "Configuración guardada", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (dialogResult == DialogResult.Yes)
                {
                    Application.Exit();
                }
            }
        }