Esempio n. 1
0
        public void ReadFile(string fileName)
        {
            XmlNode       node;
            string        src, tgt, val;
            StringBuilder sbLoadingErrors = new StringBuilder();

            if (!File.Exists(fileName))
            {
                MessageBox.Show("File does not exists!");
                return;
            }

            if (!thePageNavigator1.GotoBeginning())
            {
                return;
            }

            GlobalVariables.LoadedColumnMappings.Clear();
            GlobalVariables.LoadedUserColumnsDefinitions.Clear();
            src = tgt = val = string.Empty;
            //trfType = ETransformationType.SKIP;

            pageSourceConnection.rtbSrcConnStr.Text         = string.Empty;
            pageSourceConnection.cbxSrcTableOrViewName.Text = string.Empty;
            pageTargetConnection.rtbTgtConnStr.Text         = string.Empty;
            pageTargetConnection.cbxTgtTableOrViewName.Text = string.Empty;

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(fileName);

                // Source Connection
                node = doc.DocumentElement.SelectSingleNode("/ScdMergeWizard/SourceConnection/ConnectionString");
                if (node == null)
                {
                    node = doc.DocumentElement.SelectSingleNode("/MERGER/SourceConnection/ConnectionString");
                }
                if (node != null)
                {
                    pageSourceConnection.rtbSrcConnStr.Text = node.InnerText;
                }
                else
                {
                    sbLoadingErrors.AppendLine("Cannot read Source ConnectionString");
                }


                node = doc.DocumentElement.SelectSingleNode("/ScdMergeWizard/SourceConnection/IsTableOrView");
                if (node == null)
                {
                    node = doc.DocumentElement.SelectSingleNode("/MERGER/SourceConnection/IsTableOrView");
                }
                if (node != null)
                {
                    pageSourceConnection.rbIsTableOrView.Checked = (node.InnerText == "True");
                    pageSourceConnection.rbIsCommandText.Checked = (node.InnerText != "True");
                }
                else
                {
                    sbLoadingErrors.AppendLine("Cannot read IsTableOrView");
                }

                if (pageSourceConnection.rbIsTableOrView.Checked)
                {
                    GlobalVariables.SourceConnection = DbHelper.CreateConnection(pageSourceConnection.rtbSrcConnStr.Text);
                    pageSourceConnection.cbxSrcTableOrViewName.AddItems(DbHelper.GetTablesViewsAndSynonyms(GlobalVariables.SourceConnection));
                }


                node = doc.DocumentElement.SelectSingleNode("/ScdMergeWizard/SourceConnection/TableOrViewName");
                if (node == null)
                {
                    node = doc.DocumentElement.SelectSingleNode("/MERGER/SourceConnection/TableOrView");
                }
                if (node != null)
                {
                    pageSourceConnection.cbxSrcTableOrViewName.Text = node.InnerText;
                }
                else
                {
                    sbLoadingErrors.AppendLine("Cannot read Source TableOrViewName");
                }


                node = doc.DocumentElement.SelectSingleNode("/ScdMergeWizard/SourceConnection/CommandText");
                if (node == null)
                {
                    node = doc.DocumentElement.SelectSingleNode("/MERGER/SourceConnection/CommandText");
                }
                if (node != null)
                {
                    pageSourceConnection.rtbCommandText.Text = node.InnerText.Replace("<![CDATA[", "").Replace("]]>", "");
                }
                else
                {
                    sbLoadingErrors.AppendLine("Cannot read CommandText");
                }


                // Target Connection
                node = doc.DocumentElement.SelectSingleNode("/ScdMergeWizard/TargetConnection/ConnectionString");
                if (node == null)
                {
                    node = doc.DocumentElement.SelectSingleNode("/MERGER/TargetConnection/ConnectionString");
                }
                if (node != null)
                {
                    pageTargetConnection.rtbTgtConnStr.Text = node.InnerText;
                }
                else
                {
                    sbLoadingErrors.AppendLine("Cannot read Target ConnectionString");
                }


                GlobalVariables.TargetConnection = DbHelper.CreateConnection(pageTargetConnection.rtbTgtConnStr.Text);
                pageTargetConnection.cbxTgtTableOrViewName.AddItems(DbHelper.GetTablesViewsAndSynonyms(GlobalVariables.TargetConnection));


                node = doc.DocumentElement.SelectSingleNode("/ScdMergeWizard/TargetConnection/TableOrViewName");
                if (node == null)
                {
                    node = doc.DocumentElement.SelectSingleNode("/MERGER/TargetConnection/TableOrView");
                }
                if (node != null)
                {
                    pageTargetConnection.cbxTgtTableOrViewName.Text = node.InnerText;
                }
                else
                {
                    sbLoadingErrors.AppendLine("Cannot read TableOrViewName");
                }


                // User Variables
                foreach (XmlNode n in doc.DocumentElement.SelectSingleNode("/ScdMergeWizard/UserVariables").ChildNodes)
                {
                    MyUserVariable uv = new MyUserVariable();

                    node    = doc.DocumentElement.SelectSingleNode(string.Format("/ScdMergeWizard/UserVariables/{0}/Name", n.Name));
                    uv.Name = node.InnerText;

                    node        = doc.DocumentElement.SelectSingleNode(string.Format("/ScdMergeWizard/UserVariables/{0}/DataType", n.Name));
                    uv.DataType = node.InnerText;

                    node          = doc.DocumentElement.SelectSingleNode(string.Format("/ScdMergeWizard/UserVariables/{0}/Value", n.Name));
                    uv.Definition = node.InnerText;

                    GlobalVariables.LoadedUserColumnsDefinitions.Add(uv);
                }


                // Options
                node = doc.DocumentElement.SelectSingleNode("/ScdMergeWizard/Options/RecordsOnTargetNotFoundOnSourceMode");
                if (node != null)
                {
                    GlobalVariables.Options.RecordsOnTargetNotFoundOnSourceMode = (ERecordsOnTargetNotFoundOnSourceMode)Enum.Parse(typeof(ERecordsOnTargetNotFoundOnSourceMode), node.InnerText);
                }
                else
                {
                    node = doc.DocumentElement.SelectSingleNode("/MERGER/Options/RecordsOnTargetNotFoundOnSourceEx");
                    if (node != null)
                    {
                        if (node.InnerText.Equals("UpdateDateTo"))
                        {
                            GlobalVariables.Options.RecordsOnTargetNotFoundOnSourceMode = ERecordsOnTargetNotFoundOnSourceMode.UpdateTargetField;
                        }
                        else if (node.InnerText.Equals("PhysicallyDelete"))
                        {
                            GlobalVariables.Options.RecordsOnTargetNotFoundOnSourceMode = ERecordsOnTargetNotFoundOnSourceMode.PhysicallyDelete;
                        }
                        else if (node.InnerText.Equals("DoNothing"))
                        {
                            GlobalVariables.Options.RecordsOnTargetNotFoundOnSourceMode = ERecordsOnTargetNotFoundOnSourceMode.DoNothing;
                        }
                    }
                    else
                    {
                        sbLoadingErrors.AppendLine("Cannot read RecordsOnTargetNotFoundOnSourceMode");
                    }
                }

                node = doc.DocumentElement.SelectSingleNode("/ScdMergeWizard/Options/IgnoreDatabasePrefix");
                if (node == null)
                {
                    node = doc.DocumentElement.SelectSingleNode("/MERGER/Options/IgnoreDatabasePrefix");
                }
                if (node != null)
                {
                    GlobalVariables.Options.IgnoreDatabasePrefix = (node.InnerText == "True");
                }
                else
                {
                    sbLoadingErrors.AppendLine("Cannot read IgnoreDatabasePrefix");
                }


                node = doc.DocumentElement.SelectSingleNode("/ScdMergeWizard/Options/ShowExtendedComments");
                if (node == null)
                {
                    node = doc.DocumentElement.SelectSingleNode("/MERGER/Options/ShowExtendedComments");
                }
                if (node != null)
                {
                    GlobalVariables.Options.ShowExtendedComments = (node.InnerText == "True");
                }
                else
                {
                    sbLoadingErrors.AppendLine("Cannot read ShowExtendedComments");
                }

                node = doc.DocumentElement.SelectSingleNode("/ScdMergeWizard/Options/SCD2VersionNumberMode");
                if (node != null)
                {
                    GlobalVariables.Options.SCD2VersionNumberMode = (ESCD2VersionNumberMode)Enum.Parse(typeof(ESCD2VersionNumberMode), node.InnerText);
                }
                else
                {
                    sbLoadingErrors.AppendLine("Cannot read VersionNumberResetOnSCD2");
                }

                node = doc.DocumentElement.SelectSingleNode("/ScdMergeWizard/Options/SCD13UpdateMode");
                if (node != null)
                {
                    GlobalVariables.Options.SCD13UpdateMode = (ESCD13UpdateMode)Enum.Parse(typeof(ESCD13UpdateMode), node.InnerText);
                }
                else
                {
                    sbLoadingErrors.AppendLine("Cannot read SCD13UpdateMode");
                }

                node = doc.DocumentElement.SelectSingleNode("/ScdMergeWizard/Options/ComparisonMethod");
                if (node != null)
                {
                    GlobalVariables.Options.ComparisonMethod = (EComparisonMethod)Enum.Parse(typeof(EComparisonMethod), node.InnerText);
                }
                else
                {
                    sbLoadingErrors.AppendLine("Cannot read ComparisonMethod");
                }

                // Column Mappings
                node = doc.DocumentElement.SelectSingleNode("/ScdMergeWizard/ColumnMappings");
                if (node != null)
                {
                    foreach (XmlNode n in node.ChildNodes)
                    {
                        node = doc.DocumentElement.SelectSingleNode(string.Format("/ScdMergeWizard/ColumnMappings/{0}/SourceColumn", n.Name));
                        var srcc = node.InnerText;

                        node = doc.DocumentElement.SelectSingleNode(string.Format("/ScdMergeWizard/ColumnMappings/{0}/TransformationType", n.Name));
                        var trfc = (ETransformationCode)Enum.Parse(typeof(ETransformationCode), node.InnerText);

                        ETargetDatabaseType tgtdbt;
                        node = doc.DocumentElement.SelectSingleNode(string.Format("/ScdMergeWizard/ColumnMappings/{0}/TargetDatabaseType", n.Name));
                        if (node != null)
                        {
                            tgtdbt = (ETargetDatabaseType)Enum.Parse(typeof(ETargetDatabaseType), node.InnerText);
                        }
                        else
                        {
                            tgtdbt = ETargetDatabaseType.TARGET;
                        }

                        node = doc.DocumentElement.SelectSingleNode(string.Format("/ScdMergeWizard/ColumnMappings/{0}/TargetColumn", n.Name));
                        var tgtc = node.InnerText;

                        string val1c = string.Empty, val2c = string.Empty, cins = string.Empty, cupd = string.Empty, cdel = string.Empty;
                        // Old version
                        node = doc.DocumentElement.SelectSingleNode(string.Format("/ScdMergeWizard/ColumnMappings/{0}/Value1Column", n.Name));
                        if (node != null)
                        {
                            val1c = node.InnerText;
                        }

                        node = doc.DocumentElement.SelectSingleNode(string.Format("/ScdMergeWizard/ColumnMappings/{0}/Value2Column", n.Name));
                        if (node != null)
                        {
                            val2c = node.InnerText;
                        }

                        // New version

                        node = doc.DocumentElement.SelectSingleNode(string.Format("/ScdMergeWizard/ColumnMappings/{0}/CustomInsertValue", n.Name));
                        if (node != null)
                        {
                            cins = node.InnerText;
                        }

                        node = doc.DocumentElement.SelectSingleNode(string.Format("/ScdMergeWizard/ColumnMappings/{0}/CustomUpdateValue", n.Name));
                        if (node != null)
                        {
                            cupd = node.InnerText;
                        }

                        node = doc.DocumentElement.SelectSingleNode(string.Format("/ScdMergeWizard/ColumnMappings/{0}/CustomDeleteValue", n.Name));
                        if (node != null)
                        {
                            cdel = node.InnerText;
                        }

                        EColumnCompareMethod ccm;
                        node = doc.DocumentElement.SelectSingleNode(string.Format("/ScdMergeWizard/ColumnMappings/{0}/ColumnCompareMethod", n.Name));
                        if (node != null)
                        {
                            ccm = (EColumnCompareMethod)Enum.Parse(typeof(EColumnCompareMethod), node.InnerText);
                        }
                        else
                        {
                            ccm = EColumnCompareMethod.Default;
                        }


                        if (string.IsNullOrEmpty(cins) && !string.IsNullOrEmpty(val1c) && (trfc == ETransformationCode.SCD2_IS_ACTIVE || trfc == ETransformationCode.SCD3_PREVIOUS_VALUE || trfc == ETransformationCode.CREATED_DATE))
                        {
                            cins = val1c;
                        }
                        if (string.IsNullOrEmpty(cupd) && !string.IsNullOrEmpty(val1c) && (trfc == ETransformationCode.SCD2_DATE_FROM || trfc == ETransformationCode.SCD2_DATE_TO || trfc == ETransformationCode.SCD3_DATE_FROM || trfc == ETransformationCode.MODIFIED_DATE))
                        {
                            cupd = val1c;
                        }
                        if (string.IsNullOrEmpty(cdel) && !string.IsNullOrEmpty(val1c) && (trfc == ETransformationCode.DELETED_DATE || trfc == ETransformationCode.IS_DELETED))
                        {
                            cdel = val1c;
                        }

                        if (string.IsNullOrEmpty(cins) && !string.IsNullOrEmpty(val2c) && (trfc == ETransformationCode.SCD2_DATE_FROM || trfc == ETransformationCode.SCD2_DATE_TO || trfc == ETransformationCode.SCD3_DATE_FROM || trfc == ETransformationCode.MODIFIED_DATE || trfc == ETransformationCode.DELETED_DATE || trfc == ETransformationCode.IS_DELETED))
                        {
                            cins = val2c;
                        }
                        if (string.IsNullOrEmpty(cupd) && !string.IsNullOrEmpty(val2c) && (trfc == ETransformationCode.SCD2_IS_ACTIVE))
                        {
                            cupd = val2c;
                        }

                        GlobalVariables.LoadedColumnMappings.Add(new ColumnMapping(srcc, tgtdbt, tgtc, trfc, cins, cupd, cdel, ccm));
                    }
                }
                else
                {
                    node = doc.DocumentElement.SelectSingleNode("/MERGER/Mappings");
                    if (node != null)
                    {
                        foreach (XmlNode n in node.ChildNodes)
                        {
                            node = doc.DocumentElement.SelectSingleNode(string.Format("/MERGER/Mappings/{0}/Source", n.Name));
                            var srcc = node.InnerText;

                            node = doc.DocumentElement.SelectSingleNode(string.Format("/MERGER/Mappings/{0}/Transformation", n.Name));

                            ETransformationCode tt = ETransformationCode.SKIP;
                            try
                            {
                                tt = (ETransformationCode)Enum.Parse(typeof(ETransformationCode), node.InnerText);
                            }
                            catch { }

                            var trfc = tt;

                            node = doc.DocumentElement.SelectSingleNode(string.Format("/MERGER/Mappings/{0}/Target", n.Name));
                            var tgtc = node.InnerText;

                            GlobalVariables.LoadedColumnMappings.Add(new ColumnMapping(srcc, ETargetDatabaseType.TARGET, tgtc, trfc, string.Empty, string.Empty, string.Empty, EColumnCompareMethod.Default));
                        }
                    }
                    else
                    {
                        sbLoadingErrors.AppendLine("Cannot load ColumnMappings");
                    }
                }

                RecentFilesManager.Add(fileName);
                loadRecentFiles();
                GlobalVariables.IsProjectModified = false;
            }
            catch (Exception ex)
            {
                MyExceptionHandler.NewEx(ex);
            }

            FileName = fileName;
        }
Esempio n. 2
0
        private void save(bool isSaveAs)
        {
            if (string.IsNullOrEmpty(FileName) || isSaveAs)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.DefaultExt       = "mwpxml";
                sfd.Filter           = "SCD Merge Wizard Project Files|*.mwpxml";
                sfd.RestoreDirectory = true;
                sfd.Title            = "Save Project";

                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    FileName = sfd.FileName;
                }
                else
                {
                    return;
                }

                if (string.IsNullOrEmpty(FileName))
                {
                    return;
                }
            }

            using (XmlWriter writer = XmlWriter.Create(FileName))
            {
                writer.WriteStartElement("ScdMergeWizard");

                writer.WriteStartElement("SourceConnection");
                writer.WriteElementString("ConnectionString", pageSourceConnection.rtbSrcConnStr.Text);
                writer.WriteElementString("IsTableOrView", pageSourceConnection.rbIsTableOrView.Checked.ToString());
                writer.WriteElementString("TableOrViewName", pageSourceConnection.cbxSrcTableOrViewName.Text);
                writer.WriteElementString("CommandText", "<![CDATA[" + pageSourceConnection.rtbCommandText.Text + "]]>");
                writer.WriteEndElement();

                writer.WriteStartElement("TargetConnection");
                writer.WriteElementString("ConnectionString", pageTargetConnection.rtbTgtConnStr.Text);
                writer.WriteElementString("TableOrViewName", pageTargetConnection.cbxTgtTableOrViewName.Text);
                writer.WriteEndElement();


                writer.WriteStartElement("UserVariables");
                for (int i = 0; i < GlobalVariables.UserColumnsDefinitions.Count; i++)
                {
                    MyUserVariable uv = GlobalVariables.UserColumnsDefinitions[i];

                    writer.WriteStartElement("UserVariable_" + i.ToString());

                    writer.WriteElementString("Name", uv.Name);
                    writer.WriteElementString("DataType", uv.DataType);
                    writer.WriteElementString("Value", "<![CDATA[" + uv.Definition + "]]>");

                    writer.WriteEndElement();
                }

                writer.WriteEndElement();


                writer.WriteStartElement("Options");

                writer.WriteElementString("RecordsOnTargetNotFoundOnSourceMode", GlobalVariables.Options.RecordsOnTargetNotFoundOnSourceMode.ToString());

                writer.WriteElementString("IgnoreDatabasePrefix", GlobalVariables.Options.IgnoreDatabasePrefix.ToString());
                writer.WriteElementString("ShowExtendedComments", GlobalVariables.Options.ShowExtendedComments.ToString());
                writer.WriteElementString("SCD2VersionNumberMode", GlobalVariables.Options.SCD2VersionNumberMode.ToString());
                writer.WriteElementString("SCD13UpdateMode", GlobalVariables.Options.SCD13UpdateMode.ToString());
                writer.WriteElementString("ComparisonMethod", GlobalVariables.Options.ComparisonMethod.ToString());
                writer.WriteEndElement();

                writer.WriteStartElement("ColumnMappings");

                for (int i = 0; i < GlobalVariables.ColumnMappings.Count; i++)
                {
                    ColumnMapping cm = GlobalVariables.ColumnMappings[i];

                    writer.WriteStartElement("ColumnMapping_" + i.ToString());

                    writer.WriteElementString("SourceColumn", cm.SourceColumn);
                    writer.WriteElementString("TransformationType", cm.TransformationCode.ToString());
                    writer.WriteElementString("TargetDatabaseType", cm.TargetDatabaseType.ToString());
                    writer.WriteElementString("TargetColumn", cm.TargetColumn);
                    //writer.WriteElementString("Value1Column", cm.Value1Column);
                    //writer.WriteElementString("Value2Column", cm.Value2Column);
                    writer.WriteElementString("CustomInsertValue", cm.CustomInsertValue);
                    writer.WriteElementString("CustomUpdateValue", cm.CustomUpdateValue);
                    writer.WriteElementString("CustomDeleteValue", cm.CustomDeleteValue);

                    writer.WriteElementString("ColumnCompareMethod", cm.ColumnCompareMethod.ToString());

                    writer.WriteEndElement();
                }

                writer.WriteEndElement(); // Mappings

                writer.WriteEndElement(); // MERGER
            }

            RecentFilesManager.Add(FileName);
            GlobalVariables.IsProjectModified = false;
        }