private void LoadDataFromDump(string superCollectionName, string collectionName, string getFileName, string getToursFileName, bool replaceGuids, string contentAdminId) { SuperCollection superCollection = _storage.SuperCollections.Find(CollectionIdFromText(superCollectionName)); bool createCollection = true; if (superCollection != null) { _storage.Entry(superCollection).Collection(_ => _.Collections).Load(); createCollection = superCollection.Collections.All(candidate => candidate.Title != collectionName); } if (superCollection == null || createCollection) { // Load the Beta Content collection Collection collection = LoadCollections(superCollectionName, collectionName, contentAdminId); using (Stream getData = File.OpenRead(_baseDirectory.Value + @"Dumps\" + getFileName)) using (Stream getToursData = getToursFileName == null ? null : File.OpenRead(_baseDirectory.Value + @"Dumps\" + getToursFileName)) { LoadData(getData, getToursData, collection, replaceGuids); } // Save changes to storage _storage.SaveChanges(); } }
public static void ListIterationExample() { Console.WriteLine("--- List iteration example start ---"); var stocks = new SuperCollection { "MSFT", "GOOG", "AAPL" }; for (int i = 0; i < stocks.Count; i++) { Console.WriteLine(stocks.Get(i)); } Console.WriteLine("--- List iteration example end ---"); }
private Collection LoadCollections(string superCollectionName, string collectionName, string userId) { User user; if (userId == null) { // Anonymous user - Sandbox collection etc. user = _storage.Users.Where(candidate => candidate.NameIdentifier == null).FirstOrDefault(); } else { // Beta content user = _storage.Users.Where(candidate => candidate.NameIdentifier == userId).FirstOrDefault(); } if (user == null) { user = new User { Id = Guid.NewGuid(), NameIdentifier = userId }; user.DisplayName = (userId == null) ? _defaultUserName : userId; } // Load Collection Collection collection = new Collection(); collection.Title = collectionName; collection.Id = CollectionIdFromSuperCollection(superCollectionName, collectionName); collection.User = user; // Load SuperCollection SuperCollection superCollection = _storage.SuperCollections.FirstOrDefault(candidate => candidate.Title == superCollectionName); if (superCollection == null) { superCollection = new SuperCollection(); superCollection.Title = superCollectionName; superCollection.Id = CollectionIdFromText(superCollectionName); superCollection.User = user; _storage.SuperCollections.Add(superCollection); } if (superCollection.Collections == null) { superCollection.Collections = new System.Collections.ObjectModel.Collection <Collection>(); } superCollection.Collections.Add(collection); return(collection); }
protected static Uri UrlForCollection(SuperCollection superCollection, Collection collection) { if (superCollection == null || collection == null || string.IsNullOrEmpty(superCollection.Title) || string.IsNullOrEmpty(collection.Title)) { return(null); } string path = _hostPath.Value + "/" + FriendlyUrl.FriendlyUrlEncode(superCollection.Title) + "/"; if (string.CompareOrdinal(superCollection.Title, collection.Title) != 0) { path += FriendlyUrl.FriendlyUrlEncode(collection.Title) + "/"; } return(new Uri(path)); }
private Collection LoadCollections(string superCollectionName, string collectionName, string userId) { User user = (userId == null ? _storage.Users.FirstOrDefault(candidate => candidate.NameIdentifier == null) : _storage.Users.FirstOrDefault(candidate => candidate.NameIdentifier == userId)) ?? new User { Id = Guid.NewGuid(), NameIdentifier = userId, DisplayName = userId ?? _defaultUser }; // load collection var collection = new Collection { Title = collectionName, Id = CollectionIdFromSuperCollection(superCollectionName, collectionName), User = user }; // load supercollection SuperCollection superCollection = _storage.SuperCollections.FirstOrDefault(candidate => candidate.Title == superCollectionName); if (superCollection == null) { superCollection = new SuperCollection { Title = superCollectionName, Id = CollectionIdFromText(superCollectionName), User = user }; _storage.SuperCollections.Add(superCollection); } if (superCollection.Collections == null) { superCollection.Collections = new System.Collections.ObjectModel.Collection <Collection>(); } superCollection.Collections.Add(collection); return(collection); }
public string ImportCollection ( Guid collectionId, string collectionTitle, string collectionTheme, List <FlatTimeline> timelines, List <Tour> tours, bool makeDefault = false, bool forcePublic = false, bool keepOldGuids = false, string forceUserDisplayName = null ) { int titleCount = 1; string titleAppend = ""; string path = Regex.Replace(collectionTitle.Trim(), @"[^A-Za-z0-9\-]+", "").ToLower(); string type; Guid GUID; Guid newGUID; Dictionary <Guid, Guid> newGUIDs = new Dictionary <Guid, Guid>(); DateTime timestamp = DateTime.UtcNow; User user = _user; // ensure user logged in or provided in override if (forceUserDisplayName != null) { user = _storage.Users.Where(u => u.DisplayName == forceUserDisplayName).FirstOrDefault(); } if (user == null) { return("In order to import a collection, you must first be logged in."); } // ensure collection title is unique for user while ( _storage.Collections.Where(c => c.User.Id == user.Id && c.Path == path + titleAppend).FirstOrDefault() != null ) { titleCount++; titleAppend = "-" + titleCount; } if ((path + titleAppend).Length > 50) { return("Either the name of the collection to be imported is too long, or a collecton with this name already exists."); } // create new collection under existing user's supercollection SuperCollection superCollection = _storage.SuperCollections.Where(s => s.User.Id == user.Id).Include("User").Include("Collections").FirstOrDefault(); if (superCollection == null) { return("The collection could not be imported as you are not properly logged in. Please log out then log back in again first."); } Collection collection = new Collection { Id = keepOldGuids ? collectionId : Guid.NewGuid(), SuperCollection = superCollection, Default = makeDefault, Path = path + titleAppend, Title = collectionTitle.Trim() + titleAppend, Theme = collectionTheme == "" ? null : collectionTheme, MembersAllowed = false, Members = null, PubliclySearchable = forcePublic, User = user }; superCollection.Collections.Add(collection); // iterate through each timeline foreach (FlatTimeline flat in timelines) { // keep cross-reference between old and new timelineIds so child timelines can maintain a pointer to their parents if (!keepOldGuids) { newGUID = Guid.NewGuid(); newGUIDs.Add(flat.timeline.Id, newGUID); flat.timeline.Id = newGUID; if (flat.parentTimelineId != null) // not root timeline { flat.parentTimelineId = newGUIDs[(Guid)flat.parentTimelineId]; } } flat.timeline.Collection = collection; // iterate through each exhibit in the timeline - change last updated to now with current user for (int eachExhibit = 0; eachExhibit < flat.timeline.Exhibits.Count; eachExhibit++) { // also keep cross-references for exhibits if new GUIDs are assigned if (!keepOldGuids) { newGUID = Guid.NewGuid(); newGUIDs.Add(flat.timeline.Exhibits[eachExhibit].Id, newGUID); flat.timeline.Exhibits[eachExhibit].Id = newGUID; } flat.timeline.Exhibits[eachExhibit].Collection = collection; flat.timeline.Exhibits[eachExhibit].UpdatedBy = user; flat.timeline.Exhibits[eachExhibit].UpdatedTime = timestamp; // iterate through each content item in the exhibit for (int eachItem = 0; eachItem < flat.timeline.Exhibits[eachExhibit].ContentItems.Count; eachItem++) { // also keep cross-references for content items if new GUIDs are assigned if (!keepOldGuids) { newGUID = Guid.NewGuid(); newGUIDs.Add(flat.timeline.Exhibits[eachExhibit].ContentItems[eachItem].Id, newGUID); flat.timeline.Exhibits[eachExhibit].ContentItems[eachItem].Id = newGUID; } flat.timeline.Exhibits[eachExhibit].ContentItems[eachItem].Collection = collection; } } // add timeline to database if (flat.parentTimelineId == null) { _storage.Timelines.Add(flat.timeline); } else { Timeline parentTimeline = _storage.Timelines.Where(t => t.Id == flat.parentTimelineId) .Include("ChildTimelines.Exhibits.ContentItems") .FirstOrDefault(); parentTimeline.ChildTimelines.Add(flat.timeline); } // commit creations try { _storage.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } return("Sorry, we were unable to fully import this collection due to an unexpected error, but it may have partially been imported. Please click on My Collections to check."); } } // iterate through each tour foreach (Tour tour in tours) { tour.Collection = collection; // if using new GUIDs, create new ids for tour components and replace url guid parts using old to new GUID mapping if (!keepOldGuids) { tour.Id = Guid.NewGuid(); // iterate through each tour stop for (int eachBookmark = 0; eachBookmark < tour.Bookmarks.Count; eachBookmark++) { tour.Bookmarks[eachBookmark].Id = Guid.NewGuid(); // breakdown each tour stop url segment into parts string[] parts = tour.Bookmarks[eachBookmark].Url.Split('/'); // iterate through each part, replacing the GUID fraction of the part with the new GUID that was mapped earlier during the timelines population for (int eachPart = 1; eachPart < parts.Length; eachPart++) { if (parts[eachPart].Length == 37) { // part has two components type = parts[eachPart].Substring(0, 1); // first character in part is type GUID = new Guid(parts[eachPart].Remove(0, 1)); // rest of part is GUID } else { // part has one component type = ""; GUID = new Guid(parts[eachPart]); // entire part is GUID } GUID = newGUIDs[GUID]; // lookup the earlier mapped GUID parts[eachPart] = type + GUID.ToString(); // and replace the old GUID with mapped } // rejoin all the tour stop url segment parts back together tour.Bookmarks[eachBookmark].Url = String.Join("/", parts); } } // add the tour _storage.Tours.Add(tour); // commit tour to database try { _storage.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } return("We imported all of the timelines, exhibits and content items but we wre unable to fully import tours. Please click on My Collections to check."); } } return("The collection has been imported as \"" + collection.Title + "\". Please click on \"My Collections\" in order to see your new collection."); }
public void LoadDataFromDump(string curatorDisplayName, string collectionTitle, string collectionDumpFile, string toursDumpFile, bool curatorsDefaultCollection = true, bool replaceGUIDs = false) { // remove any excess spaces from display names curatorDisplayName = curatorDisplayName.Trim(); collectionTitle = collectionTitle.Trim(); // generate equivalent path names (uniqueness is assumed) string superCollectionPath = Regex.Replace(curatorDisplayName, @"[^A-Za-z0-9]+", "").ToLower(); // Aa-Zz and 0-9 only, converted to lower case. string collectionPath = Regex.Replace(collectionTitle, @"[^A-Za-z0-9]+", "").ToLower(); // Aa-Zz and 0-9 only, converted to lower case. // get curator's user record or create if doesn't exist User user = _storage.Users.Where(u => u.DisplayName == curatorDisplayName).FirstOrDefault(); if (user == null) { user = new User { Id = Guid.NewGuid(), DisplayName = curatorDisplayName, IdentityProvider = "Populated from JSON" }; _storage.Users.Add(user); } // get curator's supercollection record or create if doesn't exist SuperCollection superCollection = _storage.SuperCollections.Include("Collections").Where(s => s.Title == superCollectionPath).FirstOrDefault(); if (superCollection == null) { superCollection = new SuperCollection { Id = Guid.NewGuid(), Title = superCollectionPath, User = user, Collections = new System.Collections.ObjectModel.Collection <Collection>() }; _storage.SuperCollections.Add(superCollection); } // create new collection Collection collection = new Collection { Id = Guid.NewGuid(), Default = curatorsDefaultCollection, PubliclySearchable = true, Title = collectionTitle, Path = collectionPath, SuperCollection = superCollection, User = user }; superCollection.Collections.Add(collection); // populate collection from json files using (Stream jsonTimelines = File.OpenRead(_jsonDirectory + collectionDumpFile)) using (Stream jsonTours = toursDumpFile == null ? null : File.OpenRead(_jsonDirectory + toursDumpFile)) { LoadData(jsonTimelines, jsonTours, collection, replaceGUIDs); } // commit db changes _storage.SaveChanges(); }
/// <summary> /// It's possible to feed files created by the Export Collection menu item to this function. /// This function uses the same mechanism as the Import Collection menu item, the only differences being: /// 1) Instead of using the currently logged in user, we use the curatorDisplayName. /// 2) If this user doesn't exist, we create the user. /// 3) If the user doesn't have a supercollection, we create the supercollection. /// 4) We can define if the collection is the default collection here. (A pre-existing default collection for the user must not exist.) /// This function can therefore be used to seed the database with the Cosmos collection or other collections when the database is first created. /// </summary> public void ImportCollection(string curatorDisplayName, string collectionTitle, string jsonFile, bool curatorsDefaultCollection = true, bool forcePublic = true, bool keepOldGUIDs = true) { bool dirty = false; // parse json file contents first in case invalid flat collection string jsonStringified = File.ReadAllText(_jsonDirectory + jsonFile); ExportImport.FlatCollection collectionTree = JsonConvert.DeserializeObject <ExportImport.FlatCollection>(jsonStringified); // remove any excess spaces from display names curatorDisplayName = curatorDisplayName.Trim(); collectionTitle = collectionTitle.Trim(); // generate equivalent path names (uniqueness is assumed) string superCollectionPath = Regex.Replace(curatorDisplayName, @"[^A-Za-z0-9\-]+", "").ToLower(); // Aa-Zz, 0-9 and hyphen only, converted to lower case. string collectionPath = Regex.Replace(collectionTitle, @"[^A-Za-z0-9\-]+", "").ToLower(); // Aa-Zz, 0-9 and hyphen only, converted to lower case. // get curator's user record or create if doesn't exist User user = _storage.Users.Where(u => u.DisplayName == curatorDisplayName).FirstOrDefault(); if (user == null) { user = new User { Id = Guid.NewGuid(), DisplayName = curatorDisplayName, IdentityProvider = "Populated from JSON" }; _storage.Users.Add(user); dirty = true; } // get curator's supercollection record or create if doesn't exist SuperCollection superCollection = _storage.SuperCollections.Include("Collections").Where(s => s.Title == superCollectionPath).FirstOrDefault(); if (superCollection == null) { superCollection = new SuperCollection { Id = Guid.NewGuid(), Title = superCollectionPath, User = user, Collections = new System.Collections.ObjectModel.Collection <Collection>() }; _storage.SuperCollections.Add(superCollection); dirty = true; } // commit any user or supercollection creations to db if (dirty) { _storage.SaveChanges(); } // use the standard export/import mechanism to import collection Console.WriteLine ( _commonImport.ImportCollection ( collectionId: collectionTree.collection.Id, collectionTitle: collectionTitle, collectionTheme: collectionTree.collection.Theme, timelines: collectionTree.timelines, tours: collectionTree.tours, makeDefault: curatorsDefaultCollection, forcePublic: forcePublic, keepOldGuids: keepOldGUIDs, forceUserDisplayName: curatorDisplayName ) ); }