[4426] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5902] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4426] | 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;
|
---|
[4466] | 23 | using System.Collections.Generic;
|
---|
[4433] | 24 | using System.Linq;
|
---|
[4426] | 25 | using HeuristicLab.Clients.Common;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 |
|
---|
[5533] | 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 {
|
---|
[4433] | 34 | get {
|
---|
[5533] | 35 | if (instance == null) instance = new AdministrationClient();
|
---|
[4433] | 36 | return instance;
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[4481] | 40 | #region Properties
|
---|
[4441] | 41 | private ItemCollection<Platform> platforms;
|
---|
| 42 | public ItemCollection<Platform> Platforms {
|
---|
| 43 | get { return platforms; }
|
---|
| 44 | }
|
---|
[4426] | 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 | }
|
---|
[4481] | 53 | private ItemCollection<ProblemClass> problemClasses;
|
---|
| 54 | public ItemCollection<ProblemClass> ProblemClasses {
|
---|
| 55 | get { return problemClasses; }
|
---|
[4466] | 56 | }
|
---|
[4481] | 57 | private ItemCollection<Problem> problems;
|
---|
| 58 | public ItemCollection<Problem> Problems {
|
---|
| 59 | get { return problems; }
|
---|
[4466] | 60 | }
|
---|
[4481] | 61 | #endregion
|
---|
[4426] | 62 |
|
---|
[5550] | 63 | public AdministrationClient() { }
|
---|
[4426] | 64 |
|
---|
[4481] | 65 | #region Refresh
|
---|
[4433] | 66 | public void Refresh() {
|
---|
| 67 | OnRefreshing();
|
---|
[4549] | 68 |
|
---|
[5550] | 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>();
|
---|
[4433] | 74 |
|
---|
[5550] | 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) {
|
---|
[4441] | 87 | var call = new Func<Exception>(delegate() {
|
---|
| 88 | try {
|
---|
[5550] | 89 | Refresh();
|
---|
[4441] | 90 | }
|
---|
| 91 | catch (Exception ex) {
|
---|
| 92 | return ex;
|
---|
| 93 | }
|
---|
[5550] | 94 | return null;
|
---|
[4433] | 95 | });
|
---|
[4441] | 96 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
| 97 | Exception ex = call.EndInvoke(result);
|
---|
[5550] | 98 | if (ex != null) exceptionCallback(ex);
|
---|
[4433] | 99 | }, null);
|
---|
[4426] | 100 | }
|
---|
[4481] | 101 | #endregion
|
---|
[4426] | 102 |
|
---|
[4481] | 103 | #region Store
|
---|
[5550] | 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));
|
---|
[4441] | 127 | }
|
---|
[4433] | 128 | }
|
---|
[4587] | 129 | #endregion
|
---|
[4566] | 130 |
|
---|
[5550] | 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 |
|
---|
[4481] | 147 | #region Algorithm Methods
|
---|
[5550] | 148 | public static List<Guid> GetAlgorithmUsers(long algorithmId) {
|
---|
| 149 | return CallAdministrationService<List<Guid>>(s => s.GetAlgorithmUsers(algorithmId));
|
---|
[4466] | 150 | }
|
---|
[5550] | 151 | public static void UpdateAlgorithmUsers(long algorithmId, List<Guid> users) {
|
---|
| 152 | CallAdministrationService(s => s.UpdateAlgorithmUsers(algorithmId, users));
|
---|
[4466] | 153 | }
|
---|
[5550] | 154 | public static byte[] GetAlgorithmData(long algorithmId) {
|
---|
| 155 | return CallAdministrationService<byte[]>(s => s.GetAlgorithmData(algorithmId));
|
---|
[4481] | 156 | }
|
---|
[5550] | 157 | public static void UpdateAlgorithmData(long algorithmId, byte[] algorithmData) {
|
---|
| 158 | CallAdministrationService(s => s.UpdateAlgorithmData(algorithmId, algorithmData));
|
---|
[4481] | 159 | }
|
---|
| 160 | #endregion
|
---|
[4466] | 161 |
|
---|
[4481] | 162 | #region Problem Methods
|
---|
[5550] | 163 | public static List<Guid> GetProblemUsers(long problemId) {
|
---|
| 164 | return CallAdministrationService<List<Guid>>(s => s.GetProblemUsers(problemId));
|
---|
[4481] | 165 | }
|
---|
[5550] | 166 | public static void UpdateProblemUsers(long problemId, List<Guid> users) {
|
---|
| 167 | CallAdministrationService(s => s.UpdateProblemUsers(problemId, users));
|
---|
[4466] | 168 | }
|
---|
[5550] | 169 | public static byte[] GetProblemData(long problemId) {
|
---|
| 170 | return CallAdministrationService<byte[]>(s => s.GetProblemData(problemId));
|
---|
[4481] | 171 | }
|
---|
[5550] | 172 | public static void UpdateProblemData(long problemId, byte[] problemData) {
|
---|
| 173 | CallAdministrationService(s => s.UpdateProblemData(problemId, problemData));
|
---|
[4481] | 174 | }
|
---|
[4943] | 175 | #endregion
|
---|
| 176 |
|
---|
[4481] | 177 | #region Events
|
---|
[4433] | 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() {
|
---|
[5550] | 185 | var handler = Refreshed;
|
---|
[4433] | 186 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 187 | }
|
---|
[4481] | 188 | #endregion
|
---|
[4433] | 189 |
|
---|
| 190 | #region Helpers
|
---|
[5550] | 191 | private static void CallAdministrationService(Action<IAdministrationService> call) {
|
---|
[5299] | 192 | AdministrationServiceClient client = ClientFactory.CreateClient<AdministrationServiceClient, IAdministrationService>();
|
---|
[4433] | 193 | try {
|
---|
| 194 | call(client);
|
---|
| 195 | }
|
---|
| 196 | finally {
|
---|
[4426] | 197 | try {
|
---|
[4433] | 198 | client.Close();
|
---|
[4426] | 199 | }
|
---|
[4433] | 200 | catch (Exception) {
|
---|
| 201 | client.Abort();
|
---|
[4426] | 202 | }
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
[5550] | 205 | private static T CallAdministrationService<T>(Func<IAdministrationService, T> call) {
|
---|
[5299] | 206 | AdministrationServiceClient client = ClientFactory.CreateClient<AdministrationServiceClient, IAdministrationService>();
|
---|
[4433] | 207 | try {
|
---|
| 208 | return call(client);
|
---|
[4426] | 209 | }
|
---|
[4433] | 210 | finally {
|
---|
| 211 | try {
|
---|
| 212 | client.Close();
|
---|
| 213 | }
|
---|
| 214 | catch (Exception) {
|
---|
| 215 | client.Abort();
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
[4426] | 218 | }
|
---|
[4433] | 219 | #endregion
|
---|
[4426] | 220 | }
|
---|
| 221 | }
|
---|