Files
BeWoPlaner/Service/ServiceImplementations/QueryServiceImp.cs
Christian a896a7bfec cb
2019-09-17 17:44:57 +02:00

222 lines
8.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.ServiceModel;
using BeWo.Data;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.Plugins;
using BeWo.Service.ServiceContracts;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using Newtonsoft.Json;
using Utils = BeWo.Service.Core.Utils;
namespace BeWo.Service.ServiceImplementations
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
public class QueryServiceImp : IQueryService
{
public string ExecuteQuery(long pQueryOid, List<QueryParamDC> parameter)
{
try
{
DataTable table = null;
Query lQuery = DAOFactory.GenericDAO.LoadByID<Query>(pQueryOid);
string sql = lQuery.Sql;
if (!String.IsNullOrEmpty(sql))
{
if (parameter != null && parameter.Count > 0)
{
sql = this.ReplaceQueryParameter(sql, parameter);
}
table = DAOFactory.AdoDAO.ExecuteQuery(sql).Tables[0];
}
if (table == null)
{
table = new DataTable("emptytable");
}
var temp = BS.Shared.Core.Utils.XMLSerializeToString(table);
return temp;
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<QueryDC> GetAllQueriesOfType(QueryType pType)
{
try
{
var qs = PluginLoader.FindClass<QueryService>();
return qs.GetAllQueriesOfType(pType);
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public string PrepareQueryReport(string dataTableJsonString, string id)
{
try
{
var lResult = id + ".xml";
var lDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, MultitenancyOperationContextExt.Current.Tenant + @"\temp");
var lPath = Path.Combine(lDir, lResult);
var table = BS.Shared.Core.Utils.XMLDeserializeFromString<DataTable>(dataTableJsonString);
BS.Shared.Core.Utils.XMLSerialize(lPath, table);
return lResult;
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
// Hack: We don´t use the ReportTypes in Webservice Calls,
// but the Client needs to know them for Calling ASP Sites
public void ReportTypes(ReportTypes pType)
{
}
private string GetParamterKeyName(QueryParamDC item)
{
return GetParamterKeyName(item.Name);
}
private string GetParamterKeyName(string itemName)
{
string key = itemName;
key = key.Replace(" ", "_");
return key;
}
private string ReplaceQueryParameter(string sql, List<QueryParamDC> parameter)
{
string newsql = sql;
foreach (var item in parameter)
{
//if (item.Value != null)
//{
switch (item.ParamType)
{
case QueryParameterType.SupportConcept:
case QueryParameterType.Employee:
case QueryParameterType.Customer:
case QueryParameterType.Team:
case QueryParameterType.Organisation:
long oid2 = 0;
if (item.Value != null)
{
Int64.TryParse(item.Value.ToString(), out oid2);
}
newsql = newsql.Replace(":" + GetParamterKeyName(item), String.Format("{0}", oid2));
break;
case QueryParameterType.Month:
if (item.Value is DateTime)
{
DateTime dt = (DateTime)item.Value;
newsql = newsql.Replace(":" + GetParamterKeyName(item) + "_MM", String.Format("{0:MM}", dt));
newsql = newsql.Replace(":" + GetParamterKeyName(item) + "_Start", String.Format("{0:yyyy-MM}-01", dt));
dt = dt.AddMonths(1);
newsql = newsql.Replace(":" + GetParamterKeyName(item) + "_End", String.Format("{0:yyyy-MM}-01", dt));
}
break;
case QueryParameterType.Date:
if (item.Value != null && item.Value is DateTime)
{
DateTime dt = (DateTime)item.Value;
newsql = newsql.Replace(":" + GetParamterKeyName(item), String.Format("{0:yyyy-MM-dd}", dt));
}
break;
case QueryParameterType.DateRange:
if (item.Value is DateTimeSpan)
{
DateTimeSpan dts = (DateTimeSpan)item.Value;
string[] itemNames = item.Name.Split(new[] { '_' });
newsql = newsql.Replace(":" + GetParamterKeyName(itemNames[0]), String.Format("{0:yyyy-MM-dd}", dts.StartDate));
if (itemNames.Length > 1)
{
DateTime endDate = dts.EndDate;
if (endDate < DateTime.MaxValue.Date)
endDate = endDate.AddDays(1);
newsql = newsql.Replace(":" + GetParamterKeyName(itemNames[1]), String.Format("{0:yyyy-MM-dd}", endDate));
}
}
break;
case QueryParameterType.Year:
if (item.Value is DateTime)
{
DateTime dt = (DateTime)item.Value;
newsql = newsql.Replace(":year", String.Format("{0:yyyy-MM-dd}", dt));
newsql = newsql.Replace(":" + GetParamterKeyName(item) + "_Start", String.Format("{0:yyyy-MM}-01", dt));
newsql = newsql.Replace(":" + GetParamterKeyName(item) + "_End", String.Format("{0:yyyy-MM}-01", dt.AddYears(1)));
newsql = newsql.Replace(":yyyy", String.Format("{0:yyyy}", dt));
}
break;
case QueryParameterType.CurrentUser:
case QueryParameterType.CurrentEmployee:
if (item.Value != null)
{
long oid;
if (Int64.TryParse(item.Value.ToString(), out oid))
{
newsql = newsql.Replace(":" + GetParamterKeyName(item), String.Format("{0}", oid));
}
}
break;
case QueryParameterType.ValueListEntryType:
if (item.Value != null)
{
long oid;
if (Int64.TryParse(item.Value.ToString(), out oid))
{
String[] names = item.Name.Split('_');
newsql = newsql.Replace(":" + names[0], String.Format("{0}", oid));
}
}
break;
default:
if (item.Value != null)
{
newsql = newsql.Replace(":" + GetParamterKeyName(item.Name), item.Value.ToString());
}
break;
}
//}
}
return newsql;
}
}
}