/// <summary>
        /// Savaes all the Json Items in a dictionary to the File System
        /// </summary>
        /// <param name="jsonRootFolder"></param>
        /// <param name="itemType"></param>
        /// <param name="jsonItems"></param>
        /// <param name="eventLog"></param>
        /// <returns></returns>
        /// <remarks>Note: The JSON files end in the file extension .json</remarks>
        public bool SaveAllJsonFileItems(string jsonRootFolder, string itemTypeName, List <JsonItemFile> jsonItems, IEventLog eventLog)
        {
            if (jsonItems == null)
            {
                if (eventLog != null)
                {
                    eventLog.LogEvent(0, "SaveAllJsonFileItems - " + itemTypeName + " - jsonItems is null");
                }
                return(false);
            }

            bool returnValue = true;

            foreach (var item in jsonItems)
            {
                try
                {
                    // Note: JSON Files need to end in .json
                    string fullPath = FileHelper.EnsureTrailingDirectorySeparator(jsonRootFolder) + FileHelper.EnsureTrailingDirectorySeparator(itemTypeName) + (item.RelativePathBeneathTable.StartsWith("\\") ? item.RelativePathBeneathTable.Substring(1) : item.RelativePathBeneathTable) + (item.RelativePathBeneathTable.EndsWith(".json") ? "" : ".json");
                    TextFileHelper.WriteContents(fullPath, item.JsonContent, true);
                }
                catch (Exception exception)
                {
                    if (eventLog != null)
                    {
                        eventLog.LogEvent(0, "Error SaveAllJsonFileItems: (" + item.JsonFileName + ") " + exception.ToString());
                    }
                    returnValue = false;
                }
            }

            return(returnValue);
        }
예제 #2
0
        /// <summary>
        /// Returns a delimited string of the column values for all rows in a table
        /// </summary>
        /// <param name="xml"></param>
        /// <param name="tableName"></param>
        /// <param name="enforceConstraints"></param>
        /// <param name="columnName"></param>
        /// <param name="token"></param>
        /// <param name="eventLog"></param>
        /// <returns>Delimited string containing all rows in DataTable</returns>
        /// <remarks>(NEW in 2.0.0.4)</remarks>
        public static string DataTableColumnToDelimitedString(string xml, string tableName, bool enforceConstraints, string columnName, char token, IEventLog eventLog)
        {
            var dataSet = ConvertXmlStringToDataSet(xml, enforceConstraints);

            if (dataSet == null)
            {
                if (eventLog != null)
                {
                    eventLog.LogEvent(1, "ERROR Invalid xml");
                }
                return(string.Empty);
            }
            if (dataSet.Tables.Contains(tableName) == false)
            {
                if (eventLog != null)
                {
                    eventLog.LogEvent(1, "ERROR Table not part of dataset");
                }
                return(string.Empty);
            }

            var dataTable = dataSet.Tables[tableName];

            return(DataTableColumnToDelimitedString(dataTable, columnName, token, eventLog));
        }
        /// <summary>
        /// Writes some items to the file system. Skipping existing items
        /// </summary>
        /// <param name="jsonRootFolder"></param>
        /// <param name="itemsToAdd"></param>
        /// <param name="itemType"></param>
        /// <param name="itemId"></param>
        /// <param name="itemName"></param>
        /// <param name="eventLog"></param>
        /// <returns></returns>
        public bool InsertItems(string jsonRootFolder, List <JsonItemFile> itemsToAdd, Type itemType, IEventLog eventLog)
        {
            try
            {
                bool priorEnableReadCache = EnableReadCache;
                EnableReadCache = false;

                // Load the assets and then add the asset and then save it.
                var jsonFileSystemManager = new JsonFileSystemManager();
                List <JsonItemFile> items = jsonFileSystemManager.LoadAllJsonFileItems(jsonRootFolder, itemType.Name, eventLog);
                foreach (var item in itemsToAdd)
                {
                    if (items.FirstOrDefault(x => x.JsonFileName == item.JsonFileName) == null)
                    {
                        items.Add(item);
                    }
                }

                bool returnValue = jsonFileSystemManager.SaveAllJsonFileItems(jsonRootFolder, itemType.Name, items, eventLog);
                EnableReadCache = priorEnableReadCache;
                return(returnValue);
            }
            catch (Exception exception)
            {
                if (eventLog != null)
                {
                    eventLog.LogEvent(0, "ERROR Inserting " + itemType.Name + "s : " + exception);
                }

                return(false);
            }
        }
        /// <summary>
        /// Inserts an item to the Json File System
        /// </summary>
        /// <param name="jsonRootFolder">Full path to the folder containing all Json Item Files</param>
        /// <param name="item">Item to write to file system</param>
        /// <param name="itemType">Type of item to write. Pass using typeof()</param>
        /// <param name="itemId">Unique id for item</param>
        /// <param name="itemName">Name of item</param>
        /// <param name="eventLog"></param>
        /// <returns></returns>
        public bool InsertItem(string jsonRootFolder, object item, Type itemType, string itemId, string itemName, IEventLog eventLog)
        {
            try
            {
                bool priorEnableReadCache = EnableReadCache;
                EnableReadCache = false;

                // Load the assets and then add the asset and then save it.
                var jsonFileSystemManager = new JsonFileSystemManager();
                var items = jsonFileSystemManager.LoadAllJsonFileItems(jsonRootFolder, itemType.Name, eventLog);

                var jsonFileItem = GetJsonItemFileForItem(item, itemId, itemType, eventLog);
                items.Add(jsonFileItem);

                bool returnValue = jsonFileSystemManager.SaveAllJsonFileItems(jsonRootFolder, itemType.Name, items, eventLog);
                EnableReadCache = priorEnableReadCache;
                return(returnValue);
            }
            catch (Exception exception)
            {
                if (eventLog != null)
                {
                    eventLog.LogEvent(0, "ERROR Inserting " + itemType.Name + ": (" + itemId + "," + itemName + ") - " + exception);
                }
                return(false);
            }
        }
        /// <summary>
        /// Loads Json Items into Cache using default Json Root Folder and Item Type Name
        /// </summary>
        /// <returns></returns>
        public bool LoadJsonItemFilesToCache(Type itemType, IEventLog eventLog)
        {
            if (string.IsNullOrEmpty(DefaultJsonRootFolder))
            {
                if (eventLog != null)
                {
                    eventLog.LogEvent(0, "JsonFileSystemManager.EnableReadCache - DefaultJsonRootFolder is required to enable caching");
                }
                return(false);
            }

            _cachedJsonItems         = LoadAllJsonFileItems(DefaultJsonRootFolder, itemType.Name, eventLog);
            _cacheLastLoadedDateTime = DateTime.Now;
            return(true);
        }
예제 #6
0
        /// <summary>
        /// Reads data into a .NET DataTable based on the results of a SQL Server Stored Procedure
        /// </summary>
        /// <param name="connectionString">Actual Connection String</param>
        /// <param name="storedProcedureName">Name of stored procedure to execute</param>
        /// <param name="parameterNames">List of parameter names</param>
        /// <param name="parameterTypes">List of parameter types</param>
        /// <param name="parameterValues">List of parameter values</param>
        /// <param name="eventLog">StringBuilder used to store any log messages</param>
        /// <returns></returns>
        /// <remarks>NEW in 1.0.0.3<br/>
        /// FIXED in v1.0.0.4 - If empty string in parameter value of type guid we automatically pass Guid.Empty.
        /// FIXED in v1.0.0.5 - If parameterNames is null then assume call to procedure with no parameters.
        /// </remarks>
        public static DataTable ReadDataTableFromSqlServerViaStoredProcedureAndRawConnectionString(string connectionString,
                                                                                                   string storedProcedureName, List <string> parameterNames, List <string> parameterTypes,
                                                                                                   List <string> parameterValues, IEventLog eventLog)
        {
            var results = new DataTable();

            using (var conn = new SqlConnection(connectionString))
            {
                using (var command = new SqlCommand(storedProcedureName, conn)
                {
                    CommandType = CommandType.StoredProcedure
                })
                {
                    conn.Open();

                    // if parameters passed then ensure they are added as SQL Parameters
                    if (parameterNames != null)
                    {
                        // TO DO: Handle parameters here
                        for (var parameterIndex = 0; parameterIndex < parameterNames.Count; parameterIndex++)
                        {
                            try
                            {
                                DbType dbType = DbType.String;
                                switch (parameterTypes[parameterIndex].ToUpperInvariant())
                                {
                                case "GUID":
                                    dbType = DbType.Guid;
                                    break;

                                case "BOOLEAN":
                                    dbType = DbType.Boolean;
                                    break;

                                case "DATETIME":
                                    dbType = DbType.DateTime;
                                    break;

                                //                              SqlDbType.BigInt;
                                //                              SqlDbType.Binary;
                                //                              SqlDbType.Char
                                //                              SqlDbType.Decimal
                                //                              SqlDbType.Float
                                //                              SqlDbType.Int;
                                case "STRING":
                                    dbType = DbType.String;
                                    break;
                                }

                                // FIX: v1.0.0.4 - If empty string then ignore parameter.
                                if (string.IsNullOrEmpty(parameterValues[parameterIndex]))
                                {
                                    continue;
                                }

                                var parameter = new SqlParameter(parameterNames[parameterIndex],
                                                                 parameterValues[parameterIndex])
                                {
                                    DbType = dbType
                                };
                                // TO DO: Direction
                                command.Parameters.Add(parameter);
                            }
                            catch (Exception exception)
                            {
                                // Log some more details to help debug
                                if (eventLog != null)
                                {
                                    eventLog.LogEvent(0, "ReadDataTableFromSqlServerViaStoredProcedureAndRawConnectionString: " + parameterIndex + " - " + parameterNames);
                                }
                                throw new Exception("ReadDataTableFromSqlServerViaStoredProcedureAndRawConnectionString: " + parameterIndex + " - " + parameterNames, exception);
                            }
                        }
                    }

                    var adapter = new SqlDataAdapter(command);
                    adapter.Fill(results);
                    conn.Close();
                }
            }

            return(results);
        }
        /// <summary>
        /// Writes some items to the file system. Skipping existing items
        /// </summary>
        /// <param name="jsonRootFolder"></param>
        /// <param name="itemsToAdd"></param>
        /// <param name="itemType"></param>
        /// <param name="itemId"></param>
        /// <param name="itemName"></param>
        /// <param name="eventLog"></param>
        /// <returns></returns>
        public bool InsertItems(string jsonRootFolder, List <JsonItemFile> itemsToAdd, Type itemType, bool overwrite, IEventLog eventLog)
        {
            // if we are not overwriting items
            if (overwrite == false)
            {
                return(InsertItems(jsonRootFolder, itemsToAdd, itemType, eventLog));
            }

            // if we are overwriting items
            try
            {
                bool priorEnableReadCache = EnableReadCache;
                EnableReadCache = false;

                // Load the assets
                var jsonFileSystemManager       = new JsonFileSystemManager();
                List <JsonItemFile> items       = jsonFileSystemManager.LoadAllJsonFileItems(jsonRootFolder, itemType.Name, eventLog);
                List <JsonItemFile> itemsToSave = new List <JsonItemFile>();

                // Ensure all the ones we are adding are in the save list.
                // Only include existing items that are not replaced by our inserts
                foreach (var item in itemsToAdd)
                {
                    // Ensure item filename ends in .json
                    if (item.JsonFileName.EndsWith(".json") == false)
                    {
                        item.JsonFileName = item.JsonFileName + ".json";
                    }

                    itemsToSave.Add(item);
                }

                // Only include existing items that are not replaced by our inserts
                foreach (var item in items)
                {
                    // Ensure item filename ends in .json
                    if (item.JsonFileName.EndsWith(".json") == false)
                    {
                        item.JsonFileName = item.JsonFileName + ".json";
                    }

                    if (itemsToSave.FirstOrDefault(x => x.JsonFileName == item.JsonFileName) == null)
                    {
                        itemsToSave.Add(item);
                    }
                }

                // save everything back to file system.
                bool returnValue = jsonFileSystemManager.SaveAllJsonFileItems(jsonRootFolder, itemType.Name, itemsToSave, eventLog);
                EnableReadCache = priorEnableReadCache;
                return(returnValue);
            }
            catch (Exception exception)
            {
                if (eventLog != null)
                {
                    eventLog.LogEvent(0, "ERROR Inserting " + itemType.Name + "s : " + exception);
                }

                return(false);
            }
        }
예제 #8
0
        /// <summary>
        /// Returns .net dataset friendly xml based on an Atom feed
        /// </summary>
        /// <param name="atomXmlData"></param>
        /// <param name="eventLog"></param>
        /// <returns></returns>
        /// <remarks>Adapted from this forum post: http://social.msdn.microsoft.com/Forums/eu/xmlandnetfx/thread/2aad018f-7af1-41c3-bfa1-8691eed0eb38 </remarks>
        public static String AtomToXml(string atomXmlData, IEventLog eventLog)
        {
            var stringReader = new StringReader(atomXmlData);
            var reader       = new XmlTextReader(stringReader)
            {
                WhitespaceHandling = WhitespaceHandling.None
            };
            var builder = new StringBuilder();

            var fContinue = reader.Read();

            while (fContinue)
            {
                var sName = reader.Name.Replace(":", "_");

                var isClosed = reader.IsEmptyElement;
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                    builder.Append("<" + sName);
                    if (reader.HasAttributes)
                    {
                        while (reader.MoveToNextAttribute())
                        {
                            sName = reader.Name.Replace(":", "_");
                            builder.Append(" " + sName + "=\"" + reader.Value.Replace("&", "&amp;") + "\"");
                        }
                    }
                    if (isClosed)
                    {
                        builder.Append("/");
                    }
                    builder.Append(">");
                    break;

                case XmlNodeType.Text:
                    builder.Append(HttpUtility.HtmlEncode(reader.Value));
                    break;

                case XmlNodeType.EndElement:
                    builder.Append("</" + sName + ">");
                    break;
                }


                fContinue = reader.Read();
            }

            stringReader.Close();
            stringReader.Dispose();

            reader.Close();

            var sXmlResult = "<atom>" + builder + "</atom>";

            var dataSet       = new DataSet();
            var oStringReader = new StringReader(sXmlResult);

            try
            {
                dataSet.ReadXml(oStringReader, XmlReadMode.Auto);
            }
            catch (Exception exception)
            {
                if (eventLog != null)
                {
                    eventLog.LogEvent(0, "ERROR Converting Atom To Xml - " + exception);
                }
            }

            oStringReader.Close();
            oStringReader.Dispose();
            reader.Close();
            return(dataSet.Tables.Count == 0 ? null : dataSet.GetXml());
        }
예제 #9
0
        /// <summary>
        /// Returns .net dataset friendly xml based on an rss feed
        /// </summary>
        /// <param name="rssXmlData">Raw RSS data from feed</param>
        /// <param name="eventLog">Object that will receive log messages</param>
        /// <returns></returns>
        /// <remarks>Adapted from this forum post: http://social.msdn.microsoft.com/Forums/eu/xmlandnetfx/thread/2aad018f-7af1-41c3-bfa1-8691eed0eb38
        /// v1.0.0.1 FIXED: Ampersands now getting encoded via regex for attributes and text blocks.
        /// </remarks>
        public static String RssToXml(string rssXmlData, IEventLog eventLog)
        {
            using (var stringReader = new StringReader(rssXmlData))
            {
                using (var reader = new XmlTextReader(stringReader)
                {
                    WhitespaceHandling = WhitespaceHandling.None
                })
                {
                    var builder = new StringBuilder();

                    var fContinue = reader.Read();
                    while (fContinue)
                    {
                        var    sName       = reader.Name.Replace(":", "_");
                        var    isClosed    = reader.IsEmptyElement;
                        string encodeValue = string.Empty;
                        switch (reader.NodeType)
                        {
                        case XmlNodeType.Element:

                            builder.Append("<" + sName);
                            if (reader.HasAttributes)
                            {
                                while (reader.MoveToNextAttribute())
                                {
                                    // if reader value has ampersand and not encoded then encode it
                                    encodeValue = Regex.Replace(reader.Value, @"
                                        # Match & that is not part of an HTML entity.
                                        &                  # Match literal &.
                                        (?!                # But only if it is NOT...
                                          \w+;             # an alphanumeric entity,
                                        | \#[0-9]+;        # or a decimal entity,
                                        | \#x[0-9A-F]+;    # or a hexadecimal entity.
                                        )                  # End negative lookahead.",
                                                                "&amp;",
                                                                RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

                                    sName = reader.Name.Replace(":", "_");
                                    builder.Append(" " + sName + "=\"" + encodeValue + "\"");
                                }
                            }
                            if (isClosed)
                            {
                                builder.Append("/");
                            }
                            builder.Append(">");
                            break;

                        case XmlNodeType.Text:

                            // Check if encoded string contains & and if so correctly encodes it.
                            encodeValue = reader.Value;
                            if (HttpUtility.HtmlEncode(encodeValue).IndexOf("&") > -1)
                            {
                                encodeValue = Regex.Replace(encodeValue, @"
                                        # Match & that is not part of an HTML entity.
                                        &                  # Match literal &.
                                        (?!                # But only if it is NOT...
                                          \w+;             # an alphanumeric entity,
                                        | \#[0-9]+;        # or a decimal entity,
                                        | \#x[0-9A-F]+;    # or a hexadecimal entity.
                                        )                  # End negative lookahead.",
                                                            "&amp;",
                                                            RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

                                encodeValue = HttpUtility.HtmlEncode(encodeValue);
                            }

                            builder.Append(encodeValue);

                            break;

                        case XmlNodeType.EndElement:
                            builder.Append("</" + sName + ">");
                            break;
                        }


                        fContinue = reader.Read();
                    }

                    reader.Close();

                    var sXmlResult = builder.ToString();

                    var dataSet       = new DataSet();
                    var oStringReader = new StringReader(sXmlResult);
                    try
                    {
                        dataSet.ReadXml(oStringReader, XmlReadMode.Auto);
                    }
                    catch (Exception exception)
                    {
                        if (eventLog != null)
                        {
                            eventLog.LogEvent(0, "RssHelper.RssToXml - ERROR: " + exception.ToString());
                        }
                    }

                    // Make sure reader is closed and memory disposed.
                    oStringReader.Close();
                    oStringReader.Dispose();
                    oStringReader = null;

                    return(dataSet.Tables.Count == 0 ? null : dataSet.GetXml());
                }
            }
        }