Beispiel #1
0
        public async Task<PersistentObject> ExecuteActionAsync(string action, PersistentObject parent = null, Query query = null, QueryResultItem[] selectedItems = null, Dictionary<string, string> parameters = null, bool skipHooks = false)
        {
            var isObjectAction = action.StartsWith("PersistentObject.") || query == null;

            try
            {
                IsBusy = true;

                if (!skipHooks)
                {
                    string fullTypeName;
                    if (!isObjectAction)
                    {
                        query.SetNotification(null);
                        fullTypeName = query.PersistentObject.FullTypeName;
                    }
                    else
                    {
                        parent.SetNotification(null);
                        fullTypeName = parent.FullTypeName;
                    }

                    var args = new ExecuteActionArgs(action) { Parameters = parameters, PersistentObject = parent, Query = query, SelectedItems = selectedItems };
                    await Hooks.OnAction(args);

                    if (args.IsHandled)
                        return args.Result;

                    args.IsHandled = ClientActions.Get(fullTypeName).OnAction(args);
                    if (args.IsHandled)
                        return args.Result;
                }

                var data = CreateData();
                data["action"] = action;
                data["query"] = query != null ? query.ToServiceObject() : null;
                data["parent"] = parent != null ? parent.ToServiceObject() : null;
                data["selectedItems"] = selectedItems != null ? JArray.FromObject(selectedItems.Select(i => i != null ? i.ToServiceObject() : null)) : null;
                data["parameters"] = parameters != null ? JToken.FromObject(parameters) : null;

                var response = await PostAsync("ExecuteAction", data);

                var ex = (string)response["exception"] ?? (string)response["ExceptionMessage"];
                if (!string.IsNullOrEmpty(ex))
                {
                    if (isObjectAction)
                        parent.SetNotification(ex);
                    else
                        query.SetNotification(ex);

                    return null;
                }

                AuthToken = (string)response["authToken"];
                await UpdateSession(response);

                var jPo = (JObject)response["result"];
                return jPo != null ? Hooks.OnConstruct(jPo) : null;
            }
            catch (Exception e)
            {
                if (isObjectAction)
                    parent.SetNotification(e.Message);
                else
                    query.SetNotification(e.Message);

                return null;
            }
            finally
            {
                IsBusy = false;
            }
        }