private static Dictionary <Tuple <string, string>, int> FilterMatrixBySpecificCompanies(
            Dictionary <Tuple <string, string>, int> dictMatrix, List <Company> lstRelevantCompanies)
        {
            MongoCrud <Company> _mongoCrud           = new MongoCrud <Company>();
            List <Company>      listAllCompanies     = _mongoCrud.GetAllEntities().ToList <Company>();
            List <string>       lstFilteredCompanies = lstRelevantCompanies.Select(s => s.Id).ToList <string>();

            Dictionary <Tuple <string, string>, int> dictFilteredMatrix = new Dictionary <Tuple <string, string>, int>();

            foreach (Tuple <string, string> curr in dictMatrix.Keys)
            {
                if (lstFilteredCompanies.Contains(curr.Item1) && lstFilteredCompanies.Contains(curr.Item2))
                {
                    dictFilteredMatrix.Add(curr, dictMatrix[curr]);
                }
            }

            return(dictFilteredMatrix);
        }
        public static Dictionary<Tuple<string, string>, int> BuildDistancesMatrix(string strCity, 
            List<CompanySubType> lstCompanyTypes, List<Constraint> lstConstraints)
        {
            MongoCrud<Company> _mongoCrud = new MongoCrud<Company>();
            List<Company> lstAllCompanies = _mongoCrud.GetAllEntities().ToList<Company>();
            List<Company> lstRelevantCompanies = lstAllCompanies.Where(
                c => c.Location.Contains(strCity) && lstCompanyTypes.Contains(c.SubType)).ToList<Company>();

            var matrixDictionary = DistancesMatrixReader.Read(strCity);

            var filteredMatrix = DistancesMatrixHandler.FilterMatrixBySpecificCompanies(matrixDictionary, lstRelevantCompanies);

            if (lstConstraints.Count > 0)
            {
                filteredMatrix = DistancesMatrixHandler.AddConstraintsToDistancesMatrix(filteredMatrix, lstRelevantCompanies, lstConstraints);
            }

            return (filteredMatrix);
        }
        public static Dictionary <Tuple <string, string>, int> BuildDistancesMatrix(string strCity,
                                                                                    List <CompanySubType> lstCompanyTypes, List <Constraint> lstConstraints)
        {
            MongoCrud <Company> _mongoCrud           = new MongoCrud <Company>();
            List <Company>      lstAllCompanies      = _mongoCrud.GetAllEntities().ToList <Company>();
            List <Company>      lstRelevantCompanies = lstAllCompanies.Where(
                c => c.Location.Contains(strCity) && lstCompanyTypes.Contains(c.SubType)).ToList <Company>();

            var matrixDictionary = DistancesMatrixReader.Read(strCity);

            var filteredMatrix = DistancesMatrixHandler.FilterMatrixBySpecificCompanies(matrixDictionary, lstRelevantCompanies);

            if (lstConstraints.Count > 0)
            {
                filteredMatrix = DistancesMatrixHandler.AddConstraintsToDistancesMatrix(filteredMatrix, lstRelevantCompanies, lstConstraints);
            }

            return(filteredMatrix);
        }
        public void AlterAppointments()
        {
            List <DbAppointmentCompany> dacs = _mongoCrud.GetAllEntities().ToList();

            foreach (DbAppointmentCompany dac in dacs)
            {
                List <DbAppointment> daRemoval = new List <DbAppointment>();
                foreach (DbAppointment da in dac.freeAppointments)
                {
                    if (da.StartTime.Hour < 21 && da.StartTime.Hour >= 6)
                    {
                        daRemoval.Add(da);
                    }
                }

                dac.freeAppointments.RemoveRange(0, dac.freeAppointments.Count);
                dac.freeAppointments.AddRange(daRemoval);

                var t = dac;
                _mongoCrud.SaveOrUpdate(dac);
            }
        }
Exemple #5
0
 public IQueryable <User> GetUser()
 {
     return(_mongoCrud.GetAllEntities().AsQueryable());
 }
        private static Dictionary<Tuple<string, string>, int> FilterMatrixBySpecificCompanies(
            Dictionary<Tuple<string, string>, int> dictMatrix, List<Company> lstRelevantCompanies)
        {
            MongoCrud<Company> _mongoCrud = new MongoCrud<Company>();
            List<Company> listAllCompanies = _mongoCrud.GetAllEntities().ToList<Company>();
            List<string> lstFilteredCompanies = lstRelevantCompanies.Select(s => s.Id).ToList<string>();

            Dictionary<Tuple<string, string>, int> dictFilteredMatrix = new Dictionary<Tuple<string, string>, int>();

            foreach (Tuple<string, string> curr in dictMatrix.Keys)
            {
                if (lstFilteredCompanies.Contains(curr.Item1) && lstFilteredCompanies.Contains(curr.Item2))
                {
                    dictFilteredMatrix.Add(curr, dictMatrix[curr]);
                }
            }

            return (dictFilteredMatrix);
        }
 public IQueryable <Appointment> GetAppointment()
 {
     return(_mongoCrud.GetAllEntities());
 }
        public static void Run()
        {
            // Get all the companies from the DB
            MongoCrud <Company> _mongoCrud       = new MongoCrud <Company>();
            List <Company>      listAllCompanies = _mongoCrud.GetAllEntities().ToList <Company>();

            //ComapanyQueryHandler query = new ComapanyQueryHandler();
            //List<Company> listAllCompanies = query.GetCompaniesByTypeAndLocation(null, "ראשון לציון");

            BsonArray bsonItems = new BsonArray();

            int nBulkSize = 10;

            // Run over all the companies
            for (int i = 0; i < listAllCompanies.Count; i++)
            {
                int nIterationsNumber = Convert.ToInt32(Math.Ceiling(listAllCompanies.Count / 10.0));
                //int nIterationsNumber = Convert.ToInt32(Math.Round(listAllCompanies.Count / 25.0, MidpointRounding.AwayFromZero));

                for (int j = 0; j < nIterationsNumber; j++)
                {
                    string strRequestUrl = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + listAllCompanies[i].Location + "&destinations=";
                    for (int k = 0; (k < nBulkSize) && (((j * nBulkSize) + k) < listAllCompanies.Count); k++)
                    {
                        strRequestUrl += listAllCompanies[(j * nBulkSize) + k].Location + "|";
                    }
                    strRequestUrl  = strRequestUrl.Remove(strRequestUrl.Length - 1, 1);
                    strRequestUrl += "&key=AIzaSyCOnipZ0p4Khy5BhgtWhmLdkO9j4Du1-iw";

                    // Send request to google and get the response
                    var request  = WebRequest.Create(strRequestUrl);
                    var response = request.GetResponse();

                    // Convert the data to xml document
                    var xdoc = XDocument.Load(response.GetResponseStream());
                    IEnumerable <XElement> lstElements = xdoc.Element("DistanceMatrixResponse").Element("row").Elements();

                    // Run over all the results elements
                    for (int k = 0; k < lstElements.Count(); k++)
                    {
                        // Build the current way distance (id1->id2) and add it to the items array
                        BsonDocument currentDis = new BsonDocument
                        {
                            { "id1", listAllCompanies[i].Id },
                            { "id2", listAllCompanies[(j * nBulkSize) + k].Id },
                            { "distance", int.Parse(lstElements.ElementAt(k).Element("duration").Element("value").Value) }
                        };
                        bsonItems.Add(currentDis);
                    }

                    Thread.Sleep(12000);
                }

                // Build the request url for google

                /*string strRequestUrl = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + listAllCompanies[i].Location + "&destinations=";
                 * for (int j = 0; j < listAllCompanies.Count; j++)
                 * {
                 *  strRequestUrl += listAllCompanies[j].Location + "|";
                 * }
                 * strRequestUrl = strRequestUrl.Remove(strRequestUrl.Length - 1, 1);
                 *
                 * // Send request to google and get the response
                 * var request = WebRequest.Create(strRequestUrl);
                 * var response = request.GetResponse();
                 *
                 * // Convert the data to xml document
                 * var xdoc = XDocument.Load(response.GetResponseStream());
                 * IEnumerable<XElement> lstElements = xdoc.Element("DistanceMatrixResponse").Element("row").Elements();
                 *
                 * // Run over all the results elements
                 * for (int j = 0; j < lstElements.Count(); j++)
                 * {
                 *  // Build the current way distance (id1->id2) and add it to the items array
                 *  BsonDocument currentDis = new BsonDocument
                 *  {
                 *      { "id1", listAllCompanies[i].Id },
                 *      { "id2", listAllCompanies[j].Id },
                 *      { "distance", int.Parse(lstElements.ElementAt(j).Element("duration").Element("value").Value) }
                 *  };
                 *  bsonItems.Add(currentDis);
                 * }*/
            }

            // Connecting to the DBd
            MongoClient     client              = new MongoClient(@"mongodb://localhost:27017/SaveMayDay");
            var             server              = client.GetServer();
            var             database            = server.GetDatabase("SaveMayDay");
            MongoCollection distancesCollection = database.GetCollection("distances");

            BsonDocument bsonDocToSave = new BsonDocument
            {
                { "content", bsonItems }
            };

            // Save all the distances to the DB
            distancesCollection.Save(bsonDocToSave);
        }
        public static void Run()
        {
            // Get all the companies from the DB
            MongoCrud<Company> _mongoCrud = new MongoCrud<Company>();
            List<Company> listAllCompanies = _mongoCrud.GetAllEntities().ToList<Company>();

            //ComapanyQueryHandler query = new ComapanyQueryHandler();
            //List<Company> listAllCompanies = query.GetCompaniesByTypeAndLocation(null, "ראשון לציון");

            BsonArray bsonItems = new BsonArray();

            int nBulkSize = 10;

            // Run over all the companies
            for (int i = 0; i < listAllCompanies.Count; i++)
            {
                int nIterationsNumber = Convert.ToInt32(Math.Ceiling(listAllCompanies.Count / 10.0));
                //int nIterationsNumber = Convert.ToInt32(Math.Round(listAllCompanies.Count / 25.0, MidpointRounding.AwayFromZero));

                for (int j = 0; j < nIterationsNumber; j++)
                {
                    string strRequestUrl = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + listAllCompanies[i].Location + "&destinations=";
                    for (int k = 0; (k < nBulkSize) && (((j * nBulkSize) + k) < listAllCompanies.Count); k++)
                    {
                        strRequestUrl += listAllCompanies[(j * nBulkSize) + k].Location + "|";
                    }
                    strRequestUrl = strRequestUrl.Remove(strRequestUrl.Length - 1, 1);
                    strRequestUrl += "&key=AIzaSyCOnipZ0p4Khy5BhgtWhmLdkO9j4Du1-iw";

                    // Send request to google and get the response
                    var request = WebRequest.Create(strRequestUrl);
                    var response = request.GetResponse();

                    // Convert the data to xml document
                    var xdoc = XDocument.Load(response.GetResponseStream());
                    IEnumerable<XElement> lstElements = xdoc.Element("DistanceMatrixResponse").Element("row").Elements();

                    // Run over all the results elements
                    for (int k = 0; k < lstElements.Count(); k++)
                    {
                        // Build the current way distance (id1->id2) and add it to the items array
                        BsonDocument currentDis = new BsonDocument
                        {
                            { "id1", listAllCompanies[i].Id },
                            { "id2", listAllCompanies[(j * nBulkSize) + k].Id },
                            { "distance", int.Parse(lstElements.ElementAt(k).Element("duration").Element("value").Value) }
                        };
                        bsonItems.Add(currentDis);
                    }

                    Thread.Sleep(12000);
                }

                // Build the request url for google
                /*string strRequestUrl = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + listAllCompanies[i].Location + "&destinations=";
                for (int j = 0; j < listAllCompanies.Count; j++)
                {
                    strRequestUrl += listAllCompanies[j].Location + "|";
                }
                strRequestUrl = strRequestUrl.Remove(strRequestUrl.Length - 1, 1);

                // Send request to google and get the response
                var request = WebRequest.Create(strRequestUrl);
                var response = request.GetResponse();

                // Convert the data to xml document
                var xdoc = XDocument.Load(response.GetResponseStream());
                IEnumerable<XElement> lstElements = xdoc.Element("DistanceMatrixResponse").Element("row").Elements();

                // Run over all the results elements
                for (int j = 0; j < lstElements.Count(); j++)
                {
                    // Build the current way distance (id1->id2) and add it to the items array
                    BsonDocument currentDis = new BsonDocument
                    {
                        { "id1", listAllCompanies[i].Id },
                        { "id2", listAllCompanies[j].Id },
                        { "distance", int.Parse(lstElements.ElementAt(j).Element("duration").Element("value").Value) }
                    };
                    bsonItems.Add(currentDis);
                }*/
            }

            // Connecting to the DBd
            MongoClient client = new MongoClient(@"mongodb://localhost:27017/SaveMayDay");
            var server = client.GetServer();
            var database = server.GetDatabase("SaveMayDay");
            MongoCollection distancesCollection = database.GetCollection("distances");

            BsonDocument bsonDocToSave = new BsonDocument
            {
                { "content", bsonItems }
            };

            // Save all the distances to the DB
            distancesCollection.Save(bsonDocToSave);
        }