Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Clients.Hive/3.3/HiveExperiment/HiveClient.cs @ 4649

Last change on this file since 4649 was 4649, checked in by cneumuel, 13 years ago
  • moved db-context into datalayer
  • businesslayer only defines the transaction-scope
  • removed contextfactory
  • implemented convert-methods
  • made naming in db and domainobjects more consistent
  • changed wcf-service to be http-only (for now)

(#1233)

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Core;
26using HeuristicLab.Services.Hive.Common.ServiceContracts;
27using HeuristicLab.Services.Hive.Common;
28using System.Collections.Generic;
29
30namespace HeuristicLab.Clients.Hive {
31  using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
32  using HeuristicLab.Common;
33
34  [Item("Hive Client", "Connects to Hive and lists all submitted experiments by the current user.")]
35  [Creatable("Hive")]
36  public class HiveClient : Item, IProgressReporter {
37    private static object locker = new object();
38    private bool currentlyUpdating;
39
40    private ItemList<HiveExperimentClient> hiveExperiments;
41    public ItemList<HiveExperimentClient> HiveExperiments {
42      get { return hiveExperiments; }
43      set {
44        if (hiveExperiments != value) {
45          DeRegisterHiveExperimentsEvents();
46          hiveExperiments = value;
47          RegisterHiveExperimentsEvent();
48          OnHiveExperimentsChanged();
49        }
50      }
51    }
52
53    private bool isProgressing;
54    public bool IsProgressing {
55      get { return isProgressing; }
56      set {
57        if (isProgressing != value) {
58          isProgressing = value;
59          OnIsProgressingChanged();
60        }
61      }
62    }
63
64    private IProgress progress;
65    public IProgress Progress {
66      get { return progress; }
67    }
68
69    private void RegisterHiveExperimentsEvent() {
70      if (hiveExperiments != null) {
71        hiveExperiments.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<HiveExperimentClient>>(hiveExperiments_ItemsRemoved);
72      }
73    }
74
75    private void DeRegisterHiveExperimentsEvents() {
76      if (hiveExperiments != null) {
77        hiveExperiments.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<HiveExperimentClient>>(hiveExperiments_ItemsRemoved);
78      }
79    }
80
81    public HiveClient() {
82     
83    }
84
85    public override IDeepCloneable Clone(Cloner cloner) {
86      HiveClient clone = (HiveClient)base.Clone(cloner);
87      clone.hiveExperiments = (ItemList<HiveExperimentClient>)cloner.Clone(this.hiveExperiments);
88      return clone;
89    }
90
91    public void UpdateExperimentList() {
92      this.progress = new Progress("Downloading HiveExperiments...");
93      try {
94        IsProgressing = true;
95        if (this.HiveExperiments == null) {
96          this.HiveExperiments = new ItemList<HiveExperimentClient>();
97        }
98        using (var service = ServiceLocator.Instance.ServicePool.GetService()) {
99          currentlyUpdating = true;
100          IEnumerable<DT.HiveExperiment> response = service.Obj.GetHiveExperiments();
101          progress.Status = "Populating HiveExperiment list...";
102          RefreshExperimentList(response);
103          currentlyUpdating = false;
104        }
105      }
106      catch (Exception) {
107        this.HiveExperiments = null;
108        throw;
109      }
110      finally {
111        IsProgressing = false;
112      }
113    }
114
115    private void RefreshExperimentList(IEnumerable<DT.HiveExperiment> hiveExperiments) {
116      foreach (DT.HiveExperiment hiveExperimentDto in hiveExperiments) {
117        HiveExperimentClient hiveExperiment = GetHiveExperiment(hiveExperimentDto.Id);
118        if (hiveExperiment == null) {
119          // not yet there, create new
120          this.HiveExperiments.Add(new HiveExperimentClient(hiveExperimentDto));
121        } else {
122          // update
123          hiveExperiment.UpdateFromDto(hiveExperimentDto);
124        }
125      }
126    }
127
128    private HiveExperimentClient GetHiveExperiment(Guid hiveExperimentId) {
129      return this.HiveExperiments.SingleOrDefault(he => he.HiveExperimentId.Equals(hiveExperimentId));
130    }
131
132    public event EventHandler HiveExperimentsChanged;
133    private void OnHiveExperimentsChanged() {
134      var handler = HiveExperimentsChanged;
135      if (handler != null) handler(this, EventArgs.Empty);
136    }
137   
138    void hiveExperiments_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<HiveExperimentClient>> e) {
139      if (!currentlyUpdating) {
140        using (Disposable<IHiveService> service = ServiceLocator.Instance.ServicePool.GetService()) {
141          foreach (IndexedItem<HiveExperimentClient> item in e.Items) {
142            if (item.Value.HiveExperimentId != Guid.Empty) {
143              service.Obj.DeleteHiveExperiment(item.Value.HiveExperimentId);
144            }
145          }
146        }
147      }
148    }
149
150    public event EventHandler IsProgressingChanged;
151    private void OnIsProgressingChanged() {
152      var handler = IsProgressingChanged;
153      if (handler != null) handler(this, EventArgs.Empty);
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.