/// <summary>Writes a subset of the <see cref="ExcelPoolItem"/> objects of the <see cref="ExcelPool"/> into a specific <see cref="IObjectStreamWriter"/> taken into account the /// dependency structure, i.e. first 'independent' items are stored, afterwards objects which are needed such 'independent' input are stored etc. /// </summary> /// <param name="objectStreamWriter">The object stream writer.</param> /// <param name="objectNameFilter">The object name filter, i.e. at least the objects which satisfied this filter will be stored; <c>null</c> is allowed and /// in this case all items will be stored..</param> /// <param name="infoMessage">A <see cref="System.String"/> which represent a summary of the file operation, perhaps a error message (output).</param> /// <returns>A value indicating whether the operation was succeeded.</returns> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="objectStreamWriter"/> is <c>null</c>.</exception> private static bool TryWriteObjects(IObjectStreamWriter objectStreamWriter, IEnumerable <IdentifierString> objectNameFilter, out string infoMessage) { if (objectStreamWriter == null) { throw new ArgumentNullException("objectStreamWriter"); } StringBuilder infoStrBuilder = new StringBuilder(); int storedObjectCount = 0; int errorCount = 0; /* just a foreach-loop where we store the object into the stream is not o.k., because we have some dependency structure, because objects can be input * for other objects etc. For example swap rates, deposit rates etc. are input objects for a discount factor curve etc. We do a recursive approach: */ var setOfInsertedItems = new IdentifierStringDictionary <ExcelPoolItem>(isReadOnlyExceptAdding: false); if ((objectNameFilter == null) || (objectNameFilter.Count() == 0)) { foreach (var excelPoolItem in sm_Pool) { WriteObject(objectStreamWriter, excelPoolItem, setOfInsertedItems, infoStrBuilder, ref storedObjectCount, ref errorCount); } } else { foreach (var idObjectName in objectNameFilter) { ExcelPoolItem excelPoolItem; if (sm_Pool.TryGetValue(idObjectName, out excelPoolItem) == true) { WriteObject(objectStreamWriter, excelPoolItem, setOfInsertedItems, infoStrBuilder, ref storedObjectCount, ref errorCount); } else { infoStrBuilder.AppendLine("No pool item found with name '" + idObjectName.String.GetRelevantSubstring() + "'."); } } } if (storedObjectCount > 0) { infoStrBuilder.AppendLine("Wrote " + storedObjectCount + " objects."); } else { infoStrBuilder.AppendLine("No objects stored."); } if (errorCount > 0) { infoStrBuilder.AppendLine(errorCount + " errors detected!"); } infoMessage = infoStrBuilder.ToString(); return(errorCount == 0); }
/// <summary>Initializes a new instance of the <see cref="ExcelPoolItem"/> class. /// </summary> /// <param name="value">The value.</param> /// <param name="objectName">The name of <paramref name="value"/>.</param> /// <param name="objectType">The type of <paramref name="value"/>.</param> /// <param name="excelDataQueries">The excel data queries, i.e. the (user) input.</param> /// <param name="inputDependencyItems">A collection of <see cref="ExcelPoolItem"/> objects which are used as input for the construction of <paramref name="value"/>, i.e. dependent objects.</param> /// <remarks>The <see cref="IExcelDataQuery.QueryCompleted(bool)"/> will be called for each element of <paramref name="excelDataQueries"/> and the <see cref="GuidedExcelDataQuery"/> representation /// will be stored internally.</remarks> /// <example><paramref name="inputDependencyItems"/> are for example swap rates, deposit rates etc. if the current instance is a discount factor curve etc.</example> public ExcelPoolItem(IInfoOutputQueriable value, string objectName, ExcelPoolItemType objectType, IEnumerable <IExcelDataQuery> excelDataQueries, IEnumerable <ExcelPoolItem> inputDependencyItems = null) { if (value == null) { throw new ArgumentNullException("value"); } Value = value; if (objectName == null) { throw new ArgumentNullException("objectName"); } ObjectName = objectName.ToIdentifierString(); if (objectType == null) { throw new ArgumentNullException("objectType"); } ObjectType = objectType; if (excelDataQueries == null) { throw new ArgumentNullException("excelDataQueries"); } var guidedExcelDataQueryInput = new IdentifierStringDictionary <GuidedExcelDataQuery>(isReadOnlyExceptAdding: false); foreach (var inputExcelDataQuery in excelDataQueries) { inputExcelDataQuery.QueryCompleted(); guidedExcelDataQueryInput.Add(inputExcelDataQuery.Name, inputExcelDataQuery.AsCustomizeData()); } m_ExcelDataQueries = guidedExcelDataQueryInput; InputDependencyItems = inputDependencyItems; TimeStamp = DateTime.Now; }
/// <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); } }