public async Task <HttpResponseMessage> Post() { var user = GetSessionUser(Request.Headers.GetCookies().FirstOrDefault()); //如果ExpressionRoot创建时间不是今天,则进行重置 if (BusinessConfig.ExpressionRoot.CreatedTime.DayOfYear != DateTime.Now.DayOfYear) { BusinessConfig.SetExpressionRoot(); } try { if (user == null) { throw new Exception("Not authorization!"); } NameValueCollection form = Request.Content.ReadAsFormDataAsync().Result; //get data var type = form.GetValues("type").FirstOrDefault(); var expression = form.GetValues("expression").FirstOrDefault(); if (string.IsNullOrWhiteSpace(type) || string.IsNullOrWhiteSpace(expression)) { throw new Exception("Arguments can not be empty!"); } expression = expression.Replace("\r\n", ""); var newExpressionName = Guid.NewGuid().ToString(); var newExpressionSignal = MyCoreApi.CreateSignal("Expression", newExpressionName); newExpressionSignal.AddExtraInformation("expression", expression); await MyCoreApi.AddOneToExperimentAsync(BusinessConfig.ExpressionRoot.Id, newExpressionSignal); return(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(SerializeObjectToString("/expression/" + newExpressionName), System.Text.Encoding.GetEncoding("UTF-8"), "application/json") }); } catch (Exception e) { var message = e.InnerException != null ? e.InnerException.Message : e.Message; return(new HttpResponseMessage { StatusCode = HttpStatusCode.Forbidden, Content = new StringContent(message, System.Text.Encoding.GetEncoding("UTF-8"), "application/json") }); } }
public async Task <HttpResponseMessage> Restore() { var user = GetSessionUser(Request.Headers.GetCookies().FirstOrDefault()); List <NodeDto> successEntity = new List <NodeDto>(); //成功处理的Entity信息 JDBCEntity currentEntity = null; //当前处理的Entity信息 try { if (!await MyCoreApi.Authorization(Guid.Empty, user, "1")) { throw new Exception("Not authorization!"); } await MyCoreApi.clearDb(true); MyCoreApi.CoreService.StorageEngine.Init(BusinessConfig.cassandraInit); JDBCEntity root = new Experiment("jtext"); root.SetUser("root"); await MyCoreApi.AddOneToExperimentAsync(Guid.Empty, root); successEntity.Add(Mapper.Map <NodeDto>(root)); for (var shot = 1; shot < 3; shot++) { JDBCEntity exp = new Experiment(shot.ToString()); await MyCoreApi.AddOneToExperimentAsync(root.Id, exp); successEntity.Add(Mapper.Map <NodeDto>(exp)); //normal data var waveSignal1 = (FixedIntervalWaveSignal)MyCoreApi.CreateSignal("FixedWave-int", "ws1", @"StartTime=-2&SampleInterval=0.5"); await MyCoreApi.AddOneToExperimentAsync(exp.Id, waveSignal1); await waveSignal1.PutDataAsync("", GenerateIntArray()); await waveSignal1.DisposeAsync(); successEntity.Add(Mapper.Map <NodeDto>(waveSignal1)); //big data var waveSignal2 = (FixedIntervalWaveSignal)MyCoreApi.CreateSignal("FixedWave-double", "ws2", @"StartTime=-2&SampleInterval=0.001"); waveSignal2.NumberOfSamples = 2000; await MyCoreApi.AddOneToExperimentAsync(exp.Id, waveSignal2); for (int i = 0; i < 20; i++) { var array = GenerateDoubleArray(2000); await waveSignal2.PutDataAsync("", array); } await waveSignal2.DisposeAsync(); successEntity.Add(Mapper.Map <NodeDto>(waveSignal2)); } return(new HttpResponseMessage { Content = new StringContent(SerializeObjectToString(successEntity), System.Text.Encoding.GetEncoding("UTF-8"), "application/json") }); } catch (Exception e) { if (currentEntity == null) { return(new HttpResponseMessage { StatusCode = HttpStatusCode.Forbidden, Content = new StringContent(e.InnerException != null ? e.InnerException.Message : e.Message) }); } var response = new ResponseEntityMessage { Fail = new { Description = e.InnerException != null ? e.InnerException.Message : e.Message, Id = currentEntity.Id, Path = currentEntity.Path }, Success = successEntity }; return(new HttpResponseMessage { StatusCode = HttpStatusCode.Forbidden, Content = new StringContent(SerializeObjectToString(response), System.Text.Encoding.GetEncoding("UTF-8"), "application/json") }); } }
public async Task <HttpResponseMessage> Insert() { var user = GetSessionUser(Request.Headers.GetCookies().FirstOrDefault()); List <NodeDto> successEntity = new List <NodeDto>(); //成功处理的Entity信息 JDBCEntity currentEntity = null; //当前处理的Entity信息 try { Uri uri = ParseToQueryUri(Request.RequestUri); List <JDBCEntity> parents = (await MyCoreApi.FindNodeByUriAsync(uri)).ToList(); //根据uri查找父节点 if (uri.PathAndQuery.Equals("/path/") || uri.PathAndQuery.Equals("/id/")) //添加根节点的情形 { parents.Add(new Experiment { Id = Guid.Empty }); } else if (parents.Count() == 0)//根节点不存在 { throw new Exception("Parent node not exist!"); } NameValueCollection form = Request.Content.ReadAsFormDataAsync().Result; JDBCEntity newNode = null; var type = GetValueFromForm(form, "type"); var name = GetValueFromForm(form, "name"); var extras = GetValuesFromForm(form, "extra[]", true); if (string.IsNullOrWhiteSpace(name)) { throw new Exception("Entity name can not be empty!"); } switch (type.ToLower()) { case "experiment": { newNode = new Experiment(name); } break; case "signal": { var init = GetValueFromForm(form, "init"); var datatype = GetValueFromForm(form, "datatype"); newNode = MyCoreApi.CreateSignal(datatype, name, init.Replace(";", "&").Replace(",", "&").Trim("'\"()[]{}".ToCharArray())); } break; default: throw new Exception("Entity type is not supported!"); } if (extras != null)//添加ExtraInformation { foreach (var extra in extras) { var index = extra.IndexOf(":::"); if (index < 0) { return(new HttpResponseMessage(HttpStatusCode.Forbidden)); } var key = extra.Substring(0, index); var value = extra.Substring(index + 3); if (key.Equals("") || value.Equals("") || newNode.IfExtraInformationContainsKey(key)) { return(new HttpResponseMessage(HttpStatusCode.Forbidden)); } newNode.AddExtraInformation(key, value); } } if (newNode == null) { return(new HttpResponseMessage { StatusCode = HttpStatusCode.Forbidden, Content = new StringContent("Failed to create new node!") }); } foreach (var parent in parents) { if (!await MyCoreApi.Authorization(parent.Id, user, "1")) { throw new Exception("Not authorization!"); } currentEntity = parent; newNode.Id = Guid.NewGuid(); newNode.SetUser(user.UserName); var node = await MyCoreApi.AddOneToExperimentAsync(parent.Id, newNode); //添加signal successEntity.Add(Mapper.Map <NodeDto>(node)); //保存处理结果 } return(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(SerializeObjectToString(successEntity), System.Text.Encoding.GetEncoding("UTF-8"), "application/json") }); } catch (Exception e) { if (currentEntity == null) { return(new HttpResponseMessage { StatusCode = HttpStatusCode.Forbidden, Content = new StringContent(e.Message) }); } var response = new ResponseEntityMessage { Fail = new { Description = e.InnerException != null ? e.InnerException.Message : e.Message, Id = currentEntity.Id, Path = currentEntity.Path }, Success = successEntity }; return(new HttpResponseMessage { StatusCode = HttpStatusCode.Forbidden, Content = new StringContent(SerializeObjectToString(response), System.Text.Encoding.GetEncoding("UTF-8"), "application/json") }); } }