using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.MainForm.WPF; using System.Windows.Controls; using System.Windows; using HeuristicLab.BackgroundProcessing; using System.Diagnostics; using System.IO; using HeuristicLab.Persistence.Default.Xml; using HeuristicLab.Persistence.Core; using System.Collections.ObjectModel; using HeuristicLab.MainForm; using System.Data; using HeuristicLab.OKB.Client; namespace HeuristicLab.OKB.Cockpit.Admin { public partial class AlgorithmEditor : UserControl, IView, IOKBCockpitItem { public static DependencyProperty AlgorithmProperty = DependencyProperty.Register( "Algorithm", typeof(OKBAdmin.Algorithm), typeof(AlgorithmEditor)); public OKBAdmin.Algorithm Algorithm { get { return (OKBAdmin.Algorithm)GetValue(AlgorithmProperty); } set { SetValue(AlgorithmProperty, value); } } public AlgorithmEditor() { InitializeComponent(); Algorithm = null; } protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { base.OnPropertyChanged(e); if (e.Property == AlgorithmProperty) UpdateAlgorithm(); } private void UpdateAlgorithm() { if (Algorithm == null) { Clear(); return; } OnLoad(this, new RoutedEventArgs()); } private void Clear() { IdBox.Text = null; NameBox.Text = null; DescriptionBox.Text = null; } private bool isLoading = false; protected void OnLoad(object sender, RoutedEventArgs args) { if (isLoading || Algorithm == null) return; isLoading = true; OKBAdmin.Algorithm algorithm = Algorithm; OperatorGraphEditorHost editor = OperatorGraphEditor; var loader = new ObservableBackgroundWorker("downloading algorithm") { WorkerReportsProgress = true, WorkerSupportsCancellation = true, }; loader.DoWork += (s, a) => { OKBAdmin.AdminServiceClient client = ClientFactory.Create(); algorithm = client.GetCompleteAlgorithm(algorithm.Id); client.Close(); loader.ReportProgress(10); byte[] data = new DataClientHelper(loader, a) { ReportProgress = p => loader.ReportProgress(10 + 80 * p / 100) }.Load(OKBData.EntityType.Algorithm, algorithm.Id); if (data != null) { editor.LoadData(data, algorithm.Platform, OperatorGraphEditorHost.EditorType.Algorithm); loader.ReportProgress(100); } }; loader.RunWorkerCompleted += (s, a) => { if (algorithm == null) return; Algorithm = algorithm; IdBox.Text = Algorithm.Id.ToString(); NameBox.Text = Algorithm.Name; DescriptionBox.Text = Algorithm.Description; isLoading = false; }; loader.RunWorkerAsync(); } protected void OnSave(object sender, RoutedEventArgs args) { if (Algorithm == null) return; Algorithm.Name = NameBox.Text; Algorithm.Description = DescriptionBox.Text; OKBAdmin.Algorithm algorithm = Algorithm; OperatorGraphEditorHost editor = OperatorGraphEditor; var saver = new ObservableBackgroundWorker("uploading algorithm") { WorkerReportsProgress = true, WorkerSupportsCancellation = true, }; saver.DoWork += (s, a) => { OKBAdmin.AdminServiceClient client = ClientFactory.Create(); client.UpdateCompleteAlgorithm(algorithm); client.Close(); saver.ReportProgress(10); byte[] data = editor.GetAlgorithmData(); if (data != null) { saver.ReportProgress(20); new DataClientHelper(saver, a) { ReportProgress = p => saver.ReportProgress(20 + 80 * p / 100) }.Save(OKBData.EntityType.Algorithm, algorithm.Id, data); } else { saver.ReportProgress(100); } }; saver.RunWorkerAsync(); } private IEnumerable GetObjects(DataTable table, IEnumerable propertyNames) where T : class { foreach (DataRow row in table.Rows) { if (row["Id"] != DBNull.Value) { T o = Activator.CreateInstance(); foreach (string name in propertyNames) typeof(T).GetProperty(name).SetValue(o, row[name] == DBNull.Value ? null : (object)row[name], null); yield return o; } } } private bool ContentHasParameter(OKBAdmin.Parameter p) { return Algorithm.Algorithm_Parameters.Any(ap => ap.ParameterId == p.Id); } private bool ContentHasResult(OKBAdmin.Result result) { return Algorithm.Algorithm_Results.Any(ar => ar.ResultId == result.Id); } protected void OnView(object sender, EventArgs args) { if (sender == ParametersButton && Algorithm != null) EntityEditorSupport.ShowSelector(ContentHasParameter, UpdateParameters); if (sender == ResultsButton && Algorithm != null) EntityEditorSupport.ShowSelector(ContentHasResult, UpdateResults); } private void UpdateParameters(IEnumerable parameters) { Algorithm.Algorithm_Parameters.Clear(); foreach (OKBAdmin.Parameter p in parameters) { Algorithm.Algorithm_Parameters.Add(new OKBAdmin.Algorithm_Parameter() { AlgorithmId = Algorithm.Id, ParameterId = p.Id, }); } } private void UpdateResults(IEnumerable results) { Algorithm.Algorithm_Results.Clear(); foreach (OKBAdmin.Result r in results) { Algorithm.Algorithm_Results.Add(new OKBAdmin.Algorithm_Result() { AlgorithmId = Algorithm.Id, ResultId = r.Id, }); } } private void OnDownload(object sender, EventArgs args) { PersistenceHacker.Download(OKBData.EntityType.Algorithm, Algorithm.Id); } private void OnUpload(object sender, EventArgs args) { PersistenceHacker.Upload(OKBData.EntityType.Algorithm, Algorithm.Id); } #region IView Members public string Caption { get { return "Algorithm Editor"; } set { throw new NotSupportedException(); } } public event EventHandler CaptionChanged; public event EventHandler Changed; protected void OnChanged() { EventHandler handler = CaptionChanged; if (handler != null) handler(this, EventArgs.Empty); } public virtual void Close() { MainFormManager.GetMainForm().CloseView(this); IsShown = false; } public void Hide() { MainFormManager.GetMainForm().HideView(this); IsShown = false; } public bool IsShown { get; protected set; } private bool readOnly = false; public bool ReadOnly { get { return readOnly; } set { if (value == readOnly) return; readOnly = value; OnReadOnlyChanged(); } } public event EventHandler ReadOnlyChanged; protected void OnReadOnlyChanged() { EventHandler handler = ReadOnlyChanged; if (handler != null) handler(this, EventArgs.Empty); } public void Show() { MainFormManager.GetMainForm().ShowView(this); IsShown = true; } #endregion } }