示例#1
0
        public HttpResponseMessage GetTree(string node)
        {
            //  string nodeRaw = context.Request.Form["idNode"]; // IPA@123 RUBR@123 [1]IPA@123  [3]RUBR@435653
            //   string nodeRaw = context.Request.Params["node"];
            string nodeP = node;

            Models.TreeModel       model = new Models.TreeModel();
            List <Models.TreeNode> list  = new List <Models.TreeNode>();
            string catalog      = null;
            string virtualPager = null;
            bool   virtualNode  = false;

            if (node.Contains(']'))
            {
                nodeP        = node.Substring(node.LastIndexOf(']') + 1);
                virtualPager = node.Substring(1, node.LastIndexOf(']') - 1);
                virtualNode  = true;
            }
            Int64?nodeId = null;

            if (nodeP.Contains('@'))
            {
                nodeId  = Int64.Parse(nodeP.Split('@')[1]);
                catalog = nodeP.Split('@')[0];
            }


            if (HttpContext.Current.Application["tree"] == null)
            {
                InitializeTreeStore();
            }

            Dictionary <string, SimpleTreeItem> xDoc = (Dictionary <string, SimpleTreeItem>)HttpContext.Current.Application["tree"];

            // StringBuilder sb = null;

            if (nodeP == null)
            {
                throw new ArgumentNullException("nodo richiesto non valido");
            }

            List <SimpleTreeItem> ll = null;

            try
            {
                //provo prima a prendere gli item dalla memoria

                if (virtualNode)
                {
                    ll = xDoc.Where(e => e.Value.ExtendedPadre.Equals(nodeP)).Select(e => e.Value).ToList();
                    ll.Sort((x, y) => string.Compare(x.Text, y.Text));
                    if (ll.Count < int.Parse(virtualPager) + 100)
                    {
                        ll = ll.GetRange(int.Parse(virtualPager), ll.Count - int.Parse(virtualPager));
                    }
                    else
                    {
                        ll = ll.GetRange(int.Parse(virtualPager), 100);
                    }
                }
                else if (nodeId.HasValue)
                {
                    ll = xDoc.Where(e => e.Value.ExtendedPadre.Equals(nodeP)).Select(e => e.Value).ToList();
                }
                else
                {
                    ll = xDoc.Where(e => string.IsNullOrEmpty(e.Value.Padre)).Select(e => e.Value).ToList();
                }

                //se non trova figli prova a caricarli dalla banca dati
                if (ll.Count() == 0)
                {
                    ContattoService contattoService = new ContattoService();
                    Dictionary <string, SimpleTreeItem> newChildren = (Dictionary <string, SimpleTreeItem>)contattoService.LoadRubricaIndex(nodeId.Value, (IndexedCatalogs)Enum.Parse(typeof(IndexedCatalogs), catalog), 2);
                    if (newChildren != null && newChildren.Count > 1)
                    {
                        AppendChildren(newChildren);
                        ll = xDoc.Where(e => e.Value.ExtendedPadre.Equals(nodeP)).Select(e => e.Value).ToList();
                    }
                }

                if (ll.Count() > 100)
                {
                    ll.Sort((x, y) => string.Compare(x.Text, y.Text));
                    int nn     = ll.Count;
                    int gruppi = nn / 100;
                    model.totale = (nn - 1).ToString();
                    for (int i = 0; i < nn - 1; i = i + 100)
                    {
                        Models.TreeNode nodeTree = new Models.TreeNode()
                        {
                            itemId = ll[i].ExtendedValue,
                            text   = ll[i].Text,
                            cls    = ll[i].SubType
                        };
                        list.Add(nodeTree);
                        //if (sb == null)
                        //{
                        //    sb = new StringBuilder();
                        //    sb.Append("{\"Items\":[");
                        //    sb.Append(string.Format(JSON_ELEMENT_FIRST, "Da:" + ll[i].Text, "[" + i.ToString() + "]" + node, "GRP"));
                        //}
                        //else
                        //    sb.Append(string.Format(JSON_ELEMENT, "Da:" + ll[i].Text, "[" + i.ToString() + "]" + node, "GRP"));
                    }
                }
                else if (ll != null && ll.Count() > 0)
                {
                    ll.Sort((x, y) => string.Compare(x.Text, y.Text));
                    model.totale = ll.Count.ToString();
                    foreach (SimpleTreeItem xnode in ll)
                    {
                        Models.TreeNode nodeTree = new Models.TreeNode()
                        {
                            itemId = xnode.ExtendedValue,
                            text   = xnode.Text,
                            cls    = xnode.SubType
                        };
                        list.Add(nodeTree);
                        //if (sb == null)
                        //{
                        //    sb = new StringBuilder();
                        //    sb.Append("{\"Items\":[");
                        //    sb.Append(string.Format(JSON_ELEMENT_FIRST, xnode.Text, xnode.ExtendedValue, xnode.SubType));
                        //}
                        //else
                        //    sb.Append(string.Format(JSON_ELEMENT, xnode.Text, xnode.ExtendedValue, xnode.SubType));
                    }
                }

                //if (sb != null)
                //{
                //    sb.Append("]}");
                model.Items = list;
                return(this.Request.CreateResponse <Models.TreeModel>(HttpStatusCode.OK, model));
                //}
                //else
                //{
                //    return this.Request.CreateResponse<string>(HttpStatusCode.OK, string.Empty);
                //}
            }
            catch { throw new Exception("Retry Please!!"); }
        }
示例#2
0
        public static FamilyTree Load(Stream fileStream)
        {
            var           familyTree = new FamilyTree();
            List <String> lines      = new List <String>();

            var          encoding = Encoding.UTF8;
            StreamReader reader   = new StreamReader(fileStream, encoding);

            do
            {
                string textLine = reader.ReadLine();
                lines.Add(textLine);
            } while (reader.Peek() != -1);
            reader.Close();

            var root          = new List <Models.TreeNode>();
            var childrenLists = Enumerable.Range(1, 11).Select(i => new List <Models.TreeNode>()).ToList();

            childrenLists[0] = root;

            foreach (var line in lines)
            {
                int newlevel;
                if (line.Length > 2 && line[0] >= '0' && line[0] <= '9' && line[1] == ' ')
                {
                    newlevel = (line[0] - '0');
                }
                else
                {
                    throw new Exception("Unable to parse level");
                }

                var newNode = new Models.TreeNode(line.Trim());
                familyTree.FileContents += String.Concat(line, "\r\n");

                childrenLists[newlevel].Add(newNode);
                childrenLists[newlevel + 1] = newNode.Children;
            }

            //The tree is done
            var tasks = new List <Task>();

            if (root.Count < 2)
            {
                throw new Exception("File must contain Header and Trailer");
            }

            //Get header
            var node = root[0];

            if (!node.RawLine.Equals("0 HEAD"))
            {
                throw new Exception("Header not found");
            }
            //tasks.Add(new Task(() => { ReadHeader(gedcom, gedcom.Header, node.Children); }));
            root.Remove(node);

            //Get trailer
            node = root[root.Count - 1];
            if (!node.RawLine.Equals("0 TRLR"))
            {
                throw new Exception("Trailer not found");
            }
            root.Remove(node);

            //all other have an Id
            foreach (var levelZeroNode in root)
            {
                //Must be always 3 parameter 0 / Id / Type

                //Get Id
                var position = levelZeroNode.RawLine.IndexOf(' ', 2);
                if (position < 0)
                {
                    throw new Exception("Root element malformed");
                }
                var id = levelZeroNode.RawLine.Substring(2, position - 2);

                //Get Tag
                var tag = levelZeroNode.RawLine.Substring(3 + id.Length);

                if (tag.Equals("INDI"))
                {
                    var individual = new Individual(id);
                    tasks.Add(new Task(() => { ReadIndividualRecord(familyTree, individual, levelZeroNode.Children); }));
                    if (familyTree.Individuals == null)
                    {
                        familyTree.Individuals = new List <Individual>();
                    }
                    familyTree.Individuals.Add(individual);
                }
                else if (tag.Equals("FAM"))
                {
                    var family = new Family(id);
                    tasks.Add(new Task(() => { ReadFamilyRecord(familyTree, family, levelZeroNode.Children); }));
                    if (familyTree.Families == null)
                    {
                        familyTree.Families = new List <Family>();
                    }
                    familyTree.Families.Add(family);
                }
                //else if (tag.Equals("SOUR"))
                //{
                //    var source = new Source(id);
                //    tasks.Add(new Task(() => { ParseSource(gedcom, source, levelZeroNode.Children); }));
                //    gedcom.Sources.Add(source);
                //}
                //else if (tag.Equals("SUBM"))
                //{
                //    var submiter = new Submiter(id);
                //    tasks.Add(new Task(() => { ParseSubmiter(gedcom, submiter, levelZeroNode.Children); }));
                //    gedcom.Submiters.Add(submiter);
                //}
                //else if (tag.Equals("REPO"))
                //{
                //    var repository = new Repository(id);
                //    tasks.Add(new Task(() => { ParseRepository(gedcom, repository, levelZeroNode.Children); }));
                //    gedcom.Repositories.Add(repository);
                //}
                //else if (tag.Equals("NOTE"))
                //{
                //    var note = new Note(id);
                //    tasks.Add(new Task(() => { ParseNote(gedcom, note, levelZeroNode.Children); }));
                //    gedcom.Notes.Add(note);
                //}
                else
                {
                    //Console.WriteLine(tag);
                }
            }

            //Run all tasks
            foreach (var task in tasks)
            {
                task.Start();
            }

            //Wait all thread
            Task.WaitAll(tasks.ToArray());

            return(familyTree);
        }