Example #1
0
        public List <AppReturn> post([FromBody] JObject values)
        {
            //_g.CONFIG_FILE = "ngarbi.settings.json";

            DateTime startProcess = DateTime.Now;

            if (values[_g.KEY_QPARAM_JSON] != null)
            {
                // get request with complex parameters initiated
                return(RequestData(values));
            }


            //dGhpcyBpcyBhIHRlc3Q=
            byte[] bytes = Convert.FromBase64String("dGhpcyBpcyBhIHRlc3Q=");
            string text  = System.Text.Encoding.Default.GetString(bytes);

            byte[] bytes2  = System.Text.Encoding.Default.GetBytes(text);
            string textB64 = Convert.ToBase64String(bytes2);

            JObject args = AppArgs;

            // initialize return object
            AppReturn ret = new AppReturn();



            if (values.ContainsKey(_g.KEY_REQUEST_HEADER_CODE))
            {
                try
                {
                    string  header    = (String)values[_g.KEY_REQUEST_HEADER_CODE];        // extract header Base64
                    byte[]  jsonBytes = Convert.FromBase64String(header);                  // convert to byte array
                    string  jsonText  = System.Text.Encoding.Default.GetString(jsonBytes); // convert to JSON string
                    JObject json      = JObject.Parse(jsonText);                           // convert to JSON object

                    // loop through header information and add token to AppArgs if not yet existing
                    foreach (JProperty jph in (JToken)json)
                    {
                        if (!args.ContainsKey(jph.Name))
                        {
                            args.Add(jph.Name, jph.Value);
                        }
                    }

                    ret.headerProcessResult.Add("result", "Success");
                }
                catch (Exception e)
                {
                    ret.headerProcessResult.Add("ErrorDetails", e.ToString());
                    ret.headerProcessResult.Add("result", "Error");
                }
            }

            List <AppReturn> retVal        = new List <AppReturn> {
            };
            List <CommandParam> cmds       = new List <CommandParam>();
            JObject             postConfig = null;
            bool         useCommonNewKey   = false;
            bool         firstTable        = true;
            List <Int64> tblCommonId       = new List <Int64>();

            // generate collection of commands to execute
            foreach (JProperty jp in (JToken)values)
            {
                // iterate through all tables to generate CommandParams collection
                if (jp.Name == _g.KEY_PROCESS_HEADER_CODE)
                {
                    DALData.DAL.LogGlobalMessage("proces header code is supplied!", "invoke_process");
                    // this section is where application-specific process will be invoked
                    ret.invokeResult = RunModels.Invoke(jp);
                }
                else if (jp.Name == _g.KEY_REQUEST_HEADER_CODE)
                {
                    ;
                }
                else if (jp.Name == _g.KEY_PROCESS_CONFIG_CODE)
                // config code value contains instruction/parameters which
                // will determine specific process when posting
                // eg. on creation of new record with sub table, use common id
                // for both
                {
                    postConfig = (JObject)jp.Value;
                    if (postConfig["useCommonNewKey"] != null)
                    {
                        useCommonNewKey = (bool)postConfig["useCommonNewKey"];
                    }
                }

                else
                {
                    // PROCESS TABLE CREATE, UPDATE, DELETE

                    // get table object from the collection
                    DALTable tbl = AppDataset.AppTables[jp.Name];
                    // if(!firstTable && tblCommonId)
                    //
                    Int64 newRecordKeyId = -1;


                    // get collection of CommandParams per table
                    List <CommandParam> cmdsTemp = tbl.GetCommandParamsForPosting((JArray)jp.Value, args);
                    CommandParam        cmd1     = cmdsTemp[0];
                    if (firstTable)
                    {
                        // record first table record ids.
                        // iterate through the records' parameters

                        if (cmd1.newKey != -1)
                        {
                            // new record. populate tblCommonId with key id's of all commands
                            cmdsTemp.ForEach(cmd => tblCommonId.Add(cmd.paramKeyValue));
                        }

                        firstTable = false; // set first table flag to false after processing it
                    }
                    else
                    {
                        // if use common id,
                        if (useCommonNewKey && cmd1.newKey != -1 && tblCommonId.Count != 0)
                        {
                            int ctr = 0;
                            cmdsTemp.ForEach(cmd =>
                            {
                                cmd.cmdParams[DALData.PARAM_PREFIX + "p" + (int)cmd.paramKeyValuePosition] = tblCommonId[ctr];
                                ctr++;
                            });
                        }
                    }
                    if (DALData.DAL.globalError.Length != 0)
                    {
                        // error has occured, therefore empty list is returned. handle error here
                        break;
                    }



                    // append commands to the general collection for execution in bulk
                    foreach (CommandParam cmd in cmdsTemp)
                    {
                        cmds.Add(cmd);
                    }
                }

                // execute commands
            }

            List <ReturnObject> cmdResults = new List <ReturnObject>();

            // execute all commands in the collection (if any) - debug bypass by alv 2020/12/04 5:40am
            cmdResults = DALData.DAL.Excute(cmds, true);

            DateTime endProcess = DateTime.Now;

            long dur = ((endProcess.Millisecond +
                         endProcess.Second * 1000 +
                         endProcess.Minute * 60 * 1000 +
                         endProcess.Hour * 60 * 60 * 1000) -
                        (startProcess.Millisecond +
                         startProcess.Second * 1000 +
                         startProcess.Minute * 60 * 1000 +
                         startProcess.Hour * 60 * 60 * 1000));

            //ret.returnStrings.Add("Process Duration in milliseconds: " + dur.ToString() + "(ms)");
            //if (errMessage.Length!=0) ret.returnStrings.Add("Error:" + errMessage);

            //ret.stamps = new DALStamps(cmds[0].table.columns, "@alv");
            ret.stamps = null;

            ret.errorMessage = DALData.DAL.globalError;

            if (DALData.DAL.globalMessages.Count() != 0)
            {
                ret.globalMesages = DALData.DAL.globalMessages;
            }

            retVal.Add(ret);

            // ***** Generate result summary for client-side processing ******
            foreach (ReturnObject rObj in cmdResults)
            {
                string retCode = rObj.returnCode;
                if (retCode == "chgTrack" || retCode == "")
                {
                    continue;
                }

                ret = new AppReturn();

                ret.returnCode       = rObj.returnCode;
                ret.errorMessage     = rObj.result.exceptionMessage;
                ret.returnDataParams = rObj.result.returnDataParams;
                ret.result           = rObj.result.result;

                retVal.Add(ret);
            }

            return(retVal);
            //return new List<AppReturn>();
            //return new List<AppReturn> { new AppReturn() };
        }   /** End of POST method **/
Example #2
0
        public List <AppReturn> post([FromBody] JObject values)
        {
            /************************************************************************************
            * 20 Dec 2019 - alv
            * values:
            * {
            *  "<tableCode>":[{data row object 1},...,{data row object n}]
            * }
            ************************************************************************************/

            /************************************************************************************
            * 15 April 2020 - alv
            *
            * public static string KEY_REQUEST_HEADER_CODE = "__header__";
            *
            * public static string KEY_REQUEST_STAMP = "_req_stamp_";
            * public static string KEY_USER_ID = "__uid__";
            * public static string KEY_USER_RIGHTS = "__rights__";
            * public static string KEY_ACTION = "__action__";
            *
            * JSON
            *
            *  "__header__" :
            *  {
            *       "_req_stamp_":"",
            *       "__uid__":"alv",
            *       "__rights__":"",
            *       "__action__":""
            *  }
            *  "__header__" :{"_req_stamp_":"","__uid__":"alv","__rights__":"","__action__":""}
            *
            *  https://www.base64encode.org/
            *  Equivalent base64: eyJfcmVxX3N0YW1wXyI6IiIsIl9fdWlkX18iOiJhbHYiLCJfX3JpZ2h0c19fIjoiIiwiX19hY3Rpb25fXyI6IiJ9
            *
            * Converted to Base64 text
            ************************************************************************************/

            DateTime startProcess = DateTime.Now;


            //dGhpcyBpcyBhIHRlc3Q=
            byte[] bytes = Convert.FromBase64String("dGhpcyBpcyBhIHRlc3Q=");
            string text  = System.Text.Encoding.Default.GetString(bytes);

            byte[] bytes2  = System.Text.Encoding.Default.GetBytes(text);
            string textB64 = Convert.ToBase64String(bytes2);

            JObject args = AppArgs;

            if (values.ContainsKey(_g.KEY_REQUEST_HEADER_CODE))
            {
                string  header    = (String)values[_g.KEY_REQUEST_HEADER_CODE];        // extract header Base64
                byte[]  jsonBytes = Convert.FromBase64String(header);                  // convert to byte array
                string  jsonText  = System.Text.Encoding.Default.GetString(jsonBytes); // convert to JSON string
                JObject json      = JObject.Parse(jsonText);                           // convert to JSON object

                // loop through header information and add token to AppArgs if not yet existing
                foreach (JProperty jph in (JToken)json)
                {
                    if (!args.ContainsKey(jph.Name))
                    {
                        args.Add(jph.Name, jph.Value);
                    }
                }
            }

            List <AppReturn> retVal  = new List <AppReturn> {
            };
            List <CommandParam> cmds = new List <CommandParam>();

            // generate collection of commands to execute
            foreach (JProperty jp in (JToken)values)
            {
                // iterate through all tables to generate CommandParams collection
                if (jp.Name != _g.KEY_REQUEST_HEADER_CODE)
                {
                    // get table object from the collection
                    DALTable tbl = AppDataset.AppTables[jp.Name];

                    // get collection of CommandParams per table
                    List <CommandParam> cmdsTemp = tbl.GetCommandParamsForPosting((JArray)jp.Value, args);
                    if (DALData.DAL.globalError.Length != 0)
                    {
                        // error has occured, therefore empty list is returned. handle error here
                        break;
                    }

                    // append commands to the general collection for execution in bulk
                    foreach (CommandParam cmd in cmdsTemp)
                    {
                        cmds.Add(cmd);
                    }
                }

                // execute commands
            }

            // execute all commands in the collection
            List <ReturnObject> cmdResults = DALData.DAL.Excute(cmds, true);

            DateTime endProcess = DateTime.Now;

            AppReturn ret = new AppReturn();
            long      dur = ((endProcess.Millisecond +
                              endProcess.Second * 1000 +
                              endProcess.Minute * 60 * 1000 +
                              endProcess.Hour * 60 * 60 * 1000) -
                             (startProcess.Millisecond +
                              startProcess.Second * 1000 +
                              startProcess.Minute * 60 * 1000 +
                              startProcess.Hour * 60 * 60 * 1000));

            //ret.returnStrings.Add("Process Duration in milliseconds: " + dur.ToString() + "(ms)");
            //if (errMessage.Length!=0) ret.returnStrings.Add("Error:" + errMessage);

            //ret.stamps = new DALStamps(cmds[0].table.columns, "@alv");
            ret.stamps = null;

            //AppReturn ar = new AppReturn();
            //ar.stamps = new DALStamps(cmds[0].table.columns, "@alv");
            //retVal.Add(ar);

            ret.errorMessage = DALData.DAL.globalError;

            retVal.Add(ret);

            foreach (ReturnObject rObj in cmdResults)
            {
                ret = new AppReturn();
                ret.errorMessage = rObj.result.exceptionMessage;
                retVal.Add(ret);
            }

            return(retVal);
            //return new List<AppReturn> { new AppReturn() };
        }