public override Stream GetFragment(string id, string contentType)
        {
            // need to see which definition we match
                ResourcePublishingDefinition definition = null;
                UriTemplateMatch match = null;
                foreach (var resourcePublishingDefinition in _publishingDefinitions)
                {
                    var newuri = new Uri(id);
                    match = resourcePublishingDefinition.UriTemplate.Match(resourcePublishingDefinition.ResourcePrefix, newuri);
                    if (match != null)
                    {
                        definition = resourcePublishingDefinition;
                        break;
                    }
                }

                if (definition == null) { throw new Exception("Unable to find matching definition for uri " + id); }

                var sb = new StringBuilder();
                foreach (var generationDefinition in definition.FragmentGenerationDefinitions)
                {
                    try
                    {
                        var data = ExecuteQuery(_dataSourceConnectionString, generationDefinition.FragmentQuery.Replace("[[id]]", match.BoundVariables["id"]));
                        foreach (DataRow row in data.Rows)
                        {
                            var dra = new DbDataRow(row);
                            foreach (var line in generationDefinition.RdfTemplateLines)
                            {
                                var linePattern = new NTripleLinePattern(line);
                                linePattern.GenerateNTriples(sb, dra, generationDefinition.GenericTemplateExcludeColumns, contentType.Equals("xml"));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logging.LogError(1, "Error processing definition {0} {1} {2} {3}", ex.Message, generationDefinition.SnapshotQuery, _dataSourceConnectionString, ex.StackTrace);
                    }
                }

                try
                {
                    var g = new Graph();
                    var parser = new NTriplesParser(TokenQueueMode.SynchronousBufferDuringParsing);
                    var triplesStr = sb.ToString();
                    parser.Load(g, new StringReader(triplesStr));

                    if (contentType.Equals("xml"))
                    {
                        var ms = new MemoryStream();
                        var sw = new StreamWriter(ms, Encoding.UTF8);
                        var rdfxmlwriter = new RdfXmlWriter();

                        var strw = new System.IO.StringWriter();
                        rdfxmlwriter.Save(g, strw);
                        var data = strw.ToString();

                        data = data.Replace("~~~2B~~~", "%2B");
                        data = data.Replace("~~~SLASH~~~", "%2F");
                        data = data.Replace("utf-16", "utf-8");
                        sw.Write(data);
                        sw.Flush();
                        ms.Seek(0, SeekOrigin.Begin);
                        return ms;
                    }
                    else
                    {
                        var ms = new MemoryStream();
                        var sw = new StreamWriter(ms);
                        sw.Write(triplesStr);
                        sw.Flush();
                        ms.Seek(0, SeekOrigin.Begin);
                        return ms;
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogError(1, "Error getting fragment {0} {1}", ex.Message, ex.StackTrace);
                    throw;
                }
        }
Example #2
0
        public override Stream GetSnapshot(string id, string contentType)
        {
            Logging.LogInfo("GetSnapshot for " + id);
            var tmpFileName = ConfigurationReader.Configuration.HashValueStorageLocation + Path.DirectorySeparatorChar + Guid.NewGuid();

            Logging.LogInfo("Writing to file " + tmpFileName);

            try {
                using (var fs = new FileStream(tmpFileName, FileMode.Create))
                {
                    var sb = new StringBuilder();

                    foreach (var definition in _publishingDefinitions)
                    {
                        foreach (var generationDefinition in definition.FragmentGenerationDefinitions)
                        {
                            try
                            {
                                using (var connection = new OdbcConnection(_dataSourceConnectionString))
                                {
                                    connection.Open();
                                    var odbcCommand = new OdbcCommand(generationDefinition.SnapshotQuery)
                                    {
                                        Connection     = connection,
                                        CommandTimeout = 0
                                    };
                                    var dr          = odbcCommand.ExecuteReader();
                                    var schema      = dr.GetSchemaTable();
                                    var columnNames = (from DataRow row in schema.Rows select row[0].ToString().ToLower()).ToList();
                                    var flushCount  = 0;
                                    var drAdaptor   = new DbReaderDataRow(dr, columnNames);
                                    while (dr.Read())
                                    {
                                        try
                                        {
                                            flushCount++;
                                            foreach (var line in generationDefinition.RdfTemplateLines)
                                            {
                                                var pattern = new NTripleLinePattern(line);
                                                pattern.GenerateNTriples(sb, drAdaptor, generationDefinition.GenericTemplateExcludeColumns, false);
                                            }
                                        }
                                        catch (Exception dataex)
                                        {
                                            Logging.LogError(1, "Error Processing Data Line in " + generationDefinition.SnapshotQuery + " : " + dataex.Message);
                                        }

                                        if (flushCount >= 1000)
                                        {
                                            try
                                            {
                                                var sw = new StreamWriter(fs);
                                                sw.Write(sb.ToString());
                                                sw.Flush();
                                                sb = new StringBuilder();
                                            }
                                            catch (Exception ex)
                                            {
                                                var msg = ex.Message;
                                                Logging.LogError(1, "Error exporting triples " + msg + " " + ex.StackTrace);
                                            }
                                        }
                                    }

                                    if (flushCount >= 0)
                                    {
                                        try
                                        {
                                            var sw = new StreamWriter(fs);
                                            sw.Write(sb.ToString());
                                            sw.Flush();
                                            sb = new StringBuilder();
                                        }
                                        catch (Exception ex)
                                        {
                                            var msg = ex.Message;
                                            Logging.LogError(1, "Error exporting triples " + msg + " " + ex.StackTrace);
                                        }
                                    }

                                    dr.Close();
                                    connection.Close();
                                }
                            }
                            catch (Exception ext)
                            {
                                Logging.LogError(1, "Error processing definition {0} {1} {2} {3}", ext.Message, generationDefinition.SnapshotQuery, _dataSourceConnectionString, ext.StackTrace);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.LogError(1, "Exception getting snapshot " + ex.Message + " " + ex.StackTrace);
            }
            return(new FileStream(tmpFileName, FileMode.Open));
        }
        public override Stream GetSnapshot(string id, string contentType)
        {
            Logging.LogInfo("GetSnapshot for " + id);
            var tmpFileName = ConfigurationReader.Configuration.HashValueStorageLocation + Path.DirectorySeparatorChar + Guid.NewGuid();
            Logging.LogInfo("Writing to file " + tmpFileName);

            try {
                using (var fs = new FileStream(tmpFileName, FileMode.Create))
                {
                    var sb = new StringBuilder();

                    foreach (var definition in _publishingDefinitions)
                    {
                        foreach (var generationDefinition in definition.FragmentGenerationDefinitions)
                        {
                            try
                            {
                                using (var connection = new OdbcConnection(_dataSourceConnectionString))
                                {
                                    connection.Open();
                                    var odbcCommand = new OdbcCommand(generationDefinition.SnapshotQuery)
                                                          {
                                                              Connection = connection,
                                                              CommandTimeout = 0
                                                          };
                                    var dr = odbcCommand.ExecuteReader();
                                    var schema = dr.GetSchemaTable();
                                    var columnNames = (from DataRow row in schema.Rows select row[0].ToString().ToLower()).ToList();
                                    var flushCount = 0;
                                    var drAdaptor = new DbReaderDataRow(dr, columnNames);
                                    while (dr.Read())
                                    {
                                        try
                                        {
                                            flushCount++;
                                            foreach (var line in generationDefinition.RdfTemplateLines)
                                            {
                                                var pattern = new NTripleLinePattern(line);
                                                pattern.GenerateNTriples(sb, drAdaptor, generationDefinition.GenericTemplateExcludeColumns, false);
                                            }
                                        }
                                        catch (Exception dataex)
                                        {
                                            Logging.LogError(1, "Error Processing Data Line in " + generationDefinition.SnapshotQuery + " : " + dataex.Message);
                                        }

                                        if (flushCount >= 1000)
                                        {
                                            try
                                            {
                                                var sw = new StreamWriter(fs);
                                                sw.Write(sb.ToString());
                                                sw.Flush();
                                                sb = new StringBuilder();
                                            }
                                            catch (Exception ex)
                                            {
                                                var msg = ex.Message;
                                                Logging.LogError(1, "Error exporting triples " + msg + " " + ex.StackTrace);
                                            }
                                        }
                                    }

                                    if (flushCount >= 0)
                                    {
                                        try
                                        {
                                            var sw = new StreamWriter(fs);
                                            sw.Write(sb.ToString());
                                            sw.Flush();
                                            sb = new StringBuilder();
                                        }
                                        catch (Exception ex)
                                        {
                                            var msg = ex.Message;
                                            Logging.LogError(1, "Error exporting triples " + msg + " " + ex.StackTrace);
                                        }
                                    }

                                    dr.Close();
                                    connection.Close();
                                }
                            }
                            catch (Exception ext)
                            {
                                Logging.LogError(1, "Error processing definition {0} {1} {2} {3}", ext.Message, generationDefinition.SnapshotQuery, _dataSourceConnectionString, ext.StackTrace);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.LogError(1, "Exception getting snapshot " + ex.Message + " " + ex.StackTrace);
            }
            return new FileStream(tmpFileName, FileMode.Open);
        }
Example #4
0
        public override Stream GetFragment(string id, string contentType)
        {
            // need to see which definition we match
            ResourcePublishingDefinition definition = null;
            UriTemplateMatch             match      = null;

            foreach (var resourcePublishingDefinition in _publishingDefinitions)
            {
                var newuri = new Uri(id);
                match = resourcePublishingDefinition.UriTemplate.Match(resourcePublishingDefinition.ResourcePrefix, newuri);
                if (match != null)
                {
                    definition = resourcePublishingDefinition;
                    break;
                }
            }

            if (definition == null)
            {
                throw new Exception("Unable to find matching definition for uri " + id);
            }

            var sb = new StringBuilder();

            foreach (var generationDefinition in definition.FragmentGenerationDefinitions)
            {
                try
                {
                    var data = ExecuteQuery(_dataSourceConnectionString, generationDefinition.FragmentQuery.Replace("[[id]]", match.BoundVariables["id"]));
                    foreach (DataRow row in data.Rows)
                    {
                        var dra = new DbDataRow(row);
                        foreach (var line in generationDefinition.RdfTemplateLines)
                        {
                            var linePattern = new NTripleLinePattern(line);
                            linePattern.GenerateNTriples(sb, dra, generationDefinition.GenericTemplateExcludeColumns, contentType.Equals("xml"));
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogError(1, "Error processing definition {0} {1} {2} {3}", ex.Message, generationDefinition.SnapshotQuery, _dataSourceConnectionString, ex.StackTrace);
                }
            }

            try
            {
                var g          = new Graph();
                var parser     = new NTriplesParser(TokenQueueMode.SynchronousBufferDuringParsing);
                var triplesStr = sb.ToString();
                parser.Load(g, new StringReader(triplesStr));

                if (contentType.Equals("xml"))
                {
                    var ms           = new MemoryStream();
                    var sw           = new StreamWriter(ms, Encoding.UTF8);
                    var rdfxmlwriter = new RdfXmlWriter();

                    var strw = new System.IO.StringWriter();
                    rdfxmlwriter.Save(g, strw);
                    var data = strw.ToString();

                    data = data.Replace("~~~2B~~~", "%2B");
                    data = data.Replace("~~~SLASH~~~", "%2F");
                    data = data.Replace("utf-16", "utf-8");
                    sw.Write(data);
                    sw.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    return(ms);
                }
                else
                {
                    var ms = new MemoryStream();
                    var sw = new StreamWriter(ms);
                    sw.Write(triplesStr);
                    sw.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    return(ms);
                }
            }
            catch (Exception ex)
            {
                Logging.LogError(1, "Error getting fragment {0} {1}", ex.Message, ex.StackTrace);
                throw;
            }
        }