diff --git a/Services/ApiServices.cs b/Services/ApiServices.cs index b50236d..3ea6fcb 100644 --- a/Services/ApiServices.cs +++ b/Services/ApiServices.cs @@ -25,6 +25,7 @@ using Newtonsoft.Json; using ExpressBase.Objects.Helpers; using ExpressBase.Common.Helpers; +using MongoDB.Driver.Core.Connections; namespace ExpressBase.ServiceStack.Services { @@ -35,13 +36,38 @@ public class ApiServices : EbBaseService private WebFormServices WebFormService { set; get; } private EbApi Api { set; get; } - + public const string EB_LOC_ID = "eb_loc_id"; public ApiServices(IEbConnectionFactory _dbf, IEbStaticFileClient _sfc) : base(_dbf, _sfc) { this.StudioServices = base.ResolveService(); this.WebFormService = base.ResolveService(); } + private Dictionary _keyValuePairs = null; + + public Dictionary GetKeyvalueDict + { + get + { + if (_keyValuePairs == null) + { + _keyValuePairs = new Dictionary(); + foreach (string key in this.Api.FirstReaderKeyColumns) + { + if (!_keyValuePairs.ContainsKey(key)) + _keyValuePairs.Add(key, new TV { }); + } + foreach (string key in this.Api.ParameterKeyColumns) + { + if (!_keyValuePairs.ContainsKey(key)) + _keyValuePairs.Add(key, new TV { }); + } + } + return _keyValuePairs; + } + } + + public Dictionary ProcessGlobalDictionary(Dictionary data) { Dictionary globalParams = new Dictionary(); @@ -158,7 +184,7 @@ private void InitializeExecution() } } - private object GetResult(ApiResources resource) + private object GetResult(ApiResources resource, int index = 0, int parentindex = 0) { ResultWrapper res = new ResultWrapper(); @@ -199,6 +225,12 @@ private object GetResult(ApiResources resource) case EbCSVPusher pusher: res.Result = (pusher as EbCSVPusher).ExecuteCSVPusher(this.Api, this, this.FileClient, false); break; + case EbLoop loop: + res.Result = DoLoop(resource as EbLoop, index, parentindex); + break; + case EbTransaction transaction: + // res.Result = ExecuteTransaction(resource as EbTransaction1, index); + break; //case EbEncrypt encrypt: // res.Result = (encrypt as EbEncrypt).ExecuteEncrypt(this.Api); // break; @@ -483,6 +515,99 @@ private object ExecuteConnectApi(EbConnectApi apiResource) return resp; } + public bool DoLoop(EbLoop loop, int step, int parentindex) + { + EbDataTable _table; + try + { + if (parentindex == 0 && step == 1) + _table = (this.Api.Resources[step - 1].Result as EbDataSet).Tables[0]; + else + _table = (this.Api.Resources[parentindex - 1].Result as EbDataSet).Tables[0]; + + int _rowcount = _table.Rows.Count; + for (int i = 0; i < _rowcount; i++) + { + try + { + EbDataColumn cl = _table.Columns[0]; + Param _outparam = new Param + { + Name = cl.ColumnName, + Type = cl.Type.ToString(), + Value = _table.Rows[i][cl.ColumnIndex].ToString() + }; + this.Api.Resources[step].Result = _outparam; + if (this.Api.GlobalParams.ContainsKey(_outparam.Name)) + this.Api.GlobalParams[_outparam.Name] = new TV { Type = _outparam.Type, Value = _outparam.Value }; + else + this.Api.GlobalParams.Add(_outparam.Name, new TV { Type = _outparam.Type, Value = _outparam.Value }); + + + if (!this.Api.GlobalParams.ContainsKey(EB_LOC_ID)) + { + if (_table.Columns[EB_LOC_ID] != null) + { + this.Api.GlobalParams.Add(EB_LOC_ID, new TV { Type = EbDbTypes.Int32.ToString(), Value = _table.Rows[i][EB_LOC_ID].ToString() }); + } + } + + ExecuteLoop(loop, 0, step, parentindex, _table.Rows[i], null); + // message = "Loop Execution Success. counter " + i + " of " + _rowcount; + } + catch (Exception e) + { + // message = "Loop Failed to execute. counter " + i + " of " + _rowcount; + Console.WriteLine(e.Message + e.StackTrace); + } + } + // MasterResult.Add(new SqlJobResult { Message = "Loop execution Success with " + _rowcount + " iterations.", Type = ResourceType.Loop }); + } + catch (Exception e) + { + // MasterResult.Add(new SqlJobResult { Message = "Loop execution Failed" + e.Message, Type = ResourceType.Loop }); + Console.WriteLine(e.Message + e.StackTrace); + } + return true; + } + + public void ExecuteLoop(EbLoop loop, int retryof, int step, int parentindex, EbDataRow dataRow, Dictionary keyvals) + { + try + { + int counter; + string _keyvalues = (dataRow is null) ? JsonConvert.SerializeObject(keyvals) : FillKeys(dataRow); + try + { + for (counter = 0; counter < loop.InnerResources.Count; counter++) + { + if (loop.InnerResources[counter] is EbProcessor) + { + if (loop.InnerResources[counter - 1] is EbSqlReader) + if (((loop.InnerResources[counter - 1] as EbSqlReader).Result as EbDataSet).Tables[0].Rows.Count > 0) + loop.InnerResources[counter].Result = this.GetResult(loop.InnerResources[counter], counter, step); + else + { + Console.WriteLine("Datareader returned 0 rows : " + (loop.InnerResources[counter - 1] as EbSqlReader).RefId + "\n" + + JsonConvert.SerializeObject(dataRow)); + return; + } + } + loop.InnerResources[counter].Result = this.GetResult(loop.InnerResources[counter], counter, step); + } + } + catch (Exception e) + { + throw e; + } + } + catch (Exception e) + { + Console.WriteLine("Error at LoopExecution"); + throw e; + } + } + //private object ExecuteThirdPartyApi(EbThirdPartyApi thirdPartyResource) //{ // Uri uri = new Uri(ReplacePlaceholders(thirdPartyResource.Url)); @@ -581,6 +706,19 @@ private object ExecuteFormResource(EbFormResource formResource) } } + public string FillKeys(EbDataRow dataRow) + { + foreach (var item in this.Api.GlobalParams) + if (GetKeyvalueDict.ContainsKey(item.Key)) + this.GetKeyvalueDict[item.Key] = null;// this.Api.GlobalParams[item.Key]; + for (int i = 0; i < dataRow.Count; i++) + { + if (GetKeyvalueDict.ContainsKey(dataRow.Table.Columns[i].ColumnName)) + this.GetKeyvalueDict[dataRow.Table.Columns[i].ColumnName] = new TV { Value = dataRow[i].ToString(), Type = ((int)dataRow.Table.Columns[i].Type).ToString() }; + } + return JsonConvert.SerializeObject(GetKeyvalueDict); ; + } + private List GetEmailParams(EbEmailTemplate enode) { List p = new List(); diff --git a/Services/SqlJobServices.cs b/Services/SqlJobServices.cs index 34b7c29..3f94421 100644 --- a/Services/SqlJobServices.cs +++ b/Services/SqlJobServices.cs @@ -151,7 +151,7 @@ INSERT INTO int step = 0; while (step < this.SqlJob.Resources.Count) { - this.SqlJob.Resources[step].Result = GetResult(this.SqlJob.Resources[step], step, 0, 0); + this.SqlJob.Resources[step].Result = GetResult(this.SqlJob.Resources[step], step, 0); step++; } @@ -456,7 +456,7 @@ public LogLine GetLogLine(int JoblogId) return logline; } - public object GetResult(SqlJobResource resource, int index, int parentindex, int grandparent) + public object GetResult(SqlJobResource resource, int index, int parent) { ResultWrapper res = new ResultWrapper(); try @@ -465,19 +465,19 @@ public object GetResult(SqlJobResource resource, int index, int parentindex, int res.Result = this.ExcDataReader(resource as EbSqlJobReader, index); else if (resource is EbSqlJobWriter) res.Result = this.ExcDataWriter(resource as EbSqlJobWriter, index); - else if (resource is EbLoop) - res.Result = DoLoop(resource as EbLoop, index, parentindex); - else if (resource is EbTransaction) - res.Result = ExecuteTransaction(resource as EbTransaction, index); + else if (resource is EbLoop1) + res.Result = DoLoop(resource as EbLoop1, index, parent); + else if (resource is EbTransaction1) + res.Result = ExecuteTransaction(resource as EbTransaction1, index); else if (resource is EbSqlFormDataPusher) res.Result = ExecuteDataPush(resource as EbSqlFormDataPusher, index); else if (resource is EbSqlProcessor) { SqlJobResource _prev = null; - if (this.SqlJob.Resources[index] is EbTransaction) - _prev = (index != 0) ? (((this.SqlJob.Resources[index] as EbTransaction).InnerResources[parentindex] as EbLoop).InnerResources[index - 1]) : null; - else if (this.SqlJob.Resources[index] is EbLoop) - _prev = (index != 0) ? (((this.SqlJob.Resources[index] as EbLoop).InnerResources[parentindex] as EbTransaction).InnerResources[index - 1]) : null; + if (this.SqlJob.Resources[index] is EbTransaction1) + _prev = (index != 0) ? (((this.SqlJob.Resources[index] as EbTransaction1).InnerResources[parent] as EbLoop1).InnerResources[index - 1]) : null; + else if (this.SqlJob.Resources[index] is EbLoop1) + _prev = (index != 0) ? (((this.SqlJob.Resources[index] as EbLoop1).InnerResources[parent] as EbTransaction1).InnerResources[index - 1]) : null; res.Result = EvaluateProcessor(resource as EbSqlProcessor, _prev, this.GlobalParams); } @@ -576,7 +576,7 @@ private object ExcDataWriter(EbSqlJobWriter writer, int step) return false; } - public bool DoLoop(EbLoop loop, int step, int parentindex) + public bool DoLoop(EbLoop1 loop, int step, int parentindex) { // string message; EbDataTable _table = null; @@ -636,7 +636,7 @@ public bool DoLoop(EbLoop loop, int step, int parentindex) return true; } - public void ExecuteLoop(EbLoop loop, int retryof, int step, int parentindex, EbDataRow dataRow, Dictionary keyvals) + public void ExecuteLoop(EbLoop1 loop, int retryof, int step, int parentindex, EbDataRow dataRow, Dictionary keyvals) { try { @@ -670,7 +670,7 @@ public void ExecuteLoop(EbLoop loop, int retryof, int step, int parentindex, EbD { if (loop.InnerResources[counter - 1] is EbSqlJobReader) if (((loop.InnerResources[counter - 1] as EbSqlJobReader).Result as EbDataSet).Tables[0].Rows.Count > 0) - loop.InnerResources[counter].Result = this.GetResult(loop.InnerResources[counter], counter, step, parentindex); + loop.InnerResources[counter].Result = this.GetResult(loop.InnerResources[counter], counter, step); else { Console.WriteLine("Datareader returned 0 rows : " + (loop.InnerResources[counter - 1] as EbSqlJobReader).RefId + "\n" + @@ -678,7 +678,7 @@ public void ExecuteLoop(EbLoop loop, int retryof, int step, int parentindex, EbD return; } } - loop.InnerResources[counter].Result = this.GetResult(loop.InnerResources[counter], counter, step, parentindex); + loop.InnerResources[counter].Result = this.GetResult(loop.InnerResources[counter], counter, step); } // LinesResult.Add(new SqlJobResult { Mess, Type = ResourceType.Loop }); @@ -790,7 +790,7 @@ public string FillKeys(EbDataRow dataRow) return JsonConvert.SerializeObject(GetKeyvalueDict); ; } - public bool ExecuteTransaction(EbTransaction txn, int step) + public bool ExecuteTransaction(EbTransaction1 txn, int step) { string message; using (TransactionConnection = this.EbConnectionFactory.DataDB.GetNewConnection()) @@ -806,7 +806,7 @@ public bool ExecuteTransaction(EbTransaction txn, int step) { if (txn.InnerResources[counter - 1] is EbSqlJobReader) if (((txn.InnerResources[counter - 1] as EbSqlJobReader).Result as EbDataSet).Tables[0].Rows.Count > 0) - txn.InnerResources[counter].Result = this.GetResult(txn.InnerResources[counter], counter, step, 0); + txn.InnerResources[counter].Result = this.GetResult(txn.InnerResources[counter], counter, step); else { message = "Datareader returned 0 rows"; @@ -815,7 +815,7 @@ public bool ExecuteTransaction(EbTransaction txn, int step) } } else - txn.InnerResources[counter].Result = this.GetResult(txn.InnerResources[counter], counter, step, 0); + txn.InnerResources[counter].Result = this.GetResult(txn.InnerResources[counter], counter, step); trans.Commit(); message = "Transaction success"; }