// 构造一个配置文件类型的对象 public static DatabaseObject BuildFileObject( string strName, Stream fs) { DatabaseObject obj = new DatabaseObject(); fs.Seek(0, SeekOrigin.Begin); obj.Content = new byte[fs.Length]; obj.Name = strName; obj.Type = ResTree.RESTYPE_FILE; int nRet = fs.Read(obj.Content, 0, (int)fs.Length); if (nRet != fs.Length) { throw(new Exception("read stream error")); } return obj; }
// 构造一个目录类型的对象 public static DatabaseObject BuildDirObject( string strName) { DatabaseObject obj = new DatabaseObject(); obj.Content = null; obj.Name = strName; obj.Type = ResTree.RESTYPE_FOLDER; return obj; }
// 克隆 public DatabaseObject Clone() { DatabaseObject newObj = new DatabaseObject(); newObj.Name = this.Name; newObj.Type = this.Type; newObj.Style = this.Style; newObj.Content = ByteArray.GetCopy(this.Content); newObj.TimeStamp = ByteArray.GetCopy(this.TimeStamp); // 递归 for(int i=0;i<this.Children.Count;i++) { DatabaseObject newChild = ((DatabaseObject)this.Children[i]).Clone(); newChild.Parent = this; newObj.Children.Add(newChild); } return newObj; }
// 构造一个配置文件类型的对象 public static DatabaseObject BuildFileObject( string strName, string strFileName) { DatabaseObject obj = new DatabaseObject(); FileStream fs = File.Open(strFileName, FileMode.Open); if (fs.Length > 1024*1024) { throw(new Exception("obj " +strFileName+ " too larg")); } obj.Content = new byte[fs.Length]; obj.Name = strName; obj.Type = ResTree.RESTYPE_FILE; int nRet = fs.Read(obj.Content, 0, (int)fs.Length); if (nRet != fs.Length) { throw(new Exception("read obj " +strFileName+ " error")); } fs.Close(); return obj; }
// 从剪贴板粘贴整个树 private void menu_pasteTreeFromClipboard_Click(object sender, System.EventArgs e) { IDataObject iData = Clipboard.GetDataObject(); if (iData == null || iData.GetDataPresent(typeof(DatabaseObject)) == false) { MessageBox.Show(this, "剪贴板中尚不存在DatabaseObject类型数据"); return; } DatabaseObject root = (DatabaseObject)iData.GetData(typeof(DatabaseObject)); if (root == null) { MessageBox.Show(this, "GetData error"); return; } this.Nodes.Clear(); // 把以前树上的全部节点归纳为删除操作记入日志 PutAllObjToLog(ObjEventOper.Delete, this.Root); //StorageMode oldmode = this.StorageMode; //this.StorageMode = StorageMode.Memory; //DatabaseObject oldroot = this.Root; this.Root = root; this.FillAll(null); //this.Root = oldroot; //this.StorageMode = oldmode; // 把现在树上全部节点作为新增记入日志 PutAllObjToLog(ObjEventOper.New, this.Root); }
public void Initial(DatabaseObject objFile, string strPath) { this.Obj = objFile; this.Path = strPath; // this.textBox_path.Text = this.Path; }
// 将内存对象插入TreeView树 // (暂不改变服务器端的真正对象) // parameters: // nodeInsertPos 插入参考节点,在此前插入。如果==null,表示在parent下级末尾追加 public int InsertObject(TreeNode nodeParent, TreeNode nodeInsertPos, DatabaseObject obj, out string strError) { strError = ""; int nRet = 0; TreeNodeCollection children = null; if (nodeParent == null) { children = this.Nodes; } else { children = nodeParent.Nodes; } ArrayList aObject = new ArrayList(); if (obj.Type == -1 || obj.Type == ResTree.RESTYPE_DB) { aObject.AddRange(obj.Children); } else { aObject.Add(obj); } for (int i = 0; i < aObject.Count; i++) { DatabaseObject perObj = (DatabaseObject)aObject[i]; TreeNode nodeNew = new TreeNode(perObj.Name, perObj.Type, perObj.Type); Debug.Assert(perObj.Type != -1, "类型值尚未初始化"); Debug.Assert(perObj.Type != ResTree.RESTYPE_DB, "插入类型不能为DB"); if (nodeInsertPos == null) children.Add(nodeNew); else { int index = children.IndexOf(nodeInsertPos); if (index == -1) children.Add(nodeNew); else children.Insert(index, nodeNew); } string strPath = this.GetPath(nodeNew); // 把修改记录到日志 ObjEvent objevent = new ObjEvent(); objevent.Obj = perObj; objevent.Oper = ObjEventOper.New; objevent.Path = strPath; this.Log.Add(objevent); /* // 在服务器端兑现 if (this.StorageMode == StorageMode.Real) { MemoryStream stream = null; if (perObj.Type == ResTree.RESTYPE_FILE) stream = new MemoryStream(perObj.Content); string strPath = this.GetPath(nodeNew); nRet = NewServerSideObject(strPath, nodeNew.ImageIndex, stream, perObj.TimeStamp, out strError); if (nRet == -1) return -1; } */ if (perObj.Type == ResTree.RESTYPE_FOLDER) { // 递归 // ResTree.SetLoading(nodeNew); for (int j = 0; j < perObj.Children.Count; j++) { nRet = InsertObject(nodeNew, null, (DatabaseObject)perObj.Children[j], out strError); if (nRet == -1) return -1; } } if (nodeNew.Parent != null) nodeNew.Parent.Expand(); } return 0; }
// 创建新对象 void DoNewObject(bool bInsertAsChild) { NewObjectDlg dlg = new NewObjectDlg(); dlg.Font = GuiUtil.GetDefaultFont(); dlg.Type = ResTree.RESTYPE_FILE; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return; string strPath = ""; string strError = ""; DatabaseObject obj = new DatabaseObject(); obj.Name = dlg.textBox_objectName.Text; obj.Type = dlg.Type; obj.Changed = true; TreeNode node = new TreeNode(obj.Name, obj.Type, obj.Type); TreeNode nodeParent = null; if (bInsertAsChild == false) { // 插入同级 // 查重 TreeNode nodeDup = TreeViewUtil.FindNodeByText((TreeView)this, this.SelectedNode != null ? this.SelectedNode.Parent : null, dlg.textBox_objectName.Text); if (nodeDup != null) { strError = "同名对象已经存在。放弃操作。"; goto ERROR1; } if (this.SelectedNode == null) { strError = "尚未选择基准对象"; goto ERROR1; } nodeParent = this.SelectedNode.Parent; if (nodeParent == null) nodeParent = this.Nodes[0]; } else { // 插入下级 // 查重 TreeNode nodeDup = TreeViewUtil.FindNodeByText((TreeView)this, this.SelectedNode, dlg.textBox_objectName.Text); if (nodeDup != null) { strError = "同名对象已经存在。放弃操作。"; goto ERROR1; } nodeParent = this.SelectedNode; if (nodeParent == null) nodeParent = this.Nodes[0]; } nodeParent.Nodes.Add(node); strPath = TreeViewUtil.GetPath(nodeParent, '/'); DatabaseObject objParent = this.Root.LocateObject(strPath); if (objParent == null) { strError = "路径为 '" + strPath + "' 的内存对象没有找到..."; goto ERROR1; } obj.Parent = objParent; objParent.Children.Add(obj); strPath = TreeViewUtil.GetPath(node, '/'); // 把修改记录到日志 ObjEvent objevent = new ObjEvent(); objevent.Obj = obj; objevent.Oper = ObjEventOper.New; objevent.Path = strPath; this.Log.Add(objevent); /* int nRet = NewServerSideObject(strPath, dlg.Type, null, null, out strError); if (nRet == -1) goto ERROR1; // 刷新? FillAll(null); */ return; ERROR1: MessageBox.Show(this, strError); return; }
public int CreateMemoryObject(out string strError) { strError = ""; if (this.DbName == "" || this.DbName == "?") // 2006/1/20 return 0; DatabaseObject root = new DatabaseObject(); root.Type = ResTree.RESTYPE_DB; root.Name = this.DbName; root.Style = this.DbStyle; int nRet = CreateObject(root, this.DbName, out strError); if (nRet == -1) return -1; this.Root = root; return 0; }
// 将树上全部对象以特定类型加入日志中 public void PutAllObjToLog(ObjEventOper oper, DatabaseObject root) { ObjEvent objevent = new ObjEvent(); objevent.Obj = root; objevent.Oper = oper; objevent.Path = root.MakePath(this.DbName); this.Log.Add(objevent); if (oper == ObjEventOper.Delete && root.Type == ResTree.RESTYPE_FOLDER) return; // 删除目录对象, 只要这个对象进入了日志, 其下级均不必再进入 // 递归 for (int i = 0; i < root.Children.Count; i++) { PutAllObjToLog(oper, (DatabaseObject)root.Children[i]); } }
/* // 将内存对象创建为真正的服务器端对象 public int BuildRealObjects( string strDbName, DatabaseObject root, out string strError) { strError = ""; int nRet = 0; // 创建根 if (root.Type != -1) { if (root.Type == ResTree.RESTYPE_DB) goto DOCHILD; // 忽略本节点,但是继续作下级节点 // 缺省配置文件,忽略保存 if (root.IsDefaultFile() == true) return 0; MemoryStream stream = null; if (root.Type == ResTree.RESTYPE_FILE) stream = new MemoryStream(root.Content); string strPath = root.MakePath(strDbName); // 在服务器端创建对象 nRet = NewServerSideObject(strPath, root.Type, stream, root.TimeStamp, out strError); if (nRet == -1) return -1; } DOCHILD: // 递归 for(int i=0;i<root.Children.Count;i++) { DatabaseObject obj = (DatabaseObject)root.Children[i]; nRet = BuildRealObjects( strDbName, obj, out strError); if (nRet == -1) return -1; } return 0; } */ // 根据路径创建内存对象 public int CreateObject(DatabaseObject obj, string strPath, out string strError) { strError = ""; obj.Children.Clear(); if (obj.Type == ResTree.RESTYPE_FILE) { byte[] baTimeStamp = null; string strMetaData; string strOutputPath; this.channel = Channels.GetChannel(this.ServerUrl); Debug.Assert(channel != null, "Channels.GetChannel() 异常"); // string strStyle = "attachment,data,timestamp,outputpath"; string strStyle = "content,data,timestamp,outputpath"; using (MemoryStream stream = new MemoryStream()) { long lRet = channel.GetRes(strPath, stream, null, // stop, strStyle, null, // byte [] input_timestamp, out strMetaData, out baTimeStamp, out strOutputPath, out strError); if (lRet == -1) { // obj.SetData(null); obj.TimeStamp = null; return 0; // 继续处理 } obj.SetData(stream); obj.TimeStamp = baTimeStamp; } } if (obj.Type == ResTree.RESTYPE_DB || obj.Type == ResTree.RESTYPE_FOLDER) { this.channel = Channels.GetChannel(this.ServerUrl); Debug.Assert(channel != null, "Channels.GetChannel() 异常"); ResInfoItem[] items = null; DigitalPlatform.Stop stop = null; if (stopManager != null) { stop = new DigitalPlatform.Stop(); stop.Register(this.stopManager, true); // 和容器关联 stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在列目录: " + this.ServerUrl + "?" + strPath); stop.BeginLoop(); } long lRet = channel.DoDir(strPath, this.Lang, null, // 不需要返回全部语言的名字 out items, out strError); if (stopManager != null) { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.Unregister(); // 和容器关联 } this.channel = null; if (lRet == -1) return -1; if (items == null) return 0; for (int i = 0; i < items.Length; i++) { // 忽略from类型节点 if (items[i].Type == ResTree.RESTYPE_FROM) continue; DatabaseObject child = new DatabaseObject(); child.Name = items[i].Name; child.Type = items[i].Type; child.Style = items[i].Style; child.Parent = obj; obj.Children.Add(child); int nRet = CreateObject(child, strPath + "/" + items[i].Name, out strError); if (nRet == -1) return -1; } } return 0; }
/* // 把当前Real模式的对象全部构造为内存树 public int BuildMemoryTree( TreeNode treenode, out DatabaseObject root, out string strError) { Debug.Assert(false, "废止"); root = null; strError = ""; //if (this.StorageMode != StorageMode.Real) //{ // strError = "必须在Real存储模式下调用函数BuildMemoryTree()"; // return -1; //} root = new DatabaseObject(); return BuildMemoryTree( treenode, root, out strError); } */ int BuildMemoryTree( TreeNode parentTreeNode, DatabaseObject parentDatabaseObject, out string strError) { strError = ""; TreeNodeCollection children = null; if (parentTreeNode == null) { children = this.Nodes; } else { children = parentTreeNode.Nodes; } //DatabaseObject newObj = null; if (parentTreeNode != null) // 实根 { TreeNode treenode = parentTreeNode; parentDatabaseObject.Type = treenode.ImageIndex; parentDatabaseObject.Name = treenode.Text; if (treenode.ImageIndex == ResTree.RESTYPE_DB) { //newObj = parentDatabaseObject; } else if (treenode.ImageIndex == ResTree.RESTYPE_FOLDER) { /* newObj = DatabaseObject.BuildDirObject(treenode.Text); newObj.Type = treenode.ImageIndex; newObj.Parent = parentDatabaseObject; parentDatabaseObject.Children.Add(newObj); */ //newObj = parentDatabaseObject; } else if (treenode.ImageIndex == ResTree.RESTYPE_FILE) { this.channel = Channels.GetChannel(this.ServerUrl); Debug.Assert(channel != null, "Channels.GetChannel() 异常"); string strPath = ""; byte[] baTimeStamp = null; string strMetaData; string strOutputPath; strPath = this.GetPath(treenode); // string strStyle = "attachment,data,timestamp,outputpath"; string strStyle = "content,data,timestamp,outputpath"; using (MemoryStream stream = new MemoryStream()) { long lRet = channel.GetRes(strPath, stream, null, // stop, strStyle, null, // byte [] input_timestamp, out strMetaData, out baTimeStamp, out strOutputPath, out strError); if (lRet == -1) return -1; parentDatabaseObject.SetData(stream); parentDatabaseObject.TimeStamp = baTimeStamp; } return 0; } else { Debug.Assert(false, "意外的节点类型"); } } for (int i = 0; i < children.Count; i++) { TreeNode treenode = children[i]; DatabaseObject child = new DatabaseObject(); child.Parent = parentDatabaseObject; parentDatabaseObject.Children.Add(child); int nRet = BuildMemoryTree( treenode, child, out strError); if (nRet == -1) return -1; } return 0; }
public void SetRootObject(DatabaseObject root) { this.Nodes.Clear(); // 把以前树上的全部节点归纳为删除操作记入日志 PutAllObjToLog(ObjEventOper.Delete, this.Root); this.Root = root; this.FillAll(null); // 把现在树上全部节点作为新增记入日志 PutAllObjToLog(ObjEventOper.New, this.Root); }