Exemplo n.º 1
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                // Get the network setting for this service url if it exists already.
                NetWorkSetting ns = null;
                try
                {
                    ns = (from n in Configuration.Instance.NetworkSettingCollection.NetWorkSettings.ToArray(typeof(NetWorkSetting)) as NetWorkSetting[]
                          where n.HubWebServiceUrl == txtServiceUrl.Text
                          select n).FirstOrDefault();
                }
                catch
                {
                    Configuration.CreateNewNetworkSettingsFile();
                }

                // If not, create one.
                if (ns == null)
                {
                    ns = new NetWorkSetting();
                    ns.HubWebServiceUrl = txtServiceUrl.Text;
                    Configuration.Instance.AddNetworkSetting(ns);
                }
                else
                {
                    if (MessageBox.Show("This will override your existing configuration for this network.\nAre you sure you want to do that?", "Override Network Configuration?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                    {
                        this.Close();
                        return;
                    }
                }

                ns.Username          = txtUsername.Text;
                ns.EncryptedPassword = txtPassword.Text;
                ns.NetworkName       = txtNetworkname.Text;
                var selectedCert = cbCertificates.SelectedItem as Cert;
                ns.X509CertThumbprint = selectedCert == null ? null : selectedCert.Thumbprint;
                ns.GetDataMartsByUser();
                Configuration.Instance.LoadModels(ns);
                CredentialManager.SaveCredential(ns.CredentialKey, ns.Username, ns.Password);

                // Get the configuration for all models for all datamarts at this service url.
                var dmc = DnsServiceManager.GetDataMartModelConfigurations(ns);

                // Find the model processor in the existing network if exists. If not, create it.
                XmlSerializer serializer = new XmlSerializer(typeof(ModelProperties));
                foreach (var c in dmc)
                {
                    var d  = ns.DataMartList.FirstOrDefault(dm => dm.DataMartId == c.DataMartId);
                    var mp = d.ModelList.FirstOrDefault(m => m.ModelId == c.ModelId && m.ProcessorId == c.ModelProcessorId);

                    if (d != null && mp != null && !string.IsNullOrEmpty(c.Properties))
                    {
                        using (XmlTextReader reader = new XmlTextReader(new MemoryStream(new System.Text.UTF8Encoding().GetBytes(c.Properties))))
                        {
                            d.AllowUnattendedOperation             = c.UnattendedMode != Lpp.Dns.DataMart.Lib.Classes.UnattendedMode.NoUnattendedOperation;
                            d.NotifyOfNewQueries                   = c.UnattendedMode == Lpp.Dns.DataMart.Lib.Classes.UnattendedMode.NotifyOnly;
                            d.ProcessQueriesAndNotUpload           = c.UnattendedMode == Lpp.Dns.DataMart.Lib.Classes.UnattendedMode.ProcessNoUpload;
                            d.ProcessQueriesAndUploadAutomatically = c.UnattendedMode == Lpp.Dns.DataMart.Lib.Classes.UnattendedMode.ProcessAndUpload;
                            var p = new List <PropertyData>();
                            p.AddRange(((ModelProperties)serializer.Deserialize(reader)).Properties.Where(prop => prop.Name != "ConfirmPassword"));
                            mp.Properties = p;

                            if (mp.ProcessorId != Guid.Empty)
                            {
                                try
                                {
                                    //TODO: confirm the latest package exists for the model/processor combination

                                    var packageIdentifier = DnsServiceManager.GetRequestTypeIdentifier(ns, mp.ModelId, mp.ProcessorId);
                                    if (packageIdentifier != null && !File.Exists(Path.Combine(Configuration.PackagesFolderPath, packageIdentifier.Identifier + "." + packageIdentifier.Version + ".zip")))
                                    {
                                        DnsServiceManager.DownloadPackage(ns, packageIdentifier);
                                    }

                                    //string processorPath, className;
                                    //ProcessorManager.FindProcessor(mp.ModelId, mp.ProcessorId, out processorPath, out className);
                                    //mp.ProcessorPath = processorPath;
                                    //mp.ClassName = className;
                                }
                                catch (Exception ex)
                                {
                                    log.Error("Unable to load processor.", ex);
                                }
                            }
                        }
                    }
                }

                Configuration.SaveNetworkSettings();

                if (NetworkSettingChanged != null)
                {
                    NetworkSettingChanged(this, ns);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Update Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
                this.Close();
            }
        }