/// <summary>
        /// Execute command.
        /// </summary>
        /// <param name="args">
        /// Command args.
        /// Routes to generate shapes.
        /// </param>
        protected override void _Execute(params object[] args)
        {
            try
            {
                List <Route> routesWithoutGeometry = args[0] as List <Route>;

                Debug.Assert(routesWithoutGeometry != null);

                _DoGenerateRoutesShapes(routesWithoutGeometry);
            }
            catch (RouteException e)
            {
                if (e.InvalidObjects != null) // if exception throw because any Routes or Orders are invalid
                {
                    _ShowSolveValidationResult(e.InvalidObjects);
                }
                else
                {
                    App.Current.Messenger.AddError(RoutingCmdHelpers.FormatRoutingExceptionMsg(e));
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                if ((e is LicenseException) || (e is AuthenticationException) || (e is CommunicationException))
                {
                    CommonHelpers.AddRoutingErrorMessage(e);
                }
                else
                {
                    throw;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Starts operation process.
        /// </summary>
        /// <param name="args">Operation args.</param>
        /// <exception cref="Exception">Throws if any unhandles exception occurs in method.</exception>
        protected override void _Execute(params object[] args)
        {
            try
            {
                // Get current schedule.
                if (_schedulePage == null)
                {
                    _schedulePage = (OptimizeAndEditPage)App.Current.MainWindow.GetPage(PagePaths.SchedulePagePath);
                }

                Schedule schedule = _schedulePage.CurrentSchedule;

                ICollection <Order> selectedOrders = _GetOrdersWhichCanBeUnassignedFromSelection(_schedulePage.SelectedItems);
                ICollection <Order> orders         = RoutingCmdHelpers.GetOrdersIncludingPairs(schedule, selectedOrders);
                ICollection <Route> routes         = ViolationsHelper.GetRouteForUnassignOrders(schedule, orders);

                if (_CheckRoutingParams(schedule, routes, orders))
                {
                    SolveOptions options = new SolveOptions();
                    options.GenerateDirections            = App.Current.MapDisplay.TrueRoute;
                    options.FailOnInvalidOrderGeoLocation = false;

                    _SetOperationStartedStatus((string)App.Current.FindResource(UNASSIGN_ORDERS), (DateTime)schedule.PlannedDate);

                    OperationsIds.Add(App.Current.Solver.UnassignOrdersAsync(schedule, orders, options));

                    // set solve started message
                    string infoMessage = RoutingMessagesHelper.GetUnassignOperationStartedMessage(orders);

                    if (!string.IsNullOrEmpty(infoMessage))
                    {
                        App.Current.Messenger.AddInfo(infoMessage);
                    }
                }
            }
            catch (RouteException e)
            {
                if (e.InvalidObjects != null) // if exception throw because any Routes or Orders are invalid
                {
                    _ShowSolveValidationResult(e.InvalidObjects);
                }
                else
                {
                    _ShowErrorMsg(RoutingCmdHelpers.FormatRoutingExceptionMsg(e));
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                if ((e is LicenseException) || (e is AuthenticationException) || (e is CommunicationException))
                {
                    CommonHelpers.AddRoutingErrorMessage(e);
                }
                else
                {
                    throw;
                }
            }
        }
        /// <summary>
        /// Method unassignes orders from schedule
        /// </summary>
        /// <param name="orders"></param>
        /// <param name="schedule"></param>
        private void _UnassignOrdersFromSchedule(Schedule schedule, ICollection <Order> orders)
        {
            try
            {
                ICollection <Order> ordersWithPairs = RoutingCmdHelpers.GetOrdersIncludingPairs(schedule, orders);

                // Create routes collection.
                ICollection <Route> routes = ViolationsHelper.GetRouteForUnassignOrders(schedule, ordersWithPairs);

                if (_CheckRoutingParams(schedule, ordersWithPairs, routes))
                {
                    SolveOptions options = new SolveOptions();
                    options.GenerateDirections            = App.Current.MapDisplay.TrueRoute;
                    options.FailOnInvalidOrderGeoLocation = false;

                    string infoMessage = _FormatSuccessUnassigningStartedMessage(schedule, _schedulesToUnassign);

                    // Set operation info status.
                    _SetOperationStartedStatus(infoMessage, (DateTime)schedule.PlannedDate);

                    // Start solve operation.
                    App.Current.Solver.UnassignOrdersAsync(schedule, ordersWithPairs, options);
                }
                else // If routing operation was not started - clean collections and unlock UI.
                {
                    _CleanUp();
                    _UnlockUI();
                }
            }
            catch (RouteException e)
            {
                App.Current.Messenger.AddError(RoutingCmdHelpers.FormatRoutingExceptionMsg(e));
                App.Current.Messenger.AddError(string.Format(OperationIsFailedMessage, schedule));

                // Save already edited schedules.
                _UpdateOptimizeAndEditPageSchedules();
                _CleanUp();
                _UnlockUI();
            }
            catch (Exception ex)
            {
                // Save already edited schedules.
                _UpdateOptimizeAndEditPageSchedules();
                _CleanUp();
                _UnlockUI();

                if ((ex is LicenseException) || (ex is AuthenticationException) || (ex is CommunicationException))
                {
                    CommonHelpers.AddRoutingErrorMessage(ex);
                }
                else
                {
                    throw;
                }
            }
        }
        /// <summary>
        /// Handles exception thrown from a solve operation.
        /// </summary>
        /// <param name="exception">The exception thrown from a solve operation.</param>
        private void _HandleSolveError(Exception exception)
        {
            Debug.Assert(exception != null);

            var routeException = exception as RouteException;

            if (routeException != null)
            {
                if (routeException.InvalidObjects != null)
                {
                    // exception was thrown because any Routes or Orders are invalid.
                    _ShowSolveValidationResult(routeException.InvalidObjects);
                }
                else
                {
                    _ShowErrorMsg(RoutingCmdHelpers.FormatRoutingExceptionMsg(routeException));
                }

                return;
            }

            // If we have error in sync response - show it.
            var restException = exception as RestException;

            if (restException != null)
            {
                var details = new List <MessageDetail>();

                // If exception has details- process them.
                if (restException.Details != null)
                {
                    foreach (var detail in restException.Details)
                    {
                        var detailMessage = GuidsReplacer.ReplaceGuids(detail, App.Current.Project);
                        details.Add(new MessageDetail(MessageType.Error, detailMessage));
                    }
                }

                // Process exception message.
                var message = GuidsReplacer.ReplaceGuids(exception.Message, App.Current.Project);

                // Show error in messenger.
                App.Current.Messenger.AddError(message, details);

                return;
            }

            Logger.Error(exception);
            CommonHelpers.AddRoutingErrorMessage(exception);
        }
Example #5
0
        /// <summary>
        /// Excecutes command.
        /// </summary>
        /// <param name="args"></param>
        protected override void _Execute(params object[] args)
        {
            try
            {
                OptimizeAndEditPage schedulePage = (OptimizeAndEditPage)App.Current.MainWindow.GetPage(PagePaths.SchedulePagePath);
                Schedule            schedule     = schedulePage.CurrentSchedule;

                // Get selected orders.
                Collection <Order> selectedOrders = _GetOrdersWhichCanBeMovedFromSelection(schedulePage.SelectedItems);
                selectedOrders = RoutingCmdHelpers.GetOrdersIncludingPairs(schedule, selectedOrders) as Collection <Order>;
                bool keepViolOrdrsUnassigned = false;

                Debug.Assert(args[0] != null);
                ICollection <Route> targetRoutes = args[0] as ICollection <Route>;
                Debug.Assert(targetRoutes != null);

                string routeName = args[1] as string;

                if (_CheckRoutingParams(schedule, targetRoutes, selectedOrders))
                {
                    SolveOptions options = new SolveOptions();
                    options.GenerateDirections            = App.Current.MapDisplay.TrueRoute;
                    options.FailOnInvalidOrderGeoLocation = false;

                    _SetOperationStartedStatus((string)App.Current.FindResource(ASSIGN_ORDERS), (DateTime)schedule.PlannedDate);

                    // Start "Assign to best other route" operation.
                    OperationsIds.Add(App.Current.Solver.AssignOrdersAsync(schedule, selectedOrders,
                                                                           targetRoutes, null,
                                                                           keepViolOrdrsUnassigned,
                                                                           options));
                    // Set solve started message
                    string infoMessage = RoutingMessagesHelper.GetAssignOperationStartedMessage(selectedOrders, routeName);

                    if (!string.IsNullOrEmpty(infoMessage))
                    {
                        App.Current.Messenger.AddInfo(infoMessage);
                    }
                }
            }
            catch (RouteException e)
            {
                if (e.InvalidObjects != null) // If exception throw because any Routes or Orders are invalid
                {
                    _ShowSolveValidationResult(e.InvalidObjects);
                }
                else
                {
                    _ShowErrorMsg(RoutingCmdHelpers.FormatRoutingExceptionMsg(e));
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                if ((e is LicenseException) || (e is AuthenticationException) || (e is CommunicationException))
                {
                    CommonHelpers.AddRoutingErrorMessage(e);
                }
                else
                {
                    throw;
                }
            }
        }
        protected override void _Execute(params object[] args)
        {
            try
            {
                ICollection <Route> routes = _GetRoutesFromSelection(_schedulePage.SelectedItems);

                // get unlocked routes from selected routes
                // and all orders assigned to converting routes
                List <Order> orders = new List <Order>();
                foreach (Route route in routes)
                {
                    if (!route.IsLocked)
                    {
                        // get assigned orders
                        List <Order> routeOrders = new List <Order>();
                        foreach (Stop stop in route.Stops)
                        {
                            if (stop.StopType == StopType.Order)
                            {
                                routeOrders.Add(stop.AssociatedObject as Order);
                            }
                        }
                        orders.AddRange(routeOrders);
                    }
                }

                // get current schedule
                Schedule schedule = _schedulePage.CurrentSchedule;

                if (_CheckRoutingParams(schedule, routes, orders))
                {
                    SolveOptions options = new SolveOptions();
                    options.GenerateDirections            = App.Current.MapDisplay.TrueRoute;
                    options.FailOnInvalidOrderGeoLocation = false;

                    _SetOperationStartedStatus((string)App.Current.FindResource("SequenceRoutes"), (DateTime)schedule.PlannedDate);

                    OperationsIds.Add(App.Current.Solver.SequenceRoutesAsync(schedule, routes, options));

                    // set solve started message
                    string infoMessage = _FormatSuccesSolveStartedMsg(schedule, routes.Count);
                    if (!string.IsNullOrEmpty(infoMessage))
                    {
                        App.Current.Messenger.AddInfo(infoMessage);
                    }
                }
            }
            catch (RouteException e)
            {
                if (e.InvalidObjects != null) // if exception throw because any Routes or Orders are invalid
                {
                    _ShowSolveValidationResult(e.InvalidObjects);
                }
                else
                {
                    _ShowErrorMsg(RoutingCmdHelpers.FormatRoutingExceptionMsg(e));
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                if ((e is LicenseException) || (e is AuthenticationException) || (e is CommunicationException))
                {
                    CommonHelpers.AddRoutingErrorMessage(e);
                }
                else
                {
                    throw;
                }
            }
        }
Example #7
0
        protected override void _Execute(params object[] args)
        {
            AsyncOperationInfo info = null;

            // Check whether operation can be started.
            if (!_CanBuildRoutesBeStarted(out info)) // If BuildRoutes operation cannot be started - show warning and return.
            {
                Debug.Assert(info != null);          // Must be defined in _CanBuildRoutesBeStarted method.
                App.Current.MainWindow.MessageWindow.AddWarning(
                    string.Format((string)App.Current.FindResource(ALREADY_BUILDING_ROUTES_MESSAGE_RESOURCE), info.Schedule.PlannedDate.Value.ToShortDateString()));
                return;
            }

            try
            {
                Schedule schedule = _optimizeAndEditPage.CurrentSchedule;

                var inputParams = _GetBuildRoutesParameters(schedule);
                var routes      = inputParams.TargetRoutes;
                var orders      = inputParams.OrdersToAssign;

                if (_CheckRoutingParams(schedule, routes, orders))
                {
                    SolveOptions options = new SolveOptions();
                    options.GenerateDirections            = App.Current.MapDisplay.TrueRoute;
                    options.FailOnInvalidOrderGeoLocation = false;

                    _SetOperationStartedStatus((string)App.Current.FindResource(BUILD_ROUTES_STRING), (DateTime)schedule.PlannedDate);

                    OperationsIds.Add(App.Current.Solver.BuildRoutesAsync(
                                          schedule,
                                          options,
                                          inputParams));

                    // set solve started message
                    string infoMessage = _FormatSuccesSolveStartedMsg(schedule, inputParams);
                    if (!string.IsNullOrEmpty(infoMessage))
                    {
                        App.Current.Messenger.AddInfo(infoMessage);
                    }
                }
            }
            catch (RouteException e)
            {
                if (e.InvalidObjects != null) // if exception throw because any Routes or Orders are invalid
                {
                    _ShowSolveValidationResult(e.InvalidObjects);
                }
                else
                {
                    _ShowErrorMsg(RoutingCmdHelpers.FormatRoutingExceptionMsg(e));
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                if ((e is LicenseException) || (e is AuthenticationException) || (e is CommunicationException))
                {
                    CommonHelpers.AddRoutingErrorMessage(e);
                }
                else
                {
                    throw;
                }
            }
        }
Example #8
0
        /// <summary>
        /// Starts command executing.
        /// </summary>
        /// <param name="args">Command args.</param>
        protected override void _Execute(params object[] args)
        {
            // Get OptimizeAndEdit page.
            OptimizeAndEditPage schedulePage = ((MainWindow)App.Current.MainWindow).GetPage(PagePaths.SchedulePagePath) as OptimizeAndEditPage;

            Debug.Assert(schedulePage != null);

            // If editing is in progress - try to save editing.
            if (schedulePage.IsEditingInProgress)
            {
                // If editing cannot be saved - cancel editing.
                if (!schedulePage.SaveEditedItem())
                {
                    schedulePage.CancelObjectEditing();
                }
            }

            // Orders to assign.
            Collection <Order> ordersToAssign = (Collection <Order>)args[0];

            Debug.Assert(ordersToAssign != null);

            // Target object.
            Object target = (Object)args[1];

            // Include paired orders unless we are moving one order within the same route
            bool movingSingleOrderInPair = false;

            if (ordersToAssign.Count == 1 && (target is Stop))
            {
                Route targetRoute   = (target as Stop).Route;
                Order orderToAssign = ordersToAssign.ElementAt(0);
                foreach (Stop stop in targetRoute.Stops)
                {
                    Order associatedOrder = stop.AssociatedObject as Order;
                    if (associatedOrder == null)
                    {
                        continue;
                    }

                    if (associatedOrder == orderToAssign)
                    {
                        movingSingleOrderInPair = true;
                        break;
                    }
                }
            }

            if (!movingSingleOrderInPair)
            {
                ordersToAssign = RoutingCmdHelpers.GetOrdersIncludingPairs(schedulePage.CurrentSchedule, ordersToAssign) as Collection <Order>;
            }

            int?sequence = null;

            if (target is Stop)
            {
                // Set sequence only if there is a single order to assign.
                if (ordersToAssign.Count == 1)
                {
                    sequence = (target as Stop).SequenceNumber;
                }
                else // Otherwise change target to route. For more details see CR161083.
                {
                    target = (target as Stop).Route;
                }
            }

            // Get target routes.
            ICollection <Route> targetRoutes = _CreateTargetRoutes(target);

            try
            {
                // If target routes count is 0 - orders must be unassigned.
                if (targetRoutes.Count == 0)
                {
                    // Start unassigning orders.
                    _UnassignOrders(ordersToAssign);
                }
                else
                {
                    // Start moving orders.
                    _MoveOrders(ordersToAssign, targetRoutes, sequence);
                }
            }
            catch (RouteException e)
            {
                if (e.InvalidObjects != null) // If exception throw because any Routes or Orders are invalid.
                {
                    _ShowSolveValidationResult(e.InvalidObjects);
                }
                else
                {
                    _ShowErrorMsg(RoutingCmdHelpers.FormatRoutingExceptionMsg(e));
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                if ((e is LicenseException) || (e is AuthenticationException) || (e is CommunicationException))
                {
                    CommonHelpers.AddRoutingErrorMessage(e);
                }
                else
                {
                    throw;
                }
            }
        }