Ejemplo n.º 1
0
        private static Permit ExtractPermit(JObject jPerm)
        {
            var subTime = DateTime.UtcNow;
            var permit  = new Permit {
                submissionTime = subTime, approvalTime = null, deprecationTime = null
            };

            foreach (var jPermKid in jPerm.Children())
            {
                var jProp = jPermKid as JProperty;
                if (jProp == null)
                {
                    continue;
                }

                try
                {
                    var    jValue = jProp.Value;
                    JArray jArray = null;
                    if (jProp.Value.Type == JTokenType.Array)
                    {
                        jArray = (JArray)jProp.Value;
                    }

                    switch (jProp.Name)
                    {
                    case "id":
                        permit.id = !string.IsNullOrWhiteSpace(jValue.ToString())
                ? new ObjectId(jValue.ToString())
                : ObjectId.GenerateNewId();
                        break;

                    case "type":
                        permit.type = jValue.ToString();
                        break;

                    case "infState":
                        permit.infState = jValue.ToString();
                        break;

                    case "infLoc":
                        permit.infLoc = jValue.ToString();
                        break;

                    case "infDisc":
                        permit.infDisc = jValue.ToString();
                        break;

                    case "infType":
                        permit.infType = jValue.ToString();
                        break;

                    case "consType":
                        permit.consType = jValue.ToString();
                        break;

                    case "consPermits":
                        permit.consPermits = (int)jValue;
                        break;

                    case "permState":
                        permit.permState = jValue.ToString();
                        break;

                    case "distances":
                        if (jArray == null)
                        {
                            break;
                        }
                        permit.distances = new double[jArray.Count];
                        var ix = 0;
                        foreach (var jToken in jArray)
                        {
                            permit.distances[ix++] = (double)jToken;
                        }
                        break;

                    case "totalDistance":
                        permit.totalDistance = (double)jValue;
                        break;

                    case "locations":
                        var geoJson = jValue.ToString(Formatting.Indented);
                        var geoDoc  = BsonSerializer.Deserialize <GeoJsonFeatureCollection <GeoJson2DGeographicCoordinates> >(geoJson);
                        permit.locations = geoDoc;
                        break;

                    case "locationDescriptions":
                        if (jArray == null)
                        {
                            break;
                        }
                        permit.locationDescriptions = new string[jArray.Count][];
                        var il = 0;
                        foreach (var jlocDescArray in jArray)
                        {
                            permit.locationDescriptions[il] = new string[((JArray)jlocDescArray).Count];
                            var id = 0;
                            foreach (var jLocDesc in jlocDescArray)
                            {
                                permit.locationDescriptions[il][id++] = jLocDesc.ToString();
                            }
                            il++;
                        }
                        break;

                    case "parties":
                        var parties = new List <Party>();
                        if (jArray != null)
                        {
                            parties.AddRange(jArray.Select(jParty => JsonConvert.DeserializeObject <Party>(jParty.ToString())));
                        }

                        permit.parties = parties.ToArray();
                        break;
                    }
                }
                catch (Exception ex)
                {
                }
            }

            // clean up of unneeded elements
            permit.CleanParties();

            return(permit);
        }