protected override void PerformAddRelation(IReadonlyRelation <IRelatableEntity, IRelatableEntity> relation) { // Write relations to disc // TODO: Update to perform in the same manner as the NH provider i.e. in the base class by hooking AddOrUpdate if (relation.Type.RelationName != FixedRelationTypes.DefaultRelationType.RelationName) { EnsureRelationsFolderExists(); var sourceMd5 = relation.SourceId.Value.ToString().ToMd5(); var destMd5 = relation.DestinationId.Value.ToString().ToMd5(); var relationPath = Path.Combine(Settings.RelationsStoragePath, sourceMd5 + "-" + destMd5 + ".xml"); var relationFileInfo = new FileInfo(relationPath); var relationContent = RelationSerializer.ToXml(relation); var relationContentBytes = Encoding.UTF8.GetBytes(relationContent.ToString()); if (relationFileInfo.Exists) { relationFileInfo.Delete(); } using ( var writer = relationFileInfo.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) { writer.Write(relationContentBytes, 0, relationContentBytes.Length); } } }
public void RelationSerializerTests_ToXml_Works() { //Arrange var source = new StubRelatableEntity(new HiveId(Guid.NewGuid())); var destination = new StubRelatableEntity(new HiveId(Guid.NewGuid())); var relationType = new RelationType("TestRelationType"); var relation = new Relation(relationType, source, destination, new [] { new RelationMetaDatum("test1", "value1"), new RelationMetaDatum("test2", "value2") }); //Act var xml = RelationSerializer.ToXml(relation); //Assert Assert.IsTrue(xml.Root.Name == "relation"); Assert.IsTrue(xml.Root.Attribute("type").Value == relationType.RelationName); Assert.IsTrue(xml.Root.Attribute("sourceId").Value == source.Id.ToString(HiveIdFormatStyle.AsUri)); Assert.IsTrue(xml.Root.Attribute("destinationId").Value == destination.Id.ToString(HiveIdFormatStyle.AsUri)); Assert.IsTrue(xml.Root.Attribute("ordinal").Value == "0"); Assert.IsTrue(xml.Root.HasElements); Assert.IsTrue(xml.Root.Elements("metaDatum").Count() == 2); Assert.IsTrue(xml.Root.Elements("metaDatum").Single(x => x.Attribute("key").Value == "test1").Attribute("value").Value == "value1"); Assert.IsTrue(xml.Root.Elements("metaDatum").Single(x => x.Attribute("key").Value == "test2").Attribute("value").Value == "value2"); }
public void AddOrUpdate(AbstractEntity persistedEntity) { Mandate.That <ArgumentException>(typeof(File).IsAssignableFrom(persistedEntity.GetType())); var file = (File)persistedEntity; Mandate.That <ArgumentNullException>(!file.Name.IsNullOrWhiteSpace() || !file.Location.IsNullOrWhiteSpace()); // Make sure that the incoming location has the root storage area prefixed if necessary file.Location = EnsureLocationRooted(string.IsNullOrEmpty(file.Location) ? file.Name : file.Location); //ensure the file name is set which should always be based on the location file.Name = Path.GetFileName(file.Location); // Ensure we have an id for the file if (HiveIdExtensions.IsNullValueOrEmpty(file.Id)) { file.Id = GenerateId(file.Location); } // Ensure that the folder exists, if this item is a folder) if (file.IsContainer) { var dir = new DirectoryInfo(file.Location); if (!dir.Exists) { dir.Create(); } } else { var containerPath = Path.GetDirectoryName(file.Location); if (containerPath != null) { var dir = new DirectoryInfo(containerPath); if (!dir.Exists) { dir.Create(); } } } // Write the file, provided it's not a directory if (!file.IsContainer) { var physicalForWriting = new FileInfo(file.Location); if (physicalForWriting.Exists) { physicalForWriting.Delete(); } using (var writer = physicalForWriting.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) { writer.Write(file.ContentBytes, 0, file.ContentBytes.Length); } } //var physicalFile = new FileInfo(file.Location); //if (physicalFile.Name != file.Name) //{ // physicalFile.MoveTo(Path.Combine(physicalFile.Directory.FullName, file.Name)); //} // Write relations to disc foreach (var relation in file.Relations) { if (relation.Type.RelationName != FixedRelationTypes.FileRelationType.RelationName) { EnsureRelationsFolderExists(); var sourceMd5 = relation.SourceId.ToString().ToMd5(); var destMd5 = relation.DestinationId.ToString().ToMd5(); var relationPath = Path.Combine(_relationsFolder, sourceMd5 + "-" + destMd5 + ".xml"); var relationFileInfo = new FileInfo(relationPath); var relationContent = RelationSerializer.ToXml(relation); var relationContentBytes = Encoding.UTF8.GetBytes(relationContent.ToString()); if (relationFileInfo.Exists) { relationFileInfo.Delete(); } using ( var writer = relationFileInfo.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) { writer.Write(relationContentBytes, 0, relationContentBytes.Length); } } } }