protected override void ExecuteImpl(ShareManager shareManager, List <ShareDefinition> shareDefinitions) { var first = shareDefinitions.First(); if (first.Type != typeof(Catalogue)) { throw new Exception("ShareDefinition was not for a Catalogue"); } if (_targetCatalogue.Name != (string)first.Properties["Name"]) { if (!YesNo("Catalogue Name is '" + _targetCatalogue.Name + "' but ShareDefinition is for, '" + first.Properties["Name"] + "'. Import Anyway?", "Import Anyway?")) { return; } } shareManager.ImportPropertiesOnly(_targetCatalogue, first); _targetCatalogue.SaveToDatabase(); var liveCatalogueItems = _targetCatalogue.CatalogueItems; foreach (ShareDefinition sd in shareDefinitions.Skip(1)) { if (sd.Type != typeof(CatalogueItem)) { throw new Exception("Unexpected shared object of Type " + sd.Type + " (Expected ShareDefinitionList to have 1 Catalogue + N CatalogueItems)"); } var shareName = (string)sd.Properties["Name"]; var existingMatch = liveCatalogueItems.FirstOrDefault(ci => ci.Name.Equals(shareName)); if (existingMatch == null) { existingMatch = new CatalogueItem(BasicActivator.RepositoryLocator.CatalogueRepository, _targetCatalogue, shareName); } shareManager.ImportPropertiesOnly(existingMatch, sd); existingMatch.SaveToDatabase(); } Publish(_targetCatalogue); }
public void GatherAndShare_Catalogue_Test(bool goViaJson) { //Setup some objects under Catalogue that we can share var cata = new Catalogue(CatalogueRepository, "Cata"); cata.Periodicity = Catalogue.CataloguePeriodicity.BiMonthly; cata.SaveToDatabase(); var catalogueItem1 = new CatalogueItem(CatalogueRepository, cata, "Ci1"); var catalogueItem2 = new CatalogueItem(CatalogueRepository, cata, "Ci2"); var tableInfo = new TableInfo(CatalogueRepository, "Myt"); var colInfo = new ColumnInfo(CatalogueRepository, "[Mt].[C1]", "varchar(10)", tableInfo); catalogueItem1.ColumnInfo_ID = colInfo.ID; catalogueItem1.SaveToDatabase(); var ei = new ExtractionInformation(CatalogueRepository, catalogueItem1, colInfo, "UPPER(C1) as Fish"); //the logging server has a system default so should have been populated Assert.IsNotNull(cata.LiveLoggingServer_ID); //Catalogue sharing should be allowed Gatherer g = new Gatherer(RepositoryLocator); Assert.IsTrue(g.CanGatherDependencies(cata)); //gather the objects depending on Catalogue as a tree var gObj = g.GatherDependencies(cata); Assert.AreEqual(2, gObj.Children.Count); //both cata items var lmd = new LoadMetadata(CatalogueRepository); cata.LoadMetadata_ID = lmd.ID; cata.SaveToDatabase(); //get the share definition var shareManager = new ShareManager(RepositoryLocator); var shareDefinition = gObj.ToShareDefinitionWithChildren(shareManager); if (goViaJson) { var json = shareDefinition.Select(s => JsonConvertExtensions.SerializeObject(s, RepositoryLocator)).ToList(); shareDefinition = json.Select( j => JsonConvertExtensions.DeserializeObject(j, typeof(ShareDefinition), RepositoryLocator)) .Cast <ShareDefinition>() .ToList(); } //make a local change cata.Name = "fishfish"; cata.SubjectNumbers = "123"; cata.LoadMetadata_ID = null; cata.Periodicity = Catalogue.CataloguePeriodicity.Unknown; cata.SaveToDatabase(); lmd.DeleteInDatabase(); //import the saved copy shareManager.ImportSharedObject(shareDefinition); //revert the memory copy and check it got overwritten with the original saved values cata.RevertToDatabaseState(); Assert.AreEqual("Cata", cata.Name); var exports = CatalogueRepository.GetAllObjects <ObjectExport>(); Assert.IsTrue(exports.Any()); //now delete and report foreach (var d in exports) { d.DeleteInDatabase(); } //make a local change including Name cata.Name = "fishfish"; cata.SaveToDatabase(); //test importing the Catalogue properties only shareManager.ImportPropertiesOnly(cata, shareDefinition[0]); //import the defined properties but not name Assert.AreEqual("fishfish", cata.Name); Assert.AreEqual(Catalogue.CataloguePeriodicity.BiMonthly, cata.Periodicity); //reset this though Assert.IsNull(cata.LoadMetadata_ID); cata.SaveToDatabase(); cata.DeleteInDatabase(); //none of these should now exist thanks to cascade deletes Assert.IsFalse(cata.Exists()); Assert.IsFalse(catalogueItem1.Exists()); Assert.IsFalse(catalogueItem2.Exists()); //import the saved copy var newObjects = shareManager.ImportSharedObject(shareDefinition).ToArray(); Assert.AreEqual("Cata", ((Catalogue)newObjects[0]).Name); Assert.AreEqual("Ci1", ((CatalogueItem)newObjects[1]).Name); Assert.AreEqual("Ci2", ((CatalogueItem)newObjects[2]).Name); }