Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <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);
            }
        }