protected override DriverResult Editor(DynamicButtonToWorkflowsSettingsPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            DynamicButtonToWorkflowsSettingsVM dynamicButtonSettingsVM = new DynamicButtonToWorkflowsSettingsVM();

            if (updater.TryUpdateModel(dynamicButtonSettingsVM, Prefix, null, null))
            {
                var buttonsByName = dynamicButtonSettingsVM.Buttons.Where(w => w.Delete == false).GroupBy(q => new { q.ButtonName }).Select(q => new { q.Key.ButtonName, Occurrences = q.Count() });
                buttonsByName = buttonsByName.Where(q => q.Occurrences > 1);

                if (buttonsByName.ToList().Count() > 0)
                {
                    _controllerContextAccessor.Context.Controller.TempData[Prefix + "ButtonsWithErrors"] = dynamicButtonSettingsVM.Buttons;
                    updater.AddModelError("ButtonUpdateError", T("Cannot have multiple buttons with the same name ({0})", string.Join(", ", buttonsByName.Select(q => q.ButtonName))));
                }
                else
                {
                    _dynamicButtonToWorkflowsService.UpdateButtons(dynamicButtonSettingsVM.Buttons);
                }
            }
            else
            {
                _controllerContextAccessor.Context.Controller.TempData[Prefix + "ButtonsWithErrors"] = dynamicButtonSettingsVM.Buttons;
            }

            return(Editor(part, shapeHelper));
        }
Ejemplo n.º 2
0
        public int UpdateFrom6()
        {
            SchemaBuilder.AlterTable("DynamicButtonToWorkflowsRecord", table => table
                                     .AddColumn <string>("GlobalIdentifier")
                                     );

            var buttonsWithoutGuid = _dynamicButtonToWorkflowsService.GetButtons().Where(w => String.IsNullOrWhiteSpace(w.GlobalIdentifier));

            if (buttonsWithoutGuid.Count() > 0)
            {
                List <DynamicButtonToWorkflowsEdit> buttonList = new List <DynamicButtonToWorkflowsEdit>();
                var activities       = _activityRepository.Table.Where(w => w.Name.Equals("DynamicButtonEvent"));
                var typesWithButtons = _contentDefinitionManager.ListTypeDefinitions().Where(w => w.Parts.Any(p => p.PartDefinition.Name == "DynamicButtonToWorkflowsPart")).ToList();

                foreach (var button in buttonsWithoutGuid)
                {
                    // Associo un Guid a tutti i bottoni già esistenti che non ne hanno uno
                    DynamicButtonToWorkflowsEdit buttonData = new DynamicButtonToWorkflowsEdit();
                    buttonData.Id                = button.Id;
                    buttonData.ButtonText        = button.ButtonText;
                    buttonData.ButtonAsync       = button.ButtonAsync;
                    buttonData.ButtonDescription = button.ButtonDescription;
                    buttonData.ButtonMessage     = button.ButtonMessage;
                    buttonData.ButtonName        = button.ButtonName;
                    buttonData.GlobalIdentifier  = Guid.NewGuid().ToString();
                    buttonData.Delete            = false;

                    buttonList.Add(buttonData);

                    // Correggo i riferimenti ai bottoni nelle parti
                    string partSettingToEdit = string.Format("{{{0}}}", button.Id);

                    foreach (var type in typesWithButtons)
                    {
                        var part = type.Parts.Where(w => w.PartDefinition.Name == "DynamicButtonToWorkflowsPart").FirstOrDefault();
                        if (part != null)
                        {
                            part.Settings["DynamicButtonsSetting.Buttons"] = part.Settings["DynamicButtonsSetting.Buttons"].Replace(partSettingToEdit, string.Format("{{{0}}}", buttonData.GlobalIdentifier));
                            _contentDefinitionManager.StoreTypeDefinition(type);
                        }
                    }

                    // Correggo i riferimenti ai bottoni nelle activities dei workflows
                    string activitySettingToEdit = string.Format("\"DynamicButton\":\"{0}\"", button.Id);
                    var    activitiesWithButton  = activities.Where(w => w.State.Contains(activitySettingToEdit));

                    foreach (var activity in activitiesWithButton)
                    {
                        activity.State = activity.State.Replace(activitySettingToEdit, string.Format("\"DynamicButton\":\"{0}\"", buttonData.GlobalIdentifier));
                        _activityRepository.Update(activity);
                    }
                }

                _dynamicButtonToWorkflowsService.UpdateButtons(buttonList);
                _activityRepository.Flush();
            }

            return(7);
        }