예제 #1
0
        private void SettingsForm_Load(object sender, EventArgs e)
        {
            Lpp.Dns.DataMart.Client.DomainManger.DomainManager domainManager = new DomainManger.DomainManager(Configuration.PackagesFolderPath);
            try
            {
                if (ModelDescription == null)
                {
                    return;
                }

                if (ModelDescription.ProcessorId == Guid.Empty)
                {
                    ModelDescription.ProcessorId = HubModel.ModelProcessorId;
                }

                //get the package identifier and version
                var packageIdentifier = DnsServiceManager.GetRequestTypeIdentifier(NetworkSetting, ModelDescription.ModelId, ModelDescription.ProcessorId);
                if (!System.IO.File.Exists(System.IO.Path.Combine(Configuration.PackagesFolderPath, packageIdentifier.PackageName())))
                {
                    DnsServiceManager.DownloadPackage(NetworkSetting, packageIdentifier);
                }
                domainManager.Load(packageIdentifier.Identifier, packageIdentifier.Version);
                modelProcessor = domainManager.GetProcessor(ModelDescription.ProcessorId);
                ProcessorManager.UpdateProcessorSettings(ModelDescription, modelProcessor);

                if (modelProcessor is IEarlyInitializeModelProcessor)
                {
                    ((IEarlyInitializeModelProcessor)modelProcessor).Initialize(ModelDescription.ModelId, Array.Empty <DocumentWithStream>());
                }

                this.SuspendLayout();

                if (modelProcessor.ModelMetadata.Settings == null || !modelProcessor.ModelMetadata.Settings.Any())
                {
                    var noSettingsLabel = new Label();
                    noSettingsLabel.Text      = "This model processor does not have any settings.";
                    noSettingsLabel.Anchor    = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left;
                    noSettingsLabel.TextAlign = ContentAlignment.MiddleCenter;
                    tableLayoutPanel1.Controls.Add(noSettingsLabel, 0, 0);
                    tableLayoutPanel1.SetColumnSpan(noSettingsLabel, 2);
                }
                else
                {
                    _editors = new HashSet <Control>();
                    tableLayoutPanel1.RowStyles.Clear();
                    int rowIndex = 0;

                    IEnumerable <Lpp.Dns.DataMart.Model.Settings.ProcessorSetting> settings = modelProcessor.ModelMetadata.Settings;
                    if (modelProcessor.ModelMetadata.SQlProviders != null && modelProcessor.ModelMetadata.SQlProviders.Any())
                    {
                        DataMart.Client.Controls.DataSourceEditor dsEditor = new Controls.DataSourceEditor(modelProcessor.ModelMetadata, ModelDescription.Properties);
                        dsEditor.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left;

                        tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                        tableLayoutPanel1.Controls.Add(dsEditor, 0, rowIndex++);
                        tableLayoutPanel1.SetColumnSpan(dsEditor, 2);

                        settings = settings.Where(s => !Lpp.Dns.DataMart.Model.Settings.ProcessorSettings.IsDbSetting(s.Key)).ToArray();
                        _editors.Add(dsEditor);
                    }

                    settings.ToList().ForEach(s => {
                        string value = ModelDescription.Properties.Where(p => string.Equals(p.Name, s.Key, StringComparison.OrdinalIgnoreCase)).Select(p => p.Value).FirstOrDefault();

                        if (string.IsNullOrEmpty(value) && s.Required && !string.IsNullOrEmpty(s.DefaultValue))
                        {
                            value = s.DefaultValue;
                        }

                        Label lbl     = new Label();
                        lbl.AutoSize  = true;
                        lbl.Anchor    = AnchorStyles.Right;
                        lbl.TextAlign = ContentAlignment.MiddleRight;
                        lbl.Text      = s.Title.EndsWith(":") ? s.Title : s.Title + ":";

                        Control editor = null;
                        if (s.ValidValues != null)
                        {
                            ComboBox combo      = new ComboBox();
                            combo.DropDownStyle = ComboBoxStyle.DropDownList;
                            combo.Anchor        = AnchorStyles.Right | AnchorStyles.Left;
                            combo.Name          = s.Key;

                            combo.Items.AddRange(s.ValidValues.Select(v => new PropertyData(v.Key, v.Value.ToString())).ToArray());
                            if (!string.IsNullOrEmpty(value))
                            {
                                foreach (PropertyData p in combo.Items)
                                {
                                    if (string.Equals(p.Value, value, StringComparison.OrdinalIgnoreCase))
                                    {
                                        combo.SelectedItem = p;
                                        break;
                                    }
                                }
                            }

                            if (combo.SelectedIndex < 0)
                            {
                                combo.SelectedIndex = 0;
                            }

                            editor = combo;
                        }
                        else
                        {
                            if (s.ValueType == typeof(bool) || s.ValueType == typeof(Nullable <bool>))
                            {
                                CheckBox chkbox  = new CheckBox();
                                chkbox.Anchor    = AnchorStyles.Left;
                                chkbox.Text      = s.Title;
                                chkbox.TextAlign = ContentAlignment.MiddleLeft;
                                chkbox.AutoSize  = true;

                                if (!string.IsNullOrEmpty(value))
                                {
                                    bool isChecked = false;
                                    bool.TryParse(value, out isChecked);
                                    chkbox.Checked = isChecked;
                                }

                                editor = chkbox;
                                lbl    = null;
                            }
                            else if (s.ValueType == typeof(Lpp.Dns.DataMart.Model.Settings.FilePickerEditor))
                            {
                                SelectFileButton btn = new SelectFileButton(s.EditorSettings as Lpp.Dns.DataMart.Model.Settings.FilePickerEditor);
                                if (btn.Multiselect)
                                {
                                    btn.FileNames = ((value ?? "").Trim(',')).Split(',');
                                }
                                else
                                {
                                    btn.FileName = value;
                                }
                                btn.Anchor = AnchorStyles.Right | AnchorStyles.Left;
                                editor     = btn;
                            }
                            else if (s.ValueType == typeof(Lpp.Dns.DataMart.Model.Settings.FolderSelectorEditor))
                            {
                                SelectFolderButton btn = new SelectFolderButton(s.EditorSettings as Lpp.Dns.DataMart.Model.Settings.FolderSelectorEditor);
                                btn.FolderPath         = value;
                                btn.Anchor             = AnchorStyles.Right | AnchorStyles.Left;
                                editor = btn;
                            }
                            else
                            {
                                TextBox txtbox = new TextBox();
                                txtbox.Anchor  = AnchorStyles.Right | AnchorStyles.Left;
                                txtbox.Text    = value;
                                editor         = txtbox;
                            }
                        }

                        if (editor != null)
                        {
                            editor.Tag = s.Key;
                            _editors.Add(editor);

                            tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                            if (lbl != null)
                            {
                                tableLayoutPanel1.Controls.Add(lbl, 0, rowIndex);
                                tableLayoutPanel1.Controls.Add(editor, 1, rowIndex++);
                            }
                            else
                            {
                                tableLayoutPanel1.Controls.Add(editor, 0, rowIndex++);
                                tableLayoutPanel1.SetColumnSpan(editor, 2);
                            }
                        }
                    });

                    //add auto expanding row to bottom to fill empty space
                    Label emptyLabel = new Label();
                    emptyLabel.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left;
                    emptyLabel.Text   = string.Empty;
                    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 100f));
                    tableLayoutPanel1.Controls.Add(emptyLabel, 0, rowIndex);
                    tableLayoutPanel1.SetColumnSpan(emptyLabel, 2);
                }
                this.ResumeLayout(true);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                Close();
            }
            finally
            {
                domainManager.Dispose();
                this.Cursor = Cursors.Default;
            }
        }
예제 #2
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();
            }
        }