using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using FOXCourses.FoxServiceRef; namespace FOXCourses { class Program { // MAIN ==================================================================================== static void Main(string[] args) { { string transType; bool next = true; bool boolStop = true; string[] validInput = { "C", "R", "F", "U", "D", "E" }; try { for (next = true; next == boolStop; next = true) { Console.WriteLine("Enter an operation: " + "\n" + "C (Create)" + "\n" + "R (Read)" + "\n" + "F (Find)" + "\n" + "U (Update)" + "\n" + "D (Delete)" + "\n" + "E (Exit)"); transType = (Convert.ToString(Console.ReadLine())).ToUpper(); int pos = Array.IndexOf(validInput, transType); if (pos < 0) { continue; } else if (transType == "E") { boolStop = false; } else { Program.selectTransType(transType); Console.WriteLine("Do you like to continue? (Y/N)"); string request = Console.ReadLine(); boolStop = (request.ToUpper() == "Y") ? true : false; } } } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } } } //SELECTION =========================================================================================================== static void selectTransType(string transType = "") { if (transType == "F") { Program.find(); } else if (transType == "") { return; } else { Program.CRUDOperation(transType); } return; } //CRITERIA =========================================================================================================== static QueryCriteria criteria(string dataSource, string fieldName, Operator opr, string value1, string value2) { QueryCriteria criteria = new QueryCriteria(); CriteriaElement criteriaElement = new CriteriaElement(); criteriaElement.DataSourceName = dataSource; criteriaElement.FieldName = fieldName; criteriaElement.Operator = opr; criteriaElement.Value1 = value1; criteriaElement.Value2 = value2; criteria.CriteriaElement = new CriteriaElement[1] { criteriaElement }; return criteria; } // FIND ============================================================================================================ static void find() { Console.WriteLine("Select the trainer : "); string trainer = Console.ReadLine(); AxdFOXCourseQuery document = new AxdFOXCourseQuery(); QueryCriteria criteria = Program.criteria("FOXCourse", "Trainer", Operator.Equal, trainer, null); using (FoxServiceRef.FOXCourseQueryServiceClient client = new FoxServiceRef.FOXCourseQueryServiceClient()) { try { FoxServiceRef.FOXCourseQueryServiceClient titleDocument = new FoxServiceRef.FOXCourseQueryServiceClient(); document = client.find(null, criteria); foreach (AxdEntity_FoxCourse table in document.FoxCourse) { Console.WriteLine(table.Id + ' ' + table.Name); } Console.WriteLine("Press the ENTER key to continue"); Console.ReadLine(); Program.selectTransType(); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); Console.ReadLine(); client.Abort(); } } } // INPUT HANDLER ============================================================================================================ static string[] inputHandler(string _transType) { bool printTxt = false; ; string comment =""; string courseName = " "; string trainer = " "; switch (_transType) { case "C": comment = "create"; printTxt = true; break; case "R": comment = "read"; break; case "U": comment = "update"; printTxt = true; break; case "D": comment = "remove"; break; } Console.WriteLine(string.Format("Enter a course Id to {0} : ", comment)); string courseId = Console.ReadLine(); if (printTxt == true) { string addText = "(skip if no change is needed)"; Console.WriteLine(string.Format("Enter course name : {0}", addText)); courseName = Console.ReadLine(); Console.WriteLine(string.Format("Enter trainer : {0}", addText)); trainer = Console.ReadLine(); } string[] container = new string[] { courseId, courseName, trainer, comment }; return container; } // INSERT OR UPDATE LOGIC=========================================================================== static void insertUpdateFields(string _transType, AxdEntity_FoxCourse _courseTable, string[] _array) { _courseTable.Id = (_transType == "C") ? _array[0] : _courseTable.Id; _courseTable.Name = _array[1] == "" ? _courseTable.Name : _array[1]; _courseTable.Trainer = _array[2] == "" ? _courseTable.Trainer : _array[2]; if (_transType == "U") { _courseTable.action = AxdEnum_AxdEntityAction.update; _courseTable.actionSpecified = true; } } //CRUDOperation======================================================================================== static void CRUDOperation(string _transType) { try { AxdFOXCourseQuery courseInfo = null; AxdEntity_FoxCourse courseTable = null; EntityKey entityKey = null; ; EntityKey[] entityKeys = null; string[] values = Program.inputHandler(_transType); FOXCourseQueryServiceClient client = new FOXCourseQueryServiceClient(); CallContext context = new CallContext(); context.Company = "USMF"; if (_transType != "C") { KeyField keyfield = new KeyField() { Field = "Id", Value = values[0] }; entityKey = new EntityKey(); entityKey.KeyData = new KeyField[1] { keyfield }; entityKeys = new EntityKey[1] { entityKey }; courseInfo = client.read(context, entityKeys); courseTable = courseInfo.FoxCourse[0]; } string[] courseData = new string[3] { values[0], values[1], values[2] }; switch (_transType) { case "C" : courseInfo = new AxdFOXCourseQuery(); courseTable = new AxdEntity_FoxCourse(); Program.insertUpdateFields(_transType, courseTable, values); courseInfo.FoxCourse = new AxdEntity_FoxCourse[1] { courseTable }; entityKeys = client.create(context, courseInfo); break; case "D": client.delete(context, entityKeys); break; case "U": courseTable = courseInfo.FoxCourse.First(); Program.insertUpdateFields(_transType, courseTable, courseData); client.update(context, entityKeys, courseInfo); break; } if (_transType != "R") { Console.WriteLine(String.Format("Following course is {0}d.", values[3])); } Console.WriteLine(String.Format("Course Id : {0} " + "\n" + "Name : {1} " + "\n" + "Trainer : {2} ", courseTable.Id, courseTable.Name, courseTable.Trainer)); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); Console.ReadLine(); } } } }