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 |
|
---|
35 | [Item("Hive Client", "Connects to Hive and lists all submitted experiments by the current user.")]
|
---|
36 | [Creatable("Hive")]
|
---|
37 | public class HiveExperimentManagerClient : Item, IProgressReporter {
|
---|
38 | private static object locker = new object();
|
---|
39 | private bool currentlyUpdating;
|
---|
40 |
|
---|
41 | private ItemList<HiveExperimentClient> hiveExperiments;
|
---|
42 | public ItemList<HiveExperimentClient> HiveExperiments {
|
---|
43 | get { return hiveExperiments; }
|
---|
44 | set {
|
---|
45 | if (hiveExperiments != value) {
|
---|
46 | DeRegisterHiveExperimentsEvents();
|
---|
47 | hiveExperiments = value;
|
---|
48 | RegisterHiveExperimentsEvent();
|
---|
49 | OnHiveExperimentsChanged();
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private bool isProgressing;
|
---|
55 | public bool IsProgressing {
|
---|
56 | get { return isProgressing; }
|
---|
57 | set {
|
---|
58 | if (isProgressing != value) {
|
---|
59 | isProgressing = value;
|
---|
60 | OnIsProgressingChanged();
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | private IProgress progress;
|
---|
66 | public IProgress Progress {
|
---|
67 | get { return progress; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public HiveExperimentManagerClient() { }
|
---|
71 | protected HiveExperimentManagerClient(HiveExperimentManagerClient original, Cloner cloner)
|
---|
72 | : base(original, cloner) {
|
---|
73 | this.hiveExperiments = cloner.Clone(original.hiveExperiments);
|
---|
74 | }
|
---|
75 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
76 | return new HiveExperimentManagerClient(this, cloner);
|
---|
77 | }
|
---|
78 |
|
---|
79 | private void RegisterHiveExperimentsEvent() {
|
---|
80 | if (hiveExperiments != null) {
|
---|
81 | hiveExperiments.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<HiveExperimentClient>>(hiveExperiments_ItemsRemoved);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | private void DeRegisterHiveExperimentsEvents() {
|
---|
86 | if (hiveExperiments != null) {
|
---|
87 | hiveExperiments.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<HiveExperimentClient>>(hiveExperiments_ItemsRemoved);
|
---|
88 | }
|
---|
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 | }
|
---|