private UpdateCustomEntityVersionPageModuleCommand Map(CustomEntityVersionPageModule dbModule, string moduleTypeFileName)
        {
            var result = Mapper.Map <UpdateCustomEntityVersionPageModuleCommand>(dbModule);

            result.DataModel = _pageVersionModuleModelMapper.MapDataModel(moduleTypeFileName, dbModule);

            return(result);
        }
 private IQueryable <int> GetOrderedModuleIds(CustomEntityVersionPageModule versionModule)
 {
     return(_dbContext
            .CustomEntityVersionPageModules
            .AsNoTracking()
            .Where(m => m.CustomEntityVersionId == versionModule.CustomEntityVersionId && m.PageTemplateSectionId == versionModule.PageTemplateSectionId)
            .OrderBy(m => m.Ordering)
            .Select(m => m.CustomEntityVersionPageModuleId));
 }
        private CustomEntityVersionPageModuleDetails MapModule(CustomEntityVersionPageModule dbModule, IEnumerable <PageModuleTypeSummary> allModuleTypes)
        {
            var moduleType = allModuleTypes.SingleOrDefault(t => t.PageModuleTypeId == dbModule.PageModuleTypeId);

            var module = new CustomEntityVersionPageModuleDetails();

            module.ModuleType = moduleType;
            module.DataModel  = _pageVersionModuleModelMapper.MapDataModel(moduleType.FileName, dbModule);
            module.CustomEntityVersionPageModuleId = dbModule.CustomEntityVersionPageModuleId;
            module.Template = _entityVersionPageModuleMapper.GetCustomTemplate(dbModule, moduleType);

            return(module);
        }
        private CustomEntityVersionPageModuleRenderDetails Map(CustomEntityVersionPageModule versionModule, string moduleTypeFileName, string customEntityDefinitionCode, WorkFlowStatusQuery workflowStatus)
        {
            _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(customEntityDefinitionCode);

            var moduleType = _queryExecutor.GetById <PageModuleTypeSummary>(versionModule.PageModuleTypeId);

            EntityNotFoundException.ThrowIfNull(moduleType, versionModule.PageModuleTypeId);

            var result = new CustomEntityVersionPageModuleRenderDetails();

            result.CustomEntityVersionPageModuleId = versionModule.CustomEntityVersionPageModuleId;
            result.ModuleType   = moduleType;
            result.DisplayModel = _pageVersionModuleModelMapper.MapDisplayModel(moduleTypeFileName, versionModule, workflowStatus);

            return(result);
        }
        /// <summary>
        /// Gets the custom view template to render the module as if one
        /// is assigned, otherwise null.
        /// </summary>
        public PageModuleTypeTemplateSummary GetCustomTemplate(CustomEntityVersionPageModule pageModule, CustomEntityVersionPageModuleRenderDetails moduleRenderDetails)
        {
            if (!pageModule.PageModuleTypeTemplateId.HasValue)
            {
                return(null);
            }

            var template = moduleRenderDetails
                           .ModuleType
                           .Templates
                           .FirstOrDefault(t => t.PageModuleTypeTemplateId == pageModule.PageModuleTypeTemplateId);

            Debug.Assert(template != null, string.Format("The module template with id {0} could not be found for custom entity module {1}", pageModule.PageModuleTypeTemplateId, pageModule.CustomEntityVersionPageModuleId));

            return(template);
        }
        public async Task ExecuteAsync(AddCustomEntityVersionPageModuleCommand command, IExecutionContext executionContext)
        {
            var customEntityVersion = _dbContext
                                      .CustomEntityVersions
                                      .Include(s => s.CustomEntityVersionPageModules)
                                      .Include(s => s.CustomEntity)
                                      .FirstOrDefault(v => v.CustomEntityVersionId == command.CustomEntityVersionId);

            EntityNotFoundException.ThrowIfNull(customEntityVersion, command.CustomEntityVersionId);
            _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(customEntityVersion.CustomEntity.CustomEntityDefinitionCode);

            if (customEntityVersion.WorkFlowStatusId != (int)WorkFlowStatus.Draft)
            {
                throw new NotPermittedException("Page modules cannot be deleted unless the entity is in draft status");
            }

            var templateSectionSection = await _dbContext
                                         .PageTemplateSections
                                         .FirstOrDefaultAsync(l => l.PageTemplateSectionId == command.PageTemplateSectionId);

            EntityNotFoundException.ThrowIfNull(templateSectionSection, command.PageTemplateSectionId);

            var customEntityVersionModules = customEntityVersion
                                             .CustomEntityVersionPageModules
                                             .Where(m => m.PageTemplateSectionId == templateSectionSection.PageTemplateSectionId);

            CustomEntityVersionPageModule adjacentItem = null;

            if (command.AdjacentVersionModuleId.HasValue)
            {
                adjacentItem = customEntityVersionModules
                               .SingleOrDefault(m => m.CustomEntityVersionPageModuleId == command.AdjacentVersionModuleId);
                EntityNotFoundException.ThrowIfNull(adjacentItem, command.AdjacentVersionModuleId);
            }

            var newModule = new CustomEntityVersionPageModule();

            newModule.PageTemplateSection = templateSectionSection;

            await _pageModuleCommandHelper.UpdateModelAsync(command, newModule);

            newModule.CustomEntityVersion = customEntityVersion;

            _entityOrderableHelper.SetOrderingForInsert(customEntityVersionModules, newModule, command.InsertMode, adjacentItem);

            _dbContext.CustomEntityVersionPageModules.Add(newModule);

            using (var scope = _transactionScopeFactory.Create())
            {
                await _dbContext.SaveChangesAsync();

                var dependencyCommand = new UpdateUnstructuredDataDependenciesCommand(
                    CustomEntityVersionPageModuleEntityDefinition.DefinitionCode,
                    newModule.CustomEntityVersionPageModuleId,
                    command.DataModel);

                await _commandExecutor.ExecuteAsync(dependencyCommand);

                scope.Complete();
            }
            _customEntityCache.Clear(customEntityVersion.CustomEntity.CustomEntityDefinitionCode, customEntityVersion.CustomEntityId);

            command.OutputCustomEntityVersionId = newModule.CustomEntityVersionPageModuleId;

            await _messageAggregator.PublishAsync(new CustomEntityVersionModuleAddedMessage()
            {
                CustomEntityId = customEntityVersion.CustomEntityId,
                CustomEntityVersionPageModuleId = newModule.CustomEntityVersionPageModuleId,
                CustomEntityDefinitionCode      = customEntityVersion.CustomEntity.CustomEntityDefinitionCode
            });
        }