public ActionResult Edit(GraphInputModel input)
 {
     input.Id = input.Id.Replace("graphs/", string.Empty);
     if (input.Submit.ToLower() == "delete")
     {
         return RedirectToAction("Delete", "Home", new { deleteme = input.Id });
     }
     Persist(input, null);
     return RedirectToAction("Edit", "Home", new { input.Id });
 }
        private string Persist(HttpPostedFileBase file, string name, string delimiter, string editid)
        {
            var input = new GraphInputModel
                            {
                                Id = string.IsNullOrEmpty(editid) ? GuidUtil.GenerateComb().ToString() : editid,
                                Name = name,
                                Delimiter = delimiter
                            };

            using (var csvfile = new StreamReader(file.InputStream))
            {
                var line = string.Empty;
                while ((line = csvfile.ReadLine()) != null)
                {
                    var columns = new List<string>(line.Split(','));
                    var itemid = columns[0].Replace(" ", "");
                    var itemname = columns[0];
                    var itemtype = columns[1];
                    var description = columns[2];
                    var node = new NodeLineItem { Description = description, Id = itemid, ItemType = itemtype, Name = itemname };
                    if (columns[3].Contains(delimiter))
                    {
                        var delimiterlist = new List<string> { delimiter };
                        columns =
                            new List<string>(columns[3].Split(delimiterlist.ToArray(),
                                                              StringSplitOptions.RemoveEmptyEntries));
                    }
                    else
                    {
                        columns.RemoveRange(0, 3);
                    }
                    node.Other = string.Join(",", columns);
                    input.Nodes.Add(node);
                }
                file.InputStream.Position = 0;
                var id = Persist(input, file);
                return id;
            }
        }
        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;
        }