private void MapCities(String ProviderName)
    {
        ProviderName = ProviderName.ToUpper();
        String strCities = String.Empty;
        DataTable dtCities = new DataTable();
        switch (ProviderName)
        {
            case "ABHIBUS":
            case "SVR":
            case "KALLADA":
            case "KAVERI":
            case "RAJESH":
            case "SAIANJANA":
                AbhibusAPI clsAbhibusAPI = new AbhibusAPI();
                if (ProviderName == "ABHIBUS")
                    strCities = clsAbhibusAPI.getSources("http://www.abhibus.com/api/manabus_kld/server.php", "BUSMANAHY05HBAI");
                else if (ProviderName == "SVR")
                    strCities = clsAbhibusAPI.getSources("http://www.svrtravels.com/api/lovejourney/server.php", "LOVE@SVR");
                else if (ProviderName == "KALLADA")
                    strCities = clsAbhibusAPI.getSources("http://kalladatravels.com/api/lovejourney/server.php", "LVOEJOURNEYAPI");
                else if (ProviderName == "KAVERI")
                    strCities = clsAbhibusAPI.getSources("http://kaveribus.com/api/lovejourney/server.php", "LVOEJOURNEYAPI");
                else if (ProviderName == "RAJESH")
                    strCities = clsAbhibusAPI.getSources("http://rajeshtravels.in/api/lovejourney/server.php", "LOVRAJESHAPI");
                else if (ProviderName == "SAIANJANA")
                    strCities = clsAbhibusAPI.getSources("http://saianjanatravels.in/api/lovejourney/server.php", "LOVEANJANAAPI");
                break;
            case "BITLA":
                BitlaAPI clsBitlaAPI = new BitlaAPI();
                strCities = clsBitlaAPI.getSources("http://api.ticketsimply.com/api", "TSAPI*854LOVEJOURNEY");
                break;
            case "MORNINGSTAR":
                BitlaAPI clsBitlaAPI2 = new BitlaAPI();
                strCities = clsBitlaAPI2.getSources("http://api.ticketsimply.com/api", "TSAPI*1648LOVEJOURNEYDIR");
                break;
            case "TICKETGOOSE":
                TicketGooseAPI clsTicketGooseAPI = new TicketGooseAPI();
                strCities = clsTicketGooseAPI.getSources("http://ticketgoose.com/bookbustickets/services/TGSWS", "ssdtech", "prasadsir");
                break;
            case "EASYBUS":
                EasybusAPI clsEasyBusAPI = new EasybusAPI();
                strCities = clsEasyBusAPI.getSources("http://demoapi.easybus.in/server.aspx", "easybus@123");
                break;
            default:
                break;
        }

        List<KeyValuePair<int, string>> UnmappedItemList = new List<KeyValuePair<int, string>>();
        dtCities = JsonConvert.DeserializeObject<DataTable>(strCities);
        if (dtCities != null && dtCities.Rows.Count > 0)
        {
            Connection = new SqlConnection(ConnectionString);
            try
            {
                command = new SqlCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "SP_Insert_SourceCity";
                command.Connection = Connection;
                Connection.Open();
                foreach (DataRow row in dtCities.Rows)
                {
                    //command for sources table
                    command.Parameters.Add("@ID", SqlDbType.VarChar).Value = row["id"];
                    command.Parameters.Add("@Name", SqlDbType.VarChar).Value = row["name"];
                    command.Parameters.Add("@ProviderName", SqlDbType.VarChar).Value = ProviderName;
                    Object result = command.ExecuteScalar();

                    if (!result.ToString().ToUpper().Equals("SUCCESS"))
                    {
                        UnmappedItemList.Add(new KeyValuePair<int, String>(int.Parse(row["id"].ToString()), row["name"].ToString()));
                    }
                    command.Parameters.Clear();

                }

                Connection.Close();
            }
            catch (Exception ex)
            {
                //to do
                //Log Exception
            }
            finally
            {
                if (Connection.State == ConnectionState.Open)
                    Connection.Close();
            }
        }

        lblCount.InnerText = (UnmappedItemList != null) ? UnmappedItemList.Count.ToString() : "0";
        gvUnmappedItems.DataSource = UnmappedItemList;
        gvUnmappedItems.DataBind();
    }
        /// <summary>
        /// Method to get available trips for given provider
        /// </summary>
        /// <param name="ProviderName"></param>
        /// <param name="objClientAPIDetails"></param>
        /// <returns>Returns JSON string with list of available trips</returns>
        private SeatsInfo GetTripDetailsByProviderName(ClientAPIDetails objClientAPIDetails, BusesSearchFilter objBusesSearchFilter, String tripId, String markUpFare, String SeatLayoutId)
        {
            SeatsInfo objSeatsInfo = new SeatsInfo();
            try
            {
                switch (objClientAPIDetails.ProviderName)
                {

                    case "ABHIBUS":
                    case "SVR":
                    case "KALLADA":
                    case "KAVERI":
                    case "RAJESH":
                    case "SAIANJANA":
                        AbhibusAPI clsAbhibusAPI = new AbhibusAPI();
                        objSeatsInfo = clsAbhibusAPI.getBusSeatLayout(objBusesSearchFilter.SourceID
                                            , objBusesSearchFilter.DestinationID
                                            , objBusesSearchFilter.JourneyDate
                                            , tripId
                                            , "0"
                                            , objClientAPIDetails.APIURL
                                            , objClientAPIDetails.ConsumerKey);
                        break;
                    case "BITLA":
                    case "MORNINGSTAR":
                        BitlaAPI clsBitlaAPI = new BitlaAPI();
                        objSeatsInfo = clsBitlaAPI.getServiceDetails(tripId
                                            , objClientAPIDetails.APIURL
                                            , objClientAPIDetails.ConsumerKey);

                        break;
                    case "TICKETGOOSE":
                        TicketGooseAPI clsTicketGooseAPI = new TicketGooseAPI();
                        objSeatsInfo = clsTicketGooseAPI.getTripDetailsV2(objBusesSearchFilter.SourceID
                                            , objBusesSearchFilter.DestinationID
                                            , objBusesSearchFilter.JourneyDate
                                            , tripId
                                            , objClientAPIDetails.APIURL
                                            , objClientAPIDetails.ConsumerKey
                                            , objClientAPIDetails.ConsumerSecret);
                        break;
                    //easy bus 18-05-2013
                    case "EASYBUS":
                        EasybusAPI clsEasyBusAPI = new EasybusAPI();
                        objSeatsInfo = clsEasyBusAPI.getLayoutDetails(objBusesSearchFilter.SourceID
                                            , objBusesSearchFilter.DestinationID
                                            , objBusesSearchFilter.JourneyDate
                                            , tripId
                                            , SeatLayoutId
                                            , markUpFare
                                            , "0"
                                            , objClientAPIDetails.APIURL
                                            , objClientAPIDetails.ConsumerKey);
                        break;
                    default:
                        break;
                }
                objSeatsInfo.providerName = objClientAPIDetails.ProviderName;
            }
            catch (Exception ex)
            {
                //to do
                //Log Exception
            }
            return objSeatsInfo;
        }
 private AvailableTrips MORNINGSTAR(string MAPIURL, string MConsumerKey, string MConsumerSecret, int MRPID, string MpName, int Msourceid, int Mdestinationid, string Mdate)
 {
     AvailableTrips objAvailableTrips = new AvailableTrips();
     BitlaAPI clsBitlaAPI = new BitlaAPI();
     objAvailableTrips = clsBitlaAPI.MRgetAvailableRoutes(Msourceid
                         , Mdestinationid
                         , Mdate
                         , MAPIURL
                         , MConsumerKey, MRPID, MpName);
     return objAvailableTrips;
 }
        public string BookTicket(BlockSeats blockSeats, String ConsumerKey, String ConsumerSecret)
        {
            if (ValidateRequest(ConsumerKey, ConsumerSecret))
            {
                ClientAPIList objClientAPIList = new ClientAPIList();

                //if (HttpContext.Current.Cache["ClientAPI-" + ConsumerKey] == null)
                GetAPIProvidersList(ConsumerKey, ConsumerSecret);
                objClientAPIList = (ClientAPIList)HttpContext.Current.Cache["ClientAPI-" + ConsumerKey];
                ClientAPIDetails objClientAPIDetails = objClientAPIList.SingleOrDefault(element => element.ProviderName == blockSeats.ProviderName);
                BookSeatsResponse objBookResponse = new BookSeatsResponse();
                if (objClientAPIDetails != null)
                {
                    try
                    {
                        switch (blockSeats.ProviderName)
                        {
                            case "BITLA":
                            case "MORNINGSTAR":
                                BitlaAPI clsBitlaAPI = new BitlaAPI();
                                objBookResponse = clsBitlaAPI.bookTicket(blockSeats.SourceId, blockSeats.DestinationId, blockSeats.TripId,
                                    objClientAPIDetails.APIURL, objClientAPIDetails.ConsumerKey, blockSeats.NoOfSeats, blockSeats.BoardingId.ToString(), blockSeats.SeatNo, blockSeats.Title, blockSeats.Name,
                                    blockSeats.Age, blockSeats.Sex, blockSeats.Address, blockSeats.BookingRefNo, blockSeats.IdCardType, blockSeats.IdCardNo, blockSeats.IdCardIssuedBy,
                                    blockSeats.MobileNo, blockSeats.EmergencyMobileNo, blockSeats.EmailId);
                                break;
                            case "TICKETGOOSE":
                                TicketGooseAPI clsTicketGooseAPI = new TicketGooseAPI();
                                objBookResponse = clsTicketGooseAPI.BookTicket(blockSeats.BookingId, objClientAPIDetails.APIURL, objClientAPIDetails.ConsumerKey, objClientAPIDetails.ConsumerSecret);
                                break;
                            case "ABHIBUS":
                            case "SVR":
                            case "KALLADA":
                            case "KAVERI":
                            case "RAJESH":
                            case "SAIANJANA":
                                AbhibusAPI clsAbhiBusAPI = new AbhibusAPI();
                                objBookResponse = clsAbhiBusAPI.bookSeats(blockSeats.SourceId, blockSeats.DestinationId, blockSeats.JourneyDate
                                    , blockSeats.TripId, blockSeats.SeatNo, blockSeats.Title, blockSeats.Name, blockSeats.BoardingId, blockSeats.Address
                                    , blockSeats.Name, blockSeats.MobileNo, blockSeats.EmailId, blockSeats.BookingRefNo, objClientAPIDetails.APIURL
                                    , objClientAPIDetails.ConsumerKey);

                                objBookResponse.extraseatinfo = blockSeats.SourceId + "," + blockSeats.DestinationId + "," + blockSeats.JourneyDate
                              + "," + blockSeats.TripId + "," + blockSeats.SeatNo + "," + blockSeats.Title + "," + blockSeats.Name + "," + blockSeats.BoardingId + "," + blockSeats.Address
                              + "," + blockSeats.Name + "," + blockSeats.MobileNo + "," + blockSeats.EmailId + "," + blockSeats.BookingRefNo;
                                break;

                            case "EASYBUS":
                                EasybusAPI clsEasyBusAPI = new EasybusAPI();
                                objBookResponse = clsEasyBusAPI.bookSeats(blockSeats.SourceId, blockSeats.DestinationId, blockSeats.JourneyDate
                                    , blockSeats.TripId, blockSeats.SeatNo, blockSeats.Sex, blockSeats.Name, blockSeats.BoardingId, blockSeats.Address
                                    , blockSeats.Name, blockSeats.MobileNo, blockSeats.EmailId, blockSeats.BookingRefNo, objClientAPIDetails.APIURL
                                    , objClientAPIDetails.ConsumerKey);

                                objBookResponse.extraseatinfo = blockSeats.SourceId + "," + blockSeats.DestinationId + "," + blockSeats.JourneyDate
                              + "," + blockSeats.TripId + "," + blockSeats.SeatNo + "," + blockSeats.Title + "," + blockSeats.Name + "," + blockSeats.BoardingId + "," + blockSeats.Address
                              + "," + blockSeats.Name + "," + blockSeats.MobileNo + "," + blockSeats.EmailId + "," + blockSeats.BookingRefNo;

                                break;
                            default:
                                break;
                        }
                    }
                    catch (Exception ex)
                    {
                        objBookResponse.Message = ex.ToString();
                    }
                }
                else
                {
                    objBookResponse.Message = "no api found";
                }
                return JsonConvert.SerializeObject(objBookResponse);
            }
            else
            {
                return null;
                //throw new http exception
                //throw new System.Web.Http.HttpResponseException(HttpStatusCode.Forbidden);

            }
        }
        public String CancelTicket(String bookingId, String seatNos, String ProviderName, String ConsumerKey, String ConsumerSecret)
        {
            if (ValidateRequest(ConsumerKey, ConsumerSecret))
            {
                ClientAPIList objClientAPIList = new ClientAPIList();

                if (HttpContext.Current.Cache["ClientAPI-" + ConsumerKey] == null)
                    GetAPIProvidersList(ConsumerKey, ConsumerSecret);

                objClientAPIList = (ClientAPIList)HttpContext.Current.Cache["ClientAPI-" + ConsumerKey];

                ClientAPIDetails objClientAPIDetails = objClientAPIList.SingleOrDefault(element => element.ProviderName == ProviderName);

                String strCancelResponse = String.Empty;
                try
                {
                    switch (ProviderName)
                    {
                        case "BITLA":
                        case "MORNINGSTAR":
                            BitlaAPI clsBitlaAPI = new BitlaAPI();
                            strCancelResponse = clsBitlaAPI.cancelTicket(bookingId, seatNos, objClientAPIDetails.APIURL, objClientAPIDetails.ConsumerKey);
                            break;
                        case "TICKETGOOSE":
                            TicketGooseAPI clsTicketGooseAPI = new TicketGooseAPI();
                            strCancelResponse = clsTicketGooseAPI.confirmTicketCancellation(bookingId, seatNos, objClientAPIDetails.APIURL, objClientAPIDetails.ConsumerKey, objClientAPIDetails.ConsumerSecret);
                            break;
                        case "ABHIBUS":
                        case "SVR":
                        case "KALLADA":
                        case "KAVERI":
                        case "RAJESH":
                        case "SAIANJANA":
                            AbhibusAPI clsAbhiBusAPI = new AbhibusAPI();
                            strCancelResponse = "";//clsAbhiBusAPI.cancelTicket(objClientAPIDetails.APIURL, objClientAPIDetails.ConsumerKey, bookingId);
                            break;
                        case "EASYBUS":
                            EasybusAPI clsEasyBusAPI = new EasybusAPI();
                            strCancelResponse = "";//  clsEasyBusAPI.cancelTicket(bookingId, seatNos, objClientAPIDetails.APIURL, objClientAPIDetails.ConsumerKey, objClientAPIDetails.ConsumerSecret);
                            break;
                        default:
                            break;
                    }
                }
                catch (Exception)
                {
                    //
                }
                return strCancelResponse;
            }
            else
            {
                return null;
                //throw new http exception
                // throw new System.Web.Http.HttpResponseException(HttpStatusCode.Forbidden);
            }
        }
        /// <summary>
        /// Method to get available trips for given provider
        /// </summary>
        /// <param name="ProviderName"></param>
        /// <param name="objClientAPIDetails"></param>
        /// <returns>Returns JSON string with list of available trips</returns>
        private AvailableTrips GetAvailableTripsByProviderName(ClientAPIDetails objClientAPIDetails, BusesSearchFilter objBusesSearchFilter)
        {
            AvailableTrips objAvailableTrips = new AvailableTrips();
            List<object> objlist = new List<object>();
            switch (objClientAPIDetails.ProviderName)
            {

                case "ABHIBUS":
                case "SVR":
                case "KALLADA":
                case "KAVERI":
                case "RAJESH":
                case "SAIANJANA":
                    AbhibusAPI clsAbhibusAPI = new AbhibusAPI();
                    objAvailableTrips = clsAbhibusAPI.getBusAvailability(objBusesSearchFilter.SourceID
                                        , objBusesSearchFilter.DestinationID
                                        , objBusesSearchFilter.JourneyDate
                                        , 6
                                        , "0"
                                        , objClientAPIDetails.APIURL
                                        , objClientAPIDetails.ConsumerKey
                                        , objClientAPIDetails.ProviderName);
                    break;
                case "BITLA":
                case "MORNINGSTAR":
                    BitlaAPI clsBitlaAPI = new BitlaAPI();
                    objAvailableTrips = clsBitlaAPI.getAvailableRoutes(objBusesSearchFilter.SourceID
                                        , objBusesSearchFilter.DestinationID
                                        , objBusesSearchFilter.JourneyDate
                                        , objClientAPIDetails.APIURL
                                        , objClientAPIDetails.ConsumerKey, objClientAPIDetails.ProviderID);
                    break;
                case "TICKETGOOSE":
                    TicketGooseAPI clsTicketGooseAPI = new TicketGooseAPI();
                    objAvailableTrips = clsTicketGooseAPI.getTripListV2(objBusesSearchFilter.SourceID
                                        , objBusesSearchFilter.DestinationID
                                        , objBusesSearchFilter.JourneyDate
                                        , objClientAPIDetails.APIURL
                                        , objClientAPIDetails.ConsumerKey
                                        , objClientAPIDetails.ConsumerSecret, objClientAPIDetails.ProviderID);
                    break;
                //easybus
                case "EASYBUS":
                    EasybusAPI clsEasyBusAPI = new EasybusAPI();

                    break;
                default:
                    break;

            }

            return objAvailableTrips;
        }
 private AvailableTrips BITLA(string BAPIURL, string BConsumerKey, string BConsumerSecret, int BPID, string BPName, int Bsourceid, int Bdestinationid, string Bdate)
 {
     AvailableTrips objAvailableTrips = new AvailableTrips();
     BitlaAPI clsBitlaAPI = new BitlaAPI();
     objAvailableTrips = clsBitlaAPI.getAvailableRoutes(Convert.ToInt32(Bsourceid), Convert.ToInt32(Bdestinationid), Bdate, BAPIURL, BConsumerKey, BPID);
     return objAvailableTrips;
 }
 public String StoreBitlaTrips(String URL, String fromDate, String toDate, String ConsumerKey)
 {
     BitlaAPI clsBitlaAPI = new BitlaAPI();
     return clsBitlaAPI.storeBitlaTrips(URL, fromDate, toDate, ConsumerKey);
 }
        public DataSet GetcallBack(string Resrvation_Id, string url, string ConsumerKey, string ConsumerSecret, string doj)
        {
            DataSet ds = new DataSet();
            ClientAPIList objClientAPIList = new ClientAPIList();
            GetAPIProvidersList(ConsumerKey, ConsumerSecret);

            objClientAPIList = (ClientAPIList)HttpContext.Current.Cache["ClientAPI-" + ConsumerKey];

            //Check if atleast one provider is accessible
            if (objClientAPIList != null && objClientAPIList.Count > 0)
            {
                objClientAPIList.Where(e => e.ProviderName == Convert.ToString("BITLA"));
                BitlaAPI bitla = new BitlaAPI();
                SeatsInfo seat = new SeatsInfo();
                seat = bitla.getServiceDetails(Resrvation_Id, objClientAPIList[0].APIURL, objClientAPIList[0].ConsumerKey);
                ds = convertJsonStringToDataSet(JsonConvert.SerializeObject(seat));

            }
            return ds;
        }
    protected void btn_Click(object sender, EventArgs e)
    {
        try
        {
            DataSet DsSub = new DataSet();
            string JourneyDate = txtDate.Text.ToString();
            BitlaAPI objbitla = new BitlaAPI();
            IBitlaAPILayer objBitlaAPILayer = null;
            objBitlaAPILayer = new BitlaAPILayer();
            objBitlaAPILayer.URL = BitlaConstants.URL;
            objBitlaAPILayer.ApiKey = BitlaConstants.API_KEY;
            objBitlaAPILayer.Date = JourneyDate;
            DataSet dsBitlaAllAvailableRoutes = null;

            dsBitlaAllAvailableRoutes = objbitla.GetAllAvailableRoutes(objBitlaAPILayer.URL,objBitlaAPILayer.ApiKey, objBitlaAPILayer.Date);
            dsBitlaAllAvailableRoutes.WriteXml(Server.MapPath("~/Routes/" + JourneyDate + ".xml"));
            lblMsg.Text = " Submitted. ";
            XmlDataDocument XmlDocRef = new XmlDataDocument();
            // StreamWriter XmlDataRef = new StreamWriter(Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data\\XMLfiles\\" + "RefCallback.xml"), false);
            XmlDocRef.Load(Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data\\XMLfiles\\" + "RefCallback.xml"));
            foreach (DataRow row in dsBitlaAllAvailableRoutes.Tables[1].Rows)
            {
                string DirectoryPath = Server.MapPath("~/App_Data/XMLfiles");
                DirectoryInfo dir = new DirectoryInfo(DirectoryPath);
                if (!dir.Exists)
                {
                    dir.Create();
                }
                string filepath = "~/App_Data/XMLfiles/" + "RefCallback.xml";
                string DirectoryPath1 = Server.MapPath(filepath);
                DirectoryInfo dir1 = new DirectoryInfo(DirectoryPath1);

                if (dir1.Exists == true)
                {
                    DataSet ds1 = new DataSet();
                    DataTable dt = new DataTable("Refcal");
                    dt.Columns.Add("Date");
                    dt.Columns.Add("id");
                    dt.Columns.Add("travel_id");
                    dt.Columns.Add("reservation_id");
                    DataRow dr1;
                    dr1 = dt.NewRow();
                    dr1["Date"] = JourneyDate;
                    dr1["id"] = row["reservation_id"];
                    dr1["travel_id"] = row["travel_id"];
                    dr1["reservation_id"] = row["reservation_id"];
                    dt.Rows.Add(dr1);
                    ds1.Tables.Add(dt);
                    ds1.EnforceConstraints = false;
                    XmlDataDocument XmlDoc = new XmlDataDocument(ds1);
                    // Write down the XML declaration
                    // XmlDeclaration xmlDeclaration = XmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
                    // Create the root element
                    //XmlElement rootNode = XmlDoc.CreateElement("CallBack");
                    //XmlDoc.InsertBefore(xmlDeclaration, XmlDoc.DocumentElement);
                   // XmlDoc.AppendChild(rootNode);
                    XmlDoc.Save(Server.MapPath(filepath));

                }
                else
                {
                    //create node and add value
                    XmlNode node = XmlDocRef.CreateNode(XmlNodeType.Element, "Refcal", null);
                    node.InnerXml = "<Date>" + JourneyDate + "</Date><id>" + row["id"] + "</id><travel_id>" + row["travel_id"] + "</travel_id><reservation_id>" + row["reservation_id"] + "</reservation_id>";
                    //add to elements collection
                    XmlDocRef.DocumentElement.AppendChild(node);
                    //DsSub.ReadXml(Server.MapPath("~/App_Data/XMLfiles/" + "RefCallback.xml"));
                    //DataRow dr;
                    //dr = DsSub.Tables[0].NewRow();
                    //dr["Date"] = JourneyDate;
                    //dr["id"] = row["id"];
                    //dr["travel_id"] = row["travel_id"];
                    //dr["reservation_id"] = row["reservation_id"];
                    //DsSub.Tables[0].Rows.Add(dr);
                }

            }
            XmlDocRef.Save(Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data\\XMLfiles\\" + "RefCallback.xml"));
            //DataSet ds2 = new DataSet();
            //ds2.EnforceConstraints = false;
            //XmlDataDocument XmlDoc1 = new XmlDataDocument(ds2);
            //StreamWriter XmlData1 = new StreamWriter(Server.MapPath("~/App_Data/XMLfiles/" + "RefCallback.xml"), false);
            //DsSub.WriteXml(XmlData1);
            //XmlData1.Close();

        }
        catch (Exception)
        {
           // throw;
        }
    }