/// <summary> /// Verify there is a MIME type/gallery mapping for the current gallery for every MIME type, creating any /// if necessary. /// </summary> /// <returns><c>true</c> if data was changed that necessitates reloading data from the data store, <c>false</c> otherwise.</returns> private bool ConfigureMimeTypeGalleryTable() { var defaultEnabledExtensions = new List <string> { ".jpg", ".jpeg" }; var needToClearCache = false; using (var repoMt = new MimeTypeRepository()) { using (var repoMtg = new MimeTypeGalleryRepository()) { // Get MIME types that don't have a match in the MIME Type Gallery table foreach (var mtDto in repoMt.Where(mt => mt.MimeTypeGalleries.All(mtg => mtg.FKGalleryId != GalleryId))) { repoMtg.Add(new MimeTypeGalleryDto() { FKGalleryId = GalleryId, FKMimeTypeId = mtDto.MimeTypeId, IsEnabled = defaultEnabledExtensions.Contains(mtDto.FileExtension) }); needToClearCache = true; } repoMtg.Save(); } } return(needToClearCache); }
/// <summary> /// Create a new MimeType and MimeTypeGallery record for this instance. /// </summary> /// <exception cref="InvalidOperationException">Thrown when <see cref="MimeTypeId" /> is any value other than <see cref="Int32.MinValue" /></exception> private void SaveNew() { if (MimeTypeId != int.MinValue) { throw new InvalidOperationException("Cannot call SaveNew when the MIME type already exists in the data store."); } int mimeTypeId; using (var repo = new MimeTypeRepository()) { var mimeTypeDto = new MimeTypeDto() { FileExtension = Extension, MimeTypeValue = FullType, BrowserMimeTypeValue = BrowserMimeType }; repo.Add(mimeTypeDto); repo.Save(); mimeTypeId = mimeTypeDto.MimeTypeId; } using (var repo = new MimeTypeGalleryRepository()) { repo.Add(new MimeTypeGalleryDto() { FKGalleryId = GalleryId, FKMimeTypeId = mimeTypeId, IsEnabled = AllowAddToGallery }); repo.Save(); } // Clear the gallery cache. This will eventually trigger Gallery.Configure(), which will ensure that all galleries have a MimeTypeGallery record for // this MIME type. Factory.ClearGalleryCache(); }
/// <summary> /// Updates the data store records with the values of this instance. /// </summary> private void SaveExisting() { if (MimeTypeId == int.MinValue) { throw new InvalidOperationException("Cannot call SaveExisting for new MIME types."); } using (var repo = new MimeTypeRepository()) { var mtDto = repo.Find(MimeTypeId); if (mtDto != null && mtDto.MimeTypeValue != FullType) { mtDto.MimeTypeValue = FullType; repo.Save(); } } using (var repo = new MimeTypeGalleryRepository()) { var mtDto = repo.Find(MimeTypeGalleryId); if (mtDto != null && mtDto.IsEnabled != AllowAddToGallery) { mtDto.IsEnabled = AllowAddToGallery; repo.Save(); } } }
/// <summary> /// Persist the gallery-specific properties of this instance to the data store. Currently, only the <see cref="IMimeType.AllowAddToGallery" /> /// property is unique to the gallery identified in <see cref="IMimeType.GalleryId" />; the other properties are application-wide and at /// present there is no API to modify them. In other words, this method saves whether a particular MIME type is enabled or disabled for a /// particular gallery. /// </summary> /// <exception cref="InvalidOperationException">Thrown when the current instance is an application-level MIME type. Only gallery-specific /// MIME types can be persisted to the data store. Specifically, the exception is thrown when <see cref="IMimeType.GalleryId" /> or /// <see cref="IMimeType.MimeTypeGalleryId" /> is <see cref="Int32.MinValue" />.</exception> public void Save() { if ((GalleryId == int.MinValue) || (MimeTypeGalleryId == int.MinValue)) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "Cannot save. This MIME type instance is an application-level MIME type and cannot be persisted to the data store. Only gallery-specific MIME types can be saved. (GalleryId={0}, MimeTypeId={1}, MimeTypeGalleryId={2}, FileExtension={3}", GalleryId, MimeTypeId, MimeTypeGalleryId, Extension)); } //Factory.GetDataProvider().MimeType_Save(this); using (var repo = new MimeTypeGalleryRepository()) { var mtDto = repo.Find(MimeTypeGalleryId); if (mtDto != null) { mtDto.IsEnabled = AllowAddToGallery; repo.Save(); } } }
/// <summary> /// Updates the <paramref name="baseMimeTypes" /> with configuration data for the <paramref name="galleryId" />. /// Returns <c>true</c> when at least one record in the <see cref="MimeTypeGalleryDto" /> table exists for the /// <paramref name="galleryId" />; otherwise returns <c>false</c>. /// </summary> /// <param name="baseMimeTypes">A collection of MIME types to be updated with gallery-specific data.</param> /// <param name="galleryId">The gallery ID.</param> /// <returns>Returns <c>true</c> when at least one record in the <see cref="MimeTypeGalleryDto" /> table exists for the /// <paramref name="galleryId" />; otherwise returns <c>false</c>.</returns> private static bool ConfigureMimeTypesForGallery(IMimeTypeCollection baseMimeTypes, int galleryId) { //IMimeTypeCollection baseMimeTypes = LoadMimeTypes(Int32.MinValue); //IMimeTypeCollection newMimeTypes = new MimeTypeCollection(); var mediaTemplates = Factory.LoadMediaTemplates(); var foundRows = false; using (var repo = new MimeTypeGalleryRepository()) { foreach (var mtgDto in repo.Where(m => m.FKGalleryId == galleryId, m => m.MimeType)) { foundRows = true; var mimeType = baseMimeTypes.Find(mtgDto.MimeType.FileExtension); if (mimeType == null) { throw new BusinessException(String.Format(CultureInfo.CurrentCulture, "Could not find a IMimeType with file extension \"{0}\" in the list of base MIME types.", mtgDto.MimeType.FileExtension)); } mimeType.GalleryId = galleryId; mimeType.MimeTypeGalleryId = mtgDto.MimeTypeGalleryId; mimeType.AllowAddToGallery = mtgDto.IsEnabled; // Populate the media template collection. mimeType.MediaTemplates.AddRange(mediaTemplates.Find(mimeType)); // Validate the media templates. There may not be any, which is OK (for example, there isn't one defined for 'application/msword'). // But if there *IS* one defined, there must be one with a browser ID of "default". if ((mimeType.MediaTemplates.Count > 0) && (mimeType.MediaTemplates.Find("default") == null)) { throw new BusinessException(String.Format(CultureInfo.CurrentCulture, "No default media template. Could not find a media template for MIME type \"{0}\" or \"{1}\" with browser ID = \"default\".", mimeType.FullType, String.Concat(mimeType.MajorType, "/*"))); } } } return(foundRows); }
/// <summary> /// Updates the <paramref name="baseMimeTypes" /> with configuration data for the <paramref name="galleryId" />. /// Returns <c>true</c> when at least one record in the <see cref="MimeTypeGalleryDto" /> table exists for the /// <paramref name="galleryId" />; otherwise returns <c>false</c>. /// </summary> /// <param name="baseMimeTypes">A collection of MIME types to be updated with gallery-specific data.</param> /// <param name="galleryId">The gallery ID.</param> /// <returns>Returns <c>true</c> when at least one record in the <see cref="MimeTypeGalleryDto" /> table exists for the /// <paramref name="galleryId" />; otherwise returns <c>false</c>.</returns> private static bool ConfigureMimeTypesForGallery(IMimeTypeCollection baseMimeTypes, int galleryId) { //IMimeTypeCollection baseMimeTypes = LoadMimeTypes(Int32.MinValue); //IMimeTypeCollection newMimeTypes = new MimeTypeCollection(); var mediaTemplates = Factory.LoadMediaTemplates(); var foundRows = false; using (var repo = new MimeTypeGalleryRepository()) { foreach (var mtgDto in repo.Where(m => m.FKGalleryId == galleryId, m => m.MimeType)) { foundRows = true; var mimeType = baseMimeTypes.Find(mtgDto.MimeType.FileExtension); if (mimeType == null) { throw new BusinessException(String.Format(CultureInfo.CurrentCulture, "Could not find a IMimeType with file extension \"{0}\" in the list of base MIME types.", mtgDto.MimeType.FileExtension)); } mimeType.GalleryId = galleryId; mimeType.MimeTypeGalleryId = mtgDto.MimeTypeGalleryId; mimeType.AllowAddToGallery = mtgDto.IsEnabled; // Populate the media template collection. mimeType.MediaTemplates.AddRange(mediaTemplates.Find(mimeType)); // Validate the media templates. There may not be any, which is OK (for example, there isn't one defined for 'application/msword'). // But if there *IS* one defined, there must be one with a browser ID of "default". if ((mimeType.MediaTemplates.Count > 0) && (mimeType.MediaTemplates.Find("default") == null)) { throw new BusinessException(String.Format(CultureInfo.CurrentCulture, "No default media template. Could not find a media template for MIME type \"{0}\" or \"{1}\" with browser ID = \"default\".", mimeType.FullType, String.Concat(mimeType.MajorType, "/*"))); } } } return foundRows; }
/// <summary> /// Persist the gallery-specific properties of this instance to the data store. Currently, only the <see cref="IMimeType.AllowAddToGallery" /> /// property is unique to the gallery identified in <see cref="IMimeType.GalleryId" />; the other properties are application-wide and at /// present there is no API to modify them. In other words, this method saves whether a particular MIME type is enabled or disabled for a /// particular gallery. /// </summary> /// <exception cref="InvalidOperationException">Thrown when the current instance is an application-level MIME type. Only gallery-specific /// MIME types can be persisted to the data store. Specifically, the exception is thrown when <see cref="IMimeType.GalleryId" /> or /// <see cref="IMimeType.MimeTypeGalleryId" /> is <see cref="Int32.MinValue" />.</exception> public void Save() { if ((GalleryId == int.MinValue) || (MimeTypeGalleryId == int.MinValue)) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "Cannot save. This MIME type instance is an application-level MIME type and cannot be persisted to the data store. Only gallery-specific MIME types can be saved. (GalleryId={0}, MimeTypeId={1}, MimeTypeGalleryId={2}, FileExtension={3}", GalleryId, MimeTypeId, MimeTypeGalleryId, Extension)); } //Factory.GetDataProvider().MimeType_Save(this); using (var repo = new MimeTypeGalleryRepository()) { var mtDto = repo.Find(MimeTypeGalleryId); if (mtDto != null) { mtDto.IsEnabled = AllowAddToGallery; repo.Save(); } } }
/// <summary> /// Verify there is a MIME type/gallery mapping for the current gallery for every MIME type, creating any /// if necessary. /// </summary> private void ConfigureMimeTypeGalleryTable() { var defaultEnabledExtensions = new List<string> { ".jpg", ".jpeg" }; using (var repoMt = new MimeTypeRepository()) { using (var repoMtg = new MimeTypeGalleryRepository()) { // Get MIME types that don't have a match in the MIME Type Gallery table foreach (var mtDto in repoMt.Where(mt => mt.MimeTypeGalleries.All(mtg => mtg.FKGalleryId != GalleryId))) { repoMtg.Add(new MimeTypeGalleryDto() { FKGalleryId = GalleryId, FKMimeTypeId = mtDto.MimeTypeId, IsEnabled = defaultEnabledExtensions.Contains(mtDto.FileExtension) }); } repoMtg.Save(); } } }