1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Clients.Hive {
|
---|
8 | [Item("HiveItem Collection", "Represents a collection of OKB items.")]
|
---|
9 | public class HiveItemCollection<T> : ItemCollection<T> where T : class, IHiveItem {
|
---|
10 | protected HiveItemCollection(HiveItemCollection<T> original, Cloner cloner) : base(original, cloner) { }
|
---|
11 | public HiveItemCollection() : base() { }
|
---|
12 | public HiveItemCollection(IEnumerable<T> collection) : base(collection) { }
|
---|
13 |
|
---|
14 | public override IDeepCloneable Clone(Cloner cloner) { return new HiveItemCollection<T>(this, cloner); }
|
---|
15 |
|
---|
16 | protected override void OnItemsRemoved(IEnumerable<T> items) {
|
---|
17 | IEnumerable<T> successful, unsuccessful;
|
---|
18 | Exception ex;
|
---|
19 | RemoveItems(items, out successful, out unsuccessful, out ex);
|
---|
20 | list.AddRange(unsuccessful);
|
---|
21 | base.OnItemsRemoved(successful);
|
---|
22 | if (ex != null) throw ex;
|
---|
23 | }
|
---|
24 | protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
25 | IEnumerable<T> successful, unsuccessful;
|
---|
26 | Exception ex;
|
---|
27 | RemoveItems(oldItems, out successful, out unsuccessful, out ex);
|
---|
28 | list.AddRange(unsuccessful);
|
---|
29 | base.OnCollectionReset(items.Concat(unsuccessful), oldItems);
|
---|
30 | if (ex != null) throw ex;
|
---|
31 | }
|
---|
32 |
|
---|
33 | private void RemoveItems(IEnumerable<T> items, out IEnumerable<T> successful, out IEnumerable<T> unsuccessful, out Exception exception) {
|
---|
34 | List<T> removed = new List<T>();
|
---|
35 | List<T> notremoved = new List<T>();
|
---|
36 | exception = null;
|
---|
37 | foreach (T item in items) {
|
---|
38 | try {
|
---|
39 | ExperimentManagerClient.Delete(item);
|
---|
40 | removed.Add(item);
|
---|
41 | }
|
---|
42 | catch (Exception ex) {
|
---|
43 | exception = ex;
|
---|
44 | notremoved.Add(item);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | successful = removed;
|
---|
48 | unsuccessful = notremoved;
|
---|
49 | }
|
---|
50 | }
|
---|
51 | } |
---|