/// <summary> /// Verify there is a UI template/album mapping for the root album in the current gallery, creating them /// if necessary. /// </summary> /// <param name="rootAlbum">The root album.</param> private static void ConfigureUiTemplateAlbumTable(AlbumDto rootAlbum) { using (var repoUiTmpl = new UiTemplateRepository()) { using (var repoUiTmplA = new UiTemplateAlbumRepository(repoUiTmpl.Context)) { // Make sure each template category has at least one template assigned to the root album. // We do this with a union of two queries: // 1. For categories where there is at least one album assignment, determine if at least one of // those assignments is the root album. // 2. Find categories without any albums at all (this is necessary because the SelectMany() in the first // query won't return any categories that don't have related records in the template/album table). var dtos = repoUiTmpl.Where(t => t.FKGalleryId == rootAlbum.FKGalleryId) .SelectMany(t => t.TemplateAlbums, (t, tt) => new { t.TemplateType, tt.FKAlbumId }) .GroupBy(t => t.TemplateType) .Where(t => t.All(ta => ta.FKAlbumId != rootAlbum.AlbumId)) .Select(t => t.Key) .Union(repoUiTmpl.Where(t => t.FKGalleryId == rootAlbum.FKGalleryId).GroupBy(t => t.TemplateType).Where(t => t.All(t2 => !t2.TemplateAlbums.Any())).Select(t => t.Key)) ; foreach (var dto in dtos) { // We have a template type without a root album. Find the default template and assign that one. var dto1 = dto; repoUiTmplA.Add(new UiTemplateAlbumDto() { FKUiTemplateId = repoUiTmpl.Where(t => t.FKGalleryId == rootAlbum.FKGalleryId && t.TemplateType == dto1 && t.Name.Equals("default", StringComparison.OrdinalIgnoreCase)).First().UiTemplateId, FKAlbumId = rootAlbum.AlbumId }); } repoUiTmplA.Save(); } } }
/// <summary> /// Gets a collection of all UI templates from the data store. Returns an empty collection if no /// items exist. /// </summary> /// <returns>Returns a collection of all UI templates from the data store.</returns> public static IUiTemplateCollection GetUiTemplates() { IUiTemplateCollection tmpl = new UiTemplateCollection(); using (var repo = new UiTemplateRepository()) { foreach (var jDto in repo.GetAll(j => j.TemplateAlbums)) { IUiTemplate t = new UiTemplate { UiTemplateId = jDto.UiTemplateId, TemplateType = jDto.TemplateType, GalleryId = jDto.FKGalleryId, Name = jDto.Name, Description = jDto.Description, HtmlTemplate = jDto.HtmlTemplate, ScriptTemplate = jDto.ScriptTemplate }; t.RootAlbumIds.AddRange(from r in jDto.TemplateAlbums select r.FKAlbumId); tmpl.Add(t); } } return(tmpl); }
/// <summary> /// Verify there are UI templates for the current gallery that match every UI template associated with /// the template gallery, creating any if necessary. /// </summary> private void ConfigureUiTemplateTable() { using (var repoUiTmpl = new UiTemplateRepository()) { var ctx = repoUiTmpl.Context; repoUiTmpl.Load(); // Get the UI templates belonging to the template gallery. We have to do a join here because the data // model doesn't have a relationship. (Doing so would conflict with the relationship between // the UITemplateAlbum and Album tables.) var tmplForTmplGallery = from uiTmpl in ctx.UiTemplates join g in ctx.Galleries on uiTmpl.FKGalleryId equals g.GalleryId where g.IsTemplate select uiTmpl; // For each UI template, make sure one exists in the gallery foreach (var uiTmpl in tmplForTmplGallery) { if (!repoUiTmpl.Local.Any(ui => ui.TemplateType == uiTmpl.TemplateType && ui.FKGalleryId == GalleryId && ui.Name == uiTmpl.Name)) { // We need to add a UI template repoUiTmpl.Add(new UiTemplateDto() { TemplateType = uiTmpl.TemplateType, FKGalleryId = GalleryId, Name = uiTmpl.Name, Description = uiTmpl.Description, HtmlTemplate = uiTmpl.HtmlTemplate, ScriptTemplate = uiTmpl.ScriptTemplate }); } } repoUiTmpl.Save(); } }
/// <summary> /// Persist this UI template object to the data store. /// </summary> public void Save() { using (var repo = new UiTemplateRepository()) { repo.Save(this); } CacheController.RemoveCache(CacheItem.UiTemplates); }
/// <summary> /// Persist this UI template object to the data store. /// </summary> public void Save() { using (var repo = new UiTemplateRepository()) { repo.Save(this); } Factory.ClearAllCaches(); }
/// <summary> /// Deletes the UI templates associated with the current gallery. /// </summary> private void DeleteUiTemplates() { using (var repo = new UiTemplateRepository()) { foreach (var dto in repo.Where(m => m.FKGalleryId == GalleryId)) { repo.Delete(dto); } repo.Save(); } }
/// <summary> /// Permanently delete the current UI template from the data store. This action cannot be undone. /// </summary> public void Delete() { using (var repo = new UiTemplateRepository()) { var uiTmplDto = repo.Find(UiTemplateId); if (uiTmplDto != null) { repo.Delete(uiTmplDto); repo.Save(); } } CacheController.RemoveCache(CacheItem.UiTemplates); }
/// <summary> /// Permanently delete the current UI template from the data store. This action cannot be undone. /// </summary> public void Delete() { using (var repo = new UiTemplateRepository()) { var uiTmplDto = repo.Find(UiTemplateId); if (uiTmplDto != null) { repo.Delete(uiTmplDto); repo.Save(); } } Factory.ClearAllCaches(); }
/// <summary> /// Update the UITemplate table to work with the new data. We don't have any templates from a previous version, but we have the current ones for 3 in /// the database, so what we do is find the UI templates that map to the template gallery and update the FKGalleryId value to the template gallery ID /// in the data we are importing, then we'll delete any UI templates not belonging to the template gallery. /// </summary> /// <param name="ds">The DataSet.</param> /// <param name="dataStore">The data store currently being used for gallery data.</param> /// <param name="cn">The connection.</param> /// <param name="tran">The transaction.</param> private static void MigrateUiTemplates(DataSet ds, ProviderDataStore dataStore, IDbConnection cn, IDbTransaction tran) { // Get the template gallery ID for the gallery we'll be importing. At this moment this info is in the dataset because we haven't yet // imported it into the table. var tmplGalleryId = Convert.ToInt32(ds.Tables["Gallery"].Select("IsTemplate = true").First()["GalleryId"]); using (var cmd = cn.CreateCommand()) { // Get the UI templates belonging to the template gallery. We have to do a join here because the data // model doesn't have a relationship. (Doing so would conflict with the relationship between // the UITemplateAlbum and Album tables.) var uiTmplTableName = Utils.GetSqlName("UiTemplate", dataStore); using (var repo = new UiTemplateRepository()) { var ctx = repo.Context; var tmplForTmplGallery = from uiTmpl in ctx.UiTemplates join g in ctx.Galleries on uiTmpl.FKGalleryId equals g.GalleryId where g.IsTemplate select uiTmpl; // For each UI template, make sure one exists in the gallery cmd.Transaction = tran; foreach (var uiTmpl in tmplForTmplGallery) { var sql = String.Format(CultureInfo.InvariantCulture, "UPDATE {0} SET FKGalleryId = {1} WHERE UiTemplateId = {2};", uiTmplTableName, tmplGalleryId, uiTmpl.UiTemplateId); cmd.CommandText = sql; cmd.ExecuteNonQuery(); } } using (var cmdDr = cn.CreateCommand()) { cmdDr.Transaction = tran; cmdDr.CommandText = String.Format(CultureInfo.InvariantCulture, "SELECT UiTemplateId FROM {0} WHERE FKGalleryId <> {1};", uiTmplTableName, tmplGalleryId); using (var dr = cmdDr.ExecuteReader()) { while (dr != null && dr.Read()) { var sql = String.Format(CultureInfo.InvariantCulture, "DELETE FROM {0} WHERE UiTemplateId = {1};", uiTmplTableName, dr[0]); cmd.CommandText = sql; cmd.ExecuteNonQuery(); } } } } }
/// <summary> /// Gets a collection of all UI templates from the data store. Returns an empty collection if no /// items exist. /// </summary> /// <returns>Returns a collection of all UI templates from the data store.</returns> public static IUiTemplateCollection GetUiTemplates() { IUiTemplateCollection tmpl = new UiTemplateCollection(); using (var repo = new UiTemplateRepository()) { foreach (var jDto in repo.GetAll(j => j.TemplateAlbums)) { IUiTemplate t = new UiTemplate { UiTemplateId = jDto.UiTemplateId, TemplateType = jDto.TemplateType, GalleryId = jDto.FKGalleryId, Name = jDto.Name, Description = jDto.Description, HtmlTemplate = jDto.HtmlTemplate, ScriptTemplate = jDto.ScriptTemplate }; t.RootAlbumIds.AddRange(from r in jDto.TemplateAlbums select r.FKAlbumId); tmpl.Add(t); } } return tmpl; }