/// <summary>Writes a specific object into a <see cref="IObjectStreamWriter"/> and all its dependency input objects. /// </summary> /// <param name="objectStreamWriter">The object stream writer.</param> /// <param name="value">The <c>root</c> item to add, i.e. all dependency input will be added in a recursive way.</param> /// <param name="setOfInsertedItems">A collection of the <see cref="ExcelPoolItem"/> objects which are already stored in <paramref name="objectStreamWriter"/>.</param> /// <param name="infoStrBuilder">A collector for info string, as for example error messages.</param> /// <param name="storedObjectCount">The number of objects added into the <paramref name="objectStreamWriter"/> (output).</param> /// <param name="errorCount">The number of error mesages in <paramref name="infoStrBuilder"/> (output).</param> /// <remarks><paramref name="setOfInsertedItems"/> is used to avoid multiply entries in <paramref name="objectStreamWriter"/> with the same name.</remarks> private static void WriteObject(IObjectStreamWriter objectStreamWriter, ExcelPoolItem value, IdentifierStringDictionary <ExcelPoolItem> setOfInsertedItems, StringBuilder infoStrBuilder, ref int storedObjectCount, ref int errorCount) { IEnumerable <ExcelPoolItem> inputItems = value.InputDependencyItems; if ((inputItems != null) && (inputItems.Count() > 0)) // there are dependencies, i.e. store the dependencies first { foreach (var dependentExcelPoolItem in inputItems) { WriteObject(objectStreamWriter, dependentExcelPoolItem, setOfInsertedItems, infoStrBuilder, ref storedObjectCount, ref errorCount); } } /* if a item with the same name is already added to the stream check whether both objects are equal: */ ExcelPoolItem alreadyAddedItem; if (setOfInsertedItems.TryGetValue(value.ObjectName, out alreadyAddedItem) == true) { if (value.Equals(alreadyAddedItem) == false) // assume that both objects share the same adress { infoStrBuilder.AppendLine("Inconsistent data input: Object '" + value.ObjectName.String + "' with time stamp '" + value.TimeStamp.ToString("HH:mm:ss.ff") + " and " + alreadyAddedItem.TimeStamp.ToString("HH:mm:ss.ff") + " [is added]"); } } else { /* check whether the item is equal to an element of the Excel pool with the * same name and add a warning message if it is different: (plausibility check) */ ExcelPoolItem excelPoolItem; if (sm_Pool.TryGetValue(value.ObjectName, out excelPoolItem) == false) { infoStrBuilder.AppendLine("Item to add is not in the Excel pool any more."); // a internal error message errorCount++; } else if (value.Equals(excelPoolItem) == false) { infoStrBuilder.AppendLine("Inconsistent data input: Object '" + value.ObjectName.String + "' with time stamp '" + value.TimeStamp.ToString("HH:mm:ss.ff") + " [is added] and " + excelPoolItem.TimeStamp.ToString("HH:mm:ss.ff")); errorCount++; } string errorMessage; if (TryWriteObject(objectStreamWriter, value, out errorMessage) == false) { infoStrBuilder.AppendLine(errorMessage); errorCount++; } else { storedObjectCount++; } setOfInsertedItems.Add(value.Name, value); } }
/// <summary>Writes a specific object in its <see cref="GuidedExcelDataQuery"/> representation into a specific stream. /// </summary> /// <param name="objectStreamWriter">The object stream writer.</param> /// <param name="value">The <see cref="ExcelPoolItem"/> object to store.</param> /// <param name="errorMessage">A <see cref="System.String"/> object which may contains a error message.</param> /// <returns>A value indicating whether the operation succeeded.</returns> private static bool TryWriteObject(IObjectStreamWriter objectStreamWriter, ExcelPoolItem value, out string errorMessage) { try { objectStreamWriter.WriteObject(value); } catch (Exception e) { errorMessage = e.Message; return(false); } errorMessage = null; return(true); }
/// <summary>Adds a specific <see cref="ExcelPoolItem"/> object into the <see cref="ExcelPool"/>. /// </summary> /// <param name="excelPoolItem">The object to insert into the pool.</param> /// <returns>A value indicating whether <paramref name="excelPoolItem"/> has been inserted to the pool.</returns> public static ItemAddedState InsertObject(ExcelPoolItem excelPoolItem) { lock (sm_Pool) { if (excelPoolItem == null) { if (ItemAdded != null) { ItemAdded(new ItemAddedEventArgs(null, ItemAddedState.Rejected)); } return(ItemAddedState.Rejected); } ExcelPoolItem oldItem; if (sm_Pool.TryGetValue(excelPoolItem.ObjectName, out oldItem) == true) { if (oldItem.Value is IDisposable) { ((IDisposable)oldItem.Value).Dispose(); } sm_Pool[excelPoolItem.ObjectName] = excelPoolItem; if (sm_LoggingLevel == ExcelPoolLoggingLevel.TrackPoolChanges) { // Logger.Stream.Add_Info_PoolItemReplaced(senderObjectTypeName: "ExcelPool", senderObjectType: typeof(ExcelPool), senderObjectName: excelPoolItem.ObjectName.String); } if (ItemAdded != null) { ItemAdded(new ItemAddedEventArgs(excelPoolItem, ItemAddedState.Replaced)); } return(ItemAddedState.Replaced); } sm_Pool.Add(excelPoolItem); if (sm_LoggingLevel == ExcelPoolLoggingLevel.TrackPoolChanges) { // Logger.Stream.Add_Info_PoolItemAdded(senderObjectTypeName: "ExcelPool", senderObjectType: typeof(ExcelPool), senderObjectName: excelPoolItem.ObjectName.String); } if (ItemAdded != null) { ItemAdded(new ItemAddedEventArgs(excelPoolItem, ItemAddedState.Added)); } return(ItemAddedState.Added); } }
/// <summary>Initializes a new instance of the <see cref="ItemAddedEventArgs"/> class. /// </summary> /// <param name="excelPoolItem">The <see cref="ExcelPoolItem"/> object which has been added.</param> /// <param name="state">A value indicating whether <paramref name="excelPoolItem"/> replaced an other item in the <see cref="ExcelPool"/> with the same name.</param> internal ItemAddedEventArgs(ExcelPoolItem excelPoolItem, ItemAddedState state) { Item = excelPoolItem; State = state; }
/// <summary>Gets a specific element of the <see cref="ExcelPool"/>. /// </summary> /// <param name="name">The name of the object in its <see cref="IdentifierString"/> representation.</param> /// <param name="value">The requested object (output).</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> public static bool TryGetItem(IdentifierString name, out ExcelPoolItem value) { return(sm_Pool.TryGetValue(name, out value)); }
/// <summary>Converts a single <see cref="ExcelPoolItem"/> object into a <see cref="IEnumerable<ExcelPoolItem>"/> object. /// </summary> /// <param name="excelPoolItem">The <see cref="ExcelPoolItem"/> object.</param> /// <returns>A <see cref="IEnumerable<ExcelPoolItem>"/> wrapper for <paramref name="excelPoolItem"/>.</returns> private static IEnumerable <ExcelPoolItem> GetAsEnumerable(ExcelPoolItem excelPoolItem) { yield return(excelPoolItem); }