Exemplo n.º 1
0
        /// <summary>
        /// Create a new website from a template.
        /// </summary>
        /// <param name="templateTenantId">Identifies the template that will be used to create website.</param>
        /// <param name="web">New website details.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        /// <returns>Newly allocated website identifier.</returns>
        public long Create(long templateTenantId, Web web, IUnitOfWork unitOfWork = null)
        {
            // If we don't have a unit of work in place, create one so that website creation tasks can all be rolled back in the case of failure
            IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            try
            {
                // Check that website (and domain) do not already exist and can be created
                _webValidator.ValidateCreate(web);

                // Get template that determines initial website structure
                Template template = _templateRepository.Read(templateTenantId, true, unitOfWork ?? localUnitOfWork);

                // Get tenant identifier for new website
                DateTime now    = DateTime.UtcNow;
                Tenant   tenant = new Tenant {
                    Created = now, Updated = now
                };
                web.TenantId = _tenantRepository.Create(tenant, unitOfWork ?? localUnitOfWork);

                // Populate web from template
                web.CreateUserEnabled            = template.CreateUserEnabled;
                web.UserHasImage                 = template.UserHasImage;
                web.UserThumbnailImageWidth      = template.UserThumbnailImageWidth;
                web.UserThumbnailImageHeight     = template.UserThumbnailImageHeight;
                web.UserThumbnailImageResizeMode = template.UserThumbnailImageResizeMode;
                web.UserPreviewImageWidth        = template.UserPreviewImageWidth;
                web.UserPreviewImageHeight       = template.UserPreviewImageHeight;
                web.UserPreviewImageResizeMode   = template.UserPreviewImageResizeMode;
                web.UserImageMinWidth            = template.UserImageMinWidth;
                web.UserImageMinHeight           = template.UserImageMinHeight;

                // Create web
                _webRepository.Create(web, unitOfWork ?? localUnitOfWork);

                // Create domain
                web.Domains[0].TenantId = web.TenantId;
                long domainId = _domainRepository.Create(web.Domains[0], unitOfWork ?? localUnitOfWork);

                // Build website based on template
                Dictionary <ElementKeyValue, ElementKeyValue> templateElements = new Dictionary <ElementKeyValue, ElementKeyValue>();
                List <Page> navigationPages = new List <Page>();
                Page        page            = new Page {
                    TenantId = web.TenantId
                };
                BuildWebsite(web.TenantId, page, template.Page, templateElements, navigationPages, unitOfWork ?? localUnitOfWork);

                // Update navigation elements with navigation pages
                _elementService.AddNavigationPages(web.TenantId, templateElements.Select(kvp => kvp.Value).ToList(), navigationPages, unitOfWork ?? localUnitOfWork);

                // Commit work if local unit of work in place, then return newly allocated website identifier
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
                return(web.TenantId);
            }
            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();
                }
            }
        }