예제 #1
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);
        }
예제 #2
0
        public WasteWaterTreatmentPlant StandardizeAndStore(WasteWaterTreatmentPlant wwtp)
        {
            var waterPlantId = _dbUtilityAccessor.GetIdForWwtpName(wwtp.Name);

            if (wwtp.TreatmentSteps != null)
            {
                foreach (var treatmentStep in wwtp.TreatmentSteps)
                {
                    //check if treatmentstep exists for the waterplant - if not - insert it
                    int treatmentStepId;
                    if (_dbUtilityAccessor.TreatmentStepTypeForWwtpExists(treatmentStep.Name, wwtp.Name))
                    {
                        treatmentStepId = _dbUtilityAccessor.GetTreatmentStepIdForTypeAndWwtp(treatmentStep.Name, wwtp.Name);
                    }
                    else
                    {
                        var treatmentStepTypeId = _dbUtilityAccessor.GetIdForTreatmentType(treatmentStep.Name);
                        treatmentStepId = _dbSetter.SetTreatmentStep(treatmentStepTypeId, waterPlantId);
                    }

                    InsertQualityIndicators(null, treatmentStepId, treatmentStep.QualityIndicators);
                }
            }

            if (wwtp.GeneralIndicators != null)
            {
                InsertQualityIndicators(waterPlantId, null, wwtp.GeneralIndicators);
            }

            return(wwtp);
        }
예제 #3
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");
        }