/// <summary>
 /// 按函数的参数元数据定义,把传入参数转换成lisT格式。
 /// </summary>
 private void HandleInput()
 {
     _inputList = new MetaValueList();
     DataTableAndList.DataTableToList(_funcMeta.Import, _tableValueList, ref _inputList);
     DataTableAndList.DataTableToList(_funcMeta.Export, _tableValueList, ref _inputList);
     DataTableAndList.DataTableToList(_funcMeta.Changing, _tableValueList, ref _inputList);
     DataTableAndList.DataTableToList(_funcMeta.Tables, _tableValueList, ref _inputList);
 }
 public static void DataTableToList( DataTable dataTable, Dictionary<String, DataTable> tableValueList, ref MetaValueList inputList)
 {
     //  MetaValueList inputList = new MetaValueList();
     foreach (DataRow row in dataTable.Rows)
     {
         String dataType = row[FuncFieldText.DATATYPE].ToString();
         String fieldName = row[FuncFieldText.NAME].ToString();
         String defaultValue = row[FuncFieldText.DEFAULTVALUE].ToString();
       //  inputList.FieldTypeList.Add(fieldName, dataType);
         if (dataType == SAPDataType.STRUCTURE.ToString())
         {
             DataTable dt = null;
             tableValueList.TryGetValue(fieldName,out dt);
             if (dt == null)
             {
                 continue;
             }
             if (dt.Rows.Count == 1)
             {
                 DataRow structure = dt.Rows[0];
                 Dictionary<String, String> list = new Dictionary<string, string>();
                 for (int i = 0; i < dt.Columns.Count; i++)
                 {
                     list.Add(dt.Columns[i].ColumnName, structure[i].ToString());
                 }
                 inputList.StructureValueList.Add(fieldName, list);
             }
         }
         if (dataType == SAPDataType.TABLE.ToString())
         {
             DataTable dt = null;
             tableValueList.TryGetValue(fieldName, out dt);
             if (dt == null)
             {
                 continue;
             }
             if (dt.Rows.Count > 0)
             {
                 List<Dictionary<String, String>> tableList = new List<Dictionary<string, string>>();
                 foreach (DataRow structure in dt.Rows)
                 {
                     Dictionary<String, String> list = new Dictionary<string, string>();
                     for (int i = 0; i < dt.Columns.Count; i++)
                     {
                         list.Add(dt.Columns[i].ColumnName, structure[i].ToString());
                     }
                     tableList.Add(list);
                 }
                 inputList.TableValueList.Add(fieldName, tableList);
             }
         }
         else
         {
             inputList.FieldValueList.Add(fieldName, defaultValue);
         }
     }
 }
 /// <summary>
 /// 使用JSON调用RFC函数。
 /// </summary>
 /// <param name="sysName"></param>
 /// <param name="funame"></param>
 /// <param name="jsondata"></param>
 /// <returns></returns>
 public static string InvokeFunctionFromJson(string sysName, string funame, string jsondata)
 {
     MetaValueList list = null;
     if (String.IsNullOrWhiteSpace(jsondata))
     {
         list = new MetaValueList();
     }
     else
     {
         try
         {
             list = JsonConvert.DeserializeObject<MetaValueList>(jsondata);
         }
         catch (Exception ee)
         {
             return JsonConvert.SerializeObject(ee);
         }
     }
     MetaValueList outList = null;
     string output = "";
     if (list != null)
     {
         SAPFunction.InvokeFunction(sysName, funame, list, out outList);
     }
     //序列化并输出结果
     output = JsonConvert.SerializeObject(outList, new JsonSerializerSettings
     {
         Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
         {
             // errors.Add(args.ErrorContext.Error.Message);
             args.ErrorContext.Handled = true;
         }
         // Converters = { new IsoDateTimeConverter() }
     });
     return output;
 }
        /// <summary>
        /// 动态调用RFC函数
        /// </summary>
        /// <param name="sysName"></param>
        /// <param name="funame"></param>
        /// <param name="input"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        public static bool InvokeFunction(string sysName, string funame, MetaValueList input, out MetaValueList output)
        {
            try
            {
                RfcFunctionMetadata MetaData = SAPFunctionMeta.GetRfcFunctionMetadata(sysName, funame);
                IRfcFunction function = MetaData.CreateFunction();
                //初步序列化后的参数还需要进一步进行格式化,把结构体与表格都转化成SAP格式。
                if (input != null)
                {
                    //填充所有的参数
                    List<String> KeyIndex = new List<string>();
                    KeyIndex.AddRange(input.FieldValueList.Keys);
                    KeyIndex.AddRange(input.StructureValueList.Keys);
                    KeyIndex.AddRange(input.TableValueList.Keys);
                    KeyIndex.ForEach(x => x.ToUpper().Trim());
                    for (int i = 0; i < MetaData.ParameterCount; i++)
                    {
                        RfcParameterMetadata pMetadata = MetaData[i];
                        if (pMetadata.Direction == RfcDirection.EXPORT)
                        {
                            continue;
                        }
                        if (!KeyIndex.Contains(pMetadata.Name))
                        {
                            continue;
                        }

                        // Dictionary<String, Object> ParameterList = new Dictionary<string, object>();
                        if (pMetadata.DataType == RfcDataType.STRUCTURE)
                        {
                            if (input.StructureValueList.ContainsKey(pMetadata.Name))
                            {
                                Dictionary<String, String> structure;
                                input.StructureValueList.TryGetValue(pMetadata.Name, out structure);
                                IRfcStructure str = function.GetStructure(pMetadata.Name, true);
                                if (structure != null)
                                {
                                    for (int s = 0; s < str.Metadata.FieldCount; s++)
                                    {
                                        RfcFieldMetadata field = str.Metadata[s];
                                        if (structure.Keys.Contains(field.Name))
                                        {
                                            Object o = Converts.ObjectToRfcValue(structure[field.Name], field.DataType);
                                            str.SetValue(field.Name, o);
                                        }

                                    }
                                }
                            }
                        }
                        else if (pMetadata.DataType == RfcDataType.TABLE)
                        {
                            List<Dictionary<String, String>> tablelist;
                            input.TableValueList.TryGetValue(pMetadata.Name, out tablelist);
                            if (tablelist != null)
                            {
                                IRfcTable table = function.GetTable(pMetadata.Name);
                                for (int j = 0; j < tablelist.Count; j++)
                                {
                                    table.Append();
                                    for (int k = 0; k < table.Metadata.LineType.FieldCount; k++)
                                    {
                                        RfcFieldMetadata field = table.Metadata.LineType[k];
                                        if (tablelist[j].Keys.Contains(field.Name))
                                        {
                                            Object o = Converts.ObjectToRfcValue(tablelist[j][field.Name], field.DataType);
                                            table.SetValue(field.Name, o);
                                        }
                                    }
                                }
                            }

                        }
                        else
                        {
                            String value = "";
                            input.FieldValueList.TryGetValue(pMetadata.Name, out value);
                            Object o = Converts.ObjectToRfcValue(value, pMetadata.DataType);
                            function.SetValue(pMetadata.Name, o);
                        }
                    }
                }
                MetaValueList outputlist = new MetaValueList();

                try
                {
                    RfcDestination destination = SAPDestination.GetDesByName(sysName);

                    function.Invoke(destination);
                }
                catch (RfcAbapException ee)
                {
                    throw new SAPException(ee.Key + ee.Message + ee.PlainText);
                }
                catch (RfcAbapRuntimeException runtimeEx)
                {
                    throw new SAPException(runtimeEx.Key + runtimeEx.Message + runtimeEx.PlainText);
                }

                for (int i = 0; i < MetaData.ParameterCount; i++)
                {

                    RfcParameterMetadata pMetadata = MetaData[i];
                    if (pMetadata.Direction == RfcDirection.IMPORT)
                    {
                        continue;
                    }
                    // outputlist.FieldTypeList.Add(pMetadata.Name, pMetadata.DataType.ToString());
                    if (pMetadata.DataType == RfcDataType.STRUCTURE)
                    {
                        Dictionary<String, String> stru = null;
                        IRfcStructure structure = function.GetStructure(pMetadata.Name, false);
                        if (structure != null)
                        {
                            stru = new Dictionary<string, string>();
                            for (int s = 0; s < structure.Metadata.FieldCount; s++)
                            {
                                RfcFieldMetadata field = structure.Metadata[s];
                                Object result = null;

                                if (field.DataType == RfcDataType.BYTE)
                                {
                                    result = structure.GetString(field.Name);
                                }
                                else
                                {
                                    result = structure.GetValue(field.Name);
                                }

                                result = Converts.RfcToDoNetValue(result, field.DataType).ToString();
                                stru.Add(field.Name, result.ToString());
                            }
                            if (stru != null)
                            {
                                outputlist.StructureValueList.Add(pMetadata.Name, stru);
                            }
                        }
                    }
                    else if (pMetadata.DataType == RfcDataType.TABLE)
                    {
                        List<Dictionary<String, String>> outTableList = null;
                        IRfcTable outTable = function.GetTable(pMetadata.Name, false);
                        if (outTable != null)
                        {
                            outTableList = new List<Dictionary<string, string>>();
                            for (int s = 0; s < outTable.RowCount; s++)
                            {
                                IRfcStructure rfcTableLine = outTable[s];
                                Dictionary<String, String> row = new Dictionary<string, string>();
                                for (int z = 0; z < rfcTableLine.Metadata.FieldCount; z++)
                                {
                                    RfcFieldMetadata field = rfcTableLine[z].Metadata;
                                    Object result = null;

                                    if (field.DataType == RfcDataType.BYTE)
                                    {
                                        result = rfcTableLine.GetString(field.Name);
                                    }
                                    else
                                    {
                                        result = rfcTableLine.GetValue(field.Name);
                                    }
                                    result = Converts.RfcToDoNetValue(result, field.DataType).ToString();
                                    row.Add(field.Name, result.ToString());
                                }
                                outTableList.Add(row);
                            }
                        }
                        outputlist.TableValueList.Add(pMetadata.Name, outTableList);
                    }
                    else
                    {
                        Object result = function.GetValue(pMetadata.Name);
                        result = Converts.RfcToDoNetValue(result, pMetadata.DataType);
                        outputlist.FieldValueList.Add(pMetadata.Name, result.ToString());
                    }
                }
                output = outputlist;
                return true;
            }
            catch (Exception e)
            {
                output = null;
                throw new SAPException(e.Message);
            }
        }
        /// <summary>
        /// 把LIST转换成DATATABLE.
        /// </summary>
        /// <param name="outlist">调用RFC函数后,outlist包含了所有的参数与它对应的值。</param>
        /// <param name="dataTable">dataTable里已经包含了一个字段的类型描述,是结构还是表还是普通值</param>
        /// <param name="tableValueList">如果字段是复杂结构或表,把值存储在这里</param>
        public static void ListToDataTable(MetaValueList outlist, DataTable dataTable, ref Dictionary<String, DataTable> tableValueList)
        {
            foreach (DataRow row in dataTable.Rows)
            {
                String dataType = row[FuncFieldText.DATATYPE].ToString();
                String dataTypeName = row[FuncFieldText.DATATYPENAME].ToString();
                String fieldName = row[FuncFieldText.NAME].ToString();
                String defaultValue = row[FuncFieldText.DEFAULTVALUE].ToString();

                //如果参数的类型是结构,按结构的类型返回值。
                if (dataType == SAPDataType.STRUCTURE.ToString())
                {
                    Dictionary<String, String> structure = null;
                    outlist.StructureValueList.TryGetValue(fieldName, out structure);
                    if (structure == null)
                    {
                        continue;
                    }
                    DataTable dtNew = new DataTable();
                    foreach (var item in structure)
                    {
                        DataColumn dc = new DataColumn(item.Key, Type.GetType("System.String"));
                        dtNew.Columns.Add(dc);
                    }
                    if (structure != null)
                    {
                        dtNew.Rows.Add(dtNew.NewRow());
                        DataRow row1 = dtNew.Rows[0];
                        foreach (var item in structure)
                        {
                            row1[item.Key] = item.Value;
                        }
                    }
                    if (tableValueList.Keys.Contains(fieldName))
                    {
                        tableValueList[fieldName]=dtNew;
                    }
                    else
                    {
                        tableValueList.Add(fieldName, dtNew);
                    }

                }
                else if (dataType == SAPDataType.TABLE.ToString())
                {
                    List<Dictionary<String, String>> tablelist = null;
                    outlist.TableValueList.TryGetValue(fieldName, out tablelist);
                    if (tablelist == null )
                    {
                        continue;
                    }
                    if (tablelist.Count == 0)
                    {
                        if (tableValueList.Keys.Contains(fieldName))
                        {
                            var lc_dt = tableValueList[fieldName];
                            lc_dt.Clear();
                            tableValueList[fieldName] = lc_dt;
                        }
                    }
                    else
                    {
                        DataTable dtNew = new DataTable();
                        Dictionary<String, String> line = tablelist[0];
                        foreach (var item in line)
                        {
                            DataColumn dc = new DataColumn(item.Key, Type.GetType("System.String"));
                            dtNew.Columns.Add(dc);
                        }
                        foreach (Dictionary<String, String> item in tablelist)
                        {
                            DataRow newRow = dtNew.NewRow();
                            foreach (var structure in item)
                            {
                                newRow[structure.Key] = structure.Value;
                            }
                            dtNew.Rows.Add(newRow);
                        }
                        if (tableValueList.Keys.Contains(fieldName))
                        {
                            tableValueList[fieldName] = dtNew;
                        }
                        else
                        {
                            tableValueList.Add(fieldName, dtNew);
                        }
                    }

                }
                else //if(!String.IsNullOrEmpty(dataTypeName))
                {
                    String value = null ;
                    outlist.FieldValueList.TryGetValue(fieldName, out value);
                    if (value !=null)
                    {
                        row[FuncFieldText.DEFAULTVALUE] = value;
                    }

                }
            }
        }
        /// <summary>
        /// 把LIST转换成DATATABLE.
        /// </summary>
        /// <param name="outlist">调用RFC函数后,outlist包含了所有的参数与它对应的值。</param>
        /// <param name="dataTable">dataTable里已经包含了一个字段的类型描述,是结构还是表还是普通值</param>
        /// <param name="tableValueList">如果字段是复杂结构或表,把值存储在这里</param>
        public static void ListToDataTable(MetaValueList outlist, DataTable dataTable, ref Dictionary <String, DataTable> tableValueList)
        {
            foreach (DataRow row in dataTable.Rows)
            {
                String dataType     = row[FuncFieldText.DATATYPE].ToString();
                String dataTypeName = row[FuncFieldText.DATATYPENAME].ToString();
                String fieldName    = row[FuncFieldText.NAME].ToString();
                String defaultValue = row[FuncFieldText.DEFAULTVALUE].ToString();

                //如果参数的类型是结构,按结构的类型返回值。
                if (dataType == SAPDataType.STRUCTURE.ToString())
                {
                    Dictionary <String, String> structure = null;
                    outlist.StructureValueList.TryGetValue(fieldName, out structure);
                    if (structure == null)
                    {
                        continue;
                    }
                    DataTable dtNew = new DataTable();
                    foreach (var item in structure)
                    {
                        DataColumn dc = new DataColumn(item.Key, Type.GetType("System.String"));
                        dtNew.Columns.Add(dc);
                    }
                    if (structure != null)
                    {
                        dtNew.Rows.Add(dtNew.NewRow());
                        DataRow row1 = dtNew.Rows[0];
                        foreach (var item in structure)
                        {
                            row1[item.Key] = item.Value;
                        }
                    }
                    if (tableValueList.Keys.Contains(fieldName))
                    {
                        tableValueList[fieldName] = dtNew;
                    }
                    else
                    {
                        tableValueList.Add(fieldName, dtNew);
                    }
                }
                else if (dataType == SAPDataType.TABLE.ToString())
                {
                    List <Dictionary <String, String> > tablelist = null;
                    outlist.TableValueList.TryGetValue(fieldName, out tablelist);
                    if (tablelist == null)
                    {
                        continue;
                    }
                    if (tablelist.Count == 0)
                    {
                        if (tableValueList.Keys.Contains(fieldName))
                        {
                            var lc_dt = tableValueList[fieldName];
                            lc_dt.Clear();
                            tableValueList[fieldName] = lc_dt;
                        }
                    }
                    else
                    {
                        DataTable dtNew = new DataTable();
                        Dictionary <String, String> line = tablelist[0];
                        foreach (var item in line)
                        {
                            DataColumn dc = new DataColumn(item.Key, Type.GetType("System.String"));
                            dtNew.Columns.Add(dc);
                        }
                        foreach (Dictionary <String, String> item in tablelist)
                        {
                            DataRow newRow = dtNew.NewRow();
                            foreach (var structure in item)
                            {
                                newRow[structure.Key] = structure.Value;
                            }
                            dtNew.Rows.Add(newRow);
                        }
                        if (tableValueList.Keys.Contains(fieldName))
                        {
                            tableValueList[fieldName] = dtNew;
                        }
                        else
                        {
                            tableValueList.Add(fieldName, dtNew);
                        }
                    }
                }
                else //if(!String.IsNullOrEmpty(dataTypeName))
                {
                    String value = null;
                    outlist.FieldValueList.TryGetValue(fieldName, out value);
                    if (value != null)
                    {
                        row[FuncFieldText.DEFAULTVALUE] = value;
                    }
                }
            }
        }
 public static void DataTableToList(DataTable dataTable, Dictionary <String, DataTable> tableValueList, ref MetaValueList inputList)
 {
     //  MetaValueList inputList = new MetaValueList();
     foreach (DataRow row in dataTable.Rows)
     {
         String dataType     = row[FuncFieldText.DATATYPE].ToString();
         String fieldName    = row[FuncFieldText.NAME].ToString();
         String defaultValue = row[FuncFieldText.DEFAULTVALUE].ToString();
         //  inputList.FieldTypeList.Add(fieldName, dataType);
         if (dataType == SAPDataType.STRUCTURE.ToString())
         {
             DataTable dt = null;
             tableValueList.TryGetValue(fieldName, out dt);
             if (dt == null)
             {
                 continue;
             }
             if (dt.Rows.Count == 1)
             {
                 DataRow structure = dt.Rows[0];
                 Dictionary <String, String> list = new Dictionary <string, string>();
                 for (int i = 0; i < dt.Columns.Count; i++)
                 {
                     list.Add(dt.Columns[i].ColumnName, structure[i].ToString());
                 }
                 inputList.StructureValueList.Add(fieldName, list);
             }
         }
         if (dataType == SAPDataType.TABLE.ToString())
         {
             DataTable dt = null;
             tableValueList.TryGetValue(fieldName, out dt);
             if (dt == null)
             {
                 continue;
             }
             if (dt.Rows.Count > 0)
             {
                 List <Dictionary <String, String> > tableList = new List <Dictionary <string, string> >();
                 foreach (DataRow structure in dt.Rows)
                 {
                     Dictionary <String, String> list = new Dictionary <string, string>();
                     for (int i = 0; i < dt.Columns.Count; i++)
                     {
                         list.Add(dt.Columns[i].ColumnName, structure[i].ToString());
                     }
                     tableList.Add(list);
                 }
                 inputList.TableValueList.Add(fieldName, tableList);
             }
         }
         else
         {
             inputList.FieldValueList.Add(fieldName, defaultValue);
         }
     }
 }
 /// <summary>
 /// 按函数的参数元数据定义,把传入参数转换成lisT格式。
 /// </summary>
 private void HandleInput()
 {
     _inputList = new MetaValueList();
     DataTableAndList.DataTableToList(_funcMeta.Import, _tableValueList, ref _inputList);
     DataTableAndList.DataTableToList(_funcMeta.Export, _tableValueList, ref _inputList);
     DataTableAndList.DataTableToList(_funcMeta.Changing, _tableValueList, ref _inputList);
     DataTableAndList.DataTableToList(_funcMeta.Tables, _tableValueList, ref _inputList);
 }