public async Task <WorkFlowAction> GetWorkflowActionAsync(Workflow workflow)
        {
            if (workflowActions.TryGetValue(workflow.ServiceCatalogId, out WorkFlowAction action))
            {
                Debug.WriteLine("returning cached workflow action");
                return(action);
            }

            Session session = await AuthService.Instance.CheckSessionAsync();

            if (!session.IsValid)
            {
                return(null);
            }

            Debug.WriteLine("fetching workflow action");
            var relativePath = $"{RestConstants.REST_ROOT}/{RestConstants.GET_ACTIONS_FOR_ENTITY}";
            var uri          = new Uri($"{session.ServerBaseUrl}{relativePath}");

            try
            {
                GetEntityActionsBody body = WorkflowUtil.GetWorkflowActionBody(workflow, session.SessionId);
                var           json        = JsonConvert.SerializeObject(body);
                StringContent content     = new StringContent(json, Encoding.UTF8, RestConstants.JSON_HEADER);

                var response = await httpClient.PostAsync(uri, content);

                if (response.IsSuccessStatusCode)
                {
                    var responseBody = await response.Content.ReadAsStringAsync();

                    var            resultWrapper = JsonConvert.DeserializeObject <GetActionsForAnyEntityResponse>(responseBody);
                    var            actions       = resultWrapper.GetActionsForAnyEntityResult;
                    WorkFlowAction newAction     = GetActionForWorkflow(actions, session);
                    Debug.WriteLine("fetched workflow action");
                    Debug.Write(action);
                    workflowActions.Add(workflow.ServiceCatalogId, newAction);
                    return(newAction);
                }
                else
                {
                    var responseBody = await response.Content.ReadAsStringAsync();

                    throw new Exception(responseBody);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(@"             ERROR {0}", ex.Message);
            }
            return(null);
        }
        private static WorkFlowAction GetActionForWorkflow(IEnumerable <BaseActionType> actions, Session session)
        {
            WorkFlowAction workflowAction = new WorkFlowAction();
            // try for flow:
            IEnumerable <BaseActionType> mainActions = actions.Where((action, i) => action.MvcActionHandler.HandlerName.Equals(HandlerNames.RUN_FLOW));

            if (mainActions.Count() > 0)
            {
                var    strings = from mainAction in mainActions select mainAction.ElementId;
                string flowId  = strings.First();
                workflowAction.RunFlowId = flowId;
                // TODO may not need the below, but leaving for now just in case:
                workflowAction.WebViewUrl = GetWorkflowActionFlowUrl(flowId, session);
            }

            // try for URL:
            IEnumerable <BaseActionType> urlActions = actions.Where((action, i) => action.MvcActionHandler.HandlerName.Equals(HandlerNames.OPEN_URL));

            workflowAction.WebViewUrl = urlActions.Count() > 0 ? urlActions.First().Url : "";

            return(workflowAction);
        }
        async Task <string> IDataStore <Workflow> .GetItemActionUrlAsync(Workflow item)
        {
            WorkFlowAction action = await GetWorkflowActionAsync(item);

            return(action.WebViewUrl);
        }
Esempio n. 4
0
        async void OnItemSelected(object sender, SelectedItemChangedEventArgs args)
        {
            if (!(args.SelectedItem is Models.Workflow workflow))
            {
                return;
            }

            viewModel.SetIsBusy(true); // show loader in case fetching action takes time

            if (Connectivity.NetworkAccess == NetworkAccess.Internet)
            {
                WorkFlowAction action = await(viewModel.DataStore as WorkflowStore).GetWorkflowActionAsync(workflow);

                Debug.WriteLine(action);

                // see if it's a flow we can run:
                if (action?.RunFlowId != null)
                {
                    FlowExecutionStateInstruction instruction = await FlowExecutionService.StartFlowWithData(action.RunFlowId, new DataPair[0]);

                    if (instruction == null)
                    {
                        Debug.WriteLine("Flow Execution Instruction was null");
                        DoneLaunching();
                        return;
                    }

                    if (FlowExecutionService.IsShowFormType(instruction))
                    {
                        // get the form JSON
                        StandAloneFormSessionInfo formModel = await FormService.GetFormSessionSurfaceJson(instruction);

                        // launch the view to render the form:
                        FormViewModel formViewModel;
                        if (workflow.CanRunOffline)
                        {
                            formViewModel = new FormViewModel(formModel, instruction, true, workflow.ServiceCatalogId);
                        }
                        else
                        {
                            formViewModel = new FormViewModel(formModel, instruction);
                        }
                        if (formModel == null)
                        {
                            await DisplayAlert("Error", "Problem loading form data.", "OK");

                            DoneLaunching();
                            return;
                        }
                        DoneLaunching();
                        await Navigation.PushAsync(new FormPage(formViewModel));
                    }
                    else
                    {
                        // throw a modal that the flow has been started
                        DoneLaunching();
                        await DisplayAlert("Workflow", $"Flow for {workflow.EntityName} has been started", "OK");
                    }

                    return;
                }
                // Otherwise, try to embed the result in a web-view

                var actionuUrl = action.WebViewUrl;

                viewModel.SetIsBusy(false);

                if (string.IsNullOrEmpty(actionuUrl))
                {
                    await DisplayAlert("Alert", "No valid action was found for this item", "OK");

                    return;
                }
                // Manually deselect item.
                WorkflowListView.SelectedItem = null;

                await Navigation.PushAsync(new DoWorkflowPage(workflow, actionuUrl));
            }
            else
            {
                // get the form JSON

                var formInfos = await OfflineService.Instance.GetStandAloneFormSessionInfoAsync(workflow.ServiceCatalogId);

                if (formInfos == null || formInfos.Count <= 0)
                {
                    await DisplayAlert("Error", "Problem loading form data.", "OK");

                    DoneLaunching();
                    return;
                }
                StandAloneFormSessionInfo formModel = formInfos[0];

                FlowExecutionStateInstruction instruction = new FlowExecutionStateInstruction
                {
                    FormTitle = workflow.EntityName
                };
                FormViewModel formViewModel;
                if (workflow.CanRunOffline)
                {
                    formViewModel = new FormViewModel(formModel, instruction, true, workflow.ServiceCatalogId);
                }
                else
                {
                    formViewModel = new FormViewModel(formModel, instruction);
                }

                DoneLaunching();
                await Navigation.PushAsync(new FormPage(formViewModel));
            }
        }