public CarouselSlide ReadSlide(long tenantId, long elementId, long carouselSlideId, IUnitOfWork unitOfWork = null) { IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork); try { CarouselSlide slide = null; string sql = _sqlManager.GetSql("Sql.ReadCarouselSlide.sql"); dbm.SetSQL(sql); dbm.AddParameter("@TenantId", FieldType.BigInt, tenantId); dbm.AddParameter("@ElementId", FieldType.BigInt, elementId); dbm.AddParameter("@CarouselSlideId", FieldType.BigInt, carouselSlideId); dbm.ExecuteReader(); if (dbm.Read()) { slide = GetCarouselSlideFromDatabaseManager(dbm); } return(slide); } finally { if (unitOfWork == null) { dbm.Dispose(); } } }
public void ValidateSlide(CarouselSlide slide, string keyPrefix = null) { // Check that image specified if (slide.ImageTenantId == 0 || slide.ThumbnailImageUploadId == 0 || slide.PreviewImageUploadId == 0 || slide.ImageUploadId == 0) { throw new ValidationErrorException(new ValidationError(CarouselPropertyNames.Image, ElementResource.CarouselImageRequiredMessage, keyPrefix)); } // If page text specified, then a page must also be specified if (!string.IsNullOrWhiteSpace(slide.PageText) && slide.PageId == null) { throw new ValidationErrorException(new ValidationError(CarouselPropertyNames.PageText, ElementResource.CarouselPageRequiredMessage, keyPrefix)); } }
private string PostSlideForm(Form form, long tenantId, long pageId, long elementId) { // Get carousel slide details long carouselSlideId = Convert.ToInt64(form.Data); string uploadIds = ((UploadField)form.Fields["upload"]).Value; string pageValue = ((SelectListField <string>)form.Fields["page"]).Value; CarouselSlide slide = new CarouselSlide { TenantId = tenantId, ElementId = elementId, CarouselSlideId = carouselSlideId, Name = ((TextField)form.Fields["name"]).Value, Description = ((MultiLineTextField)form.Fields["description"]).Value, PageText = ((TextField)form.Fields["pageText"]).Value, PageId = pageValue == string.Empty ? null : (long?)Convert.ToInt64(pageValue), PageTenantId = pageValue == string.Empty ? null : (long?)tenantId }; if (!string.IsNullOrWhiteSpace(uploadIds)) { string[] uploadParts = uploadIds.Split('|'); slide.ImageTenantId = tenantId; slide.ThumbnailImageUploadId = Convert.ToInt64(uploadParts[0]); slide.PreviewImageUploadId = Convert.ToInt64(uploadParts[1]); slide.ImageUploadId = Convert.ToInt64(uploadParts[2]); } // Validate supplied data _carouselValidator.ValidateSlide(slide); // Determine whether or not there are uncommitted slide images bool uncommittedSlideImages = true; if (carouselSlideId > 0) { ICarouselService carouselService = (ICarouselService)_elementFactory.GetElementService(FormId); CarouselSlide currentSlide = carouselService.ReadSlide(tenantId, elementId, carouselSlideId); uncommittedSlideImages = currentSlide.ImageUploadId != slide.ImageUploadId; } // Get slide view model and return it's JSON representation as form result status CarouselSlideViewModel slideViewModel = GetSlideViewModel(slide, uncommittedSlideImages); return(JsonConvert.SerializeObject(slideViewModel)); }
public Upload ReadUpload(long tenantId, long elementId, long uploadId, string format, IUnitOfWork unitOfWork = null) { CarouselSlide slide = _carouselRepository.ReadSlide(tenantId, elementId, uploadId, unitOfWork); if (slide == null) { return(null); } switch (format) { case "preview": return((Image)_uploadService.Read(slide.ImageTenantId, slide.PreviewImageUploadId, GetCarouselSlideStorageHierarchy(elementId), unitOfWork)); case "thumbnail": return((Image)_uploadService.Read(slide.ImageTenantId, slide.ThumbnailImageUploadId, GetCarouselSlideStorageHierarchy(elementId), unitOfWork)); default: return(null); } }
/// <summary> /// TODO: Use proper URL routing here, do not hard wire these URLs. /// </summary> /// <param name="slide">Carousel slide.</param> /// <param name="uncommittedSlideImages">True if slide's image has changed and there are uncommitted image uploads, false if slide's image has not changed.</param> private CarouselSlideViewModel GetSlideViewModel(CarouselSlide slide, bool uncommittedSlideImages) { string imageUrl = null; if (uncommittedSlideImages) { imageUrl = string.Format("/uploads/{0}?t={0}", slide.ThumbnailImageUploadId); } else { imageUrl = string.Format("/elements/{0}/uploads/{1}?format=thumbnail&t={1}", slide.ElementId, slide.CarouselSlideId); } CarouselSlideViewModel slideViewModel = new CarouselSlideViewModel { CarouselSlideId = slide.CarouselSlideId.ToString(), ThumbnailImageUploadId = slide.ThumbnailImageUploadId.ToString(), PreviewImageUploadId = slide.PreviewImageUploadId.ToString(), ImageUploadId = slide.ImageUploadId.ToString(), Name = slide.Name, Description = slide.Description, PageId = slide.PageId == null ? string.Empty : slide.PageId.ToString(), PageText = slide.PageText, ImageUrl = imageUrl }; if (slide.PageId.HasValue) { if (string.IsNullOrWhiteSpace(slide.PageText)) { slideViewModel.ButtonText = _pageService.Read(slide.TenantId, slide.PageId.Value).Name; } else { slideViewModel.ButtonText = slide.PageText; } slideViewModel.ButtonUrl = "/pages/" + slide.PageId.Value; } return(slideViewModel); }
public void Update(IElementSettings settings, IUnitOfWork unitOfWork = null) { // If we don't have a unit of work in place, create one now so that we can rollback all changes in case of failure IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null; // Begin work try { // Validate slides CarouselSettings carouselSettings = (CarouselSettings)settings; foreach (CarouselSlide slide in carouselSettings.Slides) { _carouselValidator.ValidateSlide(slide); } // Get current carousel settings CarouselSettings currentCarouselSettings = (CarouselSettings)New(settings.TenantId); currentCarouselSettings.ElementId = settings.ElementId; _carouselRepository.Read(currentCarouselSettings, unitOfWork ?? localUnitOfWork); // Get slides to delete (i.e. slides that were in current settings, but not in the new settings) // Get slides with updated images List <CarouselSlide> slidesToDelete = new List <CarouselSlide>(); List <CarouselSlide> slidesWithUpdatedImages = new List <CarouselSlide>(); List <CarouselSlide> currentSlidesWithUpdatedImages = new List <CarouselSlide>(); Dictionary <long, CarouselSlide> slidesById = carouselSettings.Slides.Where(s => s.CarouselSlideId != 0).GroupBy(s => s.CarouselSlideId).ToDictionary(u => u.Key, u => u.First()); foreach (CarouselSlide currentSlide in currentCarouselSettings.Slides) { if (!slidesById.ContainsKey(currentSlide.CarouselSlideId)) { slidesToDelete.Add(currentSlide); } else { CarouselSlide slide = slidesById[currentSlide.CarouselSlideId]; if (slide.ImageUploadId != currentSlide.ImageUploadId) { slidesWithUpdatedImages.Add(slide); currentSlidesWithUpdatedImages.Add(currentSlide); } } } // Get new slides List <CarouselSlide> slidesToCreate = carouselSettings.Slides.Where(s => s.CarouselSlideId == 0).ToList(); // Commit new images slidesToCreate.AddRange(slidesWithUpdatedImages); foreach (CarouselSlide slide in slidesToCreate) { _uploadService.Commit(slide.ImageTenantId, slide.ThumbnailImageUploadId, GetCarouselSlideStorageHierarchy(slide.ElementId), unitOfWork ?? localUnitOfWork); _uploadService.Commit(slide.ImageTenantId, slide.PreviewImageUploadId, GetCarouselSlideStorageHierarchy(slide.ElementId), unitOfWork ?? localUnitOfWork); _uploadService.Commit(slide.ImageTenantId, slide.ImageUploadId, GetCarouselSlideStorageHierarchy(slide.ElementId), unitOfWork ?? localUnitOfWork); } // Update database _carouselRepository.Update((CarouselSettings)settings, unitOfWork ?? localUnitOfWork); // Delete uploads that are no longer required slidesToDelete.AddRange(currentSlidesWithUpdatedImages); foreach (CarouselSlide currentSlide in slidesToDelete) { _uploadService.Delete(currentSlide.ImageTenantId, currentSlide.ThumbnailImageUploadId, GetCarouselSlideStorageHierarchy(currentSlide.ElementId), unitOfWork ?? localUnitOfWork); _uploadService.Delete(currentSlide.ImageTenantId, currentSlide.PreviewImageUploadId, GetCarouselSlideStorageHierarchy(currentSlide.ElementId), unitOfWork ?? localUnitOfWork); _uploadService.Delete(currentSlide.ImageTenantId, currentSlide.ImageUploadId, GetCarouselSlideStorageHierarchy(currentSlide.ElementId), unitOfWork ?? localUnitOfWork); } // Commit work if local unit of work in place and return result if (localUnitOfWork != null) { localUnitOfWork.Commit(); } } catch (ValidationErrorException) { if (localUnitOfWork != null) { localUnitOfWork.Rollback(); } throw; } catch (Exception ex) { if (localUnitOfWork != null) { localUnitOfWork.Rollback(); } throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex); } finally { if (localUnitOfWork != null) { localUnitOfWork.Dispose(); } } }