private WQDefaultValueModel LoadDefaultValueModelFromConfigFile(string jsonFilePath)
        {
            using (FileStream fs = File.Open(jsonFilePath, FileMode.Open))
            {
                using (StreamReader reader = new StreamReader(fs, System.Text.Encoding.UTF8))
                {
                    using (JsonReader jr = new JsonTextReader(reader))
                    {
                        JsonSerializer serializer = new JsonSerializer();

                        try
                        {
                            return serializer.Deserialize<WQDefaultValueModel>(jr);
                        }
                        catch (Exception ex)
                        {
                            if (_wayToLoadConfigFile == WayToLoadConfigFile.ThrowExceptionIfLoadFail)
                            {
                                throw ex;
                            }
                            else if (_wayToLoadConfigFile == WayToLoadConfigFile.CreateNewConfigFileIfLoadFail)
                            {
                                var noDataModel = new WQDefaultValueModel();
                                var saveSuccess = this.SaveDefaultValueConfiguration(noDataModel);
                                if (!saveSuccess)
                                {
                                    throw new InvalidDataException("Save JSON provider data fail, please contact the EIS group");
                                }
                                return noDataModel;
                            }
                            else
                            {
                                throw new ArgumentException(_wayToLoadConfigFile.ToString() + " is not a valid way to load config file.");
                            }
                        }
                    }
                }
            }

        }
        /// <summary>
        /// Init method is called in the constructor
        /// Calling Init functino separatly is not recommended
        /// </summary>
        public void Init()
        {
            if (File.Exists(_jsonFilePath))
            {
                try
                {
                    _data = LoadDefaultValueModelFromConfigFile(_jsonFilePath);

                }
                catch (Exception ex)
                {
                    throw new InvalidDataException("JSON provider initialize fail. The provided json file is not valid." + ex.StackTrace);
                }
            }
            else
            {
                if (_createNewConfigFileIsNotExist)
                {
                    var noDataModel = new WQDefaultValueModel();
                    var saveSuccess = this.SaveDefaultValueConfiguration(noDataModel);
                    if (!saveSuccess)
                    {
                        throw new InvalidDataException("Save JSON provider data fail, please contact the EIS group");
                    }
                    _data = noDataModel;
                }
                else
                {
                    throw new FileNotFoundException("JSON provider initialize fail. The provided path " + _jsonFilePath + " could not be found.");
                }

            }
        }
 public bool SaveDefaultValueConfiguration(WQDefaultValueModel data)
 {
     //For static values, do nothing
     return true;
 }
        public bool SaveDefaultValueConfiguration(WQDefaultValueModel data)
        {
            _data = data;

            var fileMode = File.Exists(_jsonFilePath) ? FileMode.Open : FileMode.CreateNew;

            try
            {
                using (FileStream fs = File.Open(_jsonFilePath, fileMode))
                using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
                using (JsonWriter jw = new JsonTextWriter(sw))
                {
                    jw.Formatting = Formatting.Indented;

                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Serialize(jw, data);
                }
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }
Ejemplo n.º 5
0
 public bool SaveDefaultValueConfiguration(WQDefaultValueModel data)
 {
     //For static values, do nothing
     return(true);
 }