public void BinaryDataList_FetchAllRecordSetKeys_WhenHasRecordSetsOnly_ShouldReturn0() { //------------Setup for test-------------------------- // BuildTargetShape var targetShape = new StringBuilder("<DataList><rs><Col1/><Col2/></rs><rec><Col4/><Col5/></rec></DataList>"); ErrorResultTO invokeErrors; TranslatorUtils tu = new TranslatorUtils(); var dl = tu.TranslateShapeToObject(targetShape, false, out invokeErrors); //------------Execute Test--------------------------- var keys = dl.FetchAllRecordSetKeys(); //------------Assert Results------------------------- Assert.AreEqual(0, invokeErrors.FetchErrors().Count); Assert.AreEqual(2, keys.Count); Assert.AreEqual("rs", keys[0]); Assert.AreEqual("rec", keys[1]); }
public void TranslatorUtils_TranslateShapeToObject_whenRsBothAndColumnsMixed_FullRSPresentInShape() { //------------Setup for test-------------------------- var translatorUtils = new TranslatorUtils(); var shape = new StringBuilder(@"<DataList> <rs Description="""" IsEditable=""True"" ColumnIODirection=""Both""> <result Description="""" IsEditable=""True"" ColumnIODirection=""Output"" /> <val Description="""" IsEditable=""True"" ColumnIODirection=""Input"" /> </rs> </DataList>"); //------------Execute Test--------------------------- ErrorResultTO invokeErrors; var dl = translatorUtils.TranslateShapeToObject(shape, false, out invokeErrors); //------------Assert Results------------------------- IBinaryDataListEntry entry; string error; dl.TryGetEntry("rs", out entry, out error); var keys = dl.FetchAllUserKeys(); Assert.AreEqual(0, invokeErrors.FetchErrors().Count); Assert.AreEqual(1, keys.Count); Assert.AreEqual("rs", keys[0]); Assert.AreEqual("result", entry.Columns[0].ColumnName); Assert.AreEqual("val", entry.Columns[1].ColumnName); }
public void CanDataListHelperCreateTargetShape() { // BuildTargetShape var targetShape = new StringBuilder("<DataList><result Description=\"\" IsEditable=\"True\" ColumnIODirection=\"Output\" /></DataList>"); ErrorResultTO invokeErrors; TranslatorUtils tu = new TranslatorUtils(); var dl = tu.TranslateShapeToObject(targetShape, false, out invokeErrors); var keys = dl.FetchAllUserKeys(); Assert.AreEqual(0, invokeErrors.FetchErrors().Count); Assert.AreEqual(1, keys.Count); Assert.AreEqual("result", keys[0]); }
public void BinaryDataList_FetchAllRecordSetKeys_WhenHasScalars_ShouldReturn0() { //------------Setup for test-------------------------- // BuildTargetShape var targetShape = new StringBuilder("<DataList><result Description=\"\" IsEditable=\"True\" ColumnIODirection=\"Output\" /></DataList>"); ErrorResultTO invokeErrors; TranslatorUtils tu = new TranslatorUtils(); var dl = tu.TranslateShapeToObject(targetShape, false, out invokeErrors); //------------Execute Test--------------------------- var keys = dl.FetchAllRecordSetKeys(); //------------Assert Results------------------------- Assert.AreEqual(0, invokeErrors.FetchErrors().Count); Assert.AreEqual(0, keys.Count); }
public DataListXMLTranslatorWithOutSystemTags() { _format = DataListFormat.CreateFormat(GlobalConstants._XML_Without_SystemTags, EmitionTypes.XML, "text/xml"); _encoding = Encoding.UTF8; _tu = new TranslatorUtils(); }
public DataListTranslatedPayloadTO ConvertFrom(IBinaryDataList payload, out ErrorResultTO errors) { if(payload == null) { throw new ArgumentNullException("payload"); } TranslatorUtils tu = new TranslatorUtils(); StringBuilder result = new StringBuilder("<" + RootTag + ">"); errors = new ErrorResultTO(); IList<string> itemKeys = payload.FetchAllUserKeys(); foreach(string key in itemKeys) { IBinaryDataListEntry entry; string error; if(payload.TryGetEntry(key, out entry, out error)) { if(entry.IsRecordset && !entry.IsEmpty()) { var idxItr = entry.FetchRecordsetIndexes(); while(idxItr.HasMore()) { var i = idxItr.FetchNextIndex(); IList<IBinaryDataListItem> rowData = entry.FetchRecordAt(i, out error); errors.AddError(error); result.Append("<"); result.Append(entry.Namespace); result.Append(">"); foreach(IBinaryDataListItem col in rowData) { string fName = col.FieldName; result.Append("<"); result.Append(fName); result.Append(">"); try { result.Append(tu.FullCleanForEmit(col.TheValue)); } // ReSharper disable EmptyGeneralCatchClause catch(Exception) // ReSharper restore EmptyGeneralCatchClause { } result.Append("</"); result.Append(fName); result.Append(">"); } result.Append("</"); result.Append(entry.Namespace); result.Append(">"); } } else if(!entry.IsRecordset) { string fName = entry.Namespace; IBinaryDataListItem val = entry.FetchScalar(); if(val != null) { result.Append("<"); result.Append(fName); result.Append(">"); try { result.Append(tu.FullCleanForEmit(val.TheValue)); } // ReSharper disable EmptyGeneralCatchClause catch(Exception) // ReSharper restore EmptyGeneralCatchClause { } result.Append("</"); result.Append(fName); result.Append(">"); } } } } result.Append("</" + RootTag + ">"); DataListTranslatedPayloadTO tmp = new DataListTranslatedPayloadTO(result.ToString()); return tmp; }
public DataListTranslatedPayloadTO ConvertFrom(IBinaryDataList payload, out ErrorResultTO errors) { if(payload == null) { throw new ArgumentNullException("payload"); } TranslatorUtils tu = new TranslatorUtils(); StringBuilder result = new StringBuilder("<" + RootTag + ">"); errors = new ErrorResultTO(); var itemKeys = payload.FetchAllKeys(); foreach(string key in itemKeys) { IBinaryDataListEntry entry; string error; if(payload.TryGetEntry(key, out entry, out error)) { if(entry.IsRecordset) { var idxItr = entry.FetchRecordsetIndexes(); while(idxItr.HasMore() && !entry.IsEmpty()) { while(idxItr.HasMore()) { int i = idxItr.FetchNextIndex(); IList<IBinaryDataListItem> rowData = entry.FetchRecordAt(i, out error); errors.AddError(error); result.Append("<"); result.Append(entry.Namespace); result.Append(">"); foreach(IBinaryDataListItem col in rowData) { string fName = col.FieldName; result.Append("<"); result.Append(fName); result.Append(">"); // Travis.Frisinger 04.02.2013 if(!col.IsDeferredRead) { try { result.Append(tu.CleanForEmit(col.TheValue)); } catch(Exception e) { Dev2Logger.Log.Error(e); } } else { // deferred read, just print the location result.Append(!string.IsNullOrEmpty(col.TheValue) ? col.FetchDeferredLocation() : string.Empty); } result.Append("</"); result.Append(fName); result.Append(">"); } result.Append("</"); result.Append(entry.Namespace); result.Append(">"); } } } else { string fName = entry.Namespace; IBinaryDataListItem val = entry.FetchScalar(); if(val != null) { result.Append("<"); result.Append(fName); result.Append(">"); // Travis.Frisinger 04.02.2013 if(!val.IsDeferredRead) { // Dev2System.FormView is our html region, pass it by ;) try { result.Append(!entry.IsManagmentServicePayload ? tu.CleanForEmit(val.TheValue) : val.TheValue); } catch(Exception e) { Dev2Logger.Log.Error(e); } } else { // deferred read, just print the location result.Append(val.FetchDeferredLocation()); } result.Append("</"); result.Append(fName); result.Append(">"); } } } } result.Append("</" + RootTag + ">"); DataListTranslatedPayloadTO tmp = new DataListTranslatedPayloadTO(result.ToString()); return tmp; }
public StringBuilder ConvertAndFilter(IBinaryDataList payload, StringBuilder filterShape, out ErrorResultTO errors) { if(payload == null) { throw new ArgumentNullException("payload"); } StringBuilder result = new StringBuilder("<" + RootTag + ">"); errors = new ErrorResultTO(); ErrorResultTO invokeErrors; TranslatorUtils tu = new TranslatorUtils(); IBinaryDataList targetDl = _tu.TranslateShapeToObject(filterShape, false, out invokeErrors); errors.MergeErrors(invokeErrors); IList<string> itemKeys = targetDl.FetchAllKeys(); foreach(string key in itemKeys) { IBinaryDataListEntry entry; IBinaryDataListEntry tmpEntry; string error; if(payload.TryGetEntry(key, out entry, out error) && targetDl.TryGetEntry(key, out tmpEntry, out error)) { if(entry.IsRecordset) { var idxItr = entry.FetchRecordsetIndexes(); while(idxItr.HasMore()) { var i = idxItr.FetchNextIndex(); IList<IBinaryDataListItem> rowData = entry.FetchRecordAt(i, out error); if(error != string.Empty) { errors.AddError(error); } result.Append("<"); result.Append(entry.Namespace); result.Append(" "); result.Append(GlobalConstants.RowAnnotation); result.Append("="); result.Append("\"" + i + "\""); result.Append(">"); foreach(IBinaryDataListItem col in rowData) { IBinaryDataListItem col1 = col; if(tmpEntry.Columns.Any((c => c.ColumnName == col1.FieldName))) { string fName = col.FieldName; result.Append("<"); result.Append(fName); result.Append(">"); try { result.Append(tu.FullCleanForEmit(col.TheValue)); } // ReSharper disable EmptyGeneralCatchClause catch(Exception) // ReSharper restore EmptyGeneralCatchClause { } result.Append("</"); result.Append(fName); result.Append(">"); } } result.Append("</"); result.Append(entry.Namespace); result.Append(">"); } } else { string fName = entry.Namespace; IBinaryDataListItem val = entry.FetchScalar(); if(val != null) { result.Append("<"); result.Append(fName); result.Append(">"); try { result.Append(entry.Namespace != GlobalConstants.ManagementServicePayload ? tu.FullCleanForEmit(val.TheValue) : val.TheValue); } // ReSharper disable EmptyGeneralCatchClause catch // ReSharper restore EmptyGeneralCatchClause { } result.Append("</"); result.Append(fName); result.Append(">"); } } } } result.Append("</" + RootTag + ">"); return result; }
public IBinaryDataList ConvertTo(object input, StringBuilder shape, out ErrorResultTO errors) { errors = new ErrorResultTO(); TranslatorUtils tu = new TranslatorUtils(); ErrorResultTO invokeErrors; var targetDL = tu.TranslateShapeToObject(shape, false, out invokeErrors); errors.MergeErrors(invokeErrors); DataTable dbData = (input as DataTable); var rs = targetDL.FetchAllKeys(); // ensure we only have a single recordset to map too ;) if(rs != null && rs.Count != 1) { throw new Exception("DataTable translator can only map to a single recordset!"); } if(rs != null) { var rsName = rs.FirstOrDefault(); if(dbData != null) { IBinaryDataListEntry entry; // build up the columns ;) string error; if(targetDL.TryGetEntry(rsName, out entry, out error)) { if(entry.IsRecordset) { var cols = entry.Columns; IDictionary<int, string> colMapping = BuildColumnNameToIndexMap(entry.Columns, dbData.Columns, null); // now convert to binary datalist ;) int rowIdx = 1; foreach(DataRow row in dbData.Rows) { IList<IBinaryDataListItem> items = new List<IBinaryDataListItem>(cols.Count); // build up the row int idx = 0; foreach(var item in row.ItemArray) { string colName; if(colMapping.TryGetValue(idx, out colName)) { items.Add(new BinaryDataListItem(item.ToString(), rsName, colName, rowIdx)); } idx++; } // add the row ;) entry.TryPutRecordRowAt(items, rowIdx, out error); errors.AddError(error); rowIdx++; } } else { // handle a scalar coming out ;) if(dbData.Rows != null && dbData.Rows.Count == 1) { var row = dbData.Rows[0].ItemArray; // Look up the correct index from the columns ;) int pos = 0; var cols = dbData.Columns; int idx = 0; while(pos < cols.Count && idx == -1) { if(cols[pos].ColumnName == entry.Namespace) { idx = pos; } pos++; } entry.TryPutScalar(new BinaryDataListItem(row[idx].ToString(), entry.Namespace), out error); errors.AddError(error); } } } else { errors.AddError(error); } } } return targetDL; }
// internal to impl api methods private IBinaryDataList ConvertTo(byte[] input, StringBuilder targetShape, out ErrorResultTO errors, bool onlyMapInputs) { errors = new ErrorResultTO(); string payload = Encoding.UTF8.GetString(input); IBinaryDataList result = new BinaryDataList(); TranslatorUtils tu = new TranslatorUtils(); // build shape if(targetShape == null) { errors.AddError("Null payload or shape"); } else { ErrorResultTO invokeErrors; result = tu.TranslateShapeToObject(targetShape, true, out invokeErrors); errors.MergeErrors(invokeErrors); // populate the shape if(payload != string.Empty) { try { string toLoad = DataListUtil.StripCrap(payload); // clean up the rubish ;) XmlDocument xDoc = new XmlDocument(); // BUG 9626 - 2013.06.11 - TWR: ensure our DocumentElement toLoad = string.Format("<Tmp{0}>{1}</Tmp{0}>", Guid.NewGuid().ToString("N"), toLoad); xDoc.LoadXml(toLoad); if(xDoc.DocumentElement != null) { XmlNodeList children = xDoc.DocumentElement.ChildNodes; IDictionary<string, int> indexCache = new Dictionary<string, int>(); // BUG 9626 - 2013.06.11 - TWR: refactored for recursion TryConvert(children, result, indexCache, errors, onlyMapInputs); } // Transfer System Tags for(int i = 0; i < TranslationConstants.systemTags.Length; i++) { string key = TranslationConstants.systemTags.GetValue(i).ToString(); string query = String.Concat("//", key); XmlNode n = xDoc.SelectSingleNode(query); // try system namespace tags ;) if(n == null) { var values = "//" + DataListUtil.BuildSystemTagForDataList(key, false); query = values; n = xDoc.SelectSingleNode(query); } if(n != null && !string.IsNullOrEmpty(n.InnerXml)) { string bkey = DataListUtil.BuildSystemTagForDataList(key, false); string error; IBinaryDataListEntry sysEntry; if(result.TryGetEntry(bkey, out sysEntry, out error)) { sysEntry.TryPutScalar(Dev2BinaryDataListFactory.CreateBinaryItem(n.InnerXml, bkey), out error); } } } } catch(Exception e) { // if use passed in empty input they only wanted the shape ;) if(input.Length > 0) { errors.AddError(e.Message); } } } } return result; }
public IBinaryDataList ConvertTo(object input, StringBuilder shape, out ErrorResultTO errors) { errors = new ErrorResultTO(); TranslatorUtils tu = new TranslatorUtils(); ErrorResultTO invokeErrors; var targetDL = tu.TranslateShapeToObject(shape, false, out invokeErrors); errors.MergeErrors(invokeErrors); DataTable dbData = (input as DataTable); var rs = targetDL.FetchAllKeys(); // ensure we only have a single recordset to map too ;) if (rs != null && rs.Count != 1) { throw new Exception("DataTable translator can only map to a single recordset!"); } if (rs != null) { var rsName = rs.FirstOrDefault(); if (dbData != null) { IBinaryDataListEntry entry; // build up the columns ;) string error; if (targetDL.TryGetEntry(rsName, out entry, out error)) { if (entry.IsRecordset) { var cols = entry.Columns; IDictionary <int, string> colMapping = BuildColumnNameToIndexMap(entry.Columns, dbData.Columns, null); // now convert to binary datalist ;) int rowIdx = 1; foreach (DataRow row in dbData.Rows) { IList <IBinaryDataListItem> items = new List <IBinaryDataListItem>(cols.Count); // build up the row int idx = 0; foreach (var item in row.ItemArray) { string colName; if (colMapping.TryGetValue(idx, out colName)) { items.Add(new BinaryDataListItem(item.ToString(), rsName, colName, rowIdx)); } idx++; } // add the row ;) entry.TryPutRecordRowAt(items, rowIdx, out error); errors.AddError(error); rowIdx++; } } else { // handle a scalar coming out ;) if (dbData.Rows != null && dbData.Rows.Count == 1) { var row = dbData.Rows[0].ItemArray; // Look up the correct index from the columns ;) int pos = 0; var cols = dbData.Columns; int idx = 0; while (pos < cols.Count && idx == -1) { if (cols[pos].ColumnName == entry.Namespace) { idx = pos; } pos++; } entry.TryPutScalar(new BinaryDataListItem(row[idx].ToString(), entry.Namespace), out error); errors.AddError(error); } } } else { errors.AddError(error); } } } return(targetDL); }
public DataListTranslatedPayloadTO ConvertFrom(IBinaryDataList payload, out ErrorResultTO errors) { if(payload == null) { throw new ArgumentNullException("payload"); } TranslatorUtils tu = new TranslatorUtils(); StringBuilder result = new StringBuilder("<" + RootTag + ">"); errors = new ErrorResultTO(); IList<string> itemKeys = payload.FetchAllUserKeys(); foreach(string key in itemKeys) { IBinaryDataListEntry entry; string error; if(payload.TryGetEntry(key, out entry, out error)) { if(entry.IsRecordset && !entry.IsEmpty()) { var idxItr = entry.FetchRecordsetIndexes(); while(idxItr.HasMore()) { var i = idxItr.FetchNextIndex(); IList<IBinaryDataListItem> rowData = entry.FetchRecordAt(i, out error); errors.AddError(error); result.Append("<"); result.Append(entry.Namespace); result.Append(">"); foreach(IBinaryDataListItem col in rowData) { string fName = col.FieldName; result.Append("<"); result.Append(fName); result.Append(">"); try { result.Append(tu.FullCleanForEmit(col.TheValue)); } // ReSharper disable EmptyGeneralCatchClause catch(Exception) // ReSharper restore EmptyGeneralCatchClause { } result.Append("</"); result.Append(fName); result.Append(">"); } result.Append("</"); result.Append(entry.Namespace); result.Append(">"); } } else if(!entry.IsRecordset) { string fName = entry.Namespace; IBinaryDataListItem val = entry.FetchScalar(); if(val != null) { result.Append("<"); result.Append(fName); result.Append(">"); try { result.Append(tu.FullCleanForEmit(val.TheValue)); } // ReSharper disable EmptyGeneralCatchClause catch(Exception) // ReSharper restore EmptyGeneralCatchClause { } result.Append("</"); result.Append(fName); result.Append(">"); } } } } result.Append("</" + RootTag + ">"); DataListTranslatedPayloadTO tmp = new DataListTranslatedPayloadTO(result.ToString()); return tmp; }
public DataListXMLTranslatorWithOutSystemTags() { _format = DataListFormat.CreateFormat(GlobalConstants._XML_Without_SystemTags, EmitionTypes.XML, "text/xml"); _encoding = Encoding.UTF8; _tu = new TranslatorUtils(); }
public StringBuilder ConvertAndFilter(IBinaryDataList payload, StringBuilder filterShape, out ErrorResultTO errors) { if(payload == null) { throw new ArgumentNullException("payload"); } StringBuilder result = new StringBuilder("<" + RootTag + ">"); errors = new ErrorResultTO(); ErrorResultTO invokeErrors; TranslatorUtils tu = new TranslatorUtils(); IBinaryDataList targetDl = _tu.TranslateShapeToObject(filterShape, false, out invokeErrors); errors.MergeErrors(invokeErrors); IList<string> itemKeys = targetDl.FetchAllKeys(); foreach(string key in itemKeys) { IBinaryDataListEntry entry; IBinaryDataListEntry tmpEntry; string error; if(payload.TryGetEntry(key, out entry, out error) && targetDl.TryGetEntry(key, out tmpEntry, out error)) { if(entry.IsRecordset) { var idxItr = entry.FetchRecordsetIndexes(); while(idxItr.HasMore()) { var i = idxItr.FetchNextIndex(); IList<IBinaryDataListItem> rowData = entry.FetchRecordAt(i, out error); if(error != string.Empty) { errors.AddError(error); } result.Append("<"); result.Append(entry.Namespace); result.Append(" "); result.Append(GlobalConstants.RowAnnotation); result.Append("="); result.Append("\"" + i + "\""); result.Append(">"); foreach(IBinaryDataListItem col in rowData) { IBinaryDataListItem col1 = col; if(tmpEntry.Columns.Any((c => c.ColumnName == col1.FieldName))) { string fName = col.FieldName; result.Append("<"); result.Append(fName); result.Append(">"); try { result.Append(tu.FullCleanForEmit(col.TheValue)); } // ReSharper disable EmptyGeneralCatchClause catch(Exception) // ReSharper restore EmptyGeneralCatchClause { } result.Append("</"); result.Append(fName); result.Append(">"); } } result.Append("</"); result.Append(entry.Namespace); result.Append(">"); } } else { string fName = entry.Namespace; IBinaryDataListItem val = entry.FetchScalar(); if(val != null) { result.Append("<"); result.Append(fName); result.Append(">"); try { result.Append(entry.Namespace != GlobalConstants.ManagementServicePayload ? tu.FullCleanForEmit(val.TheValue) : val.TheValue); } // ReSharper disable EmptyGeneralCatchClause catch // ReSharper restore EmptyGeneralCatchClause { } result.Append("</"); result.Append(fName); result.Append(">"); } } } } result.Append("</" + RootTag + ">"); return result; }
/// <summary> /// Converts the and filter. /// </summary> /// <param name="payload">The payload.</param> /// <param name="filterShape">The filter shape.</param> /// <param name="errors">The errors.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">payload</exception> public StringBuilder ConvertAndFilter(IBinaryDataList payload, StringBuilder filterShape, out ErrorResultTO errors) { if(payload == null) { throw new ArgumentNullException("payload"); } int keyCnt = 0; errors = new ErrorResultTO(); TranslatorUtils tu = new TranslatorUtils(); ErrorResultTO invokeErrors; IBinaryDataList targetDl = tu.TranslateShapeToObject(filterShape, false, out invokeErrors); errors.MergeErrors(invokeErrors); IList<string> itemKeys = targetDl.FetchAllUserKeys(); StringBuilder result = new StringBuilder("{"); foreach(string key in itemKeys) { IBinaryDataListEntry entry; IBinaryDataListEntry tmpEntry; string error; if(payload.TryGetEntry(key, out entry, out error) && targetDl.TryGetEntry(key, out tmpEntry, out error)) { if(entry.IsRecordset) { result.Append(ProcessRecordSet(entry, out error)); errors.AddError(error); } else { result.Append(ProcessScalar(entry)); } // wack in , for field separator ;) keyCnt++; if(keyCnt < itemKeys.Count) { result.Append(","); } } errors.AddError(error); } result.Append("}"); return result; }