public ActionResult DemoGet(string id) { var graph = new Graph { nodes = new List<Node> { new Node { id = "node0", name = "node0 name", data = { {"$dim", "16"}, {"some other key", "some other value"} }, adjacencies = { new Adjacency { nodeTo = "node1", data = { {"weight", "3"} } } } }, new Node { id = "node1", name = "node1 name", data = { {"$dim", "16"}, {"some other key", "some other value"} }, adjacencies = { new Adjacency { nodeTo = "node0", data = { {"weight", "3"} } } } } } }; return Json(graph, JsonRequestBehavior.AllowGet); }
private string Persist(GraphInputModel input, HttpPostedFileBase file) { var filecontents = new StringBuilder(); var exists = true; var graph = Session.Load<Graph>("graphs/" + input.Id); if (graph == null) { graph = new Graph(); exists = false; } graph.Id = "graphs/" + input.Id; graph.Name = input.Name; graph.LastModified = DateTime.Now; foreach (var lineitem in input.Nodes) { var id = lineitem.Id.Trim(); var name = lineitem.Name.Trim(); var description = string.IsNullOrEmpty(lineitem.Description) ? string.Empty : lineitem.Description.Trim(); var itemtype = string.IsNullOrEmpty(lineitem.ItemType) ? string.Empty : lineitem.ItemType.Trim(); var otherlist = string.IsNullOrEmpty(lineitem.Other) ? new List<string>() : new List<string>( lineitem.Other.Trim().Split(',')); var data = new Dictionary<string, object>(); filecontents.Append(string.Format("{0},{1},{2},{3}{4}", name, itemtype, description, string.Join(",", otherlist.Select(e => e.Trim())), Environment.NewLine)); var node = graph.nodes.SingleOrDefault(e => e.id == id); if (node == null) { node = new Node { id = name.Replace(" ", ""), name = name, data = data }; graph.nodes.Add(node); } else { node.name = name; } if (!node.data.ContainsKey("itemtype")) { node.data.Add("itemtype", itemtype); } node.data["itemtype"] = itemtype; if (!node.data.ContainsKey("description")) { node.data.Add("description", description); } node.data["description"] = description; if (!node.data.ContainsKey("other")) { node.data.Add("other", string.Join(",", otherlist)); } else { var existingotherlist = node.data["other"].ToString().Split(','); var union = existingotherlist.Union(otherlist); node.data["other"] = string.Join(",", union); } var nodeadjacency = new Adjacency { nodeTo = node.id, data = new Dictionary<string, object> { { "adjacentname", name } } }; var columns = new List<string>(lineitem.Other.Split(',')); for (var x = 0; x < columns.Count(); x++) { var adjacentnode = graph.nodes.SingleOrDefault(e => e.id == columns[x].Replace(" ", "")); if (adjacentnode == null) { adjacentnode = new Node { id = columns[x].Replace(" ", ""), name = columns[x], data = new Dictionary<string, object> { {"itemtype", string.Empty}, {"description", string.Empty}, {"other", node.name} } }; graph.nodes.Add(adjacentnode); } var adjacentotherlist = adjacentnode.data["other"].ToString().Split(','); if (!adjacentotherlist.Contains(node.name)) { adjacentnode.data["other"] = adjacentnode.data["other"] + "," + node.name; } if (adjacentnode.adjacencies.SingleOrDefault(e => e.nodeTo == nodeadjacency.nodeTo) == null) { adjacentnode.adjacencies.Add(nodeadjacency); } if (node.adjacencies.Where(e => e.nodeTo == adjacentnode.id).Count() == 0) { var adjacentnodeadjacency = new Adjacency { nodeTo = adjacentnode.id, data = new Dictionary<string, object> { { "adjacentname", columns[x] } } }; node.adjacencies.Add(adjacentnodeadjacency); } } } Session.Store(graph); byte[] bytearray; var filename = string.Empty; var filemimetype = string.Empty; if (exists) { var attachment = MvcApplication.DocumentStore.DatabaseCommands.GetAttachment("graphs/" + input.Id); filemimetype = attachment.Metadata["ContentType"].ToString(); filename = attachment.Metadata["FileName"].ToString(); MvcApplication.DocumentStore.DatabaseCommands.DeleteAttachment("graphs/" + input.Id, null); } if (file == null) { bytearray = System.Text.Encoding.UTF8.GetBytes(filecontents.ToString()); filename = string.IsNullOrEmpty(filename) ? input.Name.Trim() + ".txt" : filename; filemimetype = string.IsNullOrEmpty(filemimetype) ? "text/plain" : filemimetype; } else { bytearray = ReadToEnd(file.InputStream); filename = file.FileName; filemimetype = file.ContentType; } using (var mem = new MemoryStream(bytearray)) { MvcApplication.DocumentStore.DatabaseCommands.PutAttachment("graphs/" + input.Id, null, mem, new RavenJObject { {"FileName", filename}, {"ContentType", filemimetype} }); } return input.Id; }