using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Windows.Markup; using System.Data; using System.Windows.Data; using System.Windows; using System.Collections.ObjectModel; namespace CustomScreens { public class BlobToXMLConverter : IValueConverter, IRequireEnvironmentContext { private ObservableCollection _budgetData = null; private Budgetdata _budgetdataitem = null; private XmlDocument templateXML = null; private XmlDocument budgetVersXML = null; private string _currentLedger = string.Empty; private string _lastLedger = string.Empty; private string _enField = string.Empty; private string currentXML = string.Empty; private XmlDocument xmlData = new XmlDocument(); #region IRequireEnvironmentContext Members public IEnvironmentContext EnvironmentContext { get; set; } #endregion /// /// IValueConverter method to convert blob data to an XML. /// /// /// /// /// /// public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { _budgetData = null; if (value != System.DBNull.Value) { try { templateXML = (XmlDocument)Conversion.Zip.Decompress((byte[])value, typeof(XmlDocument)); // Save initial value for ChangesExist comparison. this.currentXML = templateXML.InnerXml.ToString(); LoadXMLtoGrid(false); } catch { InitializeGrid(); } if (parameter != null) { ArrayExtension convParameters = parameter as ArrayExtension; _lastLedger = convParameters.Items[0].ToString(); budgetVersXML = new XmlDocument(); budgetVersXML.LoadXml(convParameters.Items[1].ToString()); } } else { InitializeGrid(); } return _budgetData; } /// /// IValueConverter method to convertback XML to byte array. /// /// /// /// /// /// public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { object returnValue = new object[] { System.Windows.Data.Binding.DoNothing }; ArrayExtension convParameters = parameter as ArrayExtension; _currentLedger = convParameters.Items[0].ToString(); ReadDatatoXml(); if (!this.templateXML.InnerXml.ToString().Equals(currentXML)) { return Conversion.Zip.Compress(templateXML); } else { return returnValue; } } #region Private Methods /// /// Method to initialize the Grid datasource. /// private void InitializeGrid() { if (_budgetData == null) { _budgetData = new ObservableCollection(); } int rowCount = _budgetData.Count; for (int i = 0; i < rowCount; i++) { _budgetData[i].templateColumn = "Column" + (i + 1); } if (_budgetData.Count < 20) { _budgetdataitem = null; int rowIndex = _budgetData.Count; for (; rowIndex < 20; rowIndex++) { _budgetdataitem = new Budgetdata(); _budgetdataitem.templateColumn = "Column" + (rowIndex + 1); _budgetdataitem.budact1 = string.Empty; _budgetdataitem.budact2 = string.Empty; _budgetdataitem.budact3 = string.Empty; _budgetdataitem.budact4 = string.Empty; _budgetdataitem.op1 = "+"; _budgetdataitem.op2 = "+"; _budgetdataitem.op3 = "+"; _budgetdataitem.fyMod1 = "0"; _budgetdataitem.fyMod2 = "0"; _budgetdataitem.fyMod3 = "0"; _budgetdataitem.fyMod4 = "0"; _budgetdataitem.colHeader = string.Empty; _budgetdataitem.UserHeader = "N"; _budgetData.Add(_budgetdataitem); } } } /// /// Method to populate dataset with the xml data. /// /// public void LoadXMLtoGrid(bool addMode) { int rowIndex = 0; if ((templateXML == null || !templateXML.HasChildNodes) && !addMode) { return; } if (addMode) { templateXML = null; } _budgetData = new ObservableCollection(); if (templateXML != null) { foreach (XmlNode colNode in templateXML.FirstChild.ChildNodes) { _budgetdataitem = new Budgetdata(); if (colNode.Attributes["fyMod1"] != null) { _budgetdataitem.fyMod1 = colNode.Attributes["fyMod1"].Value.ToString(); } else { _budgetdataitem.fyMod1 = "0"; } if (colNode.Attributes["fyMod2"] != null) { _budgetdataitem.fyMod2 = colNode.Attributes["fyMod2"].Value.ToString(); } else { _budgetdataitem.fyMod2 = "0"; } if (colNode.Attributes["fyMod3"] != null) { _budgetdataitem.fyMod3 = colNode.Attributes["fyMod3"].Value.ToString(); } else { _budgetdataitem.fyMod3 = "0"; } if (colNode.Attributes["fyMod4"] != null) { _budgetdataitem.fyMod4 = colNode.Attributes["fyMod4"].Value.ToString(); } else { _budgetdataitem.fyMod4 = "0"; } if (colNode.Attributes["budact1"] != null) { _budgetdataitem.budact1 = colNode.Attributes["budact1"].Value.ToString(); } else { _budgetdataitem.budact1 = string.Empty; } if (colNode.Attributes["budact2"] != null) { _budgetdataitem.budact2 = colNode.Attributes["budact2"].Value.ToString(); } else { _budgetdataitem.budact2 = string.Empty; } if (colNode.Attributes["budact3"] != null) { _budgetdataitem.budact3 = colNode.Attributes["budact3"].Value.ToString(); } else { _budgetdataitem.budact3 = string.Empty; } if (colNode.Attributes["budact4"] != null) { _budgetdataitem.budact4 = colNode.Attributes["budact4"].Value.ToString(); } else { _budgetdataitem.budact4 = string.Empty; } if (colNode.Attributes["op1"] != null) { _budgetdataitem.op1 = colNode.Attributes["op1"].Value.ToString(); } else { _budgetdataitem.op1 = "+"; } if (colNode.Attributes["op2"] != null) { _budgetdataitem.op2 = colNode.Attributes["op2"].Value.ToString(); } else { _budgetdataitem.op2 = "+"; } if (colNode.Attributes["op3"] != null) { _budgetdataitem.op3 = colNode.Attributes["op3"].Value.ToString(); } else { _budgetdataitem.op3 = "+"; } if (colNode.Attributes["colHeader"] != null) { _budgetdataitem.colHeader = colNode.Attributes["colHeader"].Value.ToString(); } else { _budgetdataitem.colHeader = String.Empty; } if (colNode.Attributes["UserHeader"] != null) { _budgetdataitem.UserHeader = colNode.Attributes["UserHeader"].Value.ToString(); } _budgetData.Add(_budgetdataitem); rowIndex++; } } InitializeGrid(); } /// /// Read dataset and update the xml. /// void ReadDatatoXml() { // First, transfer DataGridView control values to XML document templateXML = new XmlDocument(); XmlNode headNode = templateXML.CreateNode(XmlNodeType.Element, "XML", null); templateXML.AppendChild(headNode); for (int i = 0; i < _budgetData.Count; i++) { if (_budgetData[i].budact1 == string.Empty) { break; } string colnum = _budgetData[i].templateColumn.ToString(); XmlElement colElem = templateXML.FirstChild.OwnerDocument.CreateElement(colnum); headNode.AppendChild(colElem); if (_budgetData[i].UserHeader.ToString() != string.Empty || _budgetData[i].UserHeader.ToString() != "N") { if (!string.IsNullOrEmpty(_budgetData[i].budact1)) { string budact = _budgetData[i].budact1.ToString(); colElem.SetAttribute("budact1", budact); string term = getTerm(budact); if (term != null) { colElem.SetAttribute("term1", term); } } if (!string.IsNullOrEmpty(_budgetData[i].budact2)) { string budact = _budgetData[i].budact2.ToString(); colElem.SetAttribute("budact2", budact); string term = getTerm(budact); if (term != null) { colElem.SetAttribute("term2", term); } } if (!string.IsNullOrEmpty(_budgetData[i].budact3)) { string budact = _budgetData[i].budact3.ToString(); colElem.SetAttribute("budact3", budact); // colElem.SetAttribute("budact3", budact); string term = getTerm(budact); if (term != null) { colElem.SetAttribute("term3", term); } } if (!string.IsNullOrEmpty(_budgetData[i].budact4)) { string budact = _budgetData[i].budact4.ToString(); colElem.SetAttribute("budact4", budact); string term = getTerm(budact); if (term != null) { colElem.SetAttribute("term4", term); } } if (_budgetData[i].fyMod1.ToString() != "0") { colElem.SetAttribute("fyMod1", _budgetData[i].fyMod1.ToString()); } if (_budgetData[i].fyMod2.ToString() != "0") { colElem.SetAttribute("fyMod2", _budgetData[i].fyMod2.ToString()); } if (_budgetData[i].fyMod3.ToString() != "0") { colElem.SetAttribute("fyMod3", _budgetData[i].fyMod3.ToString()); } if (_budgetData[i].fyMod4.ToString() != "0") { colElem.SetAttribute("fyMod4", _budgetData[i].fyMod4.ToString()); } if (_budgetData[i].op1.ToString() != string.Empty) { colElem.SetAttribute("op1", _budgetData[i].op1.ToString()); } if (_budgetData[i].op2.ToString() != string.Empty) { colElem.SetAttribute("op2", _budgetData[i].op2.ToString()); } if (_budgetData[i].op3.ToString() != string.Empty) { colElem.SetAttribute("op3", _budgetData[i].op3.ToString()); } if (_budgetData[i].colHeader.ToString() != string.Empty) { colElem.SetAttribute("colHeader", _budgetData[i].colHeader.ToString()); } colElem.SetAttribute("UserHeader", _budgetData[i].UserHeader.ToString()); } } } /// /// Gets the active Encumbrance index. /// /// string getEnField() { if (!_currentLedger.Equals(_lastLedger)) { _lastLedger = _currentLedger; // Get glg_per_num for current ledger RequestDataTable glgGenMaster = new RequestDataTable(EnvironmentContext, "BT20.GLGGenMaster", true); WhereClause whereClause = new WhereClause(); whereClause.AddParameter("Gr", _lastLedger); glgGenMaster.AddWhereClause(whereClause); DataPropsDataObjectProperty glgGenProps = new DataPropsDataObjectProperty(); glgGenProps.Add(new DataPropsDataObjectProperty.DataProp("Gr", true)); glgGenProps.Add(new DataPropsDataObjectProperty.DataProp("PerNum")); glgGenMaster.DataObjectProperties.Add(glgGenProps); DataSet dsGlgGenMaster = new DataSet(); FPSDataAdapter daGlgGenMaster = new FPSDataAdapter(EnvironmentContext); dsGlgGenMaster.Tables.Add(glgGenMaster); ((System.Data.Common.DbDataAdapter)daGlgGenMaster).Fill(dsGlgGenMaster); string ndx = null; // Should only be one row... foreach (DataRow dr in glgGenMaster.Rows) { // Add ledger node ndx = dr["PerNum"].ToString(); } if (!ndx.Equals("13") && !ndx.Equals("14")) { ndx = "12"; } _enField = "En" + ndx; } return _enField; } /// /// Maps valueName to the actual glba_budact column name. /// /// /// string getTerm(string valueName) { if (valueName.Trim().Length == 0) { return null; } if (valueName[2] == ' ') { XmlNode versNode = budgetVersXML.SelectSingleNode("/XML/" + _currentLedger + "/" + valueName.Substring(0, 2)); if (versNode == null) return null; int Ndx = System.Convert.ToInt16(versNode.Attributes["BudIdx"].Value.ToString()); string ndxString = string.Format("{0:d2}", Ndx); string remainder = valueName.Substring(3); if (remainder.Equals("Level Total")) { remainder = "BudAcc"; } if (remainder.Equals("Changes")) { remainder = "TtlChange"; } if (remainder.Equals("FTE")) { remainder = "Fte"; } if (remainder.Equals("Budget")) { remainder = "BudgetAsChar"; } return remainder + ndxString; } else { if (valueName.Equals("Actual")) { return "ActualTotal"; } if (valueName.Equals("Budget Coordinator")) { return "Coord"; } if (valueName.Equals("Encumbrance")) { return getEnField(); } } return null; } #endregion } public class Budgetdata { private string _templateColumn = string.Empty; public string templateColumn { get { return _templateColumn; } set {_templateColumn = value;} } private string _budact1 = string.Empty; public string budact1 { get { return _budact1; } set {_budact1 = value; } } private string _term1 = string.Empty; public string term1 { get { return _term1; } set {_term1 = value;} } private string _budact2 = string.Empty; public string budact2 { get { return _budact2; } set {_budact2 = value; } } private string _term2 = string.Empty; public string term2 { get { return _term2; } set {_term2 = value; } } private string _budact3 = string.Empty; public string budact3 { get { return _budact3; } set {_budact3 = value; } } private string _term3 = string.Empty; public string term3 { get { return _term3; } set {_term3 = value;} } private string _budact4 = string.Empty; public string budact4 { get { return _budact4; } set {_budact4 = value; } } private string _term4 = string.Empty; public string term4 { get { return _term4; } set {_term4 = value; } } private string _fyMod1 = string.Empty; public string fyMod1 { get { return _fyMod1; } set {_fyMod1 = value; } } private string _fyMod2 = string.Empty; public string fyMod2 { get { return _fyMod2; } set {_fyMod2 = value; } } private string _fyMod3 = string.Empty; public string fyMod3 { get { return _fyMod3; } set {_fyMod3 = value; } } private string _fyMod4 = string.Empty; public string fyMod4 { get { return _fyMod4; } set {_fyMod4 = value; } } private string _op1 = string.Empty; public string op1 { get { return _op1; } set {_op1 = value; } } private string _op2 = string.Empty; public string op2 { get { return _op2; } set {_op2 = value; } } private string _op3 = string.Empty; public string op3 { get { return _op3; } set {_op3 = value; } } private string _colHeader = string.Empty; public string colHeader { get { return _colHeader; } set {_colHeader = value; } } private string _userHeader = string.Empty; public string UserHeader { get { return _userHeader; } set {_userHeader = value; } } } }