Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/HiveClient.cs @ 4424

Last change on this file since 4424 was 4424, checked in by cneumuel, 14 years ago
  • Added and updated License Information in every file
  • Sort and remove usings in every file
  • Deleted obsolete DataAccess.ADOHelper
  • Deleted some obsolete files
File size: 5.7 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.Hive.Contracts;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using HeuristicLab.Hive.Contracts.Interfaces;
29using HeuristicLab.Hive.Contracts.ResponseObjects;
30
31namespace HeuristicLab.Hive.Experiment {
32  [Item("Hive Client", "Connects to Hive and lists all submitted experiments by the current user.")]
33  [Creatable("Hive")]
34  public class HiveClient : Item, IProgressReporter {
35    private static object locker = new object();
36    private bool currentlyUpdating;
37
38    private ILog log;
39    public ILog Log {
40      get { return log; }
41    }
42
43    private HiveExperimentList hiveExperiments;
44    public HiveExperimentList HiveExperiments {
45      get { return hiveExperiments; }
46      set {
47        if (hiveExperiments != value) {
48          DeRegisterHiveExperimentsEvents();
49          hiveExperiments = value;
50          RegisterHiveExperimentsEvent();
51          OnHiveExperimentsChanged();
52        }
53      }
54    }
55
56    private bool isProgressing;
57    public bool IsProgressing {
58      get { return isProgressing; }
59      set {
60        if (isProgressing != value) {
61          isProgressing = value;
62          OnIsProgressingChanged();
63        }
64      }
65    }
66
67    private IProgress progress;
68    public IProgress Progress {
69      get { return progress; }
70    }
71
72    private void RegisterHiveExperimentsEvent() {
73      if (hiveExperiments != null) {
74        hiveExperiments.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<HiveExperiment>>(hiveExperiments_ItemsRemoved);
75      }
76    }
77
78    private void DeRegisterHiveExperimentsEvents() {
79      if (hiveExperiments != null) {
80        hiveExperiments.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<HiveExperiment>>(hiveExperiments_ItemsRemoved);
81      }
82    }
83
84    public HiveClient() {
85      this.log = new Log();
86    }
87
88    public override Common.IDeepCloneable Clone(Common.Cloner cloner) {
89      HiveClient clone = (HiveClient)base.Clone(cloner);
90      clone.log = (ILog)cloner.Clone(this.log);
91      clone.hiveExperiments = (HiveExperimentList)cloner.Clone(this.hiveExperiments);
92      return clone;
93    }
94
95    public void UpdateExperimentList() {
96      this.progress = new Progress("Downloading HiveExperiments...");
97      try {
98        IsProgressing = true;
99        if (this.HiveExperiments == null) {
100          this.HiveExperiments = new HiveExperimentList();
101        }
102        using (Disposable<IClientFacade> service = ServiceLocator.Instance.ClientFacadePool.GetService()) {
103          currentlyUpdating = true;
104          ResponseObject<HiveExperimentDtoList> response = service.Obj.GetHiveExperiments();
105          progress.Status = "Populating HiveExperiment list...";
106          RefreshExperimentList(response.Obj);
107          currentlyUpdating = false;
108        }
109      }
110      catch (Exception) {
111        this.HiveExperiments = null;
112        throw;
113      }
114      finally {
115        IsProgressing = false;
116      }
117    }
118
119    private void RefreshExperimentList(HiveExperimentDtoList hiveExperiments) {
120      foreach (HiveExperimentDto hiveExperimentDto in hiveExperiments) {
121        HiveExperiment hiveExperiment = GetHiveExperiment(hiveExperimentDto.Id);
122        if (hiveExperiment == null) {
123          // not yet there, create new
124          this.HiveExperiments.Add(new HiveExperiment(hiveExperimentDto));
125        } else {
126          // update
127          hiveExperiment.UpdateFromDto(hiveExperimentDto);
128        }
129      }
130    }
131
132    private HiveExperiment GetHiveExperiment(Guid hiveExperimentId) {
133      return this.HiveExperiments.SingleOrDefault(he => he.HiveExperimentId.Equals(hiveExperimentId));
134    }
135
136    private void LogMessage(string message) {
137      // HeuristicLab.Log is not Thread-Safe, so lock on every call
138      lock (locker) {
139        log.LogMessage(message);
140      }
141    }
142
143    public event EventHandler HiveExperimentsChanged;
144    private void OnHiveExperimentsChanged() {
145      var handler = HiveExperimentsChanged;
146      if (handler != null) handler(this, EventArgs.Empty);
147    }
148   
149    void hiveExperiments_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<HiveExperiment>> e) {
150      if (!currentlyUpdating) {
151        using (Disposable<IClientFacade> service = ServiceLocator.Instance.ClientFacadePool.GetService()) {
152          foreach (IndexedItem<HiveExperiment> item in e.Items) {
153            if (item.Value.HiveExperimentId != Guid.Empty) {
154              service.Obj.DeleteHiveExperiment(item.Value.HiveExperimentId);
155            }
156          }
157        }
158      }
159    }
160
161    public event EventHandler IsProgressingChanged;
162    private void OnIsProgressingChanged() {
163      var handler = IsProgressingChanged;
164      if (handler != null) handler(this, EventArgs.Empty);
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.