Esempio n. 1
0
        /// <summary>
        /// Internal user to validate params have been set along with data loaded.
        /// </summary>
        internal void CheckFilePath()
        {
            string errMsg = null;

            if (string.IsNullOrWhiteSpace(JSONFilePath))
            {
                errMsg = MISSING_FILE;
            }

            if (errMsg == null)
            {
                JSONStatus js = LoadData();
                if (js.Status != RESULT_STATUS.OK && js.Status != RESULT_STATUS.MISSING)
                {
                    errMsg = js.Description;
                }
            }

            //check to ensure a valid connections tring was passed.
            if (errMsg != null)
            {
                //create error
                ThrowException(errMsg);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Save all data from DataSet "RecordData" to JSON
        /// </summary>
        /// <returns></returns>
        internal JSONStatus SaveData()
        {
            JSONStatus retVal = new JSONStatus();

            try
            {
                if (RecordData == null)
                {
                    retVal.Status      = RESULT_STATUS.MISSING;
                    retVal.Description = "Record data needs to be loaded and have structure before it can be saved.";
                    return(retVal);
                }

                string json  = JsonConvert.SerializeObject(RecordData, Formatting.Indented);
                byte[] bytes = Encoding.ASCII.GetBytes(json);

                if (File.Exists(JSONFilePath))
                {
                    File.Delete(JSONFilePath);
                }

                File.WriteAllBytes(JSONFilePath, bytes);
                retVal.Description = "Success";
            }
            catch (Exception ex)
            {
                retVal.Status      = RESULT_STATUS.EXCEPTION;
                retVal.Description = ex.Message;
                retVal.StackTrace  = ex.StackTrace;
            }

            return(retVal);
        }
Esempio n. 3
0
        /// <summary>
        /// Load all data from JSON to DataSet "RecordData"
        /// </summary>
        /// <returns></returns>
        internal JSONStatus LoadData(bool refresh = false)
        {
            JSONStatus retVal = new JSONStatus();

            if (!refresh && RecordData != null && RecordData.Tables.Count != 0)
            {
                return(retVal);
            }

            try
            {
                if (!File.Exists(JSONFilePath))
                {
                    retVal.Status      = RESULT_STATUS.MISSING;
                    retVal.Description = $"Missing Data File: {JSONFilePath}";
                }
                else
                {
                    byte[] bytes      = File.ReadAllBytes(JSONFilePath);
                    string someString = Encoding.UTF8.GetString(bytes);
                    RecordData         = (DataSet)JsonConvert.DeserializeObject(someString, (typeof(DataSet)));
                    retVal.Description = "Success";
                }
            }
            catch (Exception ex)
            {
                retVal.Status      = RESULT_STATUS.EXCEPTION;
                retVal.Description = ex.Message;
                retVal.StackTrace  = ex.StackTrace;
            }

            return(retVal);
        }
Esempio n. 4
0
        /// <summary>
        /// create a table
        /// </summary>
        /// <param name="fieldValues"></param>
        /// <returns></returns>
        public JSONStatus CreateTable(string tableName, List <FIELD_VALUE> fieldValues)
        {
            JSONStatus retVal    = new JSONStatus();
            DataTable  dataTable = new DataTable();

            dataTable.TableName = tableName;

            try
            {
                //create table headers..
                foreach (var field in fieldValues)
                {
                    dataTable.Columns.Add(field.FieldName);
                }

                DataRow dr = dataTable.NewRow();

                foreach (var field in fieldValues)
                {
                    dr[field.FieldName] = field.Value;
                }

                dataTable.Rows.Add(dr);

                retVal = CreateTable(dataTable);
            }
            catch (Exception ex)
            {
                retVal.Description = ex.Message;
                retVal.Status      = RESULT_STATUS.EXCEPTION;
                retVal.StackTrace  = ex.StackTrace;
            }

            return(retVal);
        }
Esempio n. 5
0
        /// <summary>
        /// Update specific record/records
        /// </summary>
        /// <example>
        /// List<JSONHelper.FIELD_VALUE> fieldValues = new List<JSONHelper.FIELD_VALUE>();
        /// fieldValues.Add(new JSONHelper.FIELD_VALUE { FieldName = "PMType", Value = inventoryModel.PMType });
        /// fieldValues.Add(new JSONHelper.FIELD_VALUE { FieldName = "ShapeType", Value = inventoryModel.ShapeType });
        /// JSONStatus rs = jsonData.UpdateRecord("Inventory", fieldValues, $"InventoryId='{inventoryModel.InventoryId}'");
        /// </example>
        /// <param name="tableName"></param>
        /// <param name="fieldValues"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public JSONStatus UpdateRecord(string tableName, List <FIELD_VALUE> fieldValues, string where, bool createTable = false)
        {
            CheckFilePath();

            JSONStatus retVal = new JSONStatus();
            DataTable  dt     = GetTable(tableName);

            try
            {
                if (dt != null)
                {
                    foreach (FIELD_VALUE fv in fieldValues)
                    {
                        if (!dt.Columns.Contains(fv.FieldName))
                        {
                            dt.Columns.Add(fv.FieldName);
                        }
                    }
                }
                else if (dt == null && createTable)
                {
                    CreateTable(tableName, fieldValues);
                    retVal.Description = "Success";
                    return(retVal);
                }
                else if (dt == null)
                {
                    ThrowException($"'{tableName}' table does not exists.  Use CreateTable() first.");
                }

                DataRow[] exists = dt.Select(where);

                if (exists != null && exists.Length > 0)
                {
                    for (int i = 0; i < exists.Length; i++)
                    {
                        dt.Rows.Remove(exists[i]);
                        DataRow dr = dt.NewRow();

                        foreach (FIELD_VALUE fv in fieldValues)
                        {
                            dr[fv.FieldName] = fv.Value;
                        }

                        dt.Rows.Add(dr);
                    }
                }
                else
                {
                    DataRow newRow = dt.NewRow();
                    foreach (FIELD_VALUE fv in fieldValues)
                    {
                        newRow[fv.FieldName] = fv.Value;
                    }
                    dt.Rows.Add(newRow);
                }

                RecordData.Tables.Remove(tableName);
                RecordData.Tables.Add(dt);
                SaveData();
            }
            catch (Exception ex)
            {
                retVal.Status      = RESULT_STATUS.EXCEPTION;
                retVal.Description = ex.Message;
                retVal.StackTrace  = ex.StackTrace;
            }

            return(retVal);
        }