Пример #1
0
        //---------------------------------------------------------------------------------------------------------------------

        /// <summary>.</summary>
        public static void ExecuteCatalogueSeriesRefresh(IfyContext context)
        {
            string      sql    = "SELECT t.id, t.identifier, t.cat_description, t1.base_url FROM series AS t LEFT JOIN catalogue AS t1 ON t.id_catalogue=t1.id WHERE t.auto_refresh ORDER BY t.identifier;";
            int         count  = 0;
            IDataReader reader = context.GetQueryResult(sql);

            while (reader.Read())
            {
                int    id   = reader.GetInt32(0);
                string name = reader.GetString(1);
                string catalogueDescriptionUrl = reader.GetString(2);
                string catalogueBaseUrl        = context.GetValue(reader, 3);
                if (catalogueDescriptionUrl == null || catalogueDescriptionUrl == String.Empty)
                {
                    context.AddError("No catalogue description URL defined for \"" + name + "\"");
                    continue;
                }
                bool usePlaceholder = (catalogueDescriptionUrl.Contains("$(CATALOGUE)") && catalogueBaseUrl != null);
                if (usePlaceholder)
                {
                    catalogueDescriptionUrl = catalogueDescriptionUrl.Replace("$(CATALOGUE)", catalogueBaseUrl);
                }
                string catalogueUrlTemplate = null;
                try {
                    catalogueUrlTemplate = Terradue.Metadata.OpenSearch.OpenSearchDescription.GetUrlTemplate(catalogueDescriptionUrl, new string[] { "application/rdf+xml", "application/xhtml+xml" });
                } catch (XmlException) {
                    context.AddError("Catalogue description URL for \"" + name + "\" returns invalid description");
                } catch (Exception) {
                    context.AddError("No catalogue URL template found for series \"" + name + "\"");
                }
                if (catalogueUrlTemplate == null)
                {
                    continue;
                }
                if (usePlaceholder)
                {
                    catalogueUrlTemplate = catalogueUrlTemplate.Replace(catalogueBaseUrl, "$(CATALOGUE)" + (catalogueBaseUrl.EndsWith("/") ? "/" : String.Empty));
                }
                context.Execute(String.Format("UPDATE series SET cat_template={1} WHERE id={0};", id, StringUtils.EscapeSql(catalogueUrlTemplate)));
                count++;
            }
            context.AddInfo(String.Format("Updated series: {0}", count));

            count = context.GetQueryIntegerValue("SELECT COUNT(*) FROM series WHERE NOT auto_refresh;");
            if (count != 0)
            {
                context.AddInfo(String.Format("Ignored series: {0}", count));
            }

            sql    = "SELECT t.id, t.identifier, t.cat_description, t1.base_url FROM producttype AS t LEFT JOIN catalogue AS t1 ON t.id_catalogue=t1.id ORDER BY t.identifier;";
            reader = context.GetQueryResult(sql);
            while (reader.Read())
            {
                int    id   = reader.GetInt32(0);
                string name = reader.GetString(1);
                string catalogueDescriptionUrl = reader.GetString(2);
                string catalogueBaseUrl        = context.GetValue(reader, 3);
                if (catalogueDescriptionUrl == null || catalogueDescriptionUrl == String.Empty)
                {
                    context.AddError("No catalogue description URL defined for product type \"" + name + "\"");
                    continue;
                }
                bool usePlaceholder = (catalogueDescriptionUrl.Contains("$(CATALOGUE)") && catalogueBaseUrl != null);
                if (usePlaceholder)
                {
                    catalogueDescriptionUrl = catalogueDescriptionUrl.Replace("$(CATALOGUE)", catalogueBaseUrl);
                }
                string catalogueUrlTemplate = null;
                try {
                    catalogueUrlTemplate = Terradue.Metadata.OpenSearch.OpenSearchDescription.GetUrlTemplate(catalogueDescriptionUrl, new string[] { "application/rdf+xml", "application/xhtml+xml" });
                } catch (XmlException) {
                    context.AddError("Catalogue description URL for product type \"" + name + "\" returns invalid description");
                } catch (Exception) {
                    context.AddError("No catalogue URL template found for product type \"" + name + "\"");
                }
                if (catalogueUrlTemplate == null)
                {
                    continue;
                }
                if (usePlaceholder)
                {
                    catalogueUrlTemplate = catalogueUrlTemplate.Replace(catalogueBaseUrl, "$(CATALOGUE)" + (catalogueBaseUrl.EndsWith("/") ? "/" : String.Empty));
                }
                context.Execute(String.Format("UPDATE producttype SET cat_template={1} WHERE id={0};", id, StringUtils.EscapeSql(catalogueUrlTemplate)));
            }
            reader.Close();
        }
Пример #2
0
        //---------------------------------------------------------------------------------------------------------------------

        /// <summary>Adds data sets from the specified URL.</summary>
        /// <returns><c>true</c>, if the request was successful, <c>false</c> if there was an error.</returns>
        /// <param name="url">A URL at which RDF metadata content for the data sets can be obtained.</param>
        /// <param name="startIndex">The start index of the current partial request.</param>
        /// <param name="count">The number of data sets to be received with the current partial request.</param>
        protected bool AddResultsFromRdf(string url, int startIndex, int count)
        {
            /*url = Regex.Replace(url, @"([^\&\?]+)=\{startIndex\??\}", "$1=" + startIndex);
             * url = Regex.Replace(url, @"([^\&\?]+)=\{count\??\}", "$1=" + count);*/
            url = Regex.Replace(url, @"([\?&])startIndex=", (startIndex == -1 ? "" : "$1startIndex=" + startIndex));
            url = Regex.Replace(url, @"([\?&])count=", "$1count=" + count);
            context.AddDebug(3, "Catalogue query (page): " + url);

            try {
                XmlDocument doc = new XmlDocument();
                doc.Load(url);

                if (namespaceManager == null)
                {
                    namespaceManager = new XmlNamespaceManager(doc.NameTable);
                    foreach (XmlAttribute attr in doc.DocumentElement.Attributes)
                    {
                        if (attr.Prefix == "xmlns")
                        {
                            namespaceManager.AddNamespace(attr.LocalName, attr.Value);
                        }
                    }
                }

                // In the first request, retrieve meta information on results
                if (startIndex == 0)
                {
                    // Extract the number of results from the retreived XML document and add it to the local totalResults
                    XmlNode node = doc.SelectSingleNode("//rdf:Description/os:totalResults", namespaceManager);
                    int     tr   = 0;
                    if (node != null)
                    {
                        Int32.TryParse(node.InnerText, out tr);
                    }
                    TotalResults = tr;

                    // Extract the first start index (i.e. the index offset)
                    node       = doc.SelectSingleNode("//rdf:Description/os:startIndex", namespaceManager);
                    startIndex = 0;
                    if (node != null)
                    {
                        Int32.TryParse(node.InnerText, out startIndex);
                    }

                    // Extract the actual number of items per page, in case it does not correspond to the requested count value
                    node = doc.SelectSingleNode("//rdf:Description/os:itemsPerPage", namespaceManager);
                    //if (node != null) Int32.TryParse(node.InnerText, out dataSetsPerRequest);

                    // Set length of dataSets array and of metadata arrays

                    count = (TotalResults <= maxCount ? TotalResults : maxCount);
                    InitializeData(count);

                    ReceivedResults = 0;
                }

                if (count == 0 || TotalResults < minCount)
                {
                    return(true);                                       // continue only if results are requested
                }
                XmlNodeList nodes = doc.SelectNodes(DataSetXmlName, namespaceManager);
                for (int i = 0; i < nodes.Count; i++)
                {
                    DataSets[ReceivedResults] = new CatalogueDataSetInfo(this, ReceivedResults);
                    GetDataSetInformation(ReceivedResults, nodes[i] as XmlElement);
                    ReceivedResults++;
                }
                context.AddDebug(3, "Results: " + ReceivedResults);
            } catch (Exception e) {
                context.AddError("Catalogue query failed: " + e.Message + " " + url);
                return(false);
            }

            return(true);
        }
Пример #3
0
        //---------------------------------------------------------------------------------------------------------------------

        public bool Send(string subject, string body, List <string> to, List <string> cc, List <string> bcc, string contentType)
        {
            string smtpHostname = context.GetConfigValue("SmtpHostname");
            string smtpUsername = context.GetConfigValue("SmtpUsername");

            if (smtpUsername == String.Empty)
            {
                smtpUsername = null;
            }
            string smtpPassword = context.GetConfigValue("SmtpPassword");

            if (smtpPassword == String.Empty)
            {
                smtpPassword = null;
            }
            string mailSenderAddress = context.GetConfigValue("MailSenderAddress");
            string mailSender        = context.GetConfigValue("MailSender");

            if (mailSender == null)
            {
                mailSender = mailSenderAddress;
            }

            if (smtpHostname == null || mailSenderAddress == null)
            {
                string message = String.Format("Invalid mailing settings: smtpHostname = {0}\tsmtpUsername = {1}\t,mailSenderAddress = {2}", smtpHostname, smtpUsername, mailSenderAddress);
                context.LogError(this, message);
                throw new ArgumentNullException(message);
            }

            // create the mail and setting parameters
            MailMessage mail = new MailMessage();

            mail.Body    = body;
            mail.Subject = subject;
            mail.From    = new MailAddress(mailSenderAddress, mailSender);

            // Add main recipients
            foreach (string recipient in to)
            {
                mail.To.Add(new MailAddress(recipient, recipient));
            }

            // Add CC recipients
            if (cc != null)
            {
                foreach (string recipient in cc)
                {
                    mail.CC.Add(new MailAddress(recipient, recipient));
                }
            }

            // Add BCC recipients
            if (bcc != null)
            {
                foreach (string recipient in bcc)
                {
                    mail.Bcc.Add(new MailAddress(recipient, recipient));
                }
            }

            SmtpClient client = new SmtpClient(smtpHostname);

            // Add credentials if the SMTP server requires them
            if (smtpUsername != null || smtpPassword != null)
            {
                client.Credentials = new System.Net.NetworkCredential(smtpUsername, smtpPassword);
            }

            // Add alternate view if content type is defined
            if (contentType != null)
            {
                AlternateView alternateView = AlternateView.CreateAlternateViewFromString(body, new ContentType(contentType));
                mail.AlternateViews.Add(alternateView);
            }

            try {
                client.Send(mail);
                return(true);
            } catch (Exception e) {
                string message;
                if (e.Message.Contains("CDO.Message") || e.Message.Contains("535"))
                {
                    message = "Mail could not be sent, this is a site administration issue (probably caused by an invalid SMTP hostname or wrong SMTP server credentials)";
                }
                else
                {
                    message = String.Format("Mail could not be sent, this is a site administration issue: {0}{1}", e.Message, e.InnerException == null ? String.Empty : String.Format("({0})", e.InnerException.Message));
                }
                context.AddError(message);
                context.LogError(this, message);
                throw;
            }
        }