/// <summary> /// Creates a new IHiveManager and creates substitutes for the IContentStore /// </summary> /// <param name="hive">The hive.</param> /// <param name="readonlyEntityRepositoryGroup">The readonly entity session.</param> /// <param name="readonlySchemaRepositoryGroup">The readonly schema session.</param> /// <param name="entityRepository">The entity session.</param> /// <param name="schemaSession">The schema session.</param> /// <returns></returns> public static IHiveManager MockContentStore( this IHiveManager hive, out IReadonlyEntityRepositoryGroup <IContentStore> readonlyEntityRepositoryGroup, out IReadonlySchemaRepositoryGroup <IContentStore> readonlySchemaRepositoryGroup, out IEntityRepositoryGroup <IContentStore> entityRepository, out ISchemaRepositoryGroup <IContentStore> schemaSession) { return(hive.MockStore("content://", out readonlyEntityRepositoryGroup, out readonlySchemaRepositoryGroup, out entityRepository, out schemaSession)); }
/// <summary> /// Creates a new IHiveManager and creates substitutes for the IDictionaryStore /// </summary> /// <param name="hive">The hive.</param> /// <param name="readonlyEntityRepositoryGroup">The readonly entity session.</param> /// <param name="readonlySchemaRepositoryGroup">The readonly schema session.</param> /// <param name="entityRepository">The entity session.</param> /// <param name="schemaRepository">The schema session.</param> /// <returns></returns> public static IHiveManager MockDictionaryStore( this IHiveManager hive, out IReadonlyEntityRepositoryGroup <IDictionaryStore> readonlyEntityRepositoryGroup, out IReadonlySchemaRepositoryGroup <IDictionaryStore> readonlySchemaRepositoryGroup, out IEntityRepositoryGroup <IDictionaryStore> entityRepository, out ISchemaRepositoryGroup <IDictionaryStore> schemaRepository) { return(hive.MockStore("dictionary://", out readonlyEntityRepositoryGroup, out readonlySchemaRepositoryGroup, out entityRepository, out schemaRepository)); }
/// <summary> /// Mocks a store on the provided IHiveManager mock /// </summary> /// <typeparam name="TFilter">The type of the filter.</typeparam> /// <param name="hive">The hive.</param> /// <param name="uri">The URI.</param> /// <param name="readonlyEntitySession">The readonly entity session.</param> /// <param name="readonlySchemaSession">The readonly schema session.</param> /// <param name="entityRepository">The entity session.</param> /// <param name="schemaRepository">The schema session.</param> /// <returns></returns> public static IHiveManager MockStore <TFilter>( this IHiveManager hive, string uri, out IReadonlyEntityRepositoryGroup <TFilter> readonlyEntitySession, out IReadonlySchemaRepositoryGroup <TFilter> readonlySchemaSession, out IEntityRepositoryGroup <TFilter> entityRepository, out ISchemaRepositoryGroup <TFilter> schemaRepository) where TFilter : class, IProviderTypeFilter { //mock hive var hiveReader = Substitute.For <IReadonlyGroupUnit <TFilter> >(); var hiveWriter = Substitute.For <IGroupUnit <TFilter> >(); hiveReader.FrameworkContext.Returns(hive.FrameworkContext); hiveWriter.FrameworkContext.Returns(hive.FrameworkContext); //mock readers readonlyEntitySession = Substitute.For <IReadonlyEntityRepositoryGroup <TFilter> >(); readonlySchemaSession = Substitute.For <IReadonlySchemaRepositoryGroup <TFilter> >(); readonlyEntitySession.Schemas.Returns(readonlySchemaSession); hiveReader.Repositories.Returns(readonlyEntitySession); //mock writers entityRepository = Substitute.For <IEntityRepositoryGroup <TFilter> >(); entityRepository.FrameworkContext.Returns(hive.FrameworkContext); schemaRepository = Substitute.For <ISchemaRepositoryGroup <TFilter> >(); entityRepository.Schemas.Returns(schemaRepository); hiveWriter.Repositories.Returns(entityRepository); RepositoryContext fakeRepositoryContext = FakeHiveCmsManager.CreateFakeRepositoryContext(hive.FrameworkContext); var readonlyGroupUnitFactory = Substitute.For <ReadonlyGroupUnitFactory <TFilter> >(Enumerable.Empty <ReadonlyProviderSetup>(), new Uri(uri), fakeRepositoryContext, hive.FrameworkContext); readonlyGroupUnitFactory.CreateReadonly().Returns(hiveReader); readonlyGroupUnitFactory.CreateReadonly <TFilter>().Returns(hiveReader); var groupUnitFactory = Substitute.For <GroupUnitFactory <TFilter> >(Enumerable.Empty <ProviderSetup>(), new Uri(uri), fakeRepositoryContext, hive.FrameworkContext); groupUnitFactory.Create().Returns(hiveWriter); groupUnitFactory.Create <TFilter>().Returns(hiveWriter); hive.GetReader <TFilter>().Returns(readonlyGroupUnitFactory); hive.GetReader <TFilter>(null).ReturnsForAnyArgs(readonlyGroupUnitFactory); hive.GetWriter <TFilter>().Returns(groupUnitFactory); hive.GetWriter <TFilter>(null).ReturnsForAnyArgs(groupUnitFactory); return(hive); }
/// <summary> /// Returns all files, excluding folders ordered by name /// </summary> /// <param name="unit"></param> /// <returns></returns> public static IEnumerable <File> GetAllNonContainerFiles(this IReadonlyEntityRepositoryGroup <IFileStore> unit) { return(unit.GetAll <File>() .OrderBy(x => x.Name) .Where(x => !x.IsContainer)); }
public static IEnumerable <IRelationById> GetParentFileRelations(this IReadonlyEntityRepositoryGroup <IFileStore> unit, IRelatableEntity source) { Mandate.ParameterNotNull(source, "source"); return(unit.GetParentRelations(source.Id, FixedRelationTypes.DefaultRelationType)); }
public static T GetEntityByPath <T>(this IReadonlyEntityRepositoryGroup <IProviderTypeFilter> repositoryGroup, HiveId sourceId, string path, RevisionStatusType revisionStatusType, bool acceptNearestMatch = false) where T : TypedEntity { T found = null; var parts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); foreach (var part in parts) { //TODO: We need to enable more on the query context to do this properly, however there seems // to be a limitation on our business models since there's no current way to query by a version // since the query context only exposes TypedEntity and not revisions. // For now, this is going to be a really nasty lookup to do what we want. //var test1 = // repositoryGroup.ToList().Select( // x => // new // { // Entity = x, // Attribs = x.Attributes.Select(y => new { y.AttributeDefinition.Alias, Values = y.Values.ToList() }).ToArray() // }) // .ToArray(); var entityRevisionsByAlias = repositoryGroup .Where(x => x.Attribute <string>(NodeNameAttributeDefinition.AliasValue, "UrlName") == part || ((x.Attribute <string>(NodeNameAttributeDefinition.AliasValue, "UrlName") == null || x.Attribute <string>(NodeNameAttributeDefinition.AliasValue, "UrlName") == string.Empty) && x.Attribute <string>(NodeNameAttributeDefinition.AliasValue, "Name") == part)) .ToArray(); var childRelations = repositoryGroup.GetChildRelations( sourceId, FixedRelationTypes.DefaultRelationType) .Select(x => x.DestinationId) .ToArray(); var entityWithCorrectRelation = entityRevisionsByAlias.FirstOrDefault(x => childRelations.Contains(x.Id)); if (entityWithCorrectRelation == null) { continue; } //TODO: until the above issue is resolved, we actually need to lookup the entity individually by revision status var foundWithRevisionStatus = repositoryGroup.Revisions.GetLatestSnapshot <TypedEntity>(entityWithCorrectRelation.Id, revisionStatusType); if (foundWithRevisionStatus == null || foundWithRevisionStatus.Revision == null || foundWithRevisionStatus.Revision.Item == null) { continue; } found = (T)foundWithRevisionStatus.Revision.Item; sourceId = found.Id; } if (found != null && (found.Attribute <string>(NodeNameAttributeDefinition.AliasValue, "UrlName").InvariantEquals(path.Split('/').Last()) || acceptNearestMatch)) { return(found); } return(null); }
public static T GetEntityByPath <T>(this IReadonlyEntityRepositoryGroup <IProviderTypeFilter> repositoryGroup, HiveId sourceId, string path, bool acceptNearestMatch = false) where T : TypedEntity { return(repositoryGroup.GetEntityByPath <T>(sourceId, path, FixedStatusTypes.Published, acceptNearestMatch)); }
public static T GetEntityByPath <T>(this IReadonlyEntityRepositoryGroup <IProviderTypeFilter> repositoryGroup, HiveId sourceId, string path, RevisionStatusType revisionStatusType, bool acceptNearestMatch = false) where T : TypedEntity { // Don't mandate for revisionStatusType to be non-null since that's fine, it means "just get latest" Mandate.ParameterNotNull(repositoryGroup, "repositoryGroup"); Mandate.ParameterNotEmpty(sourceId, "sourceId"); Mandate.ParameterNotNullOrEmpty(path, "path"); T found = null; var parts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); const string nodeName = NodeNameAttributeDefinition.AliasValue; // For each part in the path, get nodes that match by name // Then for the children of those matches, get nodes that match by name if (parts.Length == 0) { return(null); } var revisionTypeQuery = (revisionStatusType != null) ? repositoryGroup.Query <T>().OfRevisionType(revisionStatusType) : repositoryGroup.Query <T>(); // Get the first part var firstPart = parts[0]; var withParentQuery = revisionTypeQuery.WithParentIds(sourceId); // The original query was "UrlName matches, or if it doesn't, try to match the Name provided UrlName is null." // This is a pretty complex query burden for an edge-case. To avoid this, we'll just find by the UrlName first. // If a match isn't found, then we'll do another query, splitting the workload in half var firstMatch = withParentQuery.FirstOrDefault(x => x.InnerAttribute <string>(nodeName, "UrlName") == firstPart) ?? withParentQuery.FirstOrDefault(x => (x.InnerAttribute <string>(nodeName, "UrlName") == null || x.InnerAttribute <string>(nodeName, "UrlName") == string.Empty) && x.InnerAttribute <string>(nodeName, "Name") == firstPart); if (firstMatch == null) { return(null); } // There was only one path item, so return that if (parts.Length == 1) { return(firstMatch); } // Get the children of the first match var lastMatch = firstMatch; foreach (var part in parts.Skip(1)) { withParentQuery = revisionTypeQuery.WithParentIds(lastMatch.Id); var nextMatch = withParentQuery.FirstOrDefault(x => x.InnerAttribute <string>(nodeName, "UrlName") == part) ?? withParentQuery.FirstOrDefault(x => (x.InnerAttribute <string>(nodeName, "UrlName") == null || x.InnerAttribute <string>(nodeName, "UrlName") == string.Empty) && x.InnerAttribute <string>(nodeName, "Name") == part); if (nextMatch == null) { return(null); } // We found a match lastMatch = nextMatch; } found = lastMatch; if (found != null) { var foundUrlName = found.InnerAttribute <string>(nodeName, "UrlName"); if (foundUrlName.InvariantEquals(path.Split('/').Last()) || acceptNearestMatch) { return(found); } } return(null); }