private Form GetAlbumForm(string context, long elementId) { // Get current album settings Guid elementTypeId = FormId; IAdvancedElementService elementService = (IAdvancedElementService)_elementFactory.GetElementService(elementTypeId); AlbumSettings albumSettings = (AlbumSettings)elementService.New(_authenticationService.TenantId); albumSettings.ElementId = elementId; elementService.Read(albumSettings); // Get album photo view models List <AlbumPhotoViewModel> photoViewModels = new List <AlbumPhotoViewModel>(); foreach (AlbumPhoto photo in albumSettings.Photos) { AlbumPhotoViewModel photoViewModel = new AlbumPhotoViewModel { AlbumPhotoId = photo.AlbumPhotoId.ToString(), PreviewImageUploadId = photo.PreviewImageUploadId.ToString(), Name = photo.Name, Description = photo.Description, ImageUrl = string.Format("/elements/{0}/uploads/{1}?format=preview&t={1}", photo.ElementId, photo.AlbumPhotoId) }; photoViewModels.Add(photoViewModel); } string data = JsonConvert.SerializeObject(photoViewModels); // Return form with data return(new Form { Fields = new Dictionary <string, IFormField>(), Id = FormId.ToString(), Context = context, Data = data }); }
public void Read(AlbumSettings settings, IUnitOfWork unitOfWork = null) { IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork); try { dbm.SetSQL(_sqlManager.GetSql("Sql.ReadAlbum.sql")); dbm.AddParameter("@TenantId", FieldType.BigInt, settings.TenantId); dbm.AddParameter("@ElementId", FieldType.BigInt, settings.ElementId); dbm.ExecuteReader(); dbm.Read(); settings.DisplayName = dbm.DataReaderValue("DisplayName") == DBNull.Value ? null : (string)dbm.DataReaderValue("DisplayName"); dbm.Read(); settings.Photos = new List <AlbumPhoto>(); while (dbm.Read()) { settings.Photos.Add(GetAlbumPhotoFromDatabaseManager(dbm)); } } finally { if (unitOfWork == null) { dbm.Dispose(); } } }
public void Update(AlbumSettings settings, IUnitOfWork unitOfWork = null) { AlbumPhotoCollection albumPhotoCollection = GetAlbumPhotoCollection(settings); IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null; try { IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork ?? localUnitOfWork); dbm.SetSQL(_sqlManager.GetSql("Sql.UpdateAlbum.sql")); dbm.AddParameter("@TenantId", FieldType.BigInt, settings.TenantId); dbm.AddParameter("@ElementId", FieldType.BigInt, settings.ElementId); dbm.AddParameter("@DisplayName", FieldType.NVarChar, 256, settings.DisplayName ?? (object)DBNull.Value); dbm.AddTypedParameter("@AlbumPhotos", FieldType.Structured, albumPhotoCollection.Count == 0 ? null : albumPhotoCollection, "element.AlbumPhotoTableType"); dbm.ExecuteNonQuery(); if (localUnitOfWork != null) { localUnitOfWork.Commit(); } } catch { if (localUnitOfWork != null) { localUnitOfWork.Rollback(); } throw; } finally { if (localUnitOfWork != null) { localUnitOfWork.Dispose(); } } }
private AlbumPhotoCollection GetAlbumPhotoCollection(AlbumSettings settings) { AlbumPhotoCollection albumPhotoCollection = new AlbumPhotoCollection(); foreach (AlbumPhoto photo in settings.Photos) { albumPhotoCollection.Add(photo); } return(albumPhotoCollection); }
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 album settings AlbumSettings albumSettings = (AlbumSettings)New(tenantId); albumSettings.ElementId = elementId; _albumRepository.Read(albumSettings, unitOfWork ?? localUnitOfWork); // Delete album from underlying storage _albumRepository.Delete(tenantId, elementId, unitOfWork ?? localUnitOfWork); // Delete photo images foreach (AlbumPhoto photo in albumSettings.Photos) { _uploadService.Delete(photo.ImageTenantId, photo.ThumbnailImageUploadId, GetAlbumPhotoStorageHierarchy(photo.ElementId), unitOfWork ?? localUnitOfWork); _uploadService.Delete(photo.ImageTenantId, photo.PreviewImageUploadId, GetAlbumPhotoStorageHierarchy(photo.ElementId), unitOfWork ?? localUnitOfWork); _uploadService.Delete(photo.ImageTenantId, photo.ImageUploadId, GetAlbumPhotoStorageHierarchy(photo.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(); } } }
public void Create(AlbumSettings settings, IUnitOfWork unitOfWork = null) { IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork); try { dbm.SetSQL(_sqlManager.GetSql("Sql.CreateAlbum.sql")); dbm.AddParameter("@TenantId", FieldType.BigInt, settings.TenantId); dbm.AddParameter("@ElementId", FieldType.BigInt, settings.ElementId); dbm.AddParameter("@DisplayName", FieldType.NVarChar, 256, settings.DisplayName ?? (object)DBNull.Value); dbm.ExecuteNonQuery(); } finally { if (unitOfWork == null) { dbm.Dispose(); } } }
private Form GetPhotosForm(string context, long elementId) { // Get current album settings Guid elementTypeId = FormId; IAdvancedElementService elementService = (IAdvancedElementService)_elementFactory.GetElementService(elementTypeId); AlbumSettings albumSettings = (AlbumSettings)elementService.New(_authenticationService.TenantId); albumSettings.ElementId = elementId; elementService.Read(albumSettings); // Get album photo view models List <AlbumPhotoViewModel> photoViewModels = new List <AlbumPhotoViewModel>(); foreach (AlbumPhoto photo in albumSettings.Photos) { photoViewModels.Add(GetPhotoViewModel(photo, false)); } string data = JsonConvert.SerializeObject(photoViewModels); // Construct form Form form = new Form { Fields = new Dictionary <string, IFormField>(), Id = FormId.ToString(), Context = context, Data = data }; form.Fields.Add("displayName", new TextField { Name = "displayName", Label = ElementResource.AlbumDisplayNameLabel, MaxLength = AlbumLengths.DisplayNameMaxLength, MaxLengthErrorMessage = string.Format(ElementResource.AlbumDisplayNameMaxLengthMessage, "displayName", AlbumLengths.DisplayNameMaxLength), Value = albumSettings.DisplayName }); form.SubmitLabel = ElementResource.AlbumButtonLabel; // Return result return(form); }
private void PostPhotosForm(Form form, long pageId, long elementId) { // Get tenant ID long tenantId = _authenticationService.TenantId; // Get element service IAdvancedElementService elementService = (IAdvancedElementService)_elementFactory.GetElementService(FormId); // Get updated album settings AlbumSettings albumSettings = (AlbumSettings)elementService.New(_authenticationService.TenantId); albumSettings.ElementId = elementId; albumSettings.DisplayName = string.IsNullOrWhiteSpace(((TextField)form.Fields["displayName"]).Value) ? null : ((TextField)form.Fields["displayName"]).Value; albumSettings.Photos = new List <AlbumPhoto>(); List <AlbumPhotoViewModel> photoViewModels = JsonConvert.DeserializeObject <List <AlbumPhotoViewModel> >(form.Data); for (int index = 0; index < photoViewModels.Count; index++) { AlbumPhotoViewModel photoViewModel = photoViewModels[index]; albumSettings.Photos.Add(new AlbumPhoto { AlbumPhotoId = Convert.ToInt64(photoViewModel.AlbumPhotoId), Description = photoViewModel.Description, ElementId = elementId, ImageTenantId = tenantId, ImageUploadId = Convert.ToInt64(photoViewModel.ImageUploadId), Name = photoViewModel.Name, PreviewImageUploadId = Convert.ToInt64(photoViewModel.PreviewImageUploadId), SortOrder = index, TenantId = tenantId, ThumbnailImageUploadId = Convert.ToInt64(photoViewModel.ThumbnailImageUploadId) }); } // Perform the update elementService.Update(albumSettings); }
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(); } } }