// --- protected --- protected void Save(object target, bool update) { var entity = EntityContainer.AsEntity(target); if (entity == null) { throw new ArgumentNullException("target"); } var entityType = entity.Type; var id = entity.Id; var settings = new XmlWriterSettings(); { settings.Indent = false; } var swriter = new StringWriter(); _NValues = 0; using (var writer = XmlWriter.Create(swriter, settings)) { var context = new StoreContext() { Id = id, Type = entityType, Writer = writer, }; writer.WriteStartDocument(); { SaveType(context, target, entityType); } writer.WriteEndDocument(); } if (update) { _memoAccessor.UpdateEntity(entityType.FullName, id, swriter.ToString()); } else { _memoAccessor.InsertEntity(entityType.FullName, id, swriter.ToString()); } }
private void ConvertModel() { var oldModelRoot = MemopadConstsV1.ModelRoot; var classDirs = Directory.GetDirectories(oldModelRoot); foreach (var classDir in classDirs) { var typeName = Path.GetFileName(classDir); var entityDirs = Directory.GetDirectories(classDir); foreach (var entityDir in entityDirs) { var id = Path.GetFileName(entityDir); var isMemoImage = string.Equals(typeName, "Mkamo.Model.Memo.MemoImage", StringComparison.OrdinalIgnoreCase); var fileImageDesc = default(FileImageDescription); if (isMemoImage) { /// ByteImageDescriptionからFileImageDescriptionに変換 using (var stream = new FileStream(Path.Combine(entityDir, "Image.ser"), FileMode.Open, FileAccess.Read)) { var formatter = new BinaryFormatter(); var bytesDesc = (BytesImageDescription)formatter.Deserialize(stream); var filepath = GetNewImageFilePath(); PathUtil.EnsureDirectoryExists(Path.GetDirectoryName(filepath)); File.WriteAllBytes(filepath, bytesDesc.Bytes); fileImageDesc = new FileImageDescription(Path.GetFileName(filepath)); } } var files = Directory.GetFiles(entityDir); var hasSerializable = files.Any( file => string.Equals(Path.GetExtension(file), ".ser", StringComparison.OrdinalIgnoreCase) ); foreach (var file in files) { var fileName = Path.GetFileName(file); var fileExt = Path.GetExtension(file); if (string.Equals(fileName, "entity.xml", StringComparison.OrdinalIgnoreCase)) { var xml = File.ReadAllText(file); if (hasSerializable) { xml = ReplaceSerralizableProperty(xml); } _memoAccessor.InsertEntity(typeName, id, xml); } else if (string.Equals(fileExt, ".dat", StringComparison.OrdinalIgnoreCase)) { var key = Path.GetFileNameWithoutExtension(file); var text = File.ReadAllText(file); _exDataAccessor.InsertExtendedTextData(typeName, id, key, text); } else if (string.Equals(fileExt, ".obj", StringComparison.OrdinalIgnoreCase)) { var key = Path.GetFileNameWithoutExtension(file); var bytes = File.ReadAllBytes(file); _exDataAccessor.InsertExtendedBlobData(typeName, id, key, bytes); } else if (string.Equals(fileExt, ".ser", StringComparison.OrdinalIgnoreCase)) { if (isMemoImage) { //if (fileImageDesc != null) { var bytes = default(byte[]); using (var stream = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(stream, fileImageDesc); bytes = stream.GetBuffer(); } _memoAccessor.InsertSerializableProperty(typeName, id, "Image", bytes); //} } else { var key = Path.GetFileNameWithoutExtension(file); var bytes = File.ReadAllBytes(file); _memoAccessor.InsertSerializableProperty(typeName, id, key, bytes); } } } } } }