Creating a List Mashlets Web Part using Presto Connector for C#: Example 1

 

 

  1. We can either create a new project or add this new Web Part to the Jackbe.Samples Assembly, created in the previous article for the MashletView Web Part. We'll assume the same project is used.
  2. Download and install the PC4C#, instructions can be found here, Installing the Presto Connect for C# Library.
  3. The Web Part will be dependent on 2 Assemblies bundled with the PC4C#:
    • Jackbe.PrestoConnect.Net.dll
    • NetServ.Net.Json.dll
  4. Add References to these 2 assemblies.
  5. Create a new Web Part named MashletsList and reference the PC4C# assemblies:
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using JackBe.Presto.Connect.Net;
using NetServ.Net.Json;

namespace Jackbe.Sample
{
    [Guid("5d77828a-4a62-428e-871e-93e740365d36")]
    public class MashletsListView : System.Web.UI.WebControls.WebParts.WebPart
    {
        public MashletsListView()
        {
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
        }
    }
}
  1. The following additional namespace statements will be required:
using System.ComponentModel;
using System.Data;
using System.Collections.Generic;
  1. As with the previous Mashlet, we will define Web Part properties so we can configure which Presto Mashup Server the Web Part will connect to. We also define properties to hold user credentials which may be required to access Presto Services.
        private string _Hostname;
        private string _Port;
        private string _Username;
        private string _Password;

        [WebBrowsable(true)]
        [WebDisplayName("Hostname")]
        [WebDescription("Presto Hostname")]
        [Personalizable(PersonalizationScope.Shared)]
        public String Hostname
        {
            get {     return _Hostname;   }
            set {    _Hostname = value;   }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Port")]
        [WebDescription("Presto Port number")]
        [Personalizable(PersonalizationScope.Shared)]
        public string Port
        {
            get {   return _Port;   }
            set {  _Port = value;   }
        }

        [WebBrowsable(true)]
        [WebDisplayName("User Name")]
        [WebDescription("Presto User Name")]
        [Personalizable(PersonalizationScope.Shared)]
        public string Username
        {
            get { return _Username; }
            set { _Username = value; }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Password")]
        [WebDescription("Presto User Password")]
        [Personalizable(PersonalizationScope.Shared)]
        [PasswordPropertyText(true)]
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

  1. The following GetMashletsDataSet method will connect to the Presto Mashlet Server and retrieve a summary list of all available Mashlets. This Mashlet summary data is then used to populate a DataSet object which is returned by the method.
        private DataSet GetMashletsDataSet()
        {
            DataSet dataSet = new DataSet();
            DataTable table = new DataTable();

            table.Columns.Add("Name");
            table.Columns.Add("Title");
            table.Columns.Add("Description");
            table.Columns.Add("Type");
            table.Columns.Add("Created");
            table.Columns.Add("Owner");

            Connection connection = this.GetConnection();
            JsonObject response = this.GetMashlets(connection);

            JsonObject mashletMap = (JsonObject)response["map"];

            foreach (KeyValuePair<string, IJsonType> pair in mashletMap)
            {
                string name = pair.Key;
                JsonObject config = (JsonObject)mashletMap[name];

                string title = config["title"].ToString();
                string description = config["description"].ToString();
                string type = config["type"].ToString();
                string created = config["created"].ToString();
                string owner = config["createdBy"].ToString();

                table.Rows.Add(new Object[] { name, title, description, type, created, owner });
            }

            dataSet.Tables.Add(table);
            connection.Logout();
            return dataSet;
        }

        private Connection GetConnection()
        {
            Connection connection = new Connection();
            connection.HostName = Hostname;
            connection.PortNo = Int32.Parse(Port);
            connection.Login(Username, Password);
            return connection;
        }

        private JsonObject GetMashlets(Connection connection)
        {
            JumpResponse response = connection.Invoke("MashletHub", "getMashlets");
            JsonObject json = response.ResponseBody.Json;
            return json;
        }

  1. The final part of the implementation is to add the CreateChildControls method, to retreive the DataSet of Mashlet details and bind it to a GridView UI Control.
        private GridView MashletsView;
        private DataSet MashletsDataSet;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            if (String.IsNullOrEmpty(Hostname))
            {
                Label label = new Label();
                label.Text = "Web Part not configured";
                Controls.Add(label);
                return;
            }

            MashletsView = new GridView();
            MashletsView.AllowSorting = true;
            MashletsView.AllowPaging = true;
            MashletsView.PageSize = 10;
            MashletsView.EnableSortingAndPagingCallbacks = true;
            MashletsView.Sorting += new GridViewSortEventHandler(MashletsView_Sorting);
            MashletsView.PageIndexChanging += new GridViewPageEventHandler(MashletsView_PageIndexChanging);

            MashletsDataSet = GetMashletsDataSet();
            MashletsView.DataSource = MashletsDataSet;

            Controls.Add(MashletsView);
            MashletsView.DataBind();
        }
        protected void MashletsView_Sorting(object sender, GridViewSortEventArgs e)
        {
            MashletsDataSet.Tables[0].DefaultView.Sort = e.SortExpression;
            MashletsView.DataBind();
        }
        protected void MashletsView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            MashletsView.PageIndex = e.NewPageIndex;
        }

 

0
Your rating: None