#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 HeuristicLab.Clients.Hive.WebJobManager.Services;
using HeuristicLab.Clients.Hive.WebJobManager.Services.Imports;
using HeuristicLab.Clients.OKB.Query;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace HeuristicLab.Clients.Hive.WebJobManager.Hubs
{
public class QueryHub : Hub
{
private WebLoginService weblog;
private Guid userId;
private QueryWebClient queryclient;
///
/// Loads up the services for the client (Instance is only created upon a call from a client)
///
private void loader()
{
weblog = WebLoginService.Instance;
string uid = Context.QueryString["userid"];
if (uid == null || uid == "" || Guid.Parse(uid) == Guid.Empty)
{
userId = Guid.Empty;
}
else
{
userId = Guid.Parse(uid);
queryclient = weblog.getQueryClient(userId);
}
}
///
/// Initial client request for all user, group and resource information
///
public void requestInfo()
{
loader();
queryclient.Refresh();
// var values = JsonConvert.SerializeObject(queryclient.ValueNames);
var filters = JArray.FromObject(queryclient.Filters);
for (int i = 0; i < queryclient.Filters.Count(); i++)
{
if (queryclient.Filters.ToList()[i].Label != "AND" && queryclient.Filters.ToList()[i].Label != "OR")
((JObject)filters[i]).Add("CompareType", queryclient.Filters.ToList()[i].GetType().GetProperty("Comparison").PropertyType.ToString());
}
Clients.Caller.processData(filters.ToString());
}
public void runQuery(string content)
{
loader();
JArray data = JArray.Parse(content);
CombinedFilter mainFilt = (CombinedFilter)queryclient.Filters.ToList().Find(x => x.Label == "AND").Clone();
queryclient.Refresh();
mainFilt.Filters = new List();
for (var i = 0; i < data.Count(); i++)
{
mainFilt.Filters.Add(jsonParserFilt((JObject)data[i]));
}
var idcol = queryclient.GetRunIds(mainFilt).ToList();
foreach (var id in idcol)
{
Console.WriteLine(id);
}
var json = JsonConvert.SerializeObject(idcol);
Clients.Caller.processResults(json);
}
public void fetchResult(string resultId)
{
loader();
long id = long.Parse(resultId);
queryclient.Refresh();
var dat = queryclient.GetRuns(new List { id }, true).First();
var json = JsonConvert.SerializeObject(dat);
Clients.Caller.resultLoad(json);
}
private Filter jsonParserFilt(JObject current)
{
if (current["name"] == null)
{
Filter filter = queryclient.Filters.ToList().Find(x => x.Label == current["Label"].ToString()).Clone();
Type filterType = filter.GetType();
PropertyInfo compareProp = filterType.GetProperty("Comparison");
PropertyInfo valueProp = filterType.GetProperty("Value");
if ((current["CompareType"].ToString() != "HeuristicLab.Clients.OKB.Query.EqualityComparison"))
{
if (compareProp != null && valueProp != null)
{
Assembly asm = filterType.Assembly;
Type comtype = asm.GetType(current["CompareType"].ToString());
Object comob = Enum.ToObject(comtype, int.Parse(current["Comparison"].ToString()));
Object valob = Convert.ChangeType(current["Value"].ToString(), valueProp.PropertyType);
compareProp.SetValue(filter, comob);
valueProp.SetValue(filter, valob);
}
}
return filter;
}
else
{
return jsonParserComb(current);
}
}
private Filter jsonParserComb(JObject current)
{
CombinedFilter filt;
if (current["name"].ToString() == "AND")
filt = (CombinedFilter)queryclient.Filters.ToList().Find(x => x.Label == "AND").Clone();
else
filt = (CombinedFilter)queryclient.Filters.ToList().Find(x => x.Label == "OR").Clone();
for (var i = 0; i < current["columns"][0].Count(); i++)
{
filt.Filters.Add(jsonParserFilt((JObject)current["columns"][0][i]));
}
return filt;
}
}
}