1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Clients.OKB.Administration {
|
---|
29 | [Item("OKBItem Collection", "Represents a collection of OKB items.")]
|
---|
30 | public class OKBItemCollection<T> : ItemCollection<T> where T : class, IOKBItem {
|
---|
31 | protected OKBItemCollection(OKBItemCollection<T> original, Cloner cloner) : base(original, cloner) { }
|
---|
32 | public OKBItemCollection() : base() { }
|
---|
33 | public OKBItemCollection(IEnumerable<T> collection) : base(collection) { }
|
---|
34 |
|
---|
35 | public override IDeepCloneable Clone(Cloner cloner) { return new OKBItemCollection<T>(this, cloner); }
|
---|
36 |
|
---|
37 | protected override void OnItemsRemoved(IEnumerable<T> items) {
|
---|
38 | IEnumerable<T> successful, unsuccessful;
|
---|
39 | Exception ex;
|
---|
40 | RemoveItems(items, out successful, out unsuccessful, out ex);
|
---|
41 | list.AddRange(unsuccessful);
|
---|
42 | base.OnItemsRemoved(successful);
|
---|
43 | if (ex != null) throw ex;
|
---|
44 | }
|
---|
45 | protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
46 | IEnumerable<T> successful, unsuccessful;
|
---|
47 | Exception ex;
|
---|
48 | RemoveItems(oldItems, out successful, out unsuccessful, out ex);
|
---|
49 | list.AddRange(unsuccessful);
|
---|
50 | base.OnCollectionReset(items.Concat(unsuccessful), oldItems);
|
---|
51 | if (ex != null) throw ex;
|
---|
52 | }
|
---|
53 |
|
---|
54 | private void RemoveItems(IEnumerable<T> items, out IEnumerable<T> successful, out IEnumerable<T> unsuccessful, out Exception exception) {
|
---|
55 | List<T> removed = new List<T>();
|
---|
56 | List<T> notremoved = new List<T>();
|
---|
57 | exception = null;
|
---|
58 | foreach (T item in items) {
|
---|
59 | try {
|
---|
60 | AdministrationClient.Delete(item);
|
---|
61 | removed.Add(item);
|
---|
62 | }
|
---|
63 | catch (Exception ex) {
|
---|
64 | exception = ex;
|
---|
65 | notremoved.Add(item);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | successful = removed;
|
---|
69 | unsuccessful = notremoved;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|