private static void RemoveUnselectedRelations(EntityViewModel model) { // Remove non-selected relations before mapping. foreach (var relationProperty in typeof(TModel).GetProperties().Where(p => { var genericArgument = !p.PropertyType.GenericTypeArguments.IsEmpty() && typeof(IEnumerable).IsAssignableFrom(p.PropertyType) ? p.PropertyType.GenericTypeArguments.First() : null; if (genericArgument == null || genericArgument.GenericTypeArguments.IsEmpty()) { return(false); } var relationType = typeof(SelectRelationDto <>).MakeGenericType(genericArgument.GenericTypeArguments.First()); return(genericArgument.Equals(relationType)); })) { var relation = model.GetPropertyValue(relationProperty.Name) as IEnumerable; if (relation != null) { model.SetPropertyValue(relationProperty.Name, relation.AsQueryable().Where("Selected").GetList()); } } }
protected virtual EntityViewModel Get(Type modelType, Guid?id, string url, string culture, int versionNumber, string relationsToInclude, bool useFallBack = false) { IContent content = null; EntityViewModel model = null; var map = EntityHelper.GetObjectMap(modelType); var useTagging = EntityHelper.IsServiceActive(map.ContentType, EntityServiceActions.AllowFixedTagging); if (id != null || !string.IsNullOrWhiteSpace(url)) { if (string.IsNullOrWhiteSpace(culture)) { culture = StrixPlatform.CurrentCultureCode; } relationsToInclude = GetRelationsToInclude(relationsToInclude, useTagging); if (id.HasValue) { if (versionNumber > 0) { content = this.Manager.Get(map.ContentType, id.Value, culture, versionNumber, relationsToInclude); } else { content = this.Manager.Get(map.ContentType, id.Value, culture, 0, relationsToInclude); } } else { if (versionNumber > 0) { content = this.Manager.Get(map.ContentType, url, culture, versionNumber, relationsToInclude); } else { if (useFallBack && EntityHelper.IsServiceActive(map.ContentType, EntityServiceActions.Translations)) { var query = this.Manager.Query(map.ContentType, relationsToInclude).Where("Entity.Url.ToLower().Equals(@0) AND IsCurrentVersion", url.ToLower()); content = query.Where("Culture.ToLower().Equals(@0)", StrixPlatform.CurrentCultureCode.ToLower()).GetFirst() as IContent; // If no content was found for this url and the current culture, try and // get a version for another culture and set the culture to the current one. if (content == null) { content = query.GetFirst() as IContent; if (content != null) { content.Culture = StrixPlatform.CurrentCultureCode; } } } else { content = this.Manager.Get(map.ContentType, url, culture, 0, relationsToInclude); } } } model = content.Map(modelType) as EntityViewModel; } if (model == null) { model = this.Manager.Get(map.ContentType, null).Map <TModel>(); if (id.HasValue) { model.EntityId = id.Value; } model.Url = url; } this.AddTagsAndTranslations(content, model, map, useTagging); return(model); }