public async Task <HttpResponseMessage> Delete() { var user = GetSessionUser(Request.Headers.GetCookies().FirstOrDefault()); List <NodeDto> successEntity = new List <NodeDto>(); //成功处理的Entity信息 JDBCEntity currentEntity = null; //当前处理的Entity信息 try { if (Request.RequestUri.PathAndQuery.ToLower().Equals("/entity/all"))//清空数据库 { await MyCoreApi.clearDb(); return(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent("Clear database successfully!") }); } Uri uri = ParseToQueryUri(Request.RequestUri); var nodes = await MyCoreApi.FindNodeByUriAsync(uri);//获取被删除entity var queryDict = ParseToQueryDictionary(Request.RequestUri); if ((!queryDict.ContainsKey("recursive") || queryDict["recursive"] == "false") && nodes.Count() > 1)//递归删除查询到的所有entity { throw new Exception("Please use [recursive=true] to delete queried entities!"); } if (nodes.Count() == 0)//entity不存在 { throw new Exception("No entity is found!"); } var deleteChildren = false; if (queryDict.ContainsKey("_recursive") && queryDict["_recursive"] == "true") { deleteChildren = true; } foreach (var entity in nodes) { currentEntity = entity; if (!await MyCoreApi.Authorization(currentEntity.Id, user, "1")) { throw new Exception("Not authorization!"); } await MyCoreApi.DeletAsync(entity.Id, deleteChildren); successEntity.Add(Mapper.Map <NodeDto>(currentEntity)); } 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> 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") }); } }