#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Linq; using HeuristicLab.Collections; using HeuristicLab.Core; using HeuristicLab.Services.Hive.Common.ServiceContracts; using HeuristicLab.Services.Hive.Common; using System.Collections.Generic; namespace HeuristicLab.Clients.Hive { using DT = HeuristicLab.Services.Hive.Common.DataTransfer; using HeuristicLab.Common; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Clients.Common; [Item("Hive Client", "Connects to Hive and lists all submitted experiments by the current user.")] [Creatable("Hive")] public class HiveExperimentManagerClient : Item, IProgressReporter { private static object locker = new object(); private bool currentlyUpdating; private ItemList hiveExperiments; public ItemList HiveExperiments { get { return hiveExperiments; } set { if (hiveExperiments != value) { DeRegisterHiveExperimentsEvents(); hiveExperiments = value; RegisterHiveExperimentsEvent(); OnHiveExperimentsChanged(); } } } private bool isProgressing; public bool IsProgressing { get { return isProgressing; } set { if (isProgressing != value) { isProgressing = value; OnIsProgressingChanged(); } } } private IProgress progress; public IProgress Progress { get { return progress; } } public HiveExperimentManagerClient() { } protected HiveExperimentManagerClient(HiveExperimentManagerClient original, Cloner cloner) : base(original, cloner) { this.hiveExperiments = cloner.Clone(original.hiveExperiments); } public override IDeepCloneable Clone(Cloner cloner) { return new HiveExperimentManagerClient(this, cloner); } private void RegisterHiveExperimentsEvent() { if (hiveExperiments != null) { hiveExperiments.ItemsRemoved += new CollectionItemsChangedEventHandler>(hiveExperiments_ItemsRemoved); } } private void DeRegisterHiveExperimentsEvents() { if (hiveExperiments != null) { hiveExperiments.ItemsRemoved -= new CollectionItemsChangedEventHandler>(hiveExperiments_ItemsRemoved); } } public void UpdateExperimentList() { this.progress = new Progress("Downloading HiveExperiments..."); try { IsProgressing = true; if (this.HiveExperiments == null) { this.HiveExperiments = new ItemList(); } using (var service = ServiceLocator.Instance.GetService()) { currentlyUpdating = true; IEnumerable response = service.Obj.GetHiveExperiments(); progress.Status = "Populating HiveExperiment list..."; RefreshExperimentList(response); currentlyUpdating = false; } } catch (Exception) { this.HiveExperiments = null; throw; } finally { IsProgressing = false; } } private void RefreshExperimentList(IEnumerable hiveExperiments) { foreach (DT.HiveExperiment hiveExperimentDto in hiveExperiments) { HiveExperimentClient hiveExperiment = GetHiveExperiment(hiveExperimentDto.Id); if (hiveExperiment == null) { // not yet there, create new this.HiveExperiments.Add(new HiveExperimentClient(hiveExperimentDto)); } else { // update hiveExperiment.UpdateFromDto(hiveExperimentDto); } } } private HiveExperimentClient GetHiveExperiment(Guid hiveExperimentId) { return this.HiveExperiments.SingleOrDefault(he => he.HiveExperimentId.Equals(hiveExperimentId)); } public event EventHandler HiveExperimentsChanged; private void OnHiveExperimentsChanged() { var handler = HiveExperimentsChanged; if (handler != null) handler(this, EventArgs.Empty); } void hiveExperiments_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs> e) { if (!currentlyUpdating) { using (Disposable service = ServiceLocator.Instance.GetService()) { foreach (IndexedItem item in e.Items) { if (item.Value.HiveExperimentId != Guid.Empty) { service.Obj.DeleteHiveExperiment(item.Value.HiveExperimentId); } } } } } public event EventHandler IsProgressingChanged; private void OnIsProgressingChanged() { var handler = IsProgressingChanged; if (handler != null) handler(this, EventArgs.Empty); } } }