예제 #1
0
        private LeavesResults GetResults(string leavesAPI)
        {
            LeavesResults results = new LeavesResults();

            try
            {
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(leavesAPI);
                httpWebRequest.Method = "GET";

                var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpWebResponse.StatusCode != HttpStatusCode.OK)
                {
                    string message = String.Format("leaves API failed. Received HTTP {0}", httpWebResponse.StatusCode);
                    ErrorHandler.Publish(LogLevel.Error, message);
                }

                // Get the response.
                WebResponse response = httpWebRequest.GetResponse();
                // Display the status.
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                var dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
                // Display the content.
                results = JsonConvert.DeserializeObject <LeavesResults>(responseFromServer);
                // Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                ErrorHandler.Publish(LogLevel.Error, ex);
            }

            return(results);
        }
예제 #2
0
        private void LoadResults()
        {
            #region Settings
            string tags = string.Empty;
            if (this.Settings.ContainsKey("LEAVES_ARTICLE_LISTING_TAGS") && this.Settings["LEAVES_ARTICLE_LISTING_TAGS"].Value != null && !string.IsNullOrEmpty(this.Settings["LEAVES_ARTICLE_LISTING_TAGS"].Value.ToString()))
            {
                tags = this.Settings["LEAVES_ARTICLE_LISTING_TAGS"].Value.ToString();
            }

            string Order = "desc";
            if (this.Settings.ContainsKey("LEAVES_ARTICLE_LISTING_ORDER") && this.Settings["LEAVES_ARTICLE_LISTING_ORDER"].Value != null && !string.IsNullOrEmpty(this.Settings["LEAVES_ARTICLE_LISTING_ORDER"].Value.ToString()))
            {
                Order = this.Settings["LEAVES_ARTICLE_LISTING_ORDER"].Value.ToString();
            }

            string Sort = "created";
            if (this.Settings.ContainsKey("LEAVES_ARTICLE_LISTING_SORT") && this.Settings["LEAVES_ARTICLE_LISTING_SORT"].Value != null && !string.IsNullOrEmpty(this.Settings["LEAVES_ARTICLE_LISTING_SORT"].Value.ToString()))
            {
                Sort = this.Settings["LEAVES_ARTICLE_LISTING_SORT"].Value.ToString();
            }

            string Limit = "5";
            if (this.Settings.ContainsKey("LEAVES_ARTICLE_LISTING_LIMIT") && this.Settings["LEAVES_ARTICLE_LISTING_LIMIT"].Value != null && !string.IsNullOrEmpty(this.Settings["LEAVES_ARTICLE_LISTING_LIMIT"].Value.ToString()))
            {
                Limit = this.Settings["LEAVES_ARTICLE_LISTING_LIMIT"].Value.ToString();
            }

            string Leaves_base_url = string.Empty;
            if (this.Settings.ContainsKey("LEAVES_ARTICLE_LISTING_LEAVES_BASE_URL") && this.Settings["LEAVES_ARTICLE_LISTING_LEAVES_BASE_URL"].Value != null && !string.IsNullOrEmpty(this.Settings["LEAVES_ARTICLE_LISTING_LEAVES_BASE_URL"].Value.ToString()))
            {
                Leaves_base_url = this.Settings["LEAVES_ARTICLE_LISTING_LEAVES_BASE_URL"].Value.ToString();
            }

            string Leaves_entries_url = string.Empty;
            if (this.Settings.ContainsKey("LEAVES_ARTICLE_LISTING_LEAVES_ENTRIES_URL") && this.Settings["LEAVES_ARTICLE_LISTING_LEAVES_ENTRIES_URL"].Value != null && !string.IsNullOrEmpty(this.Settings["LEAVES_ARTICLE_LISTING_LEAVES_ENTRIES_URL"].Value.ToString()))
            {
                Leaves_entries_url = this.Settings["LEAVES_ARTICLE_LISTING_LEAVES_ENTRIES_URL"].Value.ToString();
            }

            string Leaves_access_key = string.Empty;
            if (this.Settings.ContainsKey("LEAVES_ARTICLE_LISTING_LEAVES_ACCESS_KEY") && this.Settings["LEAVES_ARTICLE_LISTING_LEAVES_ACCESS_KEY"].Value != null && !string.IsNullOrEmpty(this.Settings["LEAVES_ARTICLE_LISTING_LEAVES_ACCESS_KEY"].Value.ToString()))
            {
                Leaves_access_key = this.Settings["LEAVES_ARTICLE_LISTING_LEAVES_ACCESS_KEY"].Value.ToString();
            }

            string columnCSSClass = string.Empty;
            if (this.Settings.ContainsKey("LEAVES_ARTICLE_LISTING_COLUMN_CSS_CLASS") && this.Settings["LEAVES_ARTICLE_LISTING_COLUMN_CSS_CLASS"].Value != null && !string.IsNullOrEmpty(this.Settings["LEAVES_ARTICLE_LISTING_COLUMN_CSS_CLASS"].Value.ToString()))
            {
                columnCSSClass = this.Settings["LEAVES_ARTICLE_LISTING_COLUMN_CSS_CLASS"].Value.ToString();
            }

            int charLimit = 200;
            if (this.Settings.ContainsKey("LEAVES_ARTICLE_LISTING_CHAR_LIMIT") && this.Settings["LEAVES_ARTICLE_LISTING_CHAR_LIMIT"].Value != null && !string.IsNullOrEmpty(this.Settings["LEAVES_ARTICLE_LISTING_CHAR_LIMIT"].Value.ToString()))
            {
                int.TryParse(this.Settings["LEAVES_ARTICLE_LISTING_CHAR_LIMIT"].Value.ToString(), out charLimit);
                if (charLimit <= 0)
                {
                    charLimit = 200;
                }
            }

            int titlecharLimit = 100;
            if (this.Settings.ContainsKey("LEAVES_ARTICLE_LISTING_TITLE_CHAR_LIMIT") && this.Settings["LEAVES_ARTICLE_LISTING_TITLE_CHAR_LIMIT"].Value != null && !string.IsNullOrEmpty(this.Settings["LEAVES_ARTICLE_LISTING_TITLE_CHAR_LIMIT"].Value.ToString()))
            {
                int.TryParse(this.Settings["LEAVES_ARTICLE_LISTING_TITLE_CHAR_LIMIT"].Value.ToString(), out titlecharLimit);
                if (titlecharLimit <= 0)
                {
                    titlecharLimit = 100;
                }
            }

            //int pageSizeLimit = 10;
            //if (this.Settings.ContainsKey("LEAVES_ARTICLE_LISTING_PAGE_SIZE_LIMIT") && this.Settings["LEAVES_ARTICLE_LISTING_PAGE_SIZE_LIMIT"].Value != null && !string.IsNullOrEmpty(this.Settings["LEAVES_ARTICLE_LISTING_PAGE_SIZE_LIMIT"].Value.ToString()))
            //{
            //    int.TryParse(this.Settings["LEAVES_ARTICLE_LISTING_PAGE_SIZE_LIMIT"].Value.ToString(), out pageSizeLimit);
            //    if (pageSizeLimit <= 0)
            //    {
            //        pageSizeLimit = 10;
            //    }
            //}

            //bool enabledPaggination = false;
            //if (this.Settings.ContainsKey("LEAVES_ARTICLE_LISTING_ENABLED_PAGGING") && this.Settings["LEAVES_ARTICLE_LISTING_ENABLED_PAGGING"].Value != null && !string.IsNullOrEmpty(this.Settings["LEAVES_ARTICLE_LISTING_ENABLED_PAGGING"].Value.ToString()))
            //{
            //    enabledPaggination = Convert.ToBoolean(this.Settings["LEAVES_ARTICLE_LISTING_ENABLED_PAGGING"].Value.ToString());
            //}

            string noImageUrl = "/desktopModules/CoreModules/LeavesArticleMosaic/No_Image_Available.jpg";
            if (this.Settings.ContainsKey("LEAVES_ARTICLE_LISTING_NO_IMAGE_URL") && this.Settings["LEAVES_ARTICLE_LISTING_NO_IMAGE_URL"].Value != null && !string.IsNullOrEmpty(this.Settings["LEAVES_ARTICLE_LISTING_NO_IMAGE_URL"].Value.ToString()))
            {
                noImageUrl = this.Settings["LEAVES_ARTICLE_LISTING_NO_IMAGE_URL"].Value.ToString();
            }

            #endregion

            if (!string.IsNullOrEmpty(Leaves_entries_url) && !string.IsNullOrEmpty(Leaves_access_key) && !string.IsNullOrEmpty(tags))
            {
                try
                {
                    StringBuilder sbTabDtls = new StringBuilder();
                    string        leavesAPI = string.Format("{5}access_token={0}&perPage={1}&order={2}&page={6}&sort={3}&tags={4}", Leaves_access_key, Limit, Order, Sort, tags, Leaves_entries_url, (CurrentPageIndex + 1));

                    LeavesResults results = GetResults(leavesAPI);
                    APIResults    = new APIResult();
                    APIPagination = new Pagination();

                    for (int i = 0; i <= results._embedded.items.Count - 1; i++)
                    {
                        ErrorHandler.Publish(LogLevel.Info, "Article:i=" + i + ", url:" + leavesAPI);

                        var    item = results._embedded.items[i];
                        string desc = StripHTML(item.content);
                        if (desc.Length > charLimit)
                        {
                            desc = desc.Substring(0, charLimit) + " ...";
                        }
                        if (string.IsNullOrEmpty(item.preview_picture))
                        {
                            item.preview_picture = noImageUrl;
                        }

                        string title = item.title;
                        if (!string.IsNullOrEmpty(item.title) && title.Length > titlecharLimit)
                        {
                            title = title.Substring(0, titlecharLimit) + " ...";
                        }
                        else
                        {
                            title = string.Empty;
                        }

                        this.APIResults.Items.Add(new APIResultItem()
                        {
                            Content = desc, ImageUrl = item.preview_picture, Title = title, PageUrl = item.url, ColumnCSS = columnCSSClass
                        });
                    }

                    string pathQuery = Request.Url.PathAndQuery.Split('?').GetValue(0).ToString();
                    this.APIPagination.TotalPages   = results.pages;
                    this.APIPagination.TotalItems   = results.total;
                    this.APIPagination.FirstPageUrl = pathQuery;
                    this.APIPagination.LastPageUrl  = pathQuery + "?pi=" + this.APIPagination.TotalPages;

                    for (int i = 1; i <= this.APIPagination.TotalPages; i++)
                    {
                        if (i - 1 == CurrentPageIndex)
                        {
                            this.APIPagination.Items.Add(new PaginationItem {
                                PageIndex = i, Selected = true, CssClass = "currentPage", Url = pathQuery + "?pi=" + i
                            });
                        }
                        else
                        {
                            this.APIPagination.Items.Add(new PaginationItem {
                                PageIndex = i, Selected = false, CssClass = "pageIndex", Url = pathQuery + "?pi=" + i
                            });
                        }
                    }

                    apiResults.DataSource = this.APIResults.Items;
                    apiResults.DataBind();
                    apiResultsPages.DataSource = this.APIPagination.Items;
                    apiResultsPages.DataBind();
                }
                catch (Exception ex)
                {
                    ErrorHandler.Publish(LogLevel.Error, ex);
                }
            }
        }