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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Services.Hive.Common.ServiceContracts;
|
---|
27 | using HeuristicLab.Services.Hive.Common;
|
---|
28 | using System.Collections.Generic;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Clients.Hive {
|
---|
31 | using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
32 | using HeuristicLab.Common;
|
---|
33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
34 | using HeuristicLab.Clients.Common;
|
---|
35 |
|
---|
36 | [Item("Hive Client", "Connects to Hive and lists all submitted experiments by the current user.")]
|
---|
37 | [Creatable("Hive")]
|
---|
38 | public class HiveExperimentManagerClient : Item, IProgressReporter {
|
---|
39 | private static object locker = new object();
|
---|
40 | private bool currentlyUpdating;
|
---|
41 |
|
---|
42 | private ItemList<HiveExperimentClient> hiveExperiments;
|
---|
43 | public ItemList<HiveExperimentClient> HiveExperiments {
|
---|
44 | get { return hiveExperiments; }
|
---|
45 | set {
|
---|
46 | if (hiveExperiments != value) {
|
---|
47 | DeRegisterHiveExperimentsEvents();
|
---|
48 | hiveExperiments = value;
|
---|
49 | RegisterHiveExperimentsEvent();
|
---|
50 | OnHiveExperimentsChanged();
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | private bool isProgressing;
|
---|
56 | public bool IsProgressing {
|
---|
57 | get { return isProgressing; }
|
---|
58 | set {
|
---|
59 | if (isProgressing != value) {
|
---|
60 | isProgressing = value;
|
---|
61 | OnIsProgressingChanged();
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | private IProgress progress;
|
---|
67 | public IProgress Progress {
|
---|
68 | get { return progress; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public HiveExperimentManagerClient() { }
|
---|
72 | protected HiveExperimentManagerClient(HiveExperimentManagerClient original, Cloner cloner)
|
---|
73 | : base(original, cloner) {
|
---|
74 | this.hiveExperiments = cloner.Clone(original.hiveExperiments);
|
---|
75 | }
|
---|
76 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
77 | return new HiveExperimentManagerClient(this, cloner);
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void RegisterHiveExperimentsEvent() {
|
---|
81 | if (hiveExperiments != null) {
|
---|
82 | hiveExperiments.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<HiveExperimentClient>>(hiveExperiments_ItemsRemoved);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void DeRegisterHiveExperimentsEvents() {
|
---|
87 | if (hiveExperiments != null) {
|
---|
88 | hiveExperiments.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<HiveExperimentClient>>(hiveExperiments_ItemsRemoved);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void UpdateExperimentList() {
|
---|
93 | this.progress = new Progress("Downloading HiveExperiments...");
|
---|
94 | try {
|
---|
95 | IsProgressing = true;
|
---|
96 | if (this.HiveExperiments == null) {
|
---|
97 | this.HiveExperiments = new ItemList<HiveExperimentClient>();
|
---|
98 | }
|
---|
99 | using (var service = ServiceLocator.Instance.GetService()) {
|
---|
100 | currentlyUpdating = true;
|
---|
101 | IEnumerable<DT.HiveExperiment> response = service.Obj.GetHiveExperiments();
|
---|
102 | progress.Status = "Populating HiveExperiment list...";
|
---|
103 | RefreshExperimentList(response);
|
---|
104 | currentlyUpdating = false;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | catch (Exception) {
|
---|
108 | this.HiveExperiments = null;
|
---|
109 | throw;
|
---|
110 | }
|
---|
111 | finally {
|
---|
112 | IsProgressing = false;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | private void RefreshExperimentList(IEnumerable<DT.HiveExperiment> hiveExperiments) {
|
---|
117 | foreach (DT.HiveExperiment hiveExperimentDto in hiveExperiments) {
|
---|
118 | HiveExperimentClient hiveExperiment = GetHiveExperiment(hiveExperimentDto.Id);
|
---|
119 | if (hiveExperiment == null) {
|
---|
120 | // not yet there, create new
|
---|
121 | this.HiveExperiments.Add(new HiveExperimentClient(hiveExperimentDto));
|
---|
122 | } else {
|
---|
123 | // update
|
---|
124 | hiveExperiment.UpdateFromDto(hiveExperimentDto);
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | private HiveExperimentClient GetHiveExperiment(Guid hiveExperimentId) {
|
---|
130 | return this.HiveExperiments.SingleOrDefault(he => he.HiveExperimentId.Equals(hiveExperimentId));
|
---|
131 | }
|
---|
132 |
|
---|
133 | public event EventHandler HiveExperimentsChanged;
|
---|
134 | private void OnHiveExperimentsChanged() {
|
---|
135 | var handler = HiveExperimentsChanged;
|
---|
136 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
137 | }
|
---|
138 |
|
---|
139 | void hiveExperiments_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<HiveExperimentClient>> e) {
|
---|
140 | if (!currentlyUpdating) {
|
---|
141 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
142 | foreach (IndexedItem<HiveExperimentClient> item in e.Items) {
|
---|
143 | if (item.Value.HiveExperimentId != Guid.Empty) {
|
---|
144 | service.Obj.DeleteHiveExperiment(item.Value.HiveExperimentId);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | public event EventHandler IsProgressingChanged;
|
---|
152 | private void OnIsProgressingChanged() {
|
---|
153 | var handler = IsProgressingChanged;
|
---|
154 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|