Celin.AIS : Data Browse with Dynamic Elements

CSharp · Console

/Program.cs

using Microsoft.Extensions.Logging;
using System;
using System.Dynamic;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Celin
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                    .SetMinimumLevel(LogLevel.Information)
                    .AddConsole();
            });
            var logger = loggerFactory.CreateLogger<Program>();
            logger.LogInformation("Start program...");

            var e1 = new AIS.Server("https://demo.steltix.com/jderest/v2/", loggerFactory.CreateLogger<AIS.Server>());
            e1.AuthRequest.username = "demo";
            e1.AuthRequest.password = "demo";
            try
            {
                await e1.AuthenticateAsync();

                // Fetch some data using the concrete request only
                var rs = await e1.RequestAsync<JsonElement>(new F4311.Request
                {
                    // List PO Lines at status 400 using a query
                    query = new AIS.Query
                    {
                        // All conditions must match
                        matchType = AIS.Query.MATCH_ALL,
                        condition = new []
                        {
                            // Create condition
                            new AIS.Condition
                            {
                                // Specify the field name with the table prefix
                                controlId = "F4311.NXTR",
                                // Specify condition
                                @operator = AIS.Condition.EQUAL,
                                value = new []
                                {
                                    // Set the value for the condition
                                    new AIS.Value
                                    {
                                        content = "400",
                                        specialValueId = AIS.Value.LITERAL
                                    }
                                }
                            }
                        }
                    }
                });
				// Deserialise the JsonElement from the Request into the new F4311.Response
                var qry = JsonSerializer.Deserialize<F4311.Response>(rs.GetRawText(), new JsonSerializerOptions
                {
                    Converters =
                    {
						// Pass in the custom converted for DynamicJsonElement
                        new DynamicJsonConverter()
                    }
                });
                // Simplify the return parameter
                var d = qry.fs_DATABROWSE_F4311.data.gridData;
                // Dump the query result
                Console.WriteLine("Returned {0} records, there are {1}more.", d.summary.records, d.summary.moreRecords ? string.Empty : "no ");
				// Note that the 'r' variable is now dynamic
                foreach (dynamic r in d.rowset)
                {
					// Now we can access the members without concrete class!
                    Console.WriteLine("{0,8} {1} {2,8} {3,-20} {4,-30} {5,-30} {6,12} {7,14:N}", r.F4311_DOCO, r.F4311_DCTO, r.F4311_AN8, r.F4311_LITM, r.F4311_DSC1, r.F4311_DSC2, r.F4311_UORG, r.F4311_AEXP);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            await e1.LogoutAsync();
            logger.LogInformation("Program end!");
        }
    }
	// JsonConverter for the DynamicJsonElement
	public class DynamicJsonConverter : JsonConverter<DynamicJsonElement>
	{
		public override DynamicJsonElement Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
		{
			return new DynamicJsonElement(JsonSerializer.Deserialize<JsonElement>(ref reader, options));
		}

		public override void Write(Utf8JsonWriter writer, DynamicJsonElement value, JsonSerializerOptions options)
		{
			throw new NotImplementedException();
		}
	}
	// The DynamicJsonElement
	public class DynamicJsonElement : DynamicObject
	{
		public JsonElement Element { get; }
		public override bool TryGetMember(GetMemberBinder binder, out object result)
		{
			try
			{
				result = Element.GetProperty(binder.Name);
			}
			catch
			{
				result = null;
				return false;
			}
			return true;
		}
		public JsonElement.ObjectEnumerator GetEnumerator()
		{
			return Element.EnumerateObject();
		}
		public DynamicJsonElement(JsonElement element)
		{
			Element = element;
		}
	}
	// F4311 Definition
	namespace F4311
	{
		public class Response : AIS.FormResponse
		{
			// Instead of defining row members, we use the DynamicJsonElment
			public AIS.Form<AIS.FormData<DynamicJsonElement>> fs_DATABROWSE_F4311 { get; set; }
		}
		public class Request : AIS.DatabrowserRequest
		{
			public Request()
			{
				outputType = GRID_DATA;
				dataServiceType = BROWSE;
				targetName = "F4311";
				targetType = table;
				returnControlIDs = @"KCOO|DOCO|DCTO|SFXO|LNID|MCU|CO|OKCO|OORN|OCTO|OGNO|RKCO|RORN|RCTO|RLLN|AN8|SHAN|
					DRQJ|TRDJ|PDDJ|OPDJ|ADDJ|CNDJ|PEFJ|PPDJ|PSDJ|DSVJ|DGL|PN|VR01|VR02|ITM|LITM|LOCN|LOTN|DSC1|DSC2|
					LNTY|NXTR|LTTR|UOM|UORG|UCHG|UOPN|UREC|CREC|URLV|OTQY|PRRC|AEXP|ACHG|AOPN|AREC|ARLV|AMC3|ECST|
					CSTO|CSMP|PTC|TX|EXR1|TXA1|UOM1,PQOR|UOM2|SQOR|CTRY|FY|LT|ANI|AID|OMCU|OBJ|SUB|SBLT|SBL";
			}
		}
	}
}
;