public async Task <IActionResult> Edit(string sourcename, string localId)
        {
            if (!await _authorizationService.AuthorizeAsync(User, OrchardCore.Workflows.Snippets.Permissions.ManageWorkflowSnippets))
            {
                return(Forbid());
            }

            var newLocalId          = string.IsNullOrWhiteSpace(localId) ? Guid.NewGuid().ToString() : localId;
            var availableActivities = _activityLibrary.ListActivities();

            var shortcodeTemplatesDocument = await _workflowSnippetsManager.GetWorkflowSnippetsDocumentAsync();

            if (!shortcodeTemplatesDocument.WorkflowSnippets.ContainsKey(sourcename))
            {
                return(RedirectToAction("EditProperties", new { sourcename = sourcename }));
            }

            var workflowSnippet = shortcodeTemplatesDocument.WorkflowSnippets[sourcename];

            var workflowType = workflowSnippet.WorkFlowType;

            if (workflowType == null)
            {
                return(NotFound());
            }

            //populate properties from source object
            workflowType.Name      = sourcename;
            workflowType.IsEnabled = workflowSnippet.IsEnabled;

            var workflow        = _workflowManager.NewWorkflow(workflowType);
            var workflowContext = await _workflowManager.CreateWorkflowExecutionContextAsync(workflowType, workflow);

            var activityContexts = await Task.WhenAll(workflowType.Activities.Select(x => _workflowManager.CreateActivityExecutionContextAsync(x, x.Properties)));

            // var workflowCount = await _session.QueryIndex<WorkflowIndex>(x => x.WorkflowTypeId == workflowType.WorkflowTypeId).CountAsync();


            var activityThumbnailShapes = new List <dynamic>();
            var index = 0;

            foreach (var activity in availableActivities)
            {
                activityThumbnailShapes.Add(await BuildActivityDisplay(activity, index++, sourcename, newLocalId, "Thumbnail"));
            }

            var activityDesignShapes = new List <dynamic>();

            index = 0;

            foreach (var activityContext in activityContexts)
            {
                activityDesignShapes.Add(await BuildActivityDisplay(activityContext, index++, sourcename, newLocalId, "Design"));
            }

            var activitiesDataQuery = activityContexts.Select(x => new
            {
                Id       = x.ActivityRecord.ActivityId,
                X        = x.ActivityRecord.X,
                Y        = x.ActivityRecord.Y,
                Name     = x.ActivityRecord.Name,
                IsStart  = x.ActivityRecord.IsStart,
                IsEvent  = x.Activity.IsEvent(),
                Outcomes = x.Activity.GetPossibleOutcomes(workflowContext, x).ToArray()
            });
            var workflowTypeData = new
            {
                Id          = sourcename,
                Name        = workflowType.Name,
                IsEnabled   = workflowType.IsEnabled,
                Activities  = activitiesDataQuery.ToArray(),
                Transitions = workflowType.Transitions
            };

            var wfVewModel = new WorkflowTypeViewModel
            {
                WorkflowType     = workflowType,
                WorkflowTypeJson = JsonConvert.SerializeObject(workflowTypeData, Formatting.None, new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                }),
                ActivityThumbnailShapes = activityThumbnailShapes,
                ActivityDesignShapes    = activityDesignShapes,
                ActivityCategories      = _activityLibrary.ListCategories().ToList(),
                LocalId        = newLocalId,
                LoadLocalState = !string.IsNullOrWhiteSpace(localId),
                //  WorkflowCount = workflowCount
            };

            var model = new WorkflowSnippetViewModel
            {
                Name = sourcename,
                WorkflowTypeViewModel = wfVewModel,
                //Content = template.Content,
                // WorkflowType = workflowType,
                Description = workflowSnippet.Description,
                // Categories = template.Categories
            };

            return(View(model));
        }
        public async Task <IActionResult> Edit(int id, string localId)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageWorkflows))
            {
                return(Unauthorized());
            }

            var newLocalId          = string.IsNullOrWhiteSpace(localId) ? Guid.NewGuid().ToString() : localId;
            var availableActivities = _activityLibrary.ListActivities();
            var workflowType        = await _session.GetAsync <WorkflowType>(id);

            if (workflowType == null)
            {
                return(NotFound());
            }

            var workflow        = _workflowManager.NewWorkflow(workflowType);
            var workflowContext = await _workflowManager.CreateWorkflowExecutionContextAsync(workflowType, workflow);

            var activityContexts = await Task.WhenAll(workflowType.Activities.Select(async x => await _workflowManager.CreateActivityExecutionContextAsync(x, x.Properties)));

            var activityThumbnailDisplayTasks = availableActivities.Select(async(x, i) => await BuildActivityDisplay(x, i, id, newLocalId, "Thumbnail"));
            var activityDesignDisplayTasks    = activityContexts.Select(async(x, i) => await BuildActivityDisplay(x, i, id, newLocalId, "Design"));
            var workflowCount = await _session.QueryIndex <WorkflowIndex>(x => x.WorkflowTypeId == workflowType.WorkflowTypeId).CountAsync();

            await Task.WhenAll(activityThumbnailDisplayTasks.Concat(activityDesignDisplayTasks));

            var activityThumbnailShapes = activityThumbnailDisplayTasks.Select(x => x.Result).ToList();
            var activityDesignShapes    = activityDesignDisplayTasks.Select(x => x.Result).ToList();
            var activitiesDataQuery     = activityContexts.Select(x => new
            {
                Id       = x.ActivityRecord.ActivityId,
                X        = x.ActivityRecord.X,
                Y        = x.ActivityRecord.Y,
                Name     = x.ActivityRecord.Name,
                IsStart  = x.ActivityRecord.IsStart,
                IsEvent  = x.Activity.IsEvent(),
                Outcomes = x.Activity.GetPossibleOutcomes(workflowContext, x).ToArray()
            });
            var workflowTypeData = new
            {
                Id          = workflowType.Id,
                Name        = workflowType.Name,
                IsEnabled   = workflowType.IsEnabled,
                Activities  = activitiesDataQuery.ToArray(),
                Transitions = workflowType.Transitions
            };
            var viewModel = new WorkflowTypeViewModel
            {
                WorkflowType     = workflowType,
                WorkflowTypeJson = JsonConvert.SerializeObject(workflowTypeData, Formatting.None, new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                }),
                ActivityThumbnailShapes = activityThumbnailShapes,
                ActivityDesignShapes    = activityDesignShapes,
                ActivityCategories      = _activityLibrary.ListCategories().ToList(),
                LocalId        = newLocalId,
                LoadLocalState = !string.IsNullOrWhiteSpace(localId),
                WorkflowCount  = workflowCount
            };

            return(View(viewModel));
        }