/// <summary> /// There will only be routing data if there is a custom entity page /// associated with this entity type. /// </summary> private static PageRoutingInfo FindRoutingData(IDictionary <int, ICollection <PageRoutingInfo> > routings, CustomEntityPublishStatusQuery dbStatusQuery) { PageRoutingInfo detailsRouting = null; if (routings.ContainsKey(dbStatusQuery.CustomEntityId)) { detailsRouting = routings[dbStatusQuery.CustomEntityId].FirstOrDefault(r => r.CustomEntityRouteRule != null); } return(detailsRouting); }
public async Task <PagedQueryResult <CustomEntitySummary> > ExecuteAsync(SearchCustomEntitySummariesQuery query, IExecutionContext executionContext) { var definition = _queryExecutor.GetById <CustomEntityDefinitionSummary>(query.CustomEntityDefinitionCode); EntityNotFoundException.ThrowIfNull(definition, query.CustomEntityDefinitionCode); // Get Query var dbQuery = GetQuery(query, definition); // Execute Query var dbPagedResult = dbQuery.ToPagedResult(query); var routingsQuery = new GetPageRoutingInfoByCustomEntityIdRangeQuery(dbPagedResult.Items.Select(e => e.CustomEntityId)); var routings = await _queryExecutor.ExecuteAsync(routingsQuery); var allLocales = (await _queryExecutor.GetAllAsync <ActiveLocale>()).ToDictionary(l => l.LocaleId); // Map Items var entities = new List <CustomEntitySummary>(dbPagedResult.Items.Length); foreach (var dbVersion in dbPagedResult.Items) { PageRoutingInfo detailsRouting = null; if (routings.ContainsKey(dbVersion.CustomEntityId)) { detailsRouting = routings[dbVersion.CustomEntityId].FirstOrDefault(r => r.CustomEntityRouteRule != null); } var entity = Mapper.Map <CustomEntitySummary>(dbVersion); if (dbVersion.LocaleId.HasValue) { entity.Locale = allLocales.GetOrDefault(dbVersion.LocaleId.Value); EntityNotFoundException.ThrowIfNull(entity.Locale, dbVersion.LocaleId.Value); } if (detailsRouting != null) { entity.FullPath = detailsRouting.CustomEntityRouteRule.MakeUrl(detailsRouting.PageRoute, detailsRouting.CustomEntityRoute); } entity.Model = (ICustomEntityVersionDataModel)_dbUnstructuredDataSerializer.Deserialize(dbVersion.SerializedData, definition.DataModelType); entity.AuditData.UpdateDate = dbVersion.VersionAuditData.CreateDate; entity.AuditData.Updater = dbVersion.VersionAuditData.Creator; entities.Add(entity); } return(dbPagedResult.ChangeType(entities)); }
/// <summary> /// Gets the full (relative) url of a page /// </summary> public string Page(PageRoutingInfo route) { if (route == null) { return(string.Empty); } if (route.CustomEntityRouteRule != null && route.CustomEntityRoute != null) { return(route.CustomEntityRouteRule.MakeUrl(route.PageRoute, route.CustomEntityRoute)); } return(Page(route.PageRoute)); }
/// <summary> /// Gets the url for a page at a specific page or custom entity version, loaded inside the /// visual editor. Note that this method does not validate permissions in any way, it simply /// formats the route correctly. /// </summary> /// <param name="route">The page to link to.</param> /// <param name="versionRoute">The version of the page or custom entity to link to.</param> public string VisualEditor( PageRoutingInfo route, IVersionRoute versionRoute ) { if (route == null) { return(string.Empty); } if (versionRoute == null) { throw new ArgumentNullException(nameof(versionRoute)); } var isEditingCustomEntity = versionRoute is CustomEntityVersionRoute; // Some of the latest version states will have a default view e.g. preview // or live so check for these first before we defer to showing by version number if (versionRoute.IsLatestPublishedVersion && route.IsPublished()) { // Published, so show live view return(VisualEditor(route, VisualEditorMode.Live, isEditingCustomEntity)); } else if (versionRoute.WorkFlowStatus == WorkFlowStatus.Draft) { // Draft version, so show draft return(VisualEditor(route, VisualEditorMode.Preview, isEditingCustomEntity)); } var baseUrl = Page(route); var queryParams = new Dictionary <string, string>(2); queryParams.Add("version", versionRoute.VersionId.ToString()); if (isEditingCustomEntity) { queryParams.Add(VISUAL_EDITOR_QS_EDITTYPE, VISUAL_EDITOR_QS_EDITTYPE_ENTITY); } else if (route.CustomEntityRoute != null) { queryParams.Add(VISUAL_EDITOR_QS_EDITTYPE, VISUAL_EDITOR_QS_EDITTYPE_PAGE); } var url = QueryHelpers.AddQueryString(baseUrl, queryParams); return(url); }
/// <summary> /// Maps a collection of EF CustomEntityVersion records from the db into CustomEntitySummary /// objects. /// </summary> /// <param name="dbStatusQueries">Collection of versions to map.</param> public async Task <List <CustomEntitySummary> > MapAsync(ICollection <CustomEntityPublishStatusQuery> dbStatusQueries, IExecutionContext executionContext) { var entities = new List <CustomEntitySummary>(dbStatusQueries.Count); var routingsQuery = new GetPageRoutingInfoByCustomEntityIdRangeQuery(dbStatusQueries.Select(e => e.CustomEntityId)); var routings = await _queryExecutor.ExecuteAsync(routingsQuery, executionContext); Dictionary <int, ActiveLocale> allLocales = null; Dictionary <string, CustomEntityDefinitionSummary> customEntityDefinitions = new Dictionary <string, CustomEntityDefinitionSummary>(); var hasCheckedQueryValid = false; foreach (var dbStatusQuery in dbStatusQueries) { // Validate the input data if (!hasCheckedQueryValid) { ValidateQuery(dbStatusQuery); } hasCheckedQueryValid = true; // Easy mappings var entity = new CustomEntitySummary() { AuditData = _auditDataMapper.MapUpdateAuditDataCreatorData(dbStatusQuery.CustomEntity), CustomEntityDefinitionCode = dbStatusQuery.CustomEntity.CustomEntityDefinitionCode, CustomEntityId = dbStatusQuery.CustomEntityId, HasDraft = dbStatusQuery.CustomEntityVersion.WorkFlowStatusId == (int)WorkFlowStatus.Draft, PublishStatus = PublishStatusMapper.FromCode(dbStatusQuery.CustomEntity.PublishStatusCode), PublishDate = DbDateTimeMapper.AsUtc(dbStatusQuery.CustomEntity.PublishDate), Ordering = dbStatusQuery.CustomEntity.Ordering, Title = dbStatusQuery.CustomEntityVersion.Title, UrlSlug = dbStatusQuery.CustomEntity.UrlSlug }; entity.IsPublished = entity.PublishStatus == PublishStatus.Published && entity.PublishDate <= executionContext.ExecutionDate; _auditDataMapper.MapUpdateAuditDataUpdaterData(entity.AuditData, dbStatusQuery.CustomEntityVersion); // Routing data (if any) PageRoutingInfo detailsRouting = null; if (routings.ContainsKey(dbStatusQuery.CustomEntityId)) { detailsRouting = routings[dbStatusQuery.CustomEntityId].FirstOrDefault(r => r.CustomEntityRouteRule != null); entity.FullPath = detailsRouting?.CustomEntityRouteRule?.MakeUrl(detailsRouting.PageRoute, detailsRouting.CustomEntityRoute); } // Locale data var localeId = dbStatusQuery.CustomEntity.LocaleId; if (localeId.HasValue && detailsRouting != null) { entity.Locale = detailsRouting.PageRoute.Locale; EntityNotFoundException.ThrowIfNull(entity.Locale, localeId.Value); } else if (localeId.HasValue) { // Lazy load locales, since they aren't always used if (allLocales == null) { allLocales = await GetLocalesAsync(executionContext); } entity.Locale = allLocales.GetOrDefault(localeId.Value); EntityNotFoundException.ThrowIfNull(entity.Locale, localeId.Value); } // Parse model data var definition = customEntityDefinitions.GetOrDefault(dbStatusQuery.CustomEntity.CustomEntityDefinitionCode); if (definition == null) { // Load and cache definitions var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(dbStatusQuery.CustomEntity.CustomEntityDefinitionCode); definition = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext); EntityNotFoundException.ThrowIfNull(definition, definition.CustomEntityDefinitionCode); customEntityDefinitions.Add(dbStatusQuery.CustomEntity.CustomEntityDefinitionCode, definition); } entity.Model = (ICustomEntityDataModel)_dbUnstructuredDataSerializer.Deserialize(dbStatusQuery.CustomEntityVersion.SerializedData, definition.DataModelType); entities.Add(entity); } return(entities); }
/// <summary> /// Gets the url for a page, formatted with specific visual editor /// parameters. Note that this method does not validate permissions /// in any way, it simply formats the route correctly. /// </summary> /// <param name="route">The page to link to.</param> /// <param name="visualEditorMode"> /// The mode to set the visual editor to. Note that this method cannot be /// used for VisualEditorMode.SpecificVersion and will throw an exception if /// you try. To get the url for a specific version, you need to use the overload /// accepting an IVersionRoute parameter. /// </param> /// <param name="isEditingCustomEntity"> /// For custom entity pages, this option indicates whether the editing context /// should be the custom entity rather than the (default) page. /// </param> public string VisualEditor( PageRoutingInfo route, VisualEditorMode visualEditorMode, bool isEditingCustomEntity = false ) { if (route == null) { return(string.Empty); } var baseUrl = Page(route); var queryParams = new Dictionary <string, string>(2); switch (visualEditorMode) { case VisualEditorMode.Any: var latestVersion = route.GetVersionRoute(isEditingCustomEntity, PublishStatusQuery.Latest, null); if (latestVersion.WorkFlowStatus == WorkFlowStatus.Draft || !route.IsPublished()) { queryParams.Add(VISUAL_EDITOR_QS_MODE, VISUAL_EDITOR_QS_MODE_PREVIEW); } else { queryParams.Add(VISUAL_EDITOR_QS_MODE, VISUAL_EDITOR_QS_MODE_LIVE); } break; case VisualEditorMode.Preview: queryParams.Add(VISUAL_EDITOR_QS_MODE, VISUAL_EDITOR_QS_MODE_PREVIEW); break; case VisualEditorMode.Edit: queryParams.Add(VISUAL_EDITOR_QS_MODE, VISUAL_EDITOR_QS_MODE_EDIT); break; case VisualEditorMode.Live: queryParams.Add(VISUAL_EDITOR_QS_MODE, VISUAL_EDITOR_QS_MODE_LIVE); break; case VisualEditorMode.SpecificVersion: throw new Exception($"To create a url for {nameof(VisualEditorMode)}.{nameof(VisualEditorMode.SpecificVersion)} you should use the overload that takes an {nameof(IVersionRoute)} parameter"); default: throw new Exception($"{nameof(VisualEditorMode)}.{visualEditorMode} not supported by {nameof(PageRouteLibrary)}.{nameof(VisualEditor)}()"); } if (isEditingCustomEntity) { queryParams.Add(VISUAL_EDITOR_QS_EDITTYPE, VISUAL_EDITOR_QS_EDITTYPE_ENTITY); } else if (route.CustomEntityRoute != null) { queryParams.Add(VISUAL_EDITOR_QS_EDITTYPE, VISUAL_EDITOR_QS_EDITTYPE_PAGE); } var url = QueryHelpers.AddQueryString(baseUrl, queryParams); return(url); }