#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Threading;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Clients.Hive.WebJobManager.Services;
using System.ServiceModel.Security;
namespace HeuristicLab.Clients.Hive.WebJobManager.Services.Imports
{
[Item("Hive Administrator", "Hive Administrator")]
public sealed class HiveAdminClientWeb : IContent
{
private WebLoginService weblog;
private HiveServiceLocatorWeb servloc;
public Guid userId { get; set; }
public HiveAdminClientWeb(Guid uid) {
weblog = WebLoginService.Instance;
userId = uid;
servloc = weblog.getServiceLocator(uid);
}
private IItemList resources;
public IItemList Resources
{
get { return resources; }
}
private IItemList downtimes;
public IItemList Downtimes
{
get { return downtimes; }
}
private Guid downtimeForResourceId;
public Guid DowntimeForResourceId
{
get { return downtimeForResourceId; }
set
{
downtimeForResourceId = value;
if (downtimes != null)
{
downtimes.Clear();
}
}
}
#region Events
public event EventHandler Refreshing;
private void OnRefreshing()
{
EventHandler handler = Refreshing;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Refreshed;
private void OnRefreshed()
{
var handler = Refreshed;
if (handler != null) handler(this, EventArgs.Empty);
}
#endregion
#region Refresh
public void Refresh()
{
OnRefreshing();
try
{
resources = new ItemList();
servloc.CallHiveService(service =>
{
service.GetSlaveGroups().ForEach(g => resources.Add(g));
service.GetSlaves().ForEach(s => resources.Add(s));
});
}
catch(Exception e)
{
throw e;
}
finally
{
OnRefreshed();
}
}
#endregion
#region Refresh downtime calendar
public void RefreshCalendar()
{
if (downtimeForResourceId != null && downtimeForResourceId != Guid.Empty)
{
OnRefreshing();
try
{
downtimes = new ItemList();
servloc.CallHiveService(service =>
{
service.GetDowntimesForResource(downtimeForResourceId).ForEach(d => downtimes.Add(d));
});
}
catch
{
throw;
}
finally
{
OnRefreshed();
}
}
}
#endregion
#region Store
public void Store(IHiveItem item, CancellationToken cancellationToken)
{
if (item.Id == Guid.Empty)
{
if (item is SlaveGroup)
{
item.Id = servloc.CallHiveService((s) => s.AddSlaveGroup((SlaveGroup)item));
}
if (item is Slave)
{
item.Id = servloc.CallHiveService((s) => s.AddSlave((Slave)item));
}
if (item is Downtime)
{
item.Id = servloc.CallHiveService((s) => s.AddDowntime((Downtime)item));
}
}
else
{
if (item is SlaveGroup)
{
servloc.CallHiveService((s) => s.UpdateSlaveGroup((SlaveGroup)item));
}
if (item is Slave)
{
servloc.CallHiveService((s) => s.UpdateSlave((Slave)item));
}
if (item is Downtime)
{
servloc.CallHiveService((s) => s.UpdateDowntime((Downtime)item));
}
}
}
#endregion
#region Delete
public void Delete(IHiveItem item)
{
if (item is SlaveGroup)
{
servloc.CallHiveService((s) => s.DeleteSlaveGroup(item.Id));
}
else if (item is Slave)
{
servloc.CallHiveService((s) => s.DeleteSlave(item.Id));
}
else if (item is Downtime)
{
servloc.CallHiveService((s) => s.DeleteDowntime(item.Id));
}
}
#endregion
public void ResetDowntime()
{
downtimeForResourceId = Guid.Empty;
if (downtimes != null)
{
downtimes.Clear();
}
}
internal bool CheckLogin()
{
try
{
this.Refresh();
return true;
}
catch (MessageSecurityException e)
{
return false;
}
catch(SecurityAccessDeniedException e)
{
return false;
}
}
}
}