<commit_before>﻿using CrmCodeGenerator.VSPackage.Helpers;
using CrmCodeGenerator.VSPackage.Model;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CrmCodeGenerator.VSPackage.Dialogs
{


   

    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Microsoft.VisualStudio.PlatformUI.DialogWindow
    {
        
        public Context Context;
        Settings settings;
        private EntityMetadata[] _AllEntities;
        private bool _StillOpen = true;
        public bool StillOpen
        {
            get
            {
                return _StillOpen;
            }
        }
        public Login(EnvDTE80.DTE2 dte, Settings settings)
        {
            InitializeComponent();

            var main = dte.GetMainWindow();
            this.Owner = main;
            //Loaded += delegate  { this.CenterWindow(main); };

            this.settings = settings;
            this.txtPassword.Password = settings.Password;  // PasswordBox doesn't allow 2 way binding
            this.DataContext = settings;
        }

        
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            this.HideMinimizeAndMaximizeButtons();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateStatus("In order to generate code from this template, you need to provide login credentials for your CRM system");
            UpdateStatus("The Discovery URL is the URL to your Discovery Service, you can find this URL in CRM -> Settings -> Customizations -> Developer Resources.  \n    eg " + @"https://dsc.yourdomain.com/XRMServices/2011/Discovery.svc");
            if (settings.OrgList.Contains(settings.CrmOrg) == false)
            {
                settings.OrgList.Add(settings.CrmOrg);
            }
            this.Organization.SelectedItem = settings.CrmOrg;
        }
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            _StillOpen = false;
            this.Close();
        }


        private void RefreshOrgs(object sender, RoutedEventArgs e)
        {
            settings.Password = ((PasswordBox)((Button)sender).CommandParameter).Password;  // PasswordBox doesn't allow 2 way binding, so we have to manually read it
            var origCursor = this.Cursor;
            UpdateStatus("Refreshing Orgs", true);

            try
            {
                var orgs = QuickConnection.GetOrganizations(settings.CrmSdkUrl, settings.Domain, settings.Username, settings.Password);
                var newOrgs = new ObservableCollection<String>(orgs);
                settings.OrgList = newOrgs;
            }
            catch (Exception ex)
            {
                var error = "[ERROR] " + ex.Message + (ex.InnerException != null ? "\n" + "[ERROR] " + ex.InnerException.Message : "");
                UpdateStatus(error);
                UpdateStatus("Unable to refresh organizations, check connection information");
            }

            Dispatcher.BeginInvoke(new Action(() => { this.Cursor = origCursor; }));
        }
        private void EntitiesRefresh_Click(object sender, RoutedEventArgs events)
        {
            settings.Password = ((PasswordBox)((Button)sender).CommandParameter).Password;  // PasswordBox doesn't allow 2 way binding, so we have to manually read it
            var origCursor = this.Cursor;
            UpdateStatus("Refreshing Entities...", true);

            RefreshEntityList();

            Dispatcher.BeginInvoke(new Action(() => { this.Cursor = origCursor; }));
            UpdateStatus("");
        }
        
        private void RefreshEntityList()
        {
            if (_AllEntities == null)
            {
                Update_AllEntities();
            }
            var entities = _AllEntities.Where(e =>
                            {
                                if (settings.IncludeNonStandard)
                                    return true;
                                else
                                    return !EntityHelper.NonStandard.Contains(e.LogicalName);
                            });

            var origSelection = settings.EntitiesToIncludeString;
            var newList = new ObservableCollection<string>();
            foreach (var entity in entities.OrderBy(e => e.LogicalName))
            {
                newList.Add(entity.LogicalName);
            }

            settings.EntityList = newList;
            settings.EntitiesToIncludeString = origSelection;
        }
        private void Update_AllEntities()
        {<commit_msg>[BUG] Fixed, when an error occurred while refreshing the entities, then the hourglass cursor would be stuck.<commit_after>﻿using CrmCodeGenerator.VSPackage.Helpers;
using CrmCodeGenerator.VSPackage.Model;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace CrmCodeGenerator.VSPackage.Dialogs
{


   

    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Microsoft.VisualStudio.PlatformUI.DialogWindow
    {
        
        public Context Context;
        Settings settings;
        private EntityMetadata[] _AllEntities;
        private bool _StillOpen = true;
        public bool StillOpen
        {
            get
            {
                return _StillOpen;
            }
        }
        public Login(EnvDTE80.DTE2 dte, Settings settings)
        {
            InitializeComponent();

            var main = dte.GetMainWindow();
            this.Owner = main;
            //Loaded += delegate  { this.CenterWindow(main); };

            this.settings = settings;
            this.txtPassword.Password = settings.Password;  // PasswordBox doesn't allow 2 way binding
            this.DataContext = settings;
        }

        
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            this.HideMinimizeAndMaximizeButtons();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateStatus("In order to generate code from this template, you need to provide login credentials for your CRM system");
            UpdateStatus("The Discovery URL is the URL to your Discovery Service, you can find this URL in CRM -> Settings -> Customizations -> Developer Resources.  \n    eg " + @"https://dsc.yourdomain.com/XRMServices/2011/Discovery.svc");
            if (settings.OrgList.Contains(settings.CrmOrg) == false)
            {
                settings.OrgList.Add(settings.CrmOrg);
            }
            this.Organization.SelectedItem = settings.CrmOrg;
        }
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            _StillOpen = false;
            this.Close();
        }


        private void RefreshOrgs(object sender, RoutedEventArgs e)
        {
            settings.Password = ((PasswordBox)((Button)sender).CommandParameter).Password;  // PasswordBox doesn't allow 2 way binding, so we have to manually read it
            var origCursor = this.Cursor;
            UpdateStatus("Refreshing Orgs", true);

            try
            {
                var orgs = QuickConnection.GetOrganizations(settings.CrmSdkUrl, settings.Domain, settings.Username, settings.Password);
                var newOrgs = new ObservableCollection<String>(orgs);
                settings.OrgList = newOrgs;
            }
            catch (Exception ex)
            {
                var error = "[ERROR] " + ex.Message + (ex.InnerException != null ? "\n" + "[ERROR] " + ex.InnerException.Message : "");
                UpdateStatus(error);
                UpdateStatus("Unable to refresh organizations, check connection information");
            }

            Dispatcher.BeginInvoke(new Action(() => { this.Cursor = origCursor; }));
        }
        private void EntitiesRefresh_Click(object sender, RoutedEventArgs events)
        {
            settings.Password = ((PasswordBox)((Button)sender).CommandParameter).Password;  // PasswordBox doesn't allow 2 way binding, so we have to manually read it
            var origCursor = this.Cursor;
            UpdateStatus("Refreshing Entities...", true);

            RefreshEntityList();

            Dispatcher.BeginInvoke(new Action(() => { this.Cursor = origCursor; }));
            UpdateStatus("");
        }
        
        private void RefreshEntityList()
        {
            if (_AllEntities == null)
            {
                Update_AllEntities();
            }
            if (_AllEntities == null)
            {
                VsShellUtilities.ShowMessageBox(ServiceProvider.GlobalProvider, "Will this lock vs", "Error", OLEMSGICON.OLEMSGICON_WARNING, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                return;
            }

            var entities = _AllEntities.Where(e =>
                            {
                                if (settings.IncludeNonStandard)
                                    return true;
                                else
                                    return !EntityHelper.NonStandard.Contains(e.LogicalName);
                            });

            var origSelection = settings.EntitiesToIncludeString;
            var newList = new ObservableCollection<string>();
            foreach (var entity in entities.OrderBy(e => e.LogicalName))
            {
                newList.Add(entity.LogicalName);
            }

            settings.EntityList = newList;
            settings.EntitiesToIncludeString = origSelection;
        }
        private void Update_AllEntities()
        {
{'fraction_non_alphanumeric': 0.08798076923076924, 'fraction_numerical': 0.0019230769230769232, 'mean_word_length': 1.901255230125523, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5037047', 'n_tokens_mistral': 2619, 'n_tokens_neox': 2510, 'n_words': 615}