Esempio n. 1
0
        public void Read(CarouselSettings settings, IUnitOfWork unitOfWork = null)
        {
            IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork);

            try
            {
                string sql = _sqlManager.GetSql("Sql.ReadCarousel.sql");
                dbm.SetSQL(sql);
                dbm.AddParameter("@TenantId", FieldType.BigInt, settings.TenantId);
                dbm.AddParameter("@ElementId", FieldType.BigInt, settings.ElementId);
                dbm.ExecuteReader();
                settings.Slides = new List <CarouselSlide>();
                while (dbm.Read())
                {
                    settings.Slides.Add(GetCarouselSlideFromDatabaseManager(dbm));
                }
            }
            finally
            {
                if (unitOfWork == null)
                {
                    dbm.Dispose();
                }
            }
        }
Esempio n. 2
0
        public void Update(CarouselSettings settings, IUnitOfWork unitOfWork = null)
        {
            CarouselSlideCollection carouselSlideCollection = GetCarouselSlideCollection(settings);
            IUnitOfWork             localUnitOfWork         = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            try
            {
                IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork ?? localUnitOfWork);
                string           sql = _sqlManager.GetSql("Sql.UpdateCarousel.sql");
                dbm.SetSQL(sql);
                dbm.AddParameter("@TenantId", FieldType.BigInt, settings.TenantId);
                dbm.AddParameter("@ElementId", FieldType.BigInt, settings.ElementId);
                dbm.AddTypedParameter("@CarouselSlides", FieldType.Structured, carouselSlideCollection.Count == 0 ? null : carouselSlideCollection, "element.CarouselSlideTableType");
                dbm.ExecuteNonQuery();
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
            }
            catch
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            finally
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Dispose();
                }
            }
        }
Esempio n. 3
0
        private Form GetSlidesForm(string context, long elementId)
        {
            // Get current carousel settings
            Guid elementTypeId = FormId;
            IAdvancedElementService elementService   = (IAdvancedElementService)_elementFactory.GetElementService(elementTypeId);
            CarouselSettings        carouselSettings = (CarouselSettings)elementService.New(_authenticationService.TenantId);

            carouselSettings.ElementId = elementId;
            elementService.Read(carouselSettings);

            // Get carousel slide view models
            List <CarouselSlideViewModel> slideViewModels = new List <CarouselSlideViewModel>();

            foreach (CarouselSlide slide in carouselSettings.Slides)
            {
                slideViewModels.Add(GetSlideViewModel(slide, false));
            }
            string data = JsonConvert.SerializeObject(slideViewModels);

            // Construct form
            Form form = new Form {
                Fields = new Dictionary <string, IFormField>(), Id = FormId.ToString(), Context = context, Data = data
            };

            form.SubmitLabel = ElementResource.CarouselButtonLabel;

            // Return result
            return(form);
        }
Esempio n. 4
0
        private CarouselSlideCollection GetCarouselSlideCollection(CarouselSettings settings)
        {
            CarouselSlideCollection carouselSlideCollection = new CarouselSlideCollection();

            foreach (CarouselSlide slide in settings.Slides)
            {
                carouselSlideCollection.Add(slide);
            }
            return(carouselSlideCollection);
        }
Esempio n. 5
0
        public void Delete(long tenantId, long elementId, 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
            {
                // Get carousel settings
                CarouselSettings carouselSettings = (CarouselSettings)New(tenantId);
                carouselSettings.ElementId = elementId;
                _carouselRepository.Read(carouselSettings, unitOfWork ?? localUnitOfWork);

                // Delete carousel from underlying storage
                _carouselRepository.Delete(tenantId, elementId, unitOfWork ?? localUnitOfWork);

                // Delete slide images
                foreach (CarouselSlide slide in carouselSettings.Slides)
                {
                    _uploadService.Delete(slide.ImageTenantId, slide.ThumbnailImageUploadId, GetCarouselSlideStorageHierarchy(slide.ElementId), unitOfWork ?? localUnitOfWork);
                    _uploadService.Delete(slide.ImageTenantId, slide.PreviewImageUploadId, GetCarouselSlideStorageHierarchy(slide.ElementId), unitOfWork ?? localUnitOfWork);
                    _uploadService.Delete(slide.ImageTenantId, slide.ImageUploadId, GetCarouselSlideStorageHierarchy(slide.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();
                }
            }
        }
Esempio n. 6
0
        private void PostSlidesForm(Form form, long pageId, long elementId)
        {
            // Get tenant ID
            long tenantId = _authenticationService.TenantId;

            // Get element service
            IAdvancedElementService elementService = (IAdvancedElementService)_elementFactory.GetElementService(FormId);

            // Get updated carousel settings
            CarouselSettings carouselSettings = (CarouselSettings)elementService.New(_authenticationService.TenantId);

            carouselSettings.ElementId = elementId;
            carouselSettings.Slides    = new List <CarouselSlide>();
            List <CarouselSlideViewModel> slideViewModels = JsonConvert.DeserializeObject <List <CarouselSlideViewModel> >(form.Data);

            for (int index = 0; index < slideViewModels.Count; index++)
            {
                CarouselSlideViewModel slideViewModel = slideViewModels[index];
                carouselSettings.Slides.Add(new CarouselSlide
                {
                    CarouselSlideId        = Convert.ToInt64(slideViewModel.CarouselSlideId),
                    Description            = slideViewModel.Description,
                    ElementId              = elementId,
                    ImageTenantId          = tenantId,
                    ImageUploadId          = Convert.ToInt64(slideViewModel.ImageUploadId),
                    Name                   = slideViewModel.Name,
                    PageId                 = string.IsNullOrWhiteSpace(slideViewModel.PageId) ? null : (long?)Convert.ToInt64(slideViewModel.PageId),
                    PageText               = string.IsNullOrWhiteSpace(slideViewModel.PageText) ? null : slideViewModel.PageText,
                    PageTenantId           = string.IsNullOrWhiteSpace(slideViewModel.PageId) ? null : (long?)tenantId,
                    PreviewImageUploadId   = Convert.ToInt64(slideViewModel.PreviewImageUploadId),
                    SortOrder              = index,
                    TenantId               = tenantId,
                    ThumbnailImageUploadId = Convert.ToInt64(slideViewModel.ThumbnailImageUploadId)
                });
            }

            // Perform the update
            elementService.Update(carouselSettings);
        }
Esempio n. 7
0
        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();
                }
            }
        }
Esempio n. 8
0
 public void Create(CarouselSettings settings, IUnitOfWork unitOfWork = null)
 {
 }