private ActionResultDto ChangeKarma(ITransactionScope transactionScope, ActionDto actionDto, int value) { var keyParam = GetParameter(actionDto, ActionGroup, "Key"); var key = keyParam.Value; var karma = _karmaRepository.GetByKey(transactionScope, key); if (karma == null) { karma = _karmaRepository.Create(); karma.KarmaKey = key; karma.KarmaValue = 0; } karma.KarmaValue = karma.KarmaValue + value; var newValue = karma.KarmaValue; if (newValue == 0) _karmaRepository.Delete(transactionScope, karma); else _karmaRepository.Save(transactionScope, karma); return new ActionResultDto() {Data = Convert.ToString(newValue)}; }
private ActionResultDto CreateFile(ActionDto actionDto) { var fileParameter = GetParameter(actionDto, "File", "Path"); File.Create(fileParameter.Value); return new ActionResultDto(); }
private ActionResultDto CopyFile(ActionDto actionDto) { var targetPathParameter = GetParameter(actionDto, "File", "TargetPath"); var sourePathParameter = GetParameter(actionDto, "File", "SourcePath"); File.Copy(sourePathParameter.Value, targetPathParameter.Value); return new ActionResultDto(); }
protected static ParameterDto GetParameter(ActionDto actionDto, string category, string name) { var result = actionDto.Parameters.Where(p => p.Category == category && p.Name == name).SingleOrDefault(); if (result== null) { throw new ParameterNotFoundException(); } return result; }
public string GetStats() { var actionDto = new ActionDto(); actionDto.ActionGroup = "Karma"; actionDto.Action = "Stats"; var result = ActionService.Execute(actionDto); return result.Data; }
protected override ActionResultDto ExecuteAction(ITransactionScope transactionScope, ActionDto actionDto) { var clientName = actionDto.Parameters.Where(p => p.Name == "Client").Single().Value; var client = ClientRepository.GetClientsByFilterCriteria(transactionScope, new ClientFilterCriteria() {Name = clientName}).SingleOrDefault(); var restClient = new JarvisRestClient(Log); restClient.BaseUrl = client.Hostname; return restClient.Execute<ResultDto<ActionResultDto>>(new ActionExecuteRequest(){ActionDto = actionDto}, "POST").Result; }
public void SendMessage(string hostname, string channel, string message) { var actionDto = new ActionDto(); actionDto.ActionGroup = "Irc"; actionDto.Action = "SendMessage"; actionDto.Parameters.Add(new ParameterDto(){Category = "Irc", Name = "Client", Value = hostname}); actionDto.Parameters.Add(new ParameterDto(){Category = "Irc", Name = "Channel", Value = channel}); actionDto.Parameters.Add(new ParameterDto(){Category = "Irc", Name = "Message", Value = message}); ActionService.Execute(actionDto); }
private int SendKarmaKeyAction(string action, string key) { var actionDto = new ActionDto(); actionDto.ActionGroup = "Karma"; actionDto.Action = action; actionDto.Parameters.Add(new ParameterDto() {Category = "Karma", Name = "Key", Value = key}); var result = ActionService.Execute(actionDto); return Convert.ToInt32(result.Data); }
public bool CanExecute(ITransactionScope transactionScope, ActionDto actionDto) { try { return CanExecuteAction(transactionScope, actionDto); } catch (Exception e) { Log.ErrorFormat("Error at checking executing at server action {0}: {1}", this.GetType().Name, e); } return false; }
public ActionResultDto Execute(ITransactionScope transactionScope, ActionDto actionDto) { try { return ExecuteAction(transactionScope, actionDto); } catch (Exception e) { Log.ErrorFormat("Error at executing server action {0}: {1}", this.GetType().Name, e); } return null; }
public ActionResultDto Execute(ITransactionScope transactionScope, ActionDto actionDto) { _log.InfoFormat("Executing {0}.{1}", actionDto.ActionGroup, actionDto.Action); var serverAction = _actionRegistry.GetActionHandler(actionDto.ActionGroup); if (serverAction.CanExecute(transactionScope, actionDto)) return serverAction.Execute(transactionScope, actionDto); _log.InfoFormat("ActionHandler {0} can not execute action {1}", serverAction.GetType().Name, actionDto.Action); return null; }
public override ActionResultDto DoAction(ActionDto actionDto) { switch (actionDto.Action) { case "SendMessage": { var channel = GetParameter(actionDto, "Irc", "Channel"); var message = GetParameter(actionDto, "Irc", "Message"); return SendMessage(channel.Value, message.Value); } } throw new ArgumentOutOfRangeException(); }
protected override ActionResultDto ExecuteAction(ITransactionScope transactionScope, ActionDto actionDto) { switch (actionDto.Action) { case "Increase": return ChangeKarma(transactionScope, actionDto, 1); case "Decrease": return ChangeKarma(transactionScope, actionDto, -1); case "GetKarma": return GetKarma(transactionScope); case "Stats": return GetStats(transactionScope); } throw new ArgumentOutOfRangeException(); }
protected override ActionResultDto ExecuteAction(ITransactionScope transactionScope, ActionDto actionDto) { if (actionDto.Action == "Move" || actionDto.Action == "Copy") { var sourceClientParameter = GetParameter(actionDto, "File", "SourceClient"); var targetClientParameter = GetParameter(actionDto, "File", "TargetClient"); if (sourceClientParameter.Value == targetClientParameter.Value) { return ExecuteFileAction(transactionScope, actionDto); } var sourcePathParameter = GetParameter(actionDto, "File", "SourcePath"); var targetPathParameter = GetParameter(actionDto, "File", "TargetPath"); var readDataAction = new ActionDto() { ActionGroup = "File", Action = "Read", Parameters = new List<ParameterDto>() { new ParameterDto() { Category = "File", Name = "Path", Value = sourcePathParameter.Value } } }; var readDataResult = ExecuteFileAction(transactionScope, readDataAction); var writeDataAction = new ActionDto() { ActionGroup = "File", Action = "Write", Parameters = new List<ParameterDto>() { new ParameterDto() { Category = "File", Name = "Path", Value = targetPathParameter.Value }, new ParameterDto() {Category = "File", Name = "Data", Value = readDataResult.Data} } }; ExecuteFileAction(transactionScope, writeDataAction); return new ActionResultDto(); } return ExecuteFileAction(transactionScope, actionDto); }
public override ActionResultDto DoAction(ActionDto actionDto) { switch (actionDto.Action) { case FileActionConstants.Action_Delete: return DeleteFile(actionDto); case FileActionConstants.Action_Copy: return CopyFile(actionDto); case FileActionConstants.Action_Create: return CreateFile(actionDto); case FileActionConstants.Action_Move: return MoveFile(actionDto); case FileActionConstants.Action_Read: return ReadFile(actionDto); case FileActionConstants.Action_Write: return WriteFile(actionDto); default: throw new ArgumentOutOfRangeException(); } }
private ActionResultDto WriteFile(ActionDto actionDto) { var fileParameter = GetParameter(actionDto, "File", "Path"); var dataParameter = GetParameter(actionDto, "File", "Data"); var data = JsonDeserializer.Deserialize<byte[]>(dataParameter.Value); File.WriteAllBytes(fileParameter.Value, data); return new ActionResultDto(); }
protected abstract ActionResultDto ExecuteAction(ITransactionScope transactionScope, ActionDto actionDto);
protected abstract bool CanExecuteAction(ITransactionScope transactionScope, ActionDto actionDto);
private ActionDto CreateActionDto(string sourceClientName, string sourceFilePath, string targetClientName, string targetFilePath, string action) { var actionDto = new ActionDto(); actionDto.ActionGroup = FileActionConstants.ActionGroup; actionDto.Action = action; actionDto.Parameters.Add(new ParameterDto() { Category = "File", Name = "SourceClient", Value = sourceClientName }); actionDto.Parameters.Add(new ParameterDto() { Category = "File", Name = "SourcePath", Value = sourceFilePath }); actionDto.Parameters.Add(new ParameterDto() { Category = "File", Name = "TargetClient", Value = targetClientName }); actionDto.Parameters.Add(new ParameterDto() { Category = "File", Name = "TargetPath", Value = targetFilePath }); return actionDto; }
private static ActionDto CreateActionDto(string clientName, string filePath, string action) { var actionDto = new ActionDto(); actionDto.ActionGroup = FileActionConstants.ActionGroup; actionDto.Action = action; actionDto.Parameters.Add(new ParameterDto() { Category = "File", Name = "Path", Value = filePath }); actionDto.Parameters.Add(new ParameterDto() { Category = "File", Name = "Client", Value = clientName }); return actionDto; }
public override bool CanHandleAction(ActionDto actionDto) { return actionDto.ActionGroup == "Irc"; }
protected override bool CanExecuteAction(ITransactionScope transactionScope, ActionDto actionDto) { return actionDto.ActionGroup == "Irc"; }
public virtual bool CanHandleAction(ActionDto actionDto) { return true; }
public abstract ActionResultDto DoAction(ActionDto actionDto);
private ActionResultDto ReadFile(ActionDto actionDto) { var fileParameter = GetParameter(actionDto, "File", "Path"); var data = File.ReadAllBytes(fileParameter.Value); return new ActionResultDto() { Data = JsonSerializer.Serialize(data)}; }
public ActionResultDto Execute(ActionDto actionDto) { var restResponse = JarvisRestClient.Execute<ResultDto<ActionResultDto>>(new ActionExecuteRequest() {ActionDto = actionDto}, "POST"); JarvisRestClient.CheckForException(restResponse.ResponseStatus); return restResponse.Result; }
public override bool CanHandleAction(ActionDto actionDto) { return actionDto.ActionGroup == FileActionConstants.ActionGroup; }