using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using DevComponents.DotNetBar; using System.Xml.Linq; namespace Cyberpipe { public partial class FrmMetadata : Office2007Form { List<string> pipelineLayerNames = new List<string>();//线图层名称 List<string> workwellLayerNames = new List<string>();//工井图层名称 List<string> valueLayerNames = new List<string>();//阀门图层名称 List<string> instrumenLayerNames = new List<string>();//附属物图层名称 string filename = Utility.filename; DataSet dataSet = new DataSet(); bool selectedLayer = false; public FrmMetadata() { InitializeComponent(); loadTreeView(layerTree); } private void FrmMetadata_Load(object sender, EventArgs e) { //this.ntLblC.Text = "nn"; // loadTreeView(layerTree); } private void loadTreeView(TreeView tv)//构建图层树 { try { XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlNodeList Params1 = doc.SelectNodes("/Params/layers"); //tv.ShowPlusMinus = false; //tv.Nodes.Clear(); tv.ShowLines = true; // tv.ImageList = imageList1; tv.CheckBoxes = false; #region 三级 //foreach (XmlNode paramsNode in Params1) //layers //{ // foreach (XmlNode layerNode in paramsNode) //layer // { // TreeNode nodelayer = new TreeNode(); // string layerName = layerNode.Attributes["label"].Value; // nodelayer.Text = layerName; // if (layerNode.HasChildNodes != false) // { // tv.Nodes.Add(nodelayer); // foreach (XmlNode layerchildNode in layerNode) //layerchild // { // if (layerchildNode.Name == "layerchild") // { // TreeNode nodelayerchild = new TreeNode(); // string layerType = layerchildNode.Attributes["type"].Value; // string layerchildName = layerchildNode.Attributes["label"].Value; // if (layerType == "locaserver") // { // string layerName1 = layerchildNode.Attributes["layer"].Value; // nodelayerchild.Text = layerchildName; // nodelayerchild.Tag = layerType + "|" + layerName1; // nodelayerchild.Checked = true; // nodelayer.Nodes.Add(nodelayerchild); // } // else // { // string layerName1 = layerchildNode.Attributes["layer"].Value; // if (layerchildNode.Attributes["isPipeLine"] != null) // { // pipelineLayerNames.Add(layerName1); // } // if (layerchildNode.Attributes["isWorkWell"] != null) // { // workwellLayerNames.Add(layerName1); // } // if (layerchildNode.Attributes["isValve"] != null) // { // valueLayerNames.Add(layerName1); // } // if (layerchildNode.Attributes["isInstrument"] != null) // { // instrumenLayerNames.Add(layerName1); // } // bool boollayer = AddLayers(layerName1, ""); // nodelayerchild.Text = layerchildName; // nodelayerchild.Tag = layerType + "|" + layerName1; // nodelayerchild.Checked = boollayer; // nodelayer.Nodes.Add(nodelayerchild); // } // } // else if (layerchildNode.Name == "layertype") // { // TreeNode nodelayerchild = new TreeNode(); // string layerType = layerchildNode.Attributes["label"].Value; // nodelayerchild.Text = layerType; // nodelayerchild.Tag = layerType; // nodelayerchild.Checked = true; // nodelayer.Nodes.Add(nodelayerchild); // if (layerchildNode.HasChildNodes == true) // { // foreach (XmlNode childNodeLayer in layerchildNode) // { // TreeNode nodelayerchild1 = new TreeNode(); // string layerName1 = childNodeLayer.Attributes["layer"].Value; // string layerchildName = childNodeLayer.Attributes["label"].Value; // string layerType1 = childNodeLayer.Attributes["type"].Value; // if (childNodeLayer.Attributes["isPipeLine"] != null) // { // pipelineLayerNames.Add(layerName1); // } // if (childNodeLayer.Attributes["isWorkWell"] != null) // { // workwellLayerNames.Add(layerName1); // } // if (childNodeLayer.Attributes["isValve"] != null) // { // valueLayerNames.Add(layerName1); // } // if (childNodeLayer.Attributes["isInstrument"] != null) // { // instrumenLayerNames.Add(layerName1); // } // bool boollayer = AddLayers(layerName1, ""); // nodelayerchild1.Text = layerchildName; // nodelayerchild1.Tag = layerType1 + "|" + layerName1; // nodelayerchild1.Checked = boollayer; // nodelayerchild.Nodes.Add(nodelayerchild1); // } // } // } // } // } // else // { // tv.Nodes.Add(nodelayer); // } // } //} #endregion foreach (XmlNode paramsNode in Params1) //layers { foreach (XmlNode layerNode in paramsNode) //layer { TreeNode nodelayer = new TreeNode(); string layerName = layerNode.Attributes["label"].Value; nodelayer.Text = layerName; if (layerNode.HasChildNodes != false) { tv.Nodes.Add(nodelayer); foreach (XmlNode layerchildNode in layerNode) //layerchild { if (layerchildNode.Name == "layerchild") { TreeNode nodelayerchild = new TreeNode(); string layerType = layerchildNode.Attributes["type"].Value; string layerchildName = layerchildNode.Attributes["label"].Value; if (layerType == "locaserver") { string layerName1 = layerchildNode.Attributes["layer"].Value; nodelayerchild.Text = layerchildName; nodelayerchild.Tag = layerType + "|" + layerName1; nodelayerchild.Checked = true; nodelayer.Nodes.Add(nodelayerchild); } else { string layerName1 = layerchildNode.Attributes["layer"].Value; if (layerchildNode.Attributes["isPipeLine"] != null) { pipelineLayerNames.Add(layerName1); } if (layerchildNode.Attributes["isWorkWell"] != null) { workwellLayerNames.Add(layerName1); } if (layerchildNode.Attributes["isValve"] != null) { valueLayerNames.Add(layerName1); } if (layerchildNode.Attributes["isInstrument"] != null) { instrumenLayerNames.Add(layerName1); } //bool boollayer = AddLayers(layerName1, ""); nodelayerchild.Text = layerchildName; nodelayerchild.Tag = layerType + "|" + layerName1; //nodelayerchild.Checked = boollayer; nodelayer.Nodes.Add(nodelayerchild); } } else if (layerchildNode.Name == "layertype") { TreeNode nodelayerchild = new TreeNode(); string layerType = layerchildNode.Attributes["label"].Value; nodelayerchild.Text = layerType; nodelayerchild.Tag = layerType; nodelayerchild.Checked = true; nodelayer.Nodes.Add(nodelayerchild); if (layerchildNode.HasChildNodes == true) { foreach (XmlNode childNodeLayer in layerchildNode) { if (childNodeLayer.HasChildNodes == true) { TreeNode nodelayerchild2 = new TreeNode(); string layer = childNodeLayer.Attributes["label"].Value; nodelayerchild2.Text = layer; nodelayerchild2.Tag = layer; nodelayerchild2.Checked = true; nodelayerchild.Nodes.Add(nodelayerchild2); foreach (XmlNode n in childNodeLayer) { TreeNode nodelayerchild1 = new TreeNode(); string layerName1 = n.Attributes["layer"].Value; string layerchildName = n.Attributes["label"].Value; string layerType1 = n.Attributes["type"].Value; if (n.Attributes["isPipeLine"] != null) { pipelineLayerNames.Add(layerName1); } if (n.Attributes["isWorkWell"] != null) { workwellLayerNames.Add(layerName1); } if (n.Attributes["isValve"] != null) { valueLayerNames.Add(layerName1); } if (n.Attributes["isInstrument"] != null) { instrumenLayerNames.Add(layerName1); } //bool boollayer = AddLayers(layerName1, ""); nodelayerchild1.Text = layerchildName; nodelayerchild1.Tag = layerType1 + "|" + layerName1; //nodelayerchild1.Checked = boollayer; nodelayerchild2.Nodes.Add(nodelayerchild1); } } else { TreeNode nodelayerchild1 = new TreeNode(); string layerName1 = childNodeLayer.Attributes["layer"].Value; string layerchildName = childNodeLayer.Attributes["label"].Value; string layerType1 = childNodeLayer.Attributes["type"].Value; if (childNodeLayer.Attributes["isPipeLine"] != null) { pipelineLayerNames.Add(layerName1); } if (childNodeLayer.Attributes["isWorkWell"] != null) { workwellLayerNames.Add(layerName1); } if (childNodeLayer.Attributes["isValve"] != null) { valueLayerNames.Add(layerName1); } if (childNodeLayer.Attributes["isInstrument"] != null) { instrumenLayerNames.Add(layerName1); } //bool boollayer = AddLayers(layerName1, ""); nodelayerchild1.Text = layerchildName; nodelayerchild1.Tag = layerType1 + "|" + layerName1; //nodelayerchild1.Checked = boollayer; nodelayerchild.Nodes.Add(nodelayerchild1); } } } } } } else { tv.Nodes.Add(nodelayer); } } } if (tv.Nodes.Count > 0) { //tv.Nodes[0].Expand(); if (tv.Nodes.Count > 0) { foreach (TreeNode node in tv.Nodes) { //node.Expand(); } } } //tv.ExpandAll(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void layerTree_AfterSelect(object sender, TreeViewEventArgs e) { String layername = this.layerTree.SelectedNode.Text; if (this.layerTree.SelectedNode.GetNodeCount(true) == 0) { searchLayerInfo(layername); selectedLayer = true; clearBtn.Hide(); completeBtn.Hide(); } else { } } //查询元数据 private void searchLayerInfo(String layername) { string sql = "select casic_metadatacontent.\"MDABSTRACT\",casic_metadatacontent.\"MDDESCRIPTION\",casic_metadatacontent.\"MDAUTHOR\",casic_metadatacontent.\"NT\" from casic_metadatacontent," + " casic_metadatalayer where casic_metadatacontent.\"LAYERID\" = casic_metadatalayer.\"LAYERID\" and casic_metadatalayer.\"LAYERNAME\"='" + layername+"'"; DataTable table = OledbHelper.QueryTable(sql); int num = table.Rows.Count; if (table != null && table.Rows.Count > 0) { groupBox1.Text = layername; consLblC.Text = table.Rows[0][0].ToString(); descLblC.Text = table.Rows[0][1].ToString(); authorLblC.Text = table.Rows[0][2].ToString(); ntLblC.Text = table.Rows[0][3].ToString(); } else { groupBox1.Text = layername; MessageBox.Show("没有查到该图层的元数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.consLblC.Text = ""; this.descLblC.Text = ""; this.authorLblC.Text = ""; this.ntLblC.Text = ""; } consTxt.Visible = false; descTxt.Visible = false; authorTxt.Visible = false; ntTxt.Visible = false; consLblC.Visible = true; descLblC.Visible = true; authorLblC.Visible = true; ntLblC.Visible = true; } //导出元数据 private void printBtn_Click(object sender, EventArgs e) { //printDialog1.ShowDialog(); //用默认的所有者使用通用对话框 //printPreviewDialog1.Document = this.printDocument1; //设置打印文档 //printPreviewDialog1.ShowDialog(); //if (printDialog1.ShowDialog() == DialogResult.OK) //{ // printDocument1.Print(); //} //导出xml文件 XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("metadata", new XElement("cons", consLblC.Text), new XElement("desc", descLblC.Text), new XElement("author", authorLblC.Text), new XElement("nt", ntLblC.Text))); FrmMetadataOutput FrmOut = new FrmMetadataOutput(doc,groupBox1.Text);//实例化Frm_Folder窗体 FrmSetUp(FrmOut);//调用自定义方法FrmSetUp,对Frm_Radical窗体进行设置 if (FrmOut.ShowDialog() == DialogResult.OK)//显示窗体 { MessageBox.Show("导出成功"); } } public void FrmSetUp(Form Frm) //Form或MouseEventArgs添加命名空间using System.Windows.Forms; { Frm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;//使窗体居中 } //导入元数据 private void importBtn_Click(object sender, EventArgs e) { //日志记录 LogManager.saveLog(Utility.userName,"元数据"+ this.importBtn.Text); //判断此时layerTree有没有选中的图层 if (selectedLayer)//有选中的图层,获得其名 { string name = this.layerTree.SelectedNode.Text; OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "支持格式(*.xml)|*.xml|XML数据(*.xml)|"; dlg.Multiselect = true; if (dlg.ShowDialog() == DialogResult.OK) { AddXMLMDdata(dlg.FileName); } } else { MessageBox.Show("请选择一个要导入元数据的图层"); } } //读入元数据 private void AddXMLMDdata(string strFilePath) { try { dataSet.Clear(); //清空数据集中的内容 dataSet.ReadXml(strFilePath); //读取XML文件中的内容 //string attrtitle = dataSet.Tables[0].Rows[0][0].ToString(); string attrcons = dataSet.Tables[0].Rows[0][0].ToString(); string attrdesc = dataSet.Tables[0].Rows[0][1].ToString(); string attrauthor = dataSet.Tables[0].Rows[0][2].ToString(); string attrnt = dataSet.Tables[0].Rows[0][3].ToString(); //this.titleLbl.Text = attrtitle; this.consLblC.Text = attrcons; this.descLblC.Text = attrdesc; this.authorLblC.Text = attrauthor; this.ntLblC.Text = attrnt; //MessageBox.Show(attrtitle + "==" + attrcons + "==" + attrdesc + "==" + attrauthor + "==" + attrnt); this.completeBtn.Visible = true; //this.clearBtn.Visible = true; } catch (Exception e) { } } //新建/编辑元数据 private void editBtn_Click(object sender, EventArgs e) { this.consTxt.Visible = true; this.descTxt.Visible = true; this.authorTxt.Visible = true; this.ntTxt.Visible = true; this.completeBtn.Visible = true; this.clearBtn.Visible = true; this.consTxt.Text = this.consLblC.Text; this.descTxt.Text = this.descLblC.Text; this.authorTxt.Text = this.authorLblC.Text; this.ntTxt.Text = this.ntLblC.Text; this.consLblC.Visible = false; this.descLblC.Visible = false; this.authorLblC.Visible = false; this.ntLblC.Visible = false; } //提交新建/编辑 private void completeBtn_Click(object sender, EventArgs e) { //判断是新建还是编辑 if (searchLayer(groupBox1.Text)) { //MessageBox.Show("new"); if ((this.consLblC.Visible == true) && (this.consTxt.Visible == false))//提交xml文件时新建 { saveData("new", groupBox1.Text, this.consLblC.Text, this.descLblC.Text, this.authorLblC.Text, this.ntLblC.Text); } else { saveData("new", groupBox1.Text, this.consTxt.Text, this.descTxt.Text, this.authorTxt.Text, this.ntTxt.Text); } }else{ //MessageBox.Show("update"); if ((this.consLblC.Visible == true) && (this.consTxt.Visible == false))//提交xml文件时更新 { saveData("update", groupBox1.Text, this.consLblC.Text, this.descLblC.Text, this.authorLblC.Text, this.ntLblC.Text); } else { //直接修改更新 saveData("update", groupBox1.Text, this.consTxt.Text, this.descTxt.Text, this.authorTxt.Text, this.ntTxt.Text); } } this.consTxt.Visible = false; this.descTxt.Visible = false; this.authorTxt.Visible = false; this.ntTxt.Visible = false; this.completeBtn.Visible = false; this.clearBtn.Visible = false; this.consLblC.Visible = true; this.descLblC.Visible = true; this.authorLblC.Visible = true; this.ntLblC.Visible = true; } //判断是新建还是编辑 private bool searchLayer(string layername) { string sql = "select \"LAYERID\" from casic_metadatalayer where \"LAYERNAME\" = '" + layername + "'"; DataTable table = OledbHelper.QueryTable(sql); if (table != null && table.Rows.Count > 0)//已有该图层,属于编辑 { return false; } else//没有该图层,属于新建 { return true; } } //保存元数据 private void saveData(String method,String layername,String cons,String desc,String author,String nt) { if (method.Equals("new"))//插入 { string searchSQL = "select \"LAYERID\" from casic_metadatalayer order by \"LAYERID\" desc"; DataTable table = OledbHelper.QueryTable(searchSQL); int layerid = 0; if (table.Rows.Count > 0) { int num = Convert.ToInt32(table.Rows[0][0].ToString()); layerid = num + 1; } else { layerid = 1; } //MessageBox.Show(layerid.ToString()); string insertSQL = "insert into casic_metadatalayer values ('" + layerid + "','" + layername + "')";//+ string insertSQL2 = "insert into casic_metadatacontent values ('" + layerid + "','" + cons + "','" + desc + "','" + author + "','" + nt + "')"; // "insert into casic_metadatacontent values ('" + layerid + "','" + cons + "','"+desc+"','"+author+"','"+nt+"')"; if ((OledbHelper.sqlExecuteNonQuery(insertSQL) > 0)&&(OledbHelper.sqlExecuteNonQuery(insertSQL2)>0)) { MessageBox.Show("保存元数据成功!"); this.consLblC.Text = cons; this.descLblC.Text = desc; this.authorLblC.Text = author; this.ntLblC.Text = nt; //DialogResult = DialogResult.OK; //Close(); } } else { //更新 string layeridSQL = "select layerid from casic_metadatalayer where LAYERNAME='"+layername+"'"; DataTable table = OledbHelper.QueryTable(layeridSQL); string layerid = table.Rows[0][0].ToString(); string updateSQL = "update casic_metadatacontent set casic_metadatacontent.MDABSTRACT ='" + cons.Trim() + "',casic_metadatacontent.MDDESCRIPTION ='" + desc.Trim() + "',casic_metadatacontent.MDAUTHOR ='" + author.Trim() + "',casic_metadatacontent.NT ='" + nt.Trim() + "' where casic_metadatacontent.LAYERID='" + layerid + "'"; if (OledbHelper.sqlExecuteNonQuery(updateSQL) > 0) { MessageBox.Show("更新元数据成功!"); this.consLblC.Text = cons; this.descLblC.Text = desc; this.authorLblC.Text = author; this.ntLblC.Text = nt; //DialogResult = DialogResult.OK; //Close(); } } } //清空元数据内容,但不删除该图层 private void clearBtn_Click(object sender, EventArgs e) { this.consTxt.Text = ""; this.descTxt.Text = ""; this.authorTxt.Text = ""; this.ntTxt.Text = ""; string layername = groupBox1.Text;//this.titleLbl.Text; string clearSQL = "update casic_metadatacontent set MDABSTRACT ='" + consTxt.Text.Trim() + "',MDDESCRIPTION ='" + descTxt.Text.Trim() + "',MDAUTHOR ='" + authorTxt.Text.Trim() + "',NT ='" + ntTxt.Text.Trim() + "' where LAYERID in (select LAYERID from casic_metadatalayer where LAYERNAME='" + layername + "')"; if (OledbHelper.sqlExecuteNonQuery(clearSQL) > 0) { MessageBox.Show("清空元数据成功!"); //DialogResult = DialogResult.OK; //Close(); } } //设置打印窗口 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //绘制label1中的内容 e.Graphics.DrawString(this.groupBox1.Text+"元数据", new Font("宋体", 20, FontStyle.Regular), Brushes.Black, 350, 10); //绘制textBox1中的内容 e.Graphics.DrawString(this.consLbl.Text, new Font("宋体", 15, FontStyle.Bold), Brushes.Black, 10, 100); e.Graphics.DrawString(this.consLblC.Text, new Font("宋体", 15, FontStyle.Regular), Brushes.Black, 10, 120); //绘制label3中的内容 e.Graphics.DrawString(this.descLbl.Text, new Font("宋体", 15, FontStyle.Bold), Brushes.Black, 10, 200); e.Graphics.DrawString(this.descLblC.Text, new Font("宋体", 15, FontStyle.Regular), Brushes.Black,10, 220); //绘制textBox2中的内容 e.Graphics.DrawString(this.authorLbl.Text, new Font("宋体", 15, FontStyle.Bold), Brushes.Black, 10, 300); e.Graphics.DrawString(this.authorLblC.Text, new Font("宋体", 15, FontStyle.Regular), Brushes.Black, 10, 320); //绘制label3中的内容 e.Graphics.DrawString(this.ntLbl.Text, new Font("宋体", 15, FontStyle.Bold), Brushes.Black, 10, 400); e.Graphics.DrawString(this.ntLblC.Text, new Font("宋体", 15, FontStyle.Regular), Brushes.Black, 10, 420); } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { this.consLblC.Text = ""; this.descLblC.Text = ""; this.authorLblC.Text = ""; this.ntLblC.Text = ""; string layername = this.groupBox1.Text; string clearSQL1 = "delete casic_metadatacontent where \"LAYERID\" in(select \"LAYERID\" from casic_metadatalayer where \"LAYERNAME\"= '" + layername + "')"; string clearSQL2 = "delete casic_metadatalayer where \"LAYERNAME\"= '" + layername + "'"; OledbHelper.getsqlcom(clearSQL1); OledbHelper.getsqlcom(clearSQL2); MessageBox.Show("删除元数据成功!"); } } }