示例#1
0
 /// <summary>Writes pool elements into a specific stream with respect to specific object names.
 /// </summary>
 /// <param name="objectStreamWriter">The object stream writer.</param>
 /// <param name="objectNames">The object names to take into account; if <c>null</c> all pool elements will be stored into the stream.</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 file operation was succeeded.</returns>
 public static bool TryWriteObjectsByName(IObjectStreamWriter objectStreamWriter, IEnumerable <string> objectNames, out string infoMessage)
 {
     if ((objectNames == null) || (objectNames.Count() == 0))
     {
         return(TryWriteObjects(objectStreamWriter, null, out infoMessage));
     }
     return(TryWriteObjects(objectStreamWriter, (from objName in objectNames select(IdentifierString) objName.ToIdentifierString()).ToArray(), out infoMessage));
 }
示例#2
0
        public static object WriteObjectsIntoFile(
            [ExcelArgument(Name = "path", Description = "The path", AllowReference = true)]
            object xlPath,
            [ExcelArgument(Name = "fileName", Description = "The file name", AllowReference = true)]
            object xlFileName,
            [ExcelArgument(Name = "objectNames", Description = "A [optional] list of object names to write.", AllowReference = true)]
            object xlObjectNames,
            [ExcelArgument(Name = "fileFormat", Description = "[Optional] The file format; the file extension will be used by default", AllowReference = true)]
            object xlFileFormat)
        {
            try
            {
                IExcelDataQuery fileFormatDataQuery = ExcelDataQuery.Create(xlFileFormat, "File format");

                string          fileName;
                IObjectStreamer objectStreamer;

                if (fileFormatDataQuery.IsEmpty == true)
                {
                    fileName = GetFileName(xlPath, xlFileName, ObjectStreamer.GetFileExtensions());

                    if (ObjectStreamer.TryGetObjectStreamerByFileExtension(ExtendedPath.GetExtension(fileName), out objectStreamer) == false)
                    {
                        throw new ArgumentException("Invalid file extension '" + ExtendedPath.GetExtension(fileName) + "', used default file extensions or specify the file format.");
                    }
                }
                else
                {
                    if (fileFormatDataQuery.TryGetPoolValue <IObjectStreamer>(ObjectStreamer.TryGetObjectStreamer, out objectStreamer, dataAdvice: ExcelDataAdvice.Create(ObjectStreamer.GetNames())) == false)
                    {
                        throw new ArgumentException("Invalid file format " + fileFormatDataQuery.ToString(0, 0) + ".");
                    }
                    fileName = GetFileName(xlPath, xlFileName, objectStreamer.FileExtension);
                    fileFormatDataQuery.QueryCompleted();
                }

                IExcelDataQuery      objectNamesDataQuery = ExcelDataQuery.Create(xlObjectNames);
                IEnumerable <string> objectNames          = objectNamesDataQuery.GetColumnVector <string>();
                objectNamesDataQuery.QueryCompleted();

                StreamWriter        streamWriter       = new StreamWriter(fileName, false);
                IObjectStreamWriter objectStreamWriter = objectStreamer.GetStreamWriter(streamWriter);
                string infoMessage;
                ExcelPool.TryWriteObjectsByName(objectStreamWriter, objectNames, out infoMessage);
                objectStreamWriter.Close();

                return(infoMessage.ToTimeStampString());
            }
            catch (Exception e)
            {
                return(ExcelDataConverter.GetExcelRangeErrorMessage(e.Message));
            }
        }
示例#3
0
        /// <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);
            }
        }
示例#4
0
        /// <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);
        }
示例#5
0
        /// <summary>Writes pool elements into a specific stream with respect to specific object type names.
        /// </summary>
        /// <param name="objectStreamWriter">The object stream writer.</param>
        /// <param name="excelPoolItemTypes">The object type names to take into account; if <c>null</c> all pool elements will be stored into the stream.</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 file operation was succeeded.</returns>
        public static bool WriteObjectsByTypeNames(IObjectStreamWriter objectStreamWriter, IEnumerable <ExcelPoolItemType> excelPoolItemTypes, out string infoMessage)
        {
            if ((excelPoolItemTypes == null) || (excelPoolItemTypes.Count() == 0))
            {
                return(TryWriteObjects(objectStreamWriter, null, out infoMessage));
            }
            IEnumerable <Guid> itemTypeIdentifiers = (from objTypeName in excelPoolItemTypes select objTypeName.Identifier).ToArray();

            return(TryWriteObjects(objectStreamWriter,
                                   (from poolItem in sm_Pool
                                    where itemTypeIdentifiers.Contains(poolItem.ObjectType.Identifier)
                                    select poolItem.ObjectName).ToArray(), out infoMessage));
        }
示例#6
0
 /// <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);
 }
示例#7
0
 /// <summary>Writes all pool elements into a specific stream.
 /// </summary>
 /// <param name="objectStreamWriter">The object stream writer.</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 file operation was succeeded.</returns>
 public static bool TryWriteObjects(IObjectStreamWriter objectStreamWriter, out string infoMessage)
 {
     return(TryWriteObjects(objectStreamWriter, null, out infoMessage));
 }