Example #1
0
        internal static void FlattenContext(
            SortedDictionary <string, XElement> highLevelData,
            SortedDictionary <string, XElement> sortedData,
            string scriptureBaseDir)
        {
            var stylesDir = scriptureBaseDir;             // Just use main folder. // Path.Combine(baseDirectory, SharedConstants.Styles);

            if (!Directory.Exists(stylesDir))
            {
                return;
            }

            var stylePathname = Path.Combine(scriptureBaseDir, StyleFilename);

            if (!File.Exists(stylePathname))
            {
                return;
            }

            var doc = XDocument.Load(stylePathname);
            // StStyle instances are owned by Scripture in its Styles coll prop.
            var scrElement    = highLevelData[SharedConstants.Scripture];
            var scrOwningGuid = scrElement.Attribute(SharedConstants.GuidStr).Value.ToLowerInvariant();
            var sortedStyles  = new SortedDictionary <string, XElement>(StringComparer.OrdinalIgnoreCase);

            foreach (var styleElement in doc.Root.Elements("StStyle"))
            {
                CmObjectFlatteningService.FlattenOwnedObject(
                    stylePathname,
                    sortedData,
                    styleElement,
                    scrOwningGuid, sortedStyles);                     // Restore 'ownerguid' to styleElement.
            }

            // Restore scrElement Styles property in sorted order.
            if (sortedStyles.Count == 0)
            {
                return;
            }
            var stylesOwningProp = scrElement.Element(SharedConstants.Styles)
                                   ?? CmObjectFlatteningService.AddNewPropertyElement(scrElement, SharedConstants.Styles);

            foreach (var sortedStyle in sortedStyles.Values)
            {
                stylesOwningProp.Add(sortedStyle);
            }
        }
Example #2
0
        internal static void FlattenContext(
            SortedDictionary <string, XElement> highLevelData,
            SortedDictionary <string, XElement> sortedData,
            string scriptureBaseDir)
        {
            if (!Directory.Exists(scriptureBaseDir))
            {
                return;
            }
            var pathname = Path.Combine(scriptureBaseDir, SharedConstants.ImportSettingsFilename);

            if (!File.Exists(pathname))
            {
                return;
            }

            // Owned by Scripture in ArchivedDrafts coll prop.
            var scrElement           = highLevelData[SharedConstants.Scripture];
            var scrOwningGuid        = scrElement.Attribute(SharedConstants.GuidStr).Value.ToLowerInvariant();
            var sortedImportSettings = new SortedDictionary <string, XElement>(StringComparer.OrdinalIgnoreCase);
            var doc = XDocument.Load(pathname);

            foreach (var importSettingsElement in doc.Root.Elements("ScrImportSet"))
            {
                CmObjectFlatteningService.FlattenOwnedObject(
                    pathname,
                    sortedData,
                    importSettingsElement,
                    scrOwningGuid, sortedImportSettings);                     // Restore 'ownerguid' to importSettingsElement.
            }

            // Restore scrElement ImportSettings property in sorted order.
            if (sortedImportSettings.Count == 0)
            {
                return;
            }
            var importSettingsOwningProp = scrElement.Element(SharedConstants.ImportSettings)
                                           ?? CmObjectFlatteningService.AddNewPropertyElement(scrElement, SharedConstants.ImportSettings);

            foreach (var sortedimportSettings in sortedImportSettings.Values)
            {
                importSettingsOwningProp.Add(sortedimportSettings);
            }
        }
Example #3
0
        internal static void FlattenContext(
            SortedDictionary <string, XElement> highLevelData,
            SortedDictionary <string, XElement> sortedData,
            string scriptureBaseDir)
        {
            if (!Directory.Exists(scriptureBaseDir))
            {
                return;                 // Nothing to do.
            }
            var langProjElement = highLevelData[SharedConstants.LangProject];
            var langProjGuid    = langProjElement.Attribute(SharedConstants.GuidStr).Value.ToLowerInvariant();
            var sortedLists     = new SortedDictionary <string, XElement>(StringComparer.OrdinalIgnoreCase);

            foreach (var listPathname in Directory.GetFiles(scriptureBaseDir, "*.list", SearchOption.TopDirectoryOnly))
            {
                if (listPathname.EndsWith(SharedConstants.NoteCategoriesListFilename))
                {
                    continue;                     // Wrong list.
                }
                var listDoc     = XDocument.Load(listPathname);
                var listElement = listDoc.Element("CheckList").Element(SharedConstants.CmPossibilityList);
                CmObjectFlatteningService.FlattenOwnedObject(
                    listPathname,
                    sortedData,
                    listElement,
                    langProjGuid, sortedLists);                     // Restore 'ownerguid' to list.
            }

            if (sortedLists.Count == 0)
            {
                return;
            }
            // Restore LangProj CheckLists property in sorted order.
            var checkListsProp = langProjElement.Element(CheckLists)
                                 ?? CmObjectFlatteningService.AddNewPropertyElement(langProjElement, CheckLists);

            foreach (var sortedList in sortedLists.Values)
            {
                checkListsProp.Add(sortedList);
            }
        }
        internal static void FlattenContext(
            SortedDictionary <string, XElement> highLevelData,
            SortedDictionary <string, XElement> sortedData,
            string scriptureBaseDir)
        {
            if (!Directory.Exists(scriptureBaseDir))
            {
                return;
            }

            // Owned by Scripture in ArchivedDrafts coll prop.
            var scrElement    = highLevelData[SharedConstants.Scripture];
            var scrOwningGuid = scrElement.Attribute(SharedConstants.GuidStr).Value.ToLowerInvariant();
            var sortedDrafts  = new SortedDictionary <string, XElement>(StringComparer.OrdinalIgnoreCase);

            foreach (var draftPathname in Directory.GetFiles(scriptureBaseDir, "*." + SharedConstants.ArchivedDraft, SearchOption.TopDirectoryOnly))
            {
                var doc          = XDocument.Load(draftPathname);
                var draftElement = doc.Root.Element(SharedConstants.ScrDraft);
                CmObjectFlatteningService.FlattenOwnedObject(draftPathname,
                                                             sortedData,
                                                             draftElement,
                                                             scrOwningGuid, sortedDrafts); // Restore 'ownerguid' to draftElement.
            }

            // Restore scrElement ArchivedDrafts property in sorted order.
            if (sortedDrafts.Count == 0)
            {
                return;
            }
            var draftsOwningProp = scrElement.Element(SharedConstants.ArchivedDrafts)
                                   ?? CmObjectFlatteningService.AddNewPropertyElement(scrElement, SharedConstants.ArchivedDrafts);

            foreach (var sortedDraft in sortedDrafts.Values)
            {
                draftsOwningProp.Add(sortedDraft);
            }
        }
        internal static void FlattenContext(
            SortedDictionary <string, XElement> highLevelData,
            SortedDictionary <string, XElement> sortedData,
            string linguisticsBaseDir)
        {
            var discourseDir = Path.Combine(linguisticsBaseDir, SharedConstants.DiscourseRootFolder);

            if (!Directory.Exists(discourseDir))
            {
                return;
            }
            var chartPathname = Path.Combine(discourseDir, SharedConstants.DiscourseChartFilename);

            if (!File.Exists(chartPathname))
            {
                return;
            }

            // The charts need to be sorted in guid order, before being added back into the owning prop element.
            var doc              = XDocument.Load(chartPathname);
            var root             = doc.Root;
            var discourseElement = root.Element(SharedConstants.Header).Element("DsDiscourseData");

            // Add lists back into discourseElement.
            foreach (var listPathname in Directory.GetFiles(discourseDir, "*." + SharedConstants.List))
            {
                var listDoc      = XDocument.Load(listPathname);
                var listFilename = Path.GetFileName(listPathname);
                var listElement  = listDoc.Root.Element(SharedConstants.CmPossibilityList);
                switch (listFilename)
                {
                case SharedConstants.ChartMarkersFilename:
                    discourseElement.Element(SharedConstants.ChartMarkers).Add(listElement);
                    break;

                case SharedConstants.ConstChartTemplFilename:
                    discourseElement.Element(SharedConstants.ConstChartTempl).Add(listElement);
                    break;
                }
            }
            // Add the chart elements (except the possible dummy one) into discourseElement.
            var sortedCharts = new SortedDictionary <string, XElement>(StringComparer.OrdinalIgnoreCase);

            foreach (var chartElement in root.Elements(SharedConstants.DsChart)
                     .Where(element => element.Attribute(SharedConstants.GuidStr).Value.ToLowerInvariant() != SharedConstants.EmptyGuid))
            {
                // No. Add it to discourseElement, BUT in sorted order, below, and then flatten discourseElement.
                // Restore the right main element name from the class attribute.
                var classAttr = chartElement.Attribute(SharedConstants.Class);
                chartElement.Name = classAttr.Value;
                classAttr.Remove();
                sortedCharts.Add(chartElement.Attribute(SharedConstants.GuidStr).Value.ToLowerInvariant(), chartElement);
            }

            if (sortedCharts.Count > 0)
            {
                var discourseElementOwningProp = discourseElement.Element("Charts")
                                                 ?? CmObjectFlatteningService.AddNewPropertyElement(discourseElement, "Charts");
                foreach (var sortedChartElement in sortedCharts.Values)
                {
                    discourseElementOwningProp.Add(sortedChartElement);
                }
            }
            var langProjElement = highLevelData[SharedConstants.LangProject];

            BaseDomainServices.RestoreElement(
                chartPathname,
                sortedData,
                langProjElement,
                "DiscourseData",
                discourseElement);
        }
        internal static void FlattenContext(
            SortedDictionary <string, XElement> highLevelData,
            SortedDictionary <string, XElement> sortedData,
            string linguisticsBaseDir)
        {
            var reversalDir = Path.Combine(linguisticsBaseDir, ReversalRootFolder);

            if (!Directory.Exists(reversalDir))
            {
                return;
            }

            var lexDb          = highLevelData[SharedConstants.LexDb];
            var sortedRevs     = new SortedDictionary <string, XElement>(StringComparer.OrdinalIgnoreCase);
            var unlovedFolders = new HashSet <string>();

            foreach (var revIndexDirectoryName in Directory.GetDirectories(reversalDir))
            {
                var dirInfo          = new DirectoryInfo(revIndexDirectoryName);
                var ws               = dirInfo.Name;
                var reversalPathname = Path.Combine(revIndexDirectoryName, ws + "." + SharedConstants.Reversal);
                if (!File.Exists(reversalPathname))
                {
                    // If a reversal is deleted but there were ChorusNotes associated with it the directory might be
                    // here without any reversal files inside it.
                    unlovedFolders.Add(revIndexDirectoryName);
                    continue;
                }

                var reversalDoc = XDocument.Load(reversalPathname);

                // Put entries back into index's Entries element.
                var root          = reversalDoc.Element("Reversal");
                var header        = root.Element(SharedConstants.Header);
                var revIdxElement = header.Element("ReversalIndex");

                // Restore POS list, if it exists.
                var catPathname = Path.Combine(revIndexDirectoryName, ws + "-" + SharedConstants.PartsOfSpeechFilename);
                if (File.Exists(catPathname))
                {
                    var catListDoc = XDocument.Load(catPathname);
                    BaseDomainServices.RestoreElement(
                        catPathname,
                        sortedData,
                        revIdxElement, SharedConstants.PartsOfSpeech,
                        catListDoc.Root.Element(SharedConstants.CmPossibilityList));                         // Owned elment.
                }

                // Put all records back in ReversalIndex, before sort and restore.
                // EXCEPT, if there is only one of them and it is guid.Empty, then skip it
                var sortedRecords = new SortedDictionary <string, XElement>(StringComparer.OrdinalIgnoreCase);
                foreach (var recordElement in root.Elements("ReversalIndexEntry")
                         .Where(element => element.Attribute(SharedConstants.GuidStr).Value.ToLowerInvariant() != SharedConstants.EmptyGuid))
                {
                    // Add it to Records property of revIdxElement, BUT in sorted order, below, and then flatten dnMainElement.
                    sortedRecords.Add(recordElement.Attribute(SharedConstants.GuidStr).Value.ToLowerInvariant(), recordElement);
                }

                if (sortedRecords.Count > 0)
                {
                    var recordsElementOwningProp = revIdxElement.Element("Entries")
                                                   ?? CmObjectFlatteningService.AddNewPropertyElement(revIdxElement, "Entries");

                    foreach (var sortedChartElement in sortedRecords.Values)
                    {
                        recordsElementOwningProp.Add(sortedChartElement);
                    }
                }
                CmObjectFlatteningService.FlattenOwnedObject(reversalPathname, sortedData, revIdxElement,
                                                             lexDb.Attribute(SharedConstants.GuidStr).Value.ToLowerInvariant(), sortedRevs); // Restore 'ownerguid' to indices.
            }

            foreach (var unlovedFolder in unlovedFolders)
            {
                Directory.Delete(unlovedFolder, true);
            }

            // Restore lexDb ReversalIndexes property in sorted order.
            if (sortedRevs.Count == 0)
            {
                return;
            }

            var reversalsOwningProp = lexDb.Element("ReversalIndexes") ?? CmObjectFlatteningService.AddNewPropertyElement(lexDb, "ReversalIndexes");

            foreach (var sortedRev in sortedRevs.Values)
            {
                reversalsOwningProp.Add(sortedRev);
            }
        }
        internal static void FlattenContext(
            SortedDictionary <string, XElement> highLevelData,
            SortedDictionary <string, XElement> sortedData,
            string anthropologyBaseDir)
        {
            var langProjElement = highLevelData[SharedConstants.LangProject];
            var currentPathname = Path.Combine(anthropologyBaseDir, SharedConstants.DataNotebookFilename);
            var doc             = XDocument.Load(currentPathname);
            var root            = doc.Root;
            var dnMainElement   = root.Element(SharedConstants.Header).Element("RnResearchNbk");

            // Add the record elements (except the possible dummy one) into dnMainElement.
            var sortedRecords = new SortedDictionary <string, XElement>(StringComparer.OrdinalIgnoreCase);

            foreach (var recordElement in root.Elements("RnGenericRec")
                     .Where(element => element.Attribute(SharedConstants.GuidStr).Value.ToLowerInvariant() != SharedConstants.EmptyGuid))
            {
                // Add it to Records property of dnMainElement, BUT in sorted order, below, and then flatten dnMainElement.
                sortedRecords.Add(recordElement.Attribute(SharedConstants.GuidStr).Value.ToLowerInvariant(), recordElement);
            }

            if (sortedRecords.Count > 0)
            {
                var recordsElementOwningProp = dnMainElement.Element("Records")
                                               ?? CmObjectFlatteningService.AddNewPropertyElement(dnMainElement, "Records");
                foreach (var sortedChartElement in sortedRecords.Values)
                {
                    recordsElementOwningProp.Add(sortedChartElement);
                }
            }

            // Put the RecTypes list back into place in dnMainElement, before dnMainElement is restored.
            // But only as an <objsur> element.
            var recTypesPathname = Path.Combine(anthropologyBaseDir, "RecTypes." + SharedConstants.List);

            if (File.Exists(recTypesPathname))
            {
                var listDoc = XDocument.Load(recTypesPathname);
                BaseDomainServices.RestoreElement(recTypesPathname, sortedData,
                                                  dnMainElement, "RecTypes",
                                                  listDoc.Root.Element(SharedConstants.CmPossibilityList));
            }

            BaseDomainServices.RestoreElement(currentPathname, sortedData,
                                              langProjElement, "ResearchNotebook",
                                              dnMainElement);

            // Put the lists back where they belong in LangProj.
            foreach (var listPathname in Directory.GetFiles(anthropologyBaseDir, "*." + SharedConstants.List))
            {
                var listDoc      = XDocument.Load(listPathname);
                var listRoot     = listDoc.Root;
                var listRootName = listRoot.Name.LocalName;
                if (listRootName == "RecTypes")
                {
                    continue;
                }
                BaseDomainServices.RestoreElement(listPathname,
                                                  sortedData,
                                                  langProjElement,
                                                  listRootName,
                                                  listRoot.Element(SharedConstants.CmPossibilityList));
            }
        }