internal static MenuItem NewMenuItem(EntityOperationContext eoc, EntityOperationGroup group)
        {
            var man = OperationClient.Manager;

            MenuItem menuItem = new MenuItem
            {
                Header = eoc.OperationSettings.Try(os => os.Text) ?? 
                (group == null || group.SimplifyName == null ? eoc.OperationInfo.OperationSymbol.NiceToString() :
                 group.SimplifyName(eoc.OperationInfo.OperationSymbol.NiceToString())),
                Icon = man.GetImage(eoc.OperationInfo.OperationSymbol, eoc.OperationSettings).ToSmallImage(),
                Tag = eoc.OperationInfo,
                Background = man.GetBackground(eoc.OperationInfo, eoc.OperationSettings)
            };

            if (eoc.OperationSettings != null && eoc.OperationSettings.Order != 0)
                Common.SetOrder(menuItem, eoc.OperationSettings.Order);

            AutomationProperties.SetName(menuItem, eoc.OperationInfo.OperationSymbol.Key);

            eoc.SenderButton = menuItem;

            if (eoc.CanExecute != null)
            {
                menuItem.ToolTip = eoc.CanExecute;
                menuItem.IsEnabled = false;
                ToolTipService.SetShowOnDisabled(menuItem, true);
                AutomationProperties.SetHelpText(menuItem, eoc.CanExecute);
            }
            else
            {
                menuItem.Click += (_, __) => OperationExecute(eoc);
            }
            return menuItem;
        }
        public static ToolBarButton NewToolbarButton(EntityOperationContext eoc)
        {
            var man = OperationClient.Manager;

            ToolBarButton button = new ToolBarButton
            {
                Content = man.GetText(eoc.OperationInfo.OperationSymbol, eoc.OperationSettings),
                Image = man.GetImage(eoc.OperationInfo.OperationSymbol, eoc.OperationSettings),
                Tag = eoc.OperationInfo,
                Background = man.GetBackground(eoc.OperationInfo, eoc.OperationSettings)
            };

            if (eoc.OperationSettings != null && eoc.OperationSettings.Order != 0)
                Common.SetOrder(button, eoc.OperationSettings.Order);

            AutomationProperties.SetName(button, eoc.OperationInfo.OperationSymbol.Key);

            eoc.SenderButton = button;

            if (eoc.CanExecute != null)
            {
                button.ToolTip = eoc.CanExecute;
                button.IsEnabled = false;
                ToolTipService.SetShowOnDisabled(button, true);
                AutomationProperties.SetHelpText(button, eoc.CanExecute);
            }
            else
            {
                button.Click += (_, __) => OperationExecute(eoc);
            }
            return button;
        }
        public static bool MoveToSearchControls(EntityOperationContext eoc)
        {
            var controls = eoc.EntityControl.Children<SearchControl>()
                .Where(sc => eoc.OperationInfo.OperationSymbol.Equals(OperationClient.GetConstructFromOperationKey(sc)) ||
                sc.NotSet(OperationClient.ConstructFromOperationKeyProperty) && sc.EntityType == eoc.OperationInfo.ReturnType).ToList();

            if (!controls.Any())
                return false;

            foreach (var sc in controls)
            {
                if (sc.NotSet(OperationClient.ConstructFromOperationKeyProperty))
                    OperationClient.SetConstructFromOperationKey(sc, eoc.OperationInfo.OperationSymbol);

                sc.Create = false;

                var menu = sc.Child<Menu>(b => b.Name == "menu");

                var panel = (StackPanel)menu.Parent;

                var oldButton = panel.Children<ToolBarButton>(tb => tb.Tag is OperationInfo && ((OperationInfo)tb.Tag).OperationSymbol.Equals(eoc.OperationInfo.OperationSymbol)).FirstOrDefault();
                if (oldButton != null)
                    panel.Children.Remove(oldButton);

                var index = panel.Children.IndexOf(menu);
                panel.Children.Insert(index, NewToolbarButton(eoc));
            }
            return true;
        }
        public static Entity FindAssociatedTemplates(EntityOperationContext<Entity> e)
        {
            var template = Finder.Find(new FindOptions(typeof(SMSTemplateEntity))
            {
                FilterOptions = new List<FilterOption>
                {
                    { new FilterOption("IsActive", true) { Frozen = true } },
                    { new FilterOption("AssociatedType", Server.ServerTypes[e.Entity.GetType()]) }
                },
                SearchOnLoad = true,
            });

            if (template != null)
                Navigator.Navigate(e.Entity.ToLite().ConstructFromLite(SMSMessageOperation.CreateSMSWithTemplateFromEntity, ((Lite<SMSTemplateEntity>)template).Retrieve()));

            return null;
        }
 static ProcessEntity ProcessOperation_Plan(EntityOperationContext<ProcessEntity> args)
 {
     DateTime plan = TimeZoneManager.Now;
     if (ValueLineBox.Show(ref plan, "Choose planned date", "Please, choose the date you want the process to start", "Planned date", null, null, Window.GetWindow(args.SenderButton)))
     {
         return  ((ProcessEntity)args.Entity).ToLite().ExecuteLite(ProcessOperation.Plan, plan); 
     }
     return null; 
 }
        static void OperationExecute(EntityOperationContext eoc)
        {
            if (eoc.CanExecute != null)
                throw new ApplicationException("Operation {0} is disabled: {1}".Formato(eoc.OperationInfo.OperationSymbol, eoc.CanExecute));

            if (eoc.OperationSettings != null && eoc.OperationSettings.Click != null)
            {
                IIdentifiable newIdent = eoc.OperationSettings.Click(eoc);
                if (newIdent != null)
                    eoc.EntityControl.RaiseEvent(new ChangeDataContextEventArgs(newIdent));
            }
            else
            {
                IdentifiableEntity ident = (IdentifiableEntity)(IIdentifiable)eoc.Entity;
                if (eoc.OperationInfo.OperationType == OperationType.Execute)
                {
                    if (eoc.OperationInfo.Lite.Value)
                    {
                        if (eoc.EntityControl.LooseChangesIfAny())
                        {
                            Lite<IdentifiableEntity> lite = ident.ToLite();
                            IIdentifiable newIdent = Server.Return((IOperationServer s) => s.ExecuteOperationLite(lite, eoc.OperationInfo.OperationSymbol, null));
                            if (eoc.OperationInfo.Returns)
                                eoc.EntityControl.RaiseEvent(new ChangeDataContextEventArgs(newIdent));
                        }
                    }
                    else
                    {
                        IIdentifiable newIdent = Server.Return((IOperationServer s) => s.ExecuteOperation(ident, eoc.OperationInfo.OperationSymbol, null));
                        if (eoc.OperationInfo.Returns)
                            eoc.EntityControl.RaiseEvent(new ChangeDataContextEventArgs(newIdent));
                    }
                }
                else if (eoc.OperationInfo.OperationType == OperationType.ConstructorFrom)
                {
                    IIdentifiable result = null;
                    if (eoc.OperationInfo.Lite.Value)
                    {
                        if (!eoc.EntityControl.LooseChangesIfAny())
                            return;

                        Lite<IdentifiableEntity> lite = ident.ToLite();
                        result = Server.Return((IOperationServer s) => s.ConstructFromLite(lite, eoc.OperationInfo.OperationSymbol, null));
                    }
                    else
                    {
                        result = Server.Return((IOperationServer s) => s.ConstructFrom(ident, eoc.OperationInfo.OperationSymbol, null));
                    }

                    if (result != null)
                        Navigator.Navigate(result);
                    else
                        MessageBox.Show(Window.GetWindow(eoc.EntityControl), OperationMessage.TheOperation0DidNotReturnAnEntity.NiceToString().Formato(eoc.OperationInfo.OperationSymbol.NiceToString()));

                }
                else if (eoc.OperationInfo.OperationType == OperationType.Delete)
                {
                    if (MessageBox.Show(Window.GetWindow(eoc.EntityControl), OperationMessage.PleaseConfirmYouDLikeToDeleteTheEntityFromTheSystem.NiceToString(),  OperationMessage.Delete.NiceToString(), MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    {
                        Lite<IdentifiableEntity> lite = ident.ToLite();
                        Server.Execute((IOperationServer s) => s.Delete(lite, eoc.OperationInfo.OperationSymbol, null));
                        Window.GetWindow(eoc.EntityControl).Close();
                    }
                }
            }
        }