public TemplateEditViewModel Execute(Guid?templateId) { TemplateEditViewModel templateModel; if (!templateId.HasValue) { templateModel = new TemplateEditViewModel(); } else { Root.Models.Layout templateAlias = null; TemplateEditViewModel templateViewModelAlias = null; var templateFuture = UnitOfWork.Session.QueryOver(() => templateAlias) .Where(() => templateAlias.Id == templateId && !templateAlias.IsDeleted) .SelectList(select => select .Select(() => templateAlias.Id).WithAlias(() => templateViewModelAlias.Id) .Select(() => templateAlias.Version).WithAlias(() => templateViewModelAlias.Version) .Select(() => templateAlias.Name).WithAlias(() => templateViewModelAlias.Name) .Select(() => templateAlias.PreviewUrl).WithAlias(() => templateViewModelAlias.PreviewImageUrl) .Select(() => templateAlias.LayoutPath).WithAlias(() => templateViewModelAlias.Url)) .TransformUsing(Transformers.AliasToBean <TemplateEditViewModel>()) .FutureValue <TemplateEditViewModel>(); TemplateRegionItemViewModel templateRegionAlias = null; LayoutRegion layoutRegionAlias = null; Region regionAlias = null; var regions = UnitOfWork.Session .QueryOver(() => layoutRegionAlias) .Inner.JoinAlias(c => c.Region, () => regionAlias) .Where(() => layoutRegionAlias.Layout.Id == templateId && !regionAlias.IsDeleted && !layoutRegionAlias.IsDeleted) .SelectList(select => select .Select(() => regionAlias.Id).WithAlias(() => templateRegionAlias.Id) .Select(() => layoutRegionAlias.Description).WithAlias(() => templateRegionAlias.Description) .Select(() => regionAlias.Version).WithAlias(() => templateRegionAlias.Version) .Select(() => regionAlias.RegionIdentifier).WithAlias(() => templateRegionAlias.Identifier)) .TransformUsing(Transformers.AliasToBean <TemplateRegionItemViewModel>()) .Future <TemplateRegionItemViewModel>(); templateModel = templateFuture.Value; if (templateModel == null) { throw new EntityNotFoundException(typeof(TemplateRegionItemViewModel), templateId.Value); } templateModel.Regions = regions.ToList(); templateModel.Options = layoutService.GetLayoutOptions(templateId.Value); } templateModel.CustomOptions = optionService.GetCustomOptions(); return(templateModel); }
private void SetOptions(Root.Models.Layout template, IList <OptionViewModel> options) { // Delete old ones if (template.LayoutOptions != null) { foreach (var option in template.LayoutOptions.Distinct()) { if (options == null || options.All(o => o.OptionKey != option.Key)) { Repository.Delete(option); } } } // Add new ones if (options != null) { foreach (var requestLayoutOption in options) { LayoutOption option = null; if (template.LayoutOptions != null) { option = template.LayoutOptions.FirstOrDefault(o => o.Key == requestLayoutOption.OptionKey); } if (option == null) { option = new LayoutOption(); if (template.LayoutOptions == null) { template.LayoutOptions = new List <LayoutOption>(); } template.LayoutOptions.Add(option); } option.Key = requestLayoutOption.OptionKey; option.DefaultValue = requestLayoutOption.OptionDefaultValue; option.Type = requestLayoutOption.Type; option.Layout = template; OptionService.ValidateOptionValue(option); } } }
/// <summary> /// Executes the specified request. /// </summary> /// <param name="request">The request.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public SaveTemplateResponse Execute(TemplateEditViewModel request) { if (request.Options != null) { OptionService.ValidateOptionKeysUniqueness(request.Options); } UnitOfWork.BeginTransaction(); var isNew = request.Id.HasDefaultValue(); var template = !isNew ? Repository.AsQueryable <Root.Models.Layout>() .Where(f => f.Id == request.Id) .FetchMany(f => f.LayoutRegions) .ToList() .FirstOrDefault() : new Root.Models.Layout(); if (template == null) { template = new Root.Models.Layout(); } template.Name = request.Name; template.LayoutPath = request.Url; template.Version = request.Version; template.PreviewUrl = request.PreviewImageUrl; // Edits or removes regions. if (template.LayoutRegions != null && template.LayoutRegions.Any()) { foreach (var region in template.LayoutRegions) { var requestRegion = request.Regions != null ? request.Regions.FirstOrDefault(f => f.Identifier == region.Region.RegionIdentifier) : null; if (requestRegion != null && region.Region.RegionIdentifier == requestRegion.Identifier) { region.Description = requestRegion.Description; Repository.Save(region); } else { Repository.Delete(region); } } } // Adds new region. if (request.Regions != null) { if (template.LayoutRegions == null) { template.LayoutRegions = new List <LayoutRegion>(); } var regions = GetRegions(request.Regions); foreach (var requestRegionOption in request.Regions) { if (!template.LayoutRegions.Any(f => f.Region.RegionIdentifier.Equals(requestRegionOption.Identifier, StringComparison.InvariantCultureIgnoreCase))) { var region = regions.Find(f => f.RegionIdentifier.Equals(requestRegionOption.Identifier, StringComparison.InvariantCultureIgnoreCase)); if (region == null) { if (requestRegionOption.Description == null) { requestRegionOption.Description = string.Empty; } var regionOption = new Region { RegionIdentifier = requestRegionOption.Identifier }; template.LayoutRegions.Add(new LayoutRegion { Description = requestRegionOption.Description, Region = regionOption, Layout = template }); Repository.Save(regionOption); } else { var layoutRegion = new LayoutRegion { Description = requestRegionOption.Description, Region = region, Layout = template }; template.LayoutRegions.Add(layoutRegion); Repository.Save(layoutRegion); } } } } OptionService.SetOptions <LayoutOption, Root.Models.Layout>(template, request.Options); Repository.Save(template); UnitOfWork.Commit(); // Notify if (isNew) { Events.PageEvents.Instance.OnLayoutCreated(template); } else { Events.PageEvents.Instance.OnLayoutUpdated(template); } return(new SaveTemplateResponse { Id = template.Id, TemplateName = template.Name, Version = template.Version }); }