private NewPayLoad RunAction(NewPayLoad pl) { if (mServiceActions == null) { ScanServiceAndCache(); } Console.WriteLine(">>> Payload - Run Ginger Action"); string ActionID = pl.GetValueString(); Console.WriteLine("Received RunAction, ActionID - " + ActionID); ActionHandler AH = (from x in mServiceActions where x.ServiceActionId == ActionID select x).FirstOrDefault(); if (AH == null) { Console.WriteLine("Unknown ActionID to handle - " + ActionID); throw new Exception("Unknown ActionID to handle - " + ActionID); } //Convert the Payload to GingerAction NodeGingerAction nodeGingerAction = new NodeGingerAction(); AH.NodeGingerAction = nodeGingerAction; Console.WriteLine("Found Action Handler, setting parameters"); // setting parameters List <NewPayLoad> Params = pl.GetListPayLoad(); Console.WriteLine("Found " + Params.Count + " parameters"); ActionInputParams actionInputParams = new ActionInputParams(); foreach (NewPayLoad PLP in Params) { // we get Param name and value string Name = PLP.GetValueString(); object Value = PLP.GetValueByObjectType(); Console.WriteLine("Param " + Name + " = " + Value); ActionParam AP = actionInputParams[Name]; if (AP != null) { actionInputParams[Name].Value = Value; } else { Console.WriteLine("Cannot find input param - " + Name); } } // TODO: add hookd for before and after using interface //if (IBeforeAfterAction != null) // mService.BeforeRunAction(AH.GingerAction); // mService.RunAction(AH.GingerAction); ExecuteMethod(AH, actionInputParams, nodeGingerAction); NewPayLoad PLRC = CreateActionResult(nodeGingerAction.ExInfo, nodeGingerAction.Errors, nodeGingerAction.Output.OutputValues); return(PLRC); }
public static void ExecuteMethod(ActionHandler AH, ActionInputParams p, NodeGingerAction GA) { try { ParameterInfo[] PIs = AH.MethodInfo.GetParameters(); object[] parameters = new object[PIs.Count()]; int paramnum = 0; foreach (ParameterInfo PI in PIs) { if (paramnum == 0) { // verify param 0 is GA parameters[0] = GA; } else { object ActionParam = p[PI.Name]; if (ActionParam != null) { object val = null; // For each type we need to get the val correctly so the function will get it right if (PI.ParameterType == typeof(string)) { val = p[PI.Name].Value; } else if (PI.ParameterType.IsEnum) { if (p[PI.Name].Value != null) { val = Enum.Parse(PI.ParameterType, p[PI.Name].Value.ToString()); } else { // TODO: err or check if it is nullable enum } } else if (PI.ParameterType == typeof(Int32)) { val = p[PI.Name].GetValueAsInt(); } else if (PI.ParameterType.IsGenericType && PI.ParameterType.GetGenericTypeDefinition() == typeof(List <>)) { // This is List of objects Type itemType = PI.ParameterType.GetGenericArguments()[0]; // List item type Type listType = typeof(List <>).MakeGenericType(itemType); // List with the item type // val = Activator.CreateInstance(listType); val = JSONHelper.DeserializeObject(p[PI.Name].Value.ToString(), listType); } else { val = p[PI.Name].Value; } parameters[paramnum] = val; } else { //check if param is optional then ignore if (!PI.HasDefaultValue) { throw new Exception("GingerAction is Missing Param/Value for ActionParam - " + PI.Name); } else { // from here on all params are optional... } } } paramnum++; } AH.MethodInfo.Invoke(AH.Instance, parameters); // here is where we call the action directly with the relevant parameters } catch (Exception ex) { string message = ex.Message; if (ex.InnerException != null) { message += Environment.NewLine + ex.InnerException.Message; } GA.AddError("Error when trying to invoke: " + AH.ServiceActionId + " - " + message); } }
public static void RunServiceAction(ActionHandler AH, ActionInputParams p, NodeGingerAction GA) { try { ParameterInfo[] PIs = AH.MethodInfo.GetParameters(); object[] parameters = new object[PIs.Count()]; int paramnum = 0; foreach (ParameterInfo PI in PIs) { if (paramnum == 0) { // verify param 0 is GA parameters[0] = GA; } else { object ActionParam = p[PI.Name]; if (ActionParam != null) { object val = null; // For each type we need to get the val correctly so the function will get it right if (PI.ParameterType.IsEnum) { if (p[PI.Name].Value != null) { val = Enum.Parse(PI.ParameterType, p[PI.Name].Value.ToString()); } else { // TODO: err or check if it is nullable enum } } else if (PI.ParameterType == typeof(Int32)) { val = p[PI.Name].GetValueAsInt(); } //TODO: handle all types else { val = p[PI.Name].Value; } parameters[paramnum] = val; } else { //check if param is optional then ignore if (!PI.HasDefaultValue) { throw new Exception("GingerAction is Missing Param/Value for ActionParam - " + PI.Name); } else { // from here on all params are optional... } } } paramnum++; } AH.MethodInfo.Invoke(AH.Instance, parameters); // here is where we call the action directly with the relevant parameters } catch (Exception ex) { GA.AddError("Error when trying to invoke: " + AH.ServiceActionId + " - " + ex.Message); } }
private NewPayLoad RunAction(NewPayLoad pl) { ScanService(); Console.WriteLine(">>> Payload - Run Ginger Action"); string ActionID = pl.GetValueString(); Console.WriteLine("Received RunAction, ActionID - " + ActionID); ActionHandler AH = null; //if (mDriver != null) //{ // AH = (from x in this.mDriver.ActionHandlers where x.ID == ActionID select x).FirstOrDefault(); //} //else if (mService != null) //{ AH = (from x in mServiceActions where x.ServiceActionId == ActionID select x).FirstOrDefault(); //} if (AH == null) { Console.WriteLine("Unknown ActionID to handle - " + ActionID); throw new Exception("Unknown ActionID to handle - " + ActionID); } //Conver the Payload to GingerAction NodeGingerAction NGA = new NodeGingerAction(); AH.NodeGingerAction = NGA; //AH.GingerAction.ID = ActionID; // !!!!!!!!!!!!!!!!!!!!! why do we need to keep the ID twice !! Console.WriteLine("Found Action Handler, setting parameters"); List <NewPayLoad> Params = pl.GetListPayLoad(); Console.WriteLine("Found " + Params.Count + " parameters"); ActionInputParams actionInputParams = new ActionInputParams(); foreach (NewPayLoad PLP in Params) { // we get Param name and value string Name = PLP.GetValueString(); string Value = PLP.GetValueString(); Console.WriteLine("Param " + Name + " = " + Value); ActionParam AP = actionInputParams[Name]; if (AP != null) { actionInputParams[Name].Value = Value; } else { Console.WriteLine("Cannot find input param - " + Name); } } //Console.WriteLine("Setting params done"); //TODO: print to console: Running Action: GotoURL(URL="aaa"); !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // TODO: cache !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //if (mDriver != null) //{ // mDriver.BeforeRunAction(AH.GingerAction); // mDriver.RunAction(AH.GingerAction); // mDriver.AfterRunAction(AH.GingerAction); //} //else if (mService != null) //{ // mService.BeforeRunAction(AH.GingerAction); // mService.RunAction(AH.GingerAction); // GA.Output = new ActionOutput(); RunServiceAction(AH, actionInputParams, NGA); // mService.AfterRunAction(AH.GingerAction); //} // We send back only item which can change - ExInfo and Output values NewPayLoad PLRC = new NewPayLoad("ActionResult"); //TODO: use const PLRC.AddValue(NGA.ExInfo); PLRC.AddValue(NGA.Errors); PLRC.AddListPayLoad(GetOutpuValuesPayLoad(NGA.Output.Values)); PLRC.ClosePackage(); return(PLRC); }