Example #1
0
        public TreeFormattedObject ConvertTreeObject(string treeStringObject)
        {
            if (IsJson(treeStringObject))
            {
                Debug.WriteLine("The object is a valid Json");
                //first check if the json object is in the predefined format
                if (IsCommonSchemaJson(treeStringObject))
                {
                    Debug.WriteLine("Perfect! The object is in the predefined schema");
                    var jsonObject = JsonConvert.DeserializeObject<WasteWaterTreatmentPlant>(treeStringObject);
                    var treeFormattedObject = new TreeFormattedObject()
                    {
                        IsPredefinedSchema = true,
                        Object = jsonObject
                    };

                    return treeFormattedObject;
                }
                else
                {
                    Debug.WriteLine("The Json format does not equal the predefined format");
                    var jsonObject = JsonConvert.DeserializeObject<dynamic>(treeStringObject);
                    var treeFormattedObject = new TreeFormattedObject()
                    {
                        IsPredefinedSchema = false,
                        Object = jsonObject
                    };

                    return treeFormattedObject;
                }
            }

            throw new Exception("The passed object didnt have a supported format");
        }
Example #2
0
        public WasteWaterTreatmentPlant HarmonizeJsonObject(TreeFormattedObject treeObject, string waterPlant, string treatmentStep)
        {
            int?retreivedWaterPlantId = null, retreivedTreatmentStepTypeId = null;

            //check if waterPlantId can be found
            if (!string.IsNullOrEmpty(waterPlant))
            {
                //check if its numeric
                if (int.TryParse(waterPlant, out var waterPlantId))
                {
                    retreivedWaterPlantId = waterPlantId;
                    waterPlant            = _dbUtilAccessor.GetWwtpNameForId(waterPlantId);
                }
                else
                {
                    retreivedWaterPlantId = _dbUtilAccessor.GetIdForWwtpName(waterPlant);
                }
            }

            //check if treatmentStepTypeId can be found
            if (!string.IsNullOrEmpty(treatmentStep))
            {
                //check if its numeric
                if (int.TryParse(treatmentStep, out var treatmentStepTypeId))
                {
                    retreivedTreatmentStepTypeId = treatmentStepTypeId;
                    treatmentStep = _dbUtilAccessor.GetTreatmentTypeForId(treatmentStepTypeId);
                }

                else
                {
                    retreivedTreatmentStepTypeId = _dbUtilAccessor.GetIdForWwtpName(waterPlant);
                }
            }

            var wwtp = _treeHarmonizer.HarmonizeTree(treeObject, retreivedWaterPlantId, retreivedTreatmentStepTypeId);

            if (string.IsNullOrEmpty(wwtp.Name))
            {
                wwtp.Name = waterPlant;
            }
            if (wwtp.TreatmentSteps != null && !wwtp.TreatmentSteps.Any() &&
                string.IsNullOrEmpty(wwtp.TreatmentSteps.First().Name))
            {
                wwtp.TreatmentSteps.First().Name = treatmentStep;
            }

            return(wwtp);
        }
Example #3
0
        public TreeFormattedObject PreSimplyfyJsonObject(TreeFormattedObject treeObject)
        {
            if (treeObject.IsPredefinedSchema)
            {
                Debug.WriteLine("The object is already in the recommended common schema and does no require any simplification");
                return(treeObject);
            }

            Dictionary <string, dynamic> dynamicDict = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(treeObject.Object.ToString());

            var preSimplificationResult = RemoveBlacklistedKeys(dynamicDict);

            //convert the dynamic object back into the correct format
            ExpandoObject expandoObject           = ((Dictionary <string, dynamic>)preSimplificationResult).ToExpando();
            var           serializedExpandoObject = JsonConvert.SerializeObject(expandoObject);
            var           dynamicObject           = JsonConvert.DeserializeObject <dynamic>(serializedExpandoObject);

            treeObject.Object = dynamicObject;
            return(treeObject);
        }
Example #4
0
        public WasteWaterTreatmentPlant HarmonizeTree(TreeFormattedObject treeObject, int?waterPlantId = null, int?treatmentStepTypeId = null)
        {
            WasteWaterTreatmentPlant wwtp;

            // if the object was conform to the predefined schema all along, just convert, map and return it
            if (treeObject.IsPredefinedSchema)
            {
                var wwtpObject = treeObject.Object as WasteWaterTreatmentPlant;
                _aliasNameToRealNameDict = waterPlantId != null?
                                           _utilityDbAccessor.GetAliasToRealNameOnWwtpDict((int)waterPlantId) :
                                               _utilityDbAccessor.GetAliasToRealNameOnWwtpDict(_utilityDbAccessor.GetIdForWwtpName(wwtpObject?.Name));

                foreach (var treatmentStep in wwtpObject.TreatmentSteps)
                {
                    treatmentStep.QualityIndicators = MapWaterPlantAliases(treatmentStep.QualityIndicators);
                }
                return(wwtpObject);
            }
            // ... otherwise first replace the single valeus by waterplant aliases and then try to convert it
            // if that does not work, the object is not valid

            // we can only map if waterPlantId is passed as parameter or can be read from the treeobject
            if (waterPlantId != null && _aliasNameToRealNameDict != null)
            {
                _aliasNameToRealNameDict = _utilityDbAccessor.GetAliasToRealNameOnWwtpDict((int)waterPlantId);
            }
            else
            {
                try
                {
                    var wwtpAsObject = ((JObject)treeObject.Object).ToObject <WasteWaterTreatmentPlant>();
                    var name         = wwtpAsObject.Name;
                    if (!string.IsNullOrEmpty(name))
                    {
                        waterPlantId             = waterPlantId ?? _utilityDbAccessor.GetIdForWwtpName(name);
                        _aliasNameToRealNameDict = _utilityDbAccessor.GetAliasToRealNameOnWwtpDict((int)waterPlantId);
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("Name & Id of Wwtp missing");
                }
            }

            // map the dynamic object
            dynamic mappingResult = null;

            if (_aliasNameToRealNameDict != null && _aliasNameToRealNameDict.Any())
            {
                var dynamicJsonDict = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(treeObject.Object.ToString());
                mappingResult = MapWaterPlantAliases(dynamicJsonDict);

                // convert mapping result back into the correct format
                ExpandoObject expandoObject           = ((Dictionary <string, dynamic>)mappingResult).ToExpando();
                var           serializedExpandoObject = JsonConvert.SerializeObject(expandoObject);
                mappingResult = JsonConvert.DeserializeObject <dynamic>(serializedExpandoObject);
            }

            // even if the mapping wasnt successful, we still might have a valid waterplant object
            // last try to deserialize it and checks if everything is correct afterwars
            var dynamicDict = mappingResult == null ? treeObject.Object : mappingResult;

            //<<DESERIALIZATION CHECKS>>
            //try to deserialuze the wwtp as a whole first
            if (IsWholeWwtp(dynamicDict, out wwtp))
            {
                foreach (var treatmentStep in wwtp.TreatmentSteps)
                {
                    treatmentStep.QualityIndicators = MapWaterPlantAliases(treatmentStep.QualityIndicators);
                }
                return(wwtp);
            }

            //if this didnt work, check if its an option to only deserialize parts of the wwtp
            //treatment step list
            if (waterPlantId != null && IsTreatmentSteps(dynamicDict, out wwtp))
            {
                wwtp.Name = _utilityDbAccessor.GetWwtpNameForId((int)waterPlantId);
                foreach (var treatmentStep in wwtp.TreatmentSteps)
                {
                    treatmentStep.QualityIndicators = MapWaterPlantAliases(treatmentStep.QualityIndicators);
                }
                return(wwtp);
            }

            //qualityIndicators
            if (waterPlantId != null && treatmentStepTypeId != null && IsQualityIndicators(dynamicDict, out wwtp))
            {
                wwtp.Name = _utilityDbAccessor.GetWwtpNameForId((int)waterPlantId);
                wwtp.TreatmentSteps.FirstOrDefault().Name = _utilityDbAccessor.GetTreatmentTypeForId((int)treatmentStepTypeId);
                foreach (var treatmentStep in wwtp.TreatmentSteps)
                {
                    treatmentStep.QualityIndicators = MapWaterPlantAliases(treatmentStep.QualityIndicators);
                }
                return(wwtp);
            }

            //if nothing worked so far - last try is to check if its a qualityindicatorlist that is directly assigned to the waterplant
            if (waterPlantId != null && IsQualityIndicators(dynamicDict, out wwtp, true))
            {
                wwtp.Name = _utilityDbAccessor.GetWwtpNameForId((int)waterPlantId);
                foreach (var treatmentStep in wwtp.TreatmentSteps)
                {
                    treatmentStep.QualityIndicators = MapWaterPlantAliases(treatmentStep.QualityIndicators);
                }
                return(wwtp);
            }

            throw new Exception("Could not harmonize the dynamicObject");
        }