/// <summary>
        /// Deletes routes from project collection.
        /// </summary>
        /// <param name="deletingRoutes">List of deleting routes.</param>
        private void _DeleteRoutes(IList <Route> deletingRoutes)
        {
            AppData.IDataObjectCollection <Route> routes = DataKeeper.Routes;

            // store route relative objects
            List <Driver>  deletingDrivers  = new List <Driver>();
            List <Vehicle> deletingVehicles = new List <Vehicle>();

            // remove routes
            for (int index = 0; index < deletingRoutes.Count; ++index)
            {
                Route route = deletingRoutes[index];

                Driver driver = route.Driver;
                if (!_IsObjectPresentInCollection(driver, deletingDrivers))
                {
                    deletingDrivers.Add(driver);
                }

                Vehicle vehicle = route.Vehicle;
                if (!_IsObjectPresentInCollection(vehicle, deletingVehicles))
                {
                    deletingVehicles.Add(vehicle);
                }

                routes.Remove(route);
            }

            // remove drivers
            _DeleteObjects(deletingDrivers, DataKeeper.Project.Drivers);

            // remove vehicles
            _DeleteObjects(deletingVehicles, DataKeeper.Project.Vehicles);
        }
        /// <summary>
        /// Gets vehicle to init route.
        /// </summary>
        /// <param name="list">Vehicles for first selection.</param>
        /// <param name="index">Vehicle index.</param>
        /// <returns>Vehicle object.</returns>
        private Vehicle _GetNextVehicle(IList <Vehicle> list, int index)
        {
            Vehicle obj = null;

            if (index < list.Count)
            {
                obj = list[index]; // get presented
            }
            else
            {   // create new object
                AppData.IDataObjectCollection <Vehicle> vehicles = DataKeeper.Project.Vehicles;

                obj      = new Vehicle(DataKeeper.Project.CapacitiesInfo);
                obj.Name = _GetNewName(App.Current.FindString("Vehicle"), vehicles);

                // init fuel type - use first presented
                AppData.IDataObjectCollection <FuelType> fuelTypes = DataKeeper.Project.FuelTypes;
                if (0 < fuelTypes.Count)
                {
                    obj.FuelType = fuelTypes[0];
                }

                vehicles.Add(obj);
            }

            return(obj);
        }
 /// <summary>
 /// Deletes objects from project collection.
 /// </summary>
 /// <param name="deleteingObjects">List of deleting objects.</param>
 /// <param name="objectCollection">Project object collection.</param>
 private void _DeleteObjects <T>(IList <T> deleteingObjects, AppData.IDataObjectCollection <T> objectCollection)
     where T : AppData.DataObject
 {
     for (int index = 0; index < deleteingObjects.Count; ++index)
     {
         objectCollection.Remove(deleteingObjects[index]);
     }
 }
Exemple #4
0
        ///////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Gets sorted collection.
        /// </summary>
        /// <returns>Sorted barrier collection (sorted by creation time).</returns>
        private AppData.SortedDataObjectCollection <Barrier> _GetSortedCollection()
        {
            AppData.IDataObjectCollection <Barrier> collection =
                App.Current.Project.Barriers.SearchAll(true);

            var sortedCollection =
                new AppData.SortedDataObjectCollection <Barrier>(collection,
                                                                 new CreationTimeComparer <Barrier>());

            return(sortedCollection);
        }
        /// <summary>
        /// Gets from full collection all object not presented in filtred objects.
        /// </summary>
        /// <param name="ignoredObjects">Ignored object collection.</param>
        /// <param name="allObjects">Full object collection.</param>
        /// <returns>Object colletion from full collection not presented in filtred objects.</returns>
        private IList <T> _GetNotUsedObjects <T>(ICollection <T> ignoredObjects,
                                                 AppData.IDataObjectCollection <T> allObjects)
            where T : AppData.DataObject
        {
            var filtredObject = new List <T>();

            for (int index = 0; index < allObjects.Count; ++index)
            {
                T obj = allObjects[index];
                if (null == _GetObjectByName(obj.ToString(), ignoredObjects))
                {
                    filtredObject.Add(obj);
                }
            }

            return(filtredObject);
        }
        /// <summary>
        /// Adds orders to project.
        /// </summary>
        /// <param name="ordersByDate">Order collection for date.</param>
        /// <param name="appOrdersByDate">Application order collection for date.</param>
        /// <param name="manager">Application order manager.</param>
        private void _AddOrdersToProject(IEnumerable ordersByDate,
                                         AppData.IDataObjectCollection <Order> appOrdersByDate,
                                         AppData.OrderManager manager)
        {
            Debug.Assert(null != ordersByDate);    // created
            Debug.Assert(null != appOrdersByDate); // created
            Debug.Assert(null != manager);         // created

            // check and replace data for orders with same name
            int appOrdersCount = appOrdersByDate.Count;

            foreach (Order order in ordersByDate)
            {
                bool isReplace = false;
                for (int index = 0; index < appOrdersCount; ++index)
                {
                    Order currOrder = appOrdersByDate[index];
                    // update order data
                    if (_IsOrdersEquals(currOrder, order))
                    {
                        order.CopyTo(currOrder);

                        _AddUpdateObjectWarning(currOrder);
                        _ValidateObject(currOrder);

                        if (!_updatedObjects.Contains(currOrder))
                        {
                            _updatedObjects.Add(currOrder); // only unique
                        }
                        isReplace = true;
                        break; // NOTE: process done
                    }
                }

                // order with this name not found - add new order
                if (!isReplace)
                {
                    _updatedObjects.Add(order);
                    manager.Add(order);
                    _ValidateObject(order);
                    ++_createdCount;
                }
            }
        }
        /// <summary>
        /// Gets driver to init route.
        /// </summary>
        /// <param name="list">Drivers for first selection.</param>
        /// <param name="index">Driver index.</param>
        /// <returns>Driver object.</returns>
        private Driver _GetNextDriver(IList <Driver> list, int index)
        {
            Driver obj = null;

            if (index < list.Count)
            {
                obj = list[index]; // get presented
            }
            else
            {   // create new object
                AppData.IDataObjectCollection <Driver> drivers = DataKeeper.Project.Drivers;

                obj      = new Driver();
                obj.Name = _GetNewName(App.Current.FindString("Driver"), drivers);

                drivers.Add(obj);
            }

            return(obj);
        }
        private static ICollection <DriverSpecialty> _CanAccommodateOrderDriverSpecialties(ICollection <Route> routes,
                                                                                           Order order)
        {
            Debug.Assert(null != routes);
            Debug.Assert(null != order);

            List <DriverSpecialty> routesSpecialities = new List <DriverSpecialty>();

            foreach (Route route in routes)
            {
                AppData.IDataObjectCollection <DriverSpecialty> specialities = route.Driver.Specialties;
                foreach (DriverSpecialty specialty in specialities)
                {
                    if (!routesSpecialities.Contains(specialty))
                    {
                        routesSpecialities.Add(specialty);
                    }
                }
            }

            return(_CanAccommodateOrderSpecialties(routesSpecialities, order.DriverSpecialties));
        }
        /// <summary>
        /// Does grow list of routes.
        /// </summary>
        /// <param name="vehicleNumber">New vehicle number.</param>
        private void _DoGrowList(int vehicleNumber)
        {
            _EndEditTable();

            // select objects in use
            List <Driver>  driversInUse  = new List <Driver>();
            List <Vehicle> vehiclesInUse = new List <Vehicle>();

            AppData.IDataObjectCollection <Route> routes = DataKeeper.Routes;
            for (int index = 0; index < routes.Count; ++index)
            {
                Route route = routes[index];
                if (!_IsObjectPresentInCollection(route.Driver, driversInUse))
                {
                    driversInUse.Add(route.Driver);
                }
                if (!_IsObjectPresentInCollection(route.Vehicle, vehiclesInUse))
                {
                    vehiclesInUse.Add(route.Vehicle);
                }
            }

            Project project = DataKeeper.Project;

            // filter project objects - select not used objects
            IList <Driver>  drivers  = _GetNotUsedObjects(driversInUse, project.Drivers);
            IList <Vehicle> vehicles = _GetNotUsedObjects(vehiclesInUse, project.Vehicles);

            // use older as default location
            var sortCollection =
                from loc in project.Locations
                orderby loc.CreationTime
                select loc;
            Location defaultLocation = sortCollection.First();

            // create, init and add new routes
            int maxOrders         = _GetInputedMaxOrder();
            int maxWorkTime       = _GetInputedMaxWorkTime();
            int maxTravelDuration = _GetCorrectedMaxTravelDuration(maxWorkTime);

            int presentedVehicleNumber = DataKeeper.Routes.Count;

            int needAddCount = vehicleNumber - presentedVehicleNumber;

            for (int index = 0; index < needAddCount; ++index)
            {   // create route
                Route route = project.CreateRoute();
                // init route
                route.Name  = _GetNewName(App.Current.FindString("Route"), routes);
                route.Color = RouteColorManager.Instance.NextRouteColor(routes);

                route.StartLocation = defaultLocation;
                route.EndLocation   = defaultLocation;
                route.Driver        = _GetNextDriver(drivers, index);
                route.Vehicle       = _GetNextVehicle(vehicles, index);
                TimeWindow startTimeWindow = route.StartTimeWindow;
                startTimeWindow.From    = textBoxStart.Time;
                startTimeWindow.To      = textBoxEnd.Time;
                route.MaxOrders         = maxOrders;
                route.MaxTravelDuration = maxTravelDuration;
                route.MaxTotalDuration  = maxWorkTime;

                routes.Add(route);
            }

            _StoreChanges();
        }
Exemple #10
0
        /// <summary>
        /// Adds barriers to project.
        /// </summary>
        /// <param name="objects">Barrier collection.</param>
        /// <param name="project">Project to adding objects.</param>
        private bool _AddBarriersToProject(IList <AppData.DataObject> objects, Project project)
        {
            Debug.Assert(null != objects);       // created
            Debug.Assert(0 < objects.Count);     // not empty
            Debug.Assert(objects[0] is Barrier); // supported type
            Debug.Assert(null != project);

            bool result = false;

            try
            {
                // split collection by required action
                AppData.BarrierManager manager = project.Barriers;
                foreach (Barrier barrier in objects)
                {
                    // find equals barrier in application
                    bool isUpdated = false;
                    AppData.IDataObjectCollection <Barrier> barriers =
                        manager.Search(barrier.StartDate);

                    foreach (Barrier appBarrier in barriers)
                    {
                        // update barrier data
                        if ((barrier.Name == appBarrier.Name) &&
                            barrier.FinishDate.Equals(appBarrier.FinishDate))
                        {
                            barrier.CopyTo(appBarrier);
                            _AddUpdateObjectWarning(appBarrier);
                            _ValidateObject(appBarrier);

                            // store in updated
                            if (!_updatedObjects.Contains(appBarrier))
                            {
                                _updatedObjects.Add(appBarrier); // only unique
                            }
                            isUpdated = true;
                            break; // NOTE: process done
                        }
                    }

                    // add new barrier
                    if (!isUpdated)
                    {
                        manager.Add(barrier);
                        _ValidateObject(barrier);
                        ++_createdCount;

                        _updatedObjects.Add(barrier);
                    }
                }

                // store changes
                App.Current.Project.Save();
                result = true;
            }
            catch (Exception ex)
            {
                Logger.Error(ex);

                _updatedObjects.Clear();
            }

            return(result);
        }
Exemple #11
0
        /// <summary>
        /// Adds orders to project.
        /// </summary>
        /// <param name="objects">Object collection (orders).</param>
        /// <param name="project">Project to adding objects.</param>
        private bool _AddOrdersToProject(IList <AppData.DataObject> objects, Project project)
        {
            Debug.Assert(null != objects);     // created
            Debug.Assert(0 < objects.Count);   // not empty
            Debug.Assert(objects[0] is Order); // supported type
            Debug.Assert(null != project);

            bool result = false;

            try
            {
                // NOTE: if order with this name present in application for same date -
                //       need replace data

                // convert objects to orders
                IList <Order> orders = _GetOrders(objects);

                // create unique date list
                IList <DateTime> uniqueDate = _CreateOrdersUniqueDateList(objects);

                // foreach unique date do
                AppData.OrderManager manager = project.Orders;
                for (int index = 0; index < uniqueDate.Count; ++index)
                {
                    DateTime date = uniqueDate[index];

                    IEnumerable ordersByDate = _CreateOrderListByDate(orders, date);
                    AppData.IDataObjectCollection <Order> collection = manager.Search(date);

                    // search application orders with this date
                    if (0 < collection.Count)
                    {
                        // in application present orders to this date - add orders with update
                        _AddOrdersToProject(ordersByDate, collection, manager);
                    }
                    else
                    {   // in application no orders to this date - add all orders
                        foreach (Order order in ordersByDate)
                        {
                            manager.Add(order);
                            _ValidateObject(order);
                            ++_createdCount;

                            _updatedObjects.Add(order);
                        }
                    }

                    // create new schedule if early not created
                    _CreateSchedule(date);
                }

                // store changes
                App.Current.Project.Save();
                result = true;

                // Workaround
                _UpdateGui(orders);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);

                _updatedObjects.Clear();
            }

            return(result);
        }