Ejemplo n.º 1
0
        public bool UpdatePoint(Point point)
        {
            try
            {
                #region Validations
                List <Point> points = new PointRepository().GetAllPoints().ToList();
                if (!points.Exists(p => p.Id == point.Id))
                {
                    return(false);
                }
                #endregion Validations

                #region Cache Storage
                if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "CACHE")
                {
                    var ctx = HttpContext.Current;
                    if (ctx != null)
                    {
                        try
                        {
                            var currentData = ((Point[])ctx.Cache[CacheKey]).ToList();
                            if (currentData.Exists(p => p.Id == point.Id))
                            {
                                currentData[currentData.FindIndex(x => x.Id == point.Id)].Name = point.Name;
                            }
                            ctx.Cache[CacheKey] = currentData.ToArray();

                            return(true);
                        }
                        catch (Exception ex)
                        {
                            new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                            return(false);
                        }
                    }
                }
                #endregion
                #region DB Storage MySQl
                else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MYSQL")
                {
                    try
                    {
                        var dbCon = DBConnection.Instance();
                        if (dbCon.IsConnect())
                        {
                            //suppose col0 and col1 are defined as VARCHAR in the DB
                            string query  = "UPDATE `POINTS` SET `Name`='" + point.Name + "' WHERE Id=" + point.Id;
                            var    cmd    = new MySqlCommand(query, dbCon.Connection);
                            var    reader = cmd.ExecuteNonQuery();
                            dbCon.Close();
                            return(true);
                        }
                    }
                    catch (Exception ex)
                    {
                        new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                        return(false);
                    }
                }
                #endregion DB Storage MySQl
                #region DB Storage Mongo
                else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MONGO")
                {
                    var dbCon = DBConnectionMongo.Instance();
                    MongoDB.Driver.MongoClient client = dbCon.GetClient();
                    client.StartSession();
                    var collection   = client.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"]).GetCollection <Point>("POINTS");
                    var updoneresult = collection.UpdateOneAsync(Builders <Point> .Filter.Eq("Id", point.Id), Builders <Point> .Update.Set("Name", point.Name));
                    return(true);
                }
                #endregion DB Storage Mongo
            }
            catch (Exception ex)
            {
                new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
            }
            return(false);
        }
Ejemplo n.º 2
0
        public bool DeletePoint(Point point)
        {
            try
            {
                #region Validations
                List <Point> points = new PointRepository().GetAllPoints().ToList();
                if (!points.Exists(p => p.Id == point.Id))
                {
                    return(false);
                }
                #endregion Validations

                #region Cache Storage
                if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "CACHE")
                {
                    var ctx = HttpContext.Current;
                    if (ctx != null)
                    {
                        try
                        {
                            var currentData = ((Point[])ctx.Cache[CacheKey]).ToList();
                            if (currentData.Exists(p => p.Id == point.Id))
                            {
                                currentData.RemoveAt(currentData.FindIndex(p => p.Id == point.Id));
                            }
                            ctx.Cache[CacheKey] = currentData.ToArray();

                            return(true);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                            return(false);
                        }
                    }
                }
                #endregion
                #region DB Storage MySQl
                else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MYSQL")
                {
                    try
                    {
                        var dbCon = DBConnection.Instance();
                        if (dbCon.IsConnect())
                        {
                            string query  = "DELETE FROM `ROUTES` WHERE IdSource=" + point.Id + " OR IdDestination = " + point.Id;
                            var    cmd    = new MySqlCommand(query, dbCon.Connection);
                            var    reader = cmd.ExecuteNonQuery();

                            query  = "DELETE FROM `POINTS` WHERE Id=" + point.Id;
                            cmd    = new MySqlCommand(query, dbCon.Connection);
                            reader = cmd.ExecuteNonQuery();
                            dbCon.Close();
                            if (reader == 1)
                            {
                                return(true);
                            }
                            else
                            {
                                return(false);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                        return(false);
                    }
                }
                #endregion DB Storage MySQl
                #region DB Storage Mongo
                else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MONGO")
                {
                    var dbCon = DBConnectionMongo.Instance();
                    MongoDB.Driver.MongoClient client = dbCon.GetClient();
                    client.StartSession();

                    var collectionR = client.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"]).GetCollection <Route>("ROUTES");
                    var Deleteall   = collectionR.DeleteMany(Builders <Route> .Filter.Or(
                                                                 Builders <Route> .Filter.Eq("IdSource", point.Id),
                                                                 Builders <Route> .Filter.Eq("IdDestination", point.Id)));

                    var collection = client.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"]).GetCollection <Point>("POINTS");
                    var Deleteone  = collection.DeleteOneAsync(Builders <Point> .Filter.Eq("Id", point.Id));
                    return(true);
                }
                #endregion DB Storage Mongo
            }
            catch (Exception ex)
            {
                new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
            }
            return(false);
        }
        public bool SaveRoute(Route route)
        {
            try
            {
                #region Validations
                List <Point> points = new PointRepository().GetAllPoints().ToList();
                if (!points.Exists(p => p.Id == route.IdSource))
                {
                    return(false);
                }
                else if (!points.Exists(p => p.Id == route.IdDestination))
                {
                    return(false);
                }
                List <Route> routes = new RouteRepository().GetAllRoutes().ToList();
                if (routes.Exists(r => r.IdSource == route.IdSource && r.IdDestination == route.IdDestination))
                {
                    return(false);
                }
                #endregion Validations

                #region Cache Storage
                if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "CACHE")
                {
                    var ctx = HttpContext.Current;
                    if (ctx != null)
                    {
                        try
                        {
                            var currentData = ((Route[])ctx.Cache[CacheKey]).ToList();
                            currentData.Add(route);
                            ctx.Cache[CacheKey] = currentData.ToArray();

                            return(true);
                        }
                        catch (Exception ex)
                        {
                            new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                            return(false);
                        }
                    }
                }
                #endregion
                #region DB Storage MySQl
                else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MYSQL")
                {
                    try
                    {
                        var dbCon = DBConnection.Instance();
                        if (dbCon.IsConnect())
                        {
                            //suppose col0 and col1 are defined as VARCHAR in the DB
                            string query  = "INSERT INTO `ROUTES`(`IdSource`, `IdDestination`, `RouteCost`, `RouteTime`) VALUES  (" + route.IdSource + "," + route.IdDestination + "," + route.RouteCost + "," + route.RouteTime + ")";
                            var    cmd    = new MySqlCommand(query, dbCon.Connection);
                            var    reader = cmd.ExecuteNonQuery();
                            dbCon.Close();
                            return(true);
                        }
                    }
                    catch (Exception ex)
                    {
                        new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                        return(false);
                    }
                }
                #endregion DB Storage MySQl
                #region  DB Storage Mongo
                else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MONGO")
                {
                    var dbCon = DBConnectionMongo.Instance();
                    MongoDB.Driver.MongoClient client = dbCon.GetClient();
                    client.StartSession();
                    var collection = client.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"]).GetCollection <Route>("ROUTES");
                    collection.InsertOne(route);
                    return(true);
                }
                #endregion  DB Storage Mongo
            }
            catch (Exception ex)
            {
                new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
            }
            return(false);
        }
Ejemplo n.º 4
0
        public bool SavePoint(Point point)
        {
            try
            {
                #region Validations
                List <Point> points = new PointRepository().GetAllPoints().ToList();
                if (points.Exists(p => p.Name == point.Name))
                {
                    return(false);
                }
                #endregion Validations

                #region Cache Storage
                if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "CACHE")
                {
                    var ctx = HttpContext.Current;
                    if (ctx != null)
                    {
                        try
                        {
                            var currentData = ((Point[])ctx.Cache[CacheKey]).ToList();

                            if (point.Id == -1)
                            {
                                var maxItemID = currentData.Max(x => x.Id);
                                point.Id = maxItemID + 1;
                            }
                            currentData.Add(point);
                            ctx.Cache[CacheKey] = currentData.ToArray();

                            return(true);
                        }
                        catch (Exception ex)
                        {
                            new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                            return(false);
                        }
                    }
                }
                #endregion
                #region DB Storage MySQl
                else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MYSQL")
                {
                    try
                    {
                        var dbCon = DBConnection.Instance();
                        if (dbCon.IsConnect())
                        {
                            //suppose col0 and col1 are defined as VARCHAR in the DB
                            string query  = "INSERT INTO `POINTS`(`Name`) VALUES ('" + point.Name + "')";
                            var    cmd    = new MySqlCommand(query, dbCon.Connection);
                            var    reader = cmd.ExecuteNonQuery();
                            dbCon.Close();
                            return(true);
                        }
                    }
                    catch (Exception ex)
                    {
                        new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                        return(false);
                    }
                }
                #endregion DB Storage MySQl
                #region  DB Storage Mongo
                else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MONGO")
                {
                    var dbCon = DBConnectionMongo.Instance();
                    MongoDB.Driver.MongoClient client = dbCon.GetClient();
                    client.StartSession();
                    point.Id = Convert.ToInt32(Sequence.GetNextSequenceValue("pointid", client.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"])));
                    var collection = client.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"]).GetCollection <Point>("POINTS");
                    collection.InsertOne(point);
                    return(true);
                }
                #endregion  DB Storage Mongo
            }
            catch (Exception ex)
            {
                new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
            }
            return(false);
        }
        public Delivery GetDelivery(Delivery delivery)
        {
            try
            {
                List <Route> currentDataRoute = new RouteRepository().GetAllRoutes().ToList();
                List <Point> currentDataPoint = new PointRepository().GetAllPoints().ToList();

                #region Validations
                if (!currentDataPoint.Exists(p => p.Id == delivery.IdSource))
                {
                    delivery.BestRoute = "No source point found in points repository.";
                    return(delivery);
                }
                else if (!currentDataPoint.Exists(p => p.Id == delivery.IdDestination))
                {
                    delivery.BestRoute = "No destimatiom point found in points repository.";
                    return(delivery);
                }
                else if (!currentDataPoint.Exists(p => p.Id == delivery.IdDestination))
                {
                    delivery.BestRoute = "No destimatiom point found in points repository.";
                    return(delivery);
                }

                if (string.IsNullOrEmpty(delivery.Type))
                {
                    delivery.Type = ConfigurationManager.AppSettings["dbDefaultSearchType"].ToUpper();
                }
                else if (!ConfigurationManager.AppSettings["dbSearchTypes"].Split(';').ToList().Exists(a => a.ToUpper() == delivery.Type.ToUpper()))
                {
                    delivery.Type = ConfigurationManager.AppSettings["dbDefaultSearchType"].ToUpper();
                }
                #endregion Validations

                #region Build Graph
                currentDataRoute.RemoveAll(r => r.IdSource == delivery.IdSource && r.IdDestination == delivery.IdDestination);

                int[,] graph = new int[currentDataPoint.Count(), currentDataPoint.Count()];
                int x = 0;
                foreach (Point pointX in currentDataPoint)
                {
                    int y = 0;
                    foreach (Point pointY in currentDataPoint)
                    {
                        if (currentDataRoute.Exists(r => r.IdSource == pointX.Id && r.IdDestination == pointY.Id))
                        {
                            switch (delivery.Type.ToUpper())
                            {
                            case "COST":
                                graph[x, y] = currentDataRoute.FirstOrDefault(r => r.IdSource == pointX.Id && r.IdDestination == pointY.Id).RouteCost;
                                break;

                            case "TIME":
                                graph[x, y] = currentDataRoute.FirstOrDefault(r => r.IdSource == pointX.Id && r.IdDestination == pointY.Id).RouteTime;
                                break;
                            }
                        }
                        else
                        {
                            graph[x, y] = 0;
                        }
                        y++;
                    }
                    x++;
                }
                #endregion Build Graph

                #region Get Short Path

                var path = Dijkstra.DijkstraAlgorithm(graph,
                                                      currentDataPoint.FindIndex(r => r.Id == delivery.IdSource),
                                                      currentDataPoint.FindIndex(r => r.Id == delivery.IdDestination)
                                                      );

                string formattedPath = "";
                if (path == null)
                {
                    formattedPath = "No path";
                }
                else
                {
                    int pathLength = 0;
                    for (int i = 0; i < path.Count - 1; i++)
                    {
                        pathLength += graph[path[i], path[i + 1]];
                    }

                    for (int i = 0; i < path.Count; i++)
                    {
                        formattedPath += "(" + currentDataPoint[path[i]].Id + " - " + currentDataPoint[path[i]].Name + ")";
                        if (i < path.Count - 1)
                        {
                            formattedPath += " -> ";
                        }
                    }
                    formattedPath += " - Total cost in " + delivery.Type + ": " + pathLength;
                }
                #endregion

                delivery.BestRoute = formattedPath;

                return(delivery);
            }
            catch (Exception ex)
            {
                new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                return(null);
            }
            return(delivery);
        }