Пример #1
0
        public Personography()
        {
            ClearChangeTrack();


            SourceRepository = new MoravianLivesGitHubFileStorage().ResolveStorage();

            var _pl = new MapDefinition
            {
                KeyMaps = new Dictionary <string, KeyMapConfiguration>
                {
                    {
                        "person.persName.forename",
                        new KeyMapConfiguration
                        {
                            Target  = "Name",
                            Handler = "HistoricString"
                        }
                    }
                }
            };

            // SourceRepository.StoreText("Projects/TEI_Memoirs", "ML_personography-transformMap.json", _pl.ToJson());

            Identifier(entry => entry.Id)
            .Configure(config =>
            {
                config.StaleRecordTimespan  = TimeSpan.FromSeconds(1);
                config.Collection           = "Bucknell.MoravianLives.GitHub";
                config.SourceIdentifierPath = "person.@xml:id";
                config.Set = "Personography";

                //config.MemberMapping = SourceRepository
                //    .GetText("Projects/TEI_Memoirs", "ML_personography-transformMap.json")
                //    .Result
                //    .FromJson<MapDefinition>();

                config.MemberMapping = _pl;
            })
            .SourceModel(raw =>
            {
                // First - Persons.
                var source = SourceRepository
                             .GetDynamic("Projects/TEI_Memoirs/personography/XML", "ML_personography.xml")
                             .Result;

                raw.Source = source;

                // Select and Typecast the entries.
                var items = ((JArray)source.SelectToken("TEI.text.body.listPerson"))
                            .Select(i => i.ToContentEntry(Configuration.SourceIdentifierPath))
                            .ToList();

                raw.Items = items;
            })
            .SourceValue((item, path) => item.Contents.SelectTokens(path).FirstOrDefault()?.ToString())
            .ConvertToModelType(item =>
            {
                if (item.Result == null)
                {
                    return;
                }

                item.Result.Success = false;

                switch (item.HandlerType)
                {
                case "HistoricString":
                    item.Result.Value   = (HistoricString)item.Source;
                    item.Result.Success = true;
                    break;

                case "Historical.Geography":
                    Geography tempValue = item.Source;
                    item.Result.Value   = tempValue;
                    item.Result.Success = true;
                    break;
                }
            })
            .ResolveReference(source =>
            {
                //Let's first try to identify if the item is already present on our database as a reference.
                // We'll receive a source item and try to resolve it to its 1:1 Data model.

                var sourceId = GetIdentifier(source);
                var domain   = $"{Configuration.Collection}.{Configuration.Set}";

                return(_personReference.ResolveReference(domain, sourceId, null));
            })
            .ComplexTransform(entry =>
            {
                if (entry.targetModel == null)
                {
                    return;
                }
                if (entry.sourceData?.Contents == null)
                {
                    return;
                }

                var a = entry.sourceData.Contents.ToJson();
                var b = entry.targetModel.ToJson();

                var source = entry.sourceData?.Contents;

                // First - name.

                var firstName = source?.StringValue("person.persName.forename");
                var lastName  = source?.StringValue("person.persName.surname.#text");

                var fullName = $"{firstName} {lastName}";

                // Birth

                var bd = new BirthData
                {
                    Token = source?.JValue(new[]
                    {
                        "person.birth.date[?(@.@type == 'birth' && @.@calendar == 'Gregorian' && @.@resp == 'memoir')]",
                        "person.birth.date[?(@.@type == 'birth' && @.@calendar == 'Gregorian')]",
                        "person.birth.date[?(@.@type == 'birth' && @.@calendar != 'Julian')]"
                    })
                };


                bd.Text      = bd.Token.StringValue("#text");
                bd.Timestamp = bd.Text;

                entry.targetModel.Name = $"{firstName} {lastName}";

                if (bd.Timestamp != null)
                {
                    var targetNameVariant = entry.targetModel.Name.Variants.FirstOrDefault().Value.Variants.FirstOrDefault();
                    if (targetNameVariant != null)
                    {
                        targetNameVariant.Period     = bd.Timestamp;
                        targetNameVariant.Period.End = null;

                        var nameResp = bd.Token.StringValue("@resp");
                        if (nameResp != null)
                        {
                            targetNameVariant.Comments ??= $"Source: {nameResp}";
                        }
                    }
                }

                var bpIdentifier = source.StringValue("person.birth.placeName.@ref");
                var bpName       = source.StringValue("person.birth.placeName.#text");

                if (bpIdentifier != null)
                {
                    bd.Location = _locationReference.ResolveReference(Placeography.DefaultDomain, bpIdentifier, bpName);
                }
            })
            .OnCommit(() =>
            {
                _personReference.Save();
                _locationReference.Save();
            });
        }
Пример #2
0
        public Placeography()
        {
            SourceRepository = new MoravianLivesGitHubFileStorage().ResolveStorage();

            var _pl = new MapDefinition
            {
                KeyMaps = new Dictionary <string, KeyMapConfiguration>
                {
                    {
                        "place.location.geo",
                        new KeyMapConfiguration {
                            Target = "Geography", Handler = "Historical.Geography"
                        }
                    },
                    {
                        "place.placeName[?(@.@type == 'preferred')].name.#text",
                        new KeyMapConfiguration
                        {
                            Target          = "Name",
                            Handler         = "HistoricString",
                            AternateSources = new List <string>
                            {
                                "place.placeName[?(@.@type == 'standard')].name.#text",
                                "place.placeName[?(@.@type == 'preferred')].name",
                                "place.placeName[?(@.@type == 'standard')].name",
                                "place.placeName[0].name"
                            }
                        }
                    }
                }
            };

            SourceRepository.StoreText("Projects/TEI_Memoirs", "ML_placeography-transformMap.json", _pl.ToJson());

            Identifier(entry => entry.Id)
            .Configure(config =>
            {
                config.StaleRecordTimespan  = TimeSpan.FromSeconds(1);
                config.Collection           = "Bucknell.MoravianLives.GitHub";
                config.SourceIdentifierPath = "place.@xml:id";
                config.Set = "Placeography";

                config.MemberMapping = SourceRepository
                                       .GetText("Projects/TEI_Memoirs", "ML_placeography-transformMap.json")
                                       .Result
                                       .FromJson <MapDefinition>();

                config.MemberMapping = _pl;
            })
            .SourceModel(raw =>
            {
                // First - Locations.
                var placeographySource = SourceRepository.GetDynamic("Projects/TEI_Memoirs", "ML_placeography.xml")
                                         .Result;

                raw.Source = placeographySource;

                // Select and Typecast the entries.
                raw.Items = ((JArray)placeographySource.SelectToken("TEI.text.body.listPlace"))
                            .Select(i => i.ToContentEntry(Configuration.SourceIdentifierPath))
                            .ToList();
            })
            .SourceValue((item, path) => item.Contents.SelectTokens(path).FirstOrDefault()?.ToString())
            .ConvertToModelType(item =>
            {
                item.Result.Success = false;

                switch (item.HandlerType)
                {
                case "HistoricString":
                    item.Result.Value   = (HistoricString)item.Source;
                    item.Result.Success = true;
                    break;

                case "Historical.Geography":
                    Geography tempValue = item.Source;
                    item.Result.Value   = tempValue;
                    item.Result.Success = true;
                    break;
                }
            })
            .ResolveReference(source =>
            {
                //Let's first try to identify if the item is already present on our database as a reference.
                // We'll receive a source item and try to resolve it to its 1:1 Data model.

                var sourceId = GetIdentifier(source);
                var domain   = (Configuration.Collection != null && Configuration.Set != null ? $"{Configuration.Collection}.{Configuration.Set}" : DefaultDomain);

                return(_locationReference.ResolveReference(domain, sourceId));
            })
            .ComplexTransform(entry =>
            {
                var a = entry.sourceData.Contents.ToJson();
                var b = entry.targetModel.ToJson();

                var relationshipMarker = entry.sourceData.Contents.SelectToken("relation");
                if (relationshipMarker != null)
                {
                    if (relationshipMarker.SelectToken("@name").ToString() == "containedBy")
                    {
                        var hardReferenceId = relationshipMarker.SelectToken("@normalizedId")?.ToString();

                        if (hardReferenceId == null)
                        {
                            var domain      = $"{Configuration.Collection}.{Configuration.Set}";
                            var referenceId = relationshipMarker.SelectToken("@passive").ToString();

                            var parentReference = _locationReference.ResolveReference(domain, referenceId).Id;
                            hardReferenceId     = parentReference;
                        }

                        entry.targetModel.Parent = hardReferenceId;
                    }
                }


                //Specifically for Placeography we have alternate name formats. So we'll extract and add it to the Variants list.

                var variantNames = entry.sourceData.Contents
                                   .SelectTokens("place.placeName[?(@.@type == 'variant')]").ToList();

                foreach (var i in variantNames)
                {
                    var c = i.ToJson();

                    var variantName = i.SVal("name.#text");

                    entry.targetModel.Name.SetVariant(
                        i.SVal("name.#text"),
                        JsonHelperExtensions.GetDefaultCulture(i.SVal("@xml:lang")),
                        i.SVal("note.label"),
                        null,
                        i.SVal("@when-iso")
                        );
                }
            })
            .OnCommit(() =>
            {
                _locationReference.Save();
            });


            ClearChangeTrack();
        }