Celin.AIS - Refactored Start with Sessions

CSharp · Console

/Program.cs

using System;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

namespace Celin
{
	public class Program
	{
		public static async Task Main()
		{
			// Set up the logging Infrastructure
            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                    .SetMinimumLevel(LogLevel.Error)
                    .AddConsole();
            });
            var logger = loggerFactory.CreateLogger<Program>();
            logger.LogInformation("Start program...");

			// Initialise the E1 AIS Client
            var e1 = new AIS.Server("https://demo.steltix.com/jderest/v2/", loggerFactory.CreateLogger<AIS.Server>());
			// Set Credentials
			e1.AuthRequest.username = "demo";
			e1.AuthRequest.password = "demo";
			try
			{
				// Authenticate
				await e1.AuthenticateAsync();
				Console.WriteLine("{0}, Welcome to {1}", e1.AuthResponse.userInfo.alphaName, e1.AuthResponse.environment);

				// ** Refactored ** Submit 'Work With Addresses' as StackFormRequest
				var w01012b = await e1.RequestAsync<W01012B.Response>(
					Make.Open(new W01012B.Request
						{
							// Look up AN8 2006 using a query
							query = Make.Query(
										Make.List(
											Make.Equal("1.19", "2006")))
						}));
				
				// Get the Form Parameter, semantics simplicity
				var form = w01012b.fs_P01012_W01012B;
				// Output the Form
				Console.WriteLine("{0} ({1})", form.title, w01012b.currentApp);
				Console.WriteLine("Returned {0} row(s)", form.data.gridData.summary.records);
				// Output the Grid Row
				foreach (var row in form.data.gridData.rowset)
				{
					Console.WriteLine("{0,-10} {1}", row.z_AN8_19, row.z_ALPH_20);

					// ** Refactored ** Now select the Row
					var w01012a = await e1.RequestAsync<W01012A.Response>(
						Make.Execute(w01012b, new AIS.ActionRequest
						{
							formActions = Make.List(
								Make.Select(0),
								Make.Do("14"))
						}));

					// Write out some details
					var data = w01012a.fs_P01012_W01012A.data;
					Console.WriteLine("{0} ({1})", w01012a.fs_P01012_W01012A.title, w01012a.currentApp);
					Console.WriteLine("{0} {1} {2}", data.z_AN8_21.value, data.z_AT1_36.value, data.z_ALPH_28.value);
					Console.WriteLine("{0} {1}", data.z_ADD1_40.value, data.z_ADD2_42.value);

					// ** Refactored ** Close the Form (press the Cancel button)
					w01012b = await e1.RequestAsync<W01012B.Response>(
						Make.Execute(w01012a, new AIS.ActionRequest
						{
							formActions = Make.List(
								Make.Do("12"))
						}));

					// Check out the returned Form
					Console.WriteLine("Back to: {0} ({1})", w01012b.fs_P01012_W01012B.title, w01012b.currentApp);

					// ** Refactored ** Finally close the Session
					var close = await e1.RequestAsync<AIS.FormResponse>(
						Make.Close(w01012b));

					// The return should have blank session parameters
					Console.WriteLine("StackId: {0}, StateId: {1}, Rid: {2}", close.stackId, close.stateId, close.rid);
					
					// Break out of the foreach statement, just in case
					break;
				}
				// Sign-out
				await e1.LogoutAsync();
				Console.WriteLine("Goodbye!");
			}
			catch (Exception ex)
			{
				logger.LogCritical(ex, "Ooobs, some bad shite happened!!");
			}
		}
	}
	// AIS Element Factories
    public class Make
	{
		// Generic list converter
		public static IEnumerable<T> List<T>(params T[] items)
			=> items;
		// Literal value
		public static AIS.Value Literal(string content)
			=> new AIS.Value
			{
				content = content,
				specialValueId = AIS.Value.LITERAL
			};
		// Equal condition
		public static AIS.Condition Equal(string controlId, string value)
			=> new AIS.Condition
			{
				controlId = controlId,
				@operator = AIS.Condition.EQUAL,
				value = List(Literal(value))
			};
		// Select FormAction
		public static AIS.FormAction Select(int row)
			=> new AIS.FormAction
			{
				controlID = string.Format("1.{0}", row),
				command = AIS.FormAction.SelectRow
			};
		// Do FormAction
		public static AIS.FormAction Do(string controlId)
			=> new AIS.FormAction
			{
				controlID = controlId,
				command = AIS.FormAction.DoAction
			};
		// Query
		public static AIS.Query Query(IEnumerable<AIS.Condition> condition)
			=> new AIS.Query
			{
				matchType = AIS.Query.MATCH_ALL,
				autoFind = true,
				condition = condition
			};
		// Open Stack Form
		public static AIS.StackFormRequest Open(AIS.FormRequest fm)
			=> new AIS.StackFormRequest
			{
				action = AIS.StackFormRequest.open,
				formRequest = fm
			};
		static readonly Regex FormPat = new Regex("[^_]+_([^_]+).*");
		// Execute Stack Form
		public static AIS.StackFormRequest Execute(AIS.FormResponse rs, AIS.ActionRequest rq)
		{
			rq.formOID = FormPat.Match(rs.currentApp).Groups[1].Value;
			return new AIS.StackFormRequest
			{
				action = AIS.StackFormRequest.execute,
				stackId = rs.stackId,
				stateId = rs.stateId,
				rid = rs.rid,
				actionRequest = rq
			};
		}
		// Close Stack Form
		public static AIS.StackFormRequest Close(AIS.FormResponse rs)
			=> new AIS.StackFormRequest
			{
				action = AIS.StackFormRequest.close,
				stackId = rs.stackId,
				stateId = rs.stateId,
				rid = rs.rid
			};
	}
	// Namespace for the Work With Addresses Form
	namespace W01012B
	{
		// The Grid Defintion
		public class Row
		{
			// Grid Columns
			public long z_AN8_19 { get; set; }
			public string z_ALPH_20 { get; set; }
		}
		// The Form Defintion
		public class FormData : AIS.FormData<Row>
		{
			// Form Field "Search Type"
			public AIS.FormField<string> z_AT1_54 { get; set; }
		}
		// Form Response
		public class Response : AIS.FormResponse
		{
			// Form Parameter
			public AIS.Form<FormData> fs_P01012_W01012B { get; set; }
		}
		// Form Request
		public class Request : AIS.FormRequest
		{
			public Request()
			{
				// Form name to call
				formName = "P01012_W01012B";
				// Version
				version = "ZJDE0001";
				// Read-only
				formServiceAction = "R";
				// Return Maximum 10 rows
				maxPageSize = "10";
				// Return Form Field 54 and Grid Columns 19 and 20
				returnControlIDs = "54|1[19,20]";
			}
		}
	}
	// Namespace for the Address Fix Inspect Form
	namespace W01012A
	{
		public class FormData : AIS.FormData<AIS.Row>
		{
			public AIS.FormField<string> z_AC20_411 { get; set; }
			public AIS.FormField<string> z_EXCHG_300 { get; set; }
			public AIS.FormField<string> z_YEARSTAR_303 { get; set; }
			public AIS.FormField<string> z_REVRNG_305 { get; set; }
			public AIS.FormField<string> z_AC17_402 { get; set; }
			public AIS.FormField<string> z_AC18_405 { get; set; }
			public AIS.FormField<string> z_AC19_408 { get; set; }
			public AIS.FormField<string> z_TICKER_298 { get; set; }
			public AIS.FormField<string> z_ALPH_28 { get; set; }
			public AIS.FormField<int> z_AN8_21 { get; set; }
			public AIS.FormField<string> z_MLNM_38 { get; set; }
			public AIS.FormField<string> z_AT1_36 { get; set; }
			public AIS.FormField<string> z_TAX_34 { get; set; }
			public AIS.FormField<string> z_CLASS01_284 { get; set; }
			public AIS.FormField<string> z_AC15_396 { get; set; }
			public AIS.FormField<string> z_AC16_399 { get; set; }
			public AIS.FormField<string> z_ALKY_32 { get; set; }
			public AIS.FormField<string> z_ADD4_46 { get; set; }
			public AIS.FormField<string> z_CLASS04_290 { get; set; }
			public AIS.FormField<string> z_CLASS05_292 { get; set; }
			public AIS.FormField<string> z_CLASS03_286 { get; set; }
			public AIS.FormField<string> z_CLASS02_288 { get; set; }
			public AIS.FormField<string> z_ADD3_44 { get; set; }
			public AIS.FormField<string> z_ADD2_42 { get; set; }
			public AIS.FormField<string> z_ADD1_40 { get; set; }
			public AIS.FormField<string> z_COUN_58 { get; set; }
			public AIS.FormField<string> z_CTR_56 { get; set; }
			public AIS.FormField<string> z_AC10_381 { get; set; }
			public AIS.FormField<string> z_CM_140 { get; set; }
			public AIS.FormField<string> z_SBLI_132 { get; set; }
			public AIS.FormField<string> z_AC08_375 { get; set; }
			public AIS.FormField<string> z_TAXC_134 { get; set; }
			public AIS.FormField<string> z_SIC_136 { get; set; }
			public AIS.FormField<string> z_AC09_378 { get; set; }
			public AIS.FormField<string> z_LNGP_138 { get; set; }
			public AIS.FormField<string> z_ADDS_54 { get; set; }
			public AIS.FormField<string> z_CTY1_52 { get; set; }
			public AIS.FormField<string> z_ADDZ_50 { get; set; }
			public AIS.FormField<string> z_ATR_67 { get; set; }
			public AIS.FormField<string> z_AC13_390 { get; set; }
			public AIS.FormField<string> z_AC14_393 { get; set; }
			public AIS.FormField<string> z_AC11_384 { get; set; }
			public AIS.FormField<string> z_AC12_387 { get; set; }
			public AIS.FormField<string> z_TX2_146 { get; set; }
			public AIS.FormField<string> z_TXCT_148 { get; set; }
			public AIS.FormField<string> z_ATP_64 { get; set; }
			public AIS.FormField<string> z_MCU_62 { get; set; }
			public AIS.FormField<string> z_AC03_360 { get; set; }
			public AIS.FormField<string> z_AC27_481 { get; set; }
			public AIS.FormField<string> z_AC24_472 { get; set; }
			public AIS.FormField<string> z_AC01_354 { get; set; }
			public AIS.FormField<string> z_AC25_475 { get; set; }
			public AIS.FormField<string> z_AC02_356 { get; set; }
			public AIS.FormField<string> z_AC26_478 { get; set; }
			public AIS.FormField<string> z_AC30_490 { get; set; }
			public AIS.FormField<char> z_ATPR_130 { get; set; }
			public AIS.FormField<char> z_AT3_251 { get; set; }
			public AIS.FormField<string> z_AC07_372 { get; set; }
			public AIS.FormField<string> z_AC04_363 { get; set; }
			public AIS.FormField<string> z_AC28_484 { get; set; }
			public AIS.FormField<string> z_AC05_366 { get; set; }
			public AIS.FormField<string> z_AC29_487 { get; set; }
			public AIS.FormField<string> z_AC06_369 { get; set; }
			public AIS.FormField<char> z_ATE_129 { get; set; }
			public AIS.FormField<int> z_AN86_330 { get; set; }
			public AIS.FormField<string> z_DUNS01_456 { get; set; }
			public AIS.FormField<string> z_DUNS02_457 { get; set; }
			public AIS.FormField<string> z_DUNS03_458 { get; set; }
			public AIS.FormField<string> z_AC21_463 { get; set; }
			public AIS.FormField<string> z_AC22_466 { get; set; }
			public AIS.FormField<string> z_AC23_469 { get; set; }
			public AIS.FormField<int> z_GROWTHR_311 { get; set; }
			public AIS.FormField<int> z_NOE_313 { get; set; }
			public AIS.FormField<string> z_AEMPGP_316 { get; set; }
			public AIS.FormField<int> z_PA8_318 { get; set; }
			public AIS.FormField<int> z_AN82_320 { get; set; }
			public AIS.FormField<int> z_AN81_322 { get; set; }
			public AIS.FormField<char> z_AT2_444 { get; set; }
			public AIS.FormField<int> z_AN83_324 { get; set; }
			public AIS.FormField<int> z_AN84_326 { get; set; }
			public AIS.FormField<int> z_AN85_328 { get; set; }
		}
		// Form Response
		public class Response : AIS.FormResponse
		{
			// Form Parameter
			public AIS.Form<FormData> fs_P01012_W01012A { get; set; }
		}
	}
}
;