Beispiel #1
0
        private string PostPhotoForm(Form form, long tenantId, long pageId, long elementId)
        {
            // Get album photo details
            long       albumPhotoId = Convert.ToInt64(form.Data);
            string     uploadIds    = ((UploadField)form.Fields["upload"]).Value;
            AlbumPhoto photo        = new AlbumPhoto {
                TenantId     = tenantId,
                ElementId    = elementId,
                AlbumPhotoId = albumPhotoId,
                Name         = ((TextField)form.Fields["name"]).Value,
                Description  = ((MultiLineTextField)form.Fields["description"]).Value
            };

            if (!string.IsNullOrWhiteSpace(uploadIds))
            {
                string[] uploadParts = uploadIds.Split('|');
                photo.ImageTenantId          = tenantId;
                photo.ThumbnailImageUploadId = Convert.ToInt64(uploadParts[0]);
                photo.PreviewImageUploadId   = Convert.ToInt64(uploadParts[1]);
                photo.ImageUploadId          = Convert.ToInt64(uploadParts[2]);
            }

            // Validate supplied data
            _albumValidator.ValidatePhoto(photo);

            // Determine whether or not there are uncommitted photo images
            bool uncommittedPhotoImages = true;

            if (albumPhotoId > 0)
            {
                IAlbumService albumService = (IAlbumService)_elementFactory.GetElementService(FormId);
                AlbumPhoto    currentPhoto = albumService.ReadPhoto(tenantId, elementId, albumPhotoId);
                uncommittedPhotoImages = currentPhoto.ImageUploadId != photo.ImageUploadId;
            }

            // Get photo view model and return it's JSON representation as form result status
            AlbumPhotoViewModel photoViewModel = GetPhotoViewModel(photo, uncommittedPhotoImages);

            return(JsonConvert.SerializeObject(photoViewModel));
        }
Beispiel #2
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
                AlbumSettings albumSettings = (AlbumSettings)settings;
                foreach (AlbumPhoto photo in albumSettings.Photos)
                {
                    _albumValidator.ValidatePhoto(photo);
                }

                // Get current album settings
                AlbumSettings currentAlbumSettings = (AlbumSettings)New(settings.TenantId);
                currentAlbumSettings.ElementId = settings.ElementId;
                _albumRepository.Read(currentAlbumSettings, unitOfWork ?? localUnitOfWork);

                // Get photos to delete (i.e. photos that were in current settings, but not in the new settings)
                // Get photos with updated images
                List <AlbumPhoto>             photosToDelete                 = new List <AlbumPhoto>();
                List <AlbumPhoto>             photosWithUpdatedImages        = new List <AlbumPhoto>();
                List <AlbumPhoto>             currentPhotosWithUpdatedImages = new List <AlbumPhoto>();
                Dictionary <long, AlbumPhoto> photosById = albumSettings.Photos.Where(p => p.AlbumPhotoId != 0).GroupBy(s => s.AlbumPhotoId).ToDictionary(u => u.Key, u => u.First());
                foreach (AlbumPhoto currentPhoto in currentAlbumSettings.Photos)
                {
                    if (!photosById.ContainsKey(currentPhoto.AlbumPhotoId))
                    {
                        photosToDelete.Add(currentPhoto);
                    }
                    else
                    {
                        AlbumPhoto photo = photosById[currentPhoto.AlbumPhotoId];
                        if (photo.ImageUploadId != currentPhoto.ImageUploadId)
                        {
                            photosWithUpdatedImages.Add(photo);
                            currentPhotosWithUpdatedImages.Add(currentPhoto);
                        }
                    }
                }

                // Get new photos
                List <AlbumPhoto> photosToCreate = albumSettings.Photos.Where(s => s.AlbumPhotoId == 0).ToList();

                // Commit new images
                photosToCreate.AddRange(photosWithUpdatedImages);
                foreach (AlbumPhoto photo in photosToCreate)
                {
                    _uploadService.Commit(photo.ImageTenantId, photo.ThumbnailImageUploadId, GetAlbumPhotoStorageHierarchy(photo.ElementId), unitOfWork ?? localUnitOfWork);
                    _uploadService.Commit(photo.ImageTenantId, photo.PreviewImageUploadId, GetAlbumPhotoStorageHierarchy(photo.ElementId), unitOfWork ?? localUnitOfWork);
                    _uploadService.Commit(photo.ImageTenantId, photo.ImageUploadId, GetAlbumPhotoStorageHierarchy(photo.ElementId), unitOfWork ?? localUnitOfWork);
                }

                // Update database
                _albumRepository.Update((AlbumSettings)settings, unitOfWork ?? localUnitOfWork);

                // Delete uploads that are no longer required
                photosToDelete.AddRange(currentPhotosWithUpdatedImages);
                foreach (AlbumPhoto currentPhoto in photosToDelete)
                {
                    _uploadService.Delete(currentPhoto.ImageTenantId, currentPhoto.ThumbnailImageUploadId, GetAlbumPhotoStorageHierarchy(currentPhoto.ElementId), unitOfWork ?? localUnitOfWork);
                    _uploadService.Delete(currentPhoto.ImageTenantId, currentPhoto.PreviewImageUploadId, GetAlbumPhotoStorageHierarchy(currentPhoto.ElementId), unitOfWork ?? localUnitOfWork);
                    _uploadService.Delete(currentPhoto.ImageTenantId, currentPhoto.ImageUploadId, GetAlbumPhotoStorageHierarchy(currentPhoto.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();
                }
            }
        }