[8055] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8055] | 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.Clients.Common;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Clients.OKB.Administration {
|
---|
| 30 | [Item("AdministrationClient", "OKB administration client.")]
|
---|
| 31 | public sealed class AdministrationClient : IContent {
|
---|
| 32 | private static AdministrationClient instance;
|
---|
| 33 | public static AdministrationClient Instance {
|
---|
| 34 | get {
|
---|
| 35 | if (instance == null) instance = new AdministrationClient();
|
---|
| 36 | return instance;
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | #region Properties
|
---|
| 41 | private ItemCollection<Platform> platforms;
|
---|
| 42 | public ItemCollection<Platform> Platforms {
|
---|
| 43 | get { return platforms; }
|
---|
| 44 | }
|
---|
| 45 | private ItemCollection<AlgorithmClass> algorithmClasses;
|
---|
| 46 | public ItemCollection<AlgorithmClass> AlgorithmClasses {
|
---|
| 47 | get { return algorithmClasses; }
|
---|
| 48 | }
|
---|
| 49 | private ItemCollection<Algorithm> algorithms;
|
---|
| 50 | public ItemCollection<Algorithm> Algorithms {
|
---|
| 51 | get { return algorithms; }
|
---|
| 52 | }
|
---|
| 53 | private ItemCollection<ProblemClass> problemClasses;
|
---|
| 54 | public ItemCollection<ProblemClass> ProblemClasses {
|
---|
| 55 | get { return problemClasses; }
|
---|
| 56 | }
|
---|
| 57 | private ItemCollection<Problem> problems;
|
---|
| 58 | public ItemCollection<Problem> Problems {
|
---|
| 59 | get { return problems; }
|
---|
| 60 | }
|
---|
| 61 | #endregion
|
---|
| 62 |
|
---|
| 63 | public AdministrationClient() { }
|
---|
| 64 |
|
---|
| 65 | #region Refresh
|
---|
| 66 | public void Refresh() {
|
---|
| 67 | OnRefreshing();
|
---|
| 68 |
|
---|
| 69 | platforms = new OKBItemCollection<Platform>();
|
---|
| 70 | algorithmClasses = new OKBItemCollection<AlgorithmClass>();
|
---|
| 71 | algorithms = new OKBItemCollection<Algorithm>();
|
---|
| 72 | problemClasses = new OKBItemCollection<ProblemClass>();
|
---|
| 73 | problems = new OKBItemCollection<Problem>();
|
---|
| 74 |
|
---|
| 75 | try {
|
---|
| 76 | platforms.AddRange(CallAdministrationService<List<Platform>>(s => s.GetPlatforms()).OrderBy(x => x.Name));
|
---|
| 77 | algorithmClasses.AddRange(CallAdministrationService<List<AlgorithmClass>>(s => s.GetAlgorithmClasses()).OrderBy(x => x.Name));
|
---|
| 78 | algorithms.AddRange(CallAdministrationService<List<Algorithm>>(s => s.GetAlgorithms()).OrderBy(x => x.Name));
|
---|
| 79 | problemClasses.AddRange(CallAdministrationService<List<ProblemClass>>(s => s.GetProblemClasses()).OrderBy(x => x.Name));
|
---|
| 80 | problems.AddRange(CallAdministrationService<List<Problem>>(s => s.GetProblems()).OrderBy(x => x.Name));
|
---|
| 81 | }
|
---|
| 82 | finally {
|
---|
| 83 | OnRefreshed();
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | public void RefreshAsync(Action<Exception> exceptionCallback) {
|
---|
| 87 | var call = new Func<Exception>(delegate() {
|
---|
| 88 | try {
|
---|
| 89 | Refresh();
|
---|
| 90 | }
|
---|
| 91 | catch (Exception ex) {
|
---|
| 92 | return ex;
|
---|
| 93 | }
|
---|
| 94 | return null;
|
---|
| 95 | });
|
---|
| 96 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
| 97 | Exception ex = call.EndInvoke(result);
|
---|
| 98 | if (ex != null) exceptionCallback(ex);
|
---|
| 99 | }, null);
|
---|
| 100 | }
|
---|
| 101 | #endregion
|
---|
| 102 |
|
---|
| 103 | #region Store
|
---|
| 104 | public static void Store(IOKBItem item) {
|
---|
| 105 | if (item.Id == 0) {
|
---|
| 106 | if (item is Platform)
|
---|
| 107 | item.Id = CallAdministrationService<long>(s => s.AddPlatform((Platform)item));
|
---|
| 108 | else if (item is AlgorithmClass)
|
---|
| 109 | item.Id = CallAdministrationService<long>(s => s.AddAlgorithmClass((AlgorithmClass)item));
|
---|
| 110 | else if (item is Algorithm)
|
---|
| 111 | item.Id = CallAdministrationService<long>(s => s.AddAlgorithm((Algorithm)item));
|
---|
| 112 | else if (item is ProblemClass)
|
---|
| 113 | item.Id = CallAdministrationService<long>(s => s.AddProblemClass((ProblemClass)item));
|
---|
| 114 | else if (item is Problem)
|
---|
| 115 | item.Id = CallAdministrationService<long>(s => s.AddProblem((Problem)item));
|
---|
| 116 | } else {
|
---|
| 117 | if (item is Platform)
|
---|
| 118 | CallAdministrationService(s => s.UpdatePlatform((Platform)item));
|
---|
| 119 | else if (item is AlgorithmClass)
|
---|
| 120 | CallAdministrationService(s => s.UpdateAlgorithmClass((AlgorithmClass)item));
|
---|
| 121 | else if (item is Algorithm)
|
---|
| 122 | CallAdministrationService(s => s.UpdateAlgorithm((Algorithm)item));
|
---|
| 123 | else if (item is ProblemClass)
|
---|
| 124 | CallAdministrationService(s => s.UpdateProblemClass((ProblemClass)item));
|
---|
| 125 | else if (item is Problem)
|
---|
| 126 | CallAdministrationService(s => s.UpdateProblem((Problem)item));
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | #endregion
|
---|
| 130 |
|
---|
| 131 | #region Delete
|
---|
| 132 | public static void Delete(IOKBItem item) {
|
---|
| 133 | if (item is Platform)
|
---|
| 134 | CallAdministrationService(s => s.DeletePlatform(item.Id));
|
---|
| 135 | else if (item is AlgorithmClass)
|
---|
| 136 | CallAdministrationService(s => s.DeleteAlgorithmClass(item.Id));
|
---|
| 137 | else if (item is Algorithm)
|
---|
| 138 | CallAdministrationService(s => s.DeleteAlgorithm(item.Id));
|
---|
| 139 | else if (item is ProblemClass)
|
---|
| 140 | CallAdministrationService(s => s.DeleteProblemClass(item.Id));
|
---|
| 141 | else if (item is Problem)
|
---|
| 142 | CallAdministrationService(s => s.DeleteProblem(item.Id));
|
---|
| 143 | item.Id = 0;
|
---|
| 144 | }
|
---|
| 145 | #endregion
|
---|
| 146 |
|
---|
| 147 | #region Algorithm Methods
|
---|
| 148 | public static List<Guid> GetAlgorithmUsers(long algorithmId) {
|
---|
| 149 | return CallAdministrationService<List<Guid>>(s => s.GetAlgorithmUsers(algorithmId));
|
---|
| 150 | }
|
---|
| 151 | public static void UpdateAlgorithmUsers(long algorithmId, List<Guid> users) {
|
---|
| 152 | CallAdministrationService(s => s.UpdateAlgorithmUsers(algorithmId, users));
|
---|
| 153 | }
|
---|
| 154 | public static byte[] GetAlgorithmData(long algorithmId) {
|
---|
| 155 | return CallAdministrationService<byte[]>(s => s.GetAlgorithmData(algorithmId));
|
---|
| 156 | }
|
---|
| 157 | public static void UpdateAlgorithmData(long algorithmId, byte[] algorithmData) {
|
---|
| 158 | CallAdministrationService(s => s.UpdateAlgorithmData(algorithmId, algorithmData));
|
---|
| 159 | }
|
---|
| 160 | #endregion
|
---|
| 161 |
|
---|
| 162 | #region Problem Methods
|
---|
| 163 | public static List<Guid> GetProblemUsers(long problemId) {
|
---|
| 164 | return CallAdministrationService<List<Guid>>(s => s.GetProblemUsers(problemId));
|
---|
| 165 | }
|
---|
| 166 | public static void UpdateProblemUsers(long problemId, List<Guid> users) {
|
---|
| 167 | CallAdministrationService(s => s.UpdateProblemUsers(problemId, users));
|
---|
| 168 | }
|
---|
| 169 | public static byte[] GetProblemData(long problemId) {
|
---|
| 170 | return CallAdministrationService<byte[]>(s => s.GetProblemData(problemId));
|
---|
| 171 | }
|
---|
| 172 | public static void UpdateProblemData(long problemId, byte[] problemData) {
|
---|
| 173 | CallAdministrationService(s => s.UpdateProblemData(problemId, problemData));
|
---|
| 174 | }
|
---|
| 175 | #endregion
|
---|
| 176 |
|
---|
| 177 | #region Events
|
---|
| 178 | public event EventHandler Refreshing;
|
---|
| 179 | private void OnRefreshing() {
|
---|
| 180 | EventHandler handler = Refreshing;
|
---|
| 181 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 182 | }
|
---|
| 183 | public event EventHandler Refreshed;
|
---|
| 184 | private void OnRefreshed() {
|
---|
| 185 | var handler = Refreshed;
|
---|
| 186 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 187 | }
|
---|
| 188 | #endregion
|
---|
| 189 |
|
---|
| 190 | #region Helpers
|
---|
| 191 | private static void CallAdministrationService(Action<IAdministrationService> call) {
|
---|
| 192 | AdministrationServiceClient client = ClientFactory.CreateClient<AdministrationServiceClient, IAdministrationService>();
|
---|
| 193 | try {
|
---|
| 194 | call(client);
|
---|
| 195 | }
|
---|
| 196 | finally {
|
---|
| 197 | try {
|
---|
| 198 | client.Close();
|
---|
| 199 | }
|
---|
| 200 | catch (Exception) {
|
---|
| 201 | client.Abort();
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | private static T CallAdministrationService<T>(Func<IAdministrationService, T> call) {
|
---|
| 206 | AdministrationServiceClient client = ClientFactory.CreateClient<AdministrationServiceClient, IAdministrationService>();
|
---|
| 207 | try {
|
---|
| 208 | return call(client);
|
---|
| 209 | }
|
---|
| 210 | finally {
|
---|
| 211 | try {
|
---|
| 212 | client.Close();
|
---|
| 213 | }
|
---|
| 214 | catch (Exception) {
|
---|
| 215 | client.Abort();
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | #endregion
|
---|
| 220 | }
|
---|
| 221 | }
|
---|