public static Hierarchy NewHierarchy(Tagset tagset)
 {
     if (tagset == null) { throw new Exception("Given tagset was null."); }
     return new Hierarchy()
     {
         Name = tagset.Name,
         Tagset = tagset,
         Nodes = new List<Node>()
     };
 }
 public static Tag NewTag(string name, Tagset tagset)
 {
     if (name == null) { throw new Exception("Given name was null."); }
     if (tagset == null) { throw new Exception("Given tagset was null."); }
     return new Tag()
     {
         Name = name,
         Tagset = tagset,
         ObjectTagRelations = new List<ObjectTagRelation>()
     };
 }
示例#3
0
        /// <summary>
        /// Inserts hierarchies
        /// </summary>
        /// <param name="pathToHierarchiesFile"></param>
        private static void InsertHierarchies(string pathToHierarchiesFile)
        {
            Console.WriteLine("Inserting Hierarchies");
            string[] allLines = File.ReadAllLines(pathToHierarchiesFile)
                                .Skip(1) //Skipping the first line cause it's documentation
                                .ToArray();

            int lineCount = 1;

            foreach (string line in allLines)
            {
                Console.WriteLine("Inserting line number: " + lineCount);

                //File format: TagsetName:HierarchyName:ParrentTag:ChildTag:ChildTag:ChildTag:(...)
                string[] split         = line.Split(":");
                string   tagsetName    = split[0];
                string   hierarchyName = split[1];
                string   parentTagName = split[2];

                using (var context = new ObjectContext())
                {
                    //Finding tagset:
                    Tagset tagsetFromDb = context.Tagsets
                                          .Where(ts => ts.Name.Equals(tagsetName))
                                          .Include(ts => ts.Tags)
                                          .Include(ts => ts.Hierarchies)
                                          .FirstOrDefault();

                    //See if hierarchy exists:
                    Hierarchy hierarchyFromDb = context.Hierarchies
                                                .Include(h => h.Nodes)
                                                .Where(h => h.Name.Equals(hierarchyName))
                                                .FirstOrDefault();

                    //If hierarchyFromDb does not exist, create it:
                    if (hierarchyFromDb == null)
                    {
                        hierarchyFromDb = DomainClassFactory.NewHierarchy(tagsetFromDb);
                        tagsetFromDb.Hierarchies.Add(hierarchyFromDb);
                        //hierarchyFromDb.Tagset = tagsetFromDb;
                        context.Update(tagsetFromDb);
                        context.Update(hierarchyFromDb);
                        context.SaveChanges();
                    }

                    //Finding parent tag:
                    Tag parentTagFromDb = context.Tags
                                          .Where(t => t.TagsetId == tagsetFromDb.Id && t.Name.Equals(parentTagName))
                                          .FirstOrDefault();

                    //If parentTag does not exist, create it:
                    if (parentTagFromDb == null)
                    {
                        parentTagFromDb = DomainClassFactory.NewTag(parentTagName, tagsetFromDb);
                        tagsetFromDb.Tags.Add(parentTagFromDb);
                        context.Tags.Add(parentTagFromDb);
                        context.Update(tagsetFromDb);
                        context.SaveChanges();
                    }

                    //Finding parent node:
                    Node parentNodeFromDb = context.Nodes
                                            .Include(n => n.Children)
                                            .Where(n => n.HierarchyId == hierarchyFromDb.Id && n.TagId == parentTagFromDb.Id)
                                            .FirstOrDefault();

                    //If parent node does not exist, create it:
                    if (parentNodeFromDb == null)
                    {
                        //Probably root node:
                        parentNodeFromDb = DomainClassFactory.NewNode(parentTagFromDb, hierarchyFromDb);
                        hierarchyFromDb.Nodes.Add(parentNodeFromDb);
                        context.Update(hierarchyFromDb);
                        context.SaveChanges();

                        hierarchyFromDb.RootNodeId = parentNodeFromDb.Id;
                        context.Update(hierarchyFromDb);
                        context.SaveChanges();
                    }

                    //Adding child nodes:
                    for (int i = 3; i < split.Length; i++)
                    {
                        string childTagName = split[i];

                        Tag childTagFromDb = context.Tags
                                             .Where(t => t.TagsetId == tagsetFromDb.Id && t.Name.Equals(childTagName))
                                             .FirstOrDefault();

                        //If child tag does not exist, create it:
                        if (childTagFromDb == null)
                        {
                            childTagFromDb        = DomainClassFactory.NewTag(childTagName, tagsetFromDb);
                            childTagFromDb.Tagset = tagsetFromDb;
                            tagsetFromDb.Tags.Add(childTagFromDb);
                            context.Update(tagsetFromDb);
                            context.SaveChanges();
                        }

                        Node newChildNode = DomainClassFactory.NewNode(childTagFromDb, hierarchyFromDb);
                        parentNodeFromDb.Children.Add(newChildNode);
                        hierarchyFromDb.Nodes.Add(newChildNode);
                        context.Update(parentNodeFromDb);
                        context.Update(hierarchyFromDb);
                        context.SaveChanges();
                    }
                }
                lineCount++;
            }
        }
示例#4
0
        /// <summary>
        /// Parses and inserts tags and tagsets. Also tags Photos.
        /// </summary>
        /// <param name="pathToTagFile"></param>
        /// <param name="pathToErrorLogFile"></param>
        static void InsertTags(string pathToTagFile, string pathToErrorLogFile)
        {
            Console.WriteLine("Inserting TagsSets and Tags:");
            int lineCount = 1;

            string[] linesInFile = File.ReadAllLines(pathToTagFile);
            //Looping over each line in the tag file.
            foreach (string line in linesInFile)
            {
                Console.WriteLine("Inserting line: " + lineCount++ + " out of " + linesInFile.Length);
                //File format: "FileName:TagSet:Tag:TagSet:Tag:(...)"
                string[] split       = line.Split(":");
                string   fileName    = split[0];
                int      numTagPairs = (split.Length - 2) / 2;
                //Looping over each pair of tags:
                for (int i = 0; i < numTagPairs; i++)
                {
                    string tagsetName = split[(i * 2) + 1];
                    string tagName    = split[(i * 2) + 2];

                    using (var context = new ObjectContext())
                    {
                        //Adding tagset to db:
                        Tagset tagsetFromDb = context.Tagsets
                                              .Where(ts => ts.Name.Equals(tagsetName))
                                              .Include(ts => ts.Tags)
                                              .FirstOrDefault();

                        //If tagset doesn't exist in db, add it:
                        if (tagsetFromDb == null)
                        {
                            tagsetFromDb = DomainClassFactory.NewTagSet(tagsetName);
                            context.Tagsets.Add(tagsetFromDb);
                            context.SaveChanges();

                            //Also creates a tag with same name:
                            Tag tagWithSameNameAsTagset = DomainClassFactory.NewTag(tagsetName, tagsetFromDb);
                            //Add tag to tagset:
                            tagWithSameNameAsTagset.Tagset = tagsetFromDb;
                            tagsetFromDb.Tags.Add(tagWithSameNameAsTagset);
                            //Add and update changes:
                            context.Tags.Add(tagWithSameNameAsTagset);
                            context.SaveChanges();
                        }

                        //Checking if tag exists, and creates it if it doesn't exist.
                        Tag tagFromDb = context.Tags
                                        .Where(t => t.TagsetId == tagsetFromDb.Id && t.Name.Equals(tagName))
                                        .Include(t => t.ObjectTagRelations)
                                        .FirstOrDefault();

                        //If tag doesn't exist in db, add it
                        if (tagFromDb == null)
                        {
                            tagFromDb = DomainClassFactory.NewTag(tagName, tagsetFromDb);
                            context.Tags.Add(tagFromDb);
                            context.SaveChanges();
                        }

                        //Add tag to tagset if tagset doesn't have it:
                        if (!tagsetFromDb.Tags
                            .Any(t => t.TagsetId == tagFromDb.Id)) //If tag does not exist in tagset, add it
                        {
                            tagsetFromDb.Tags.Add(tagFromDb);
                            tagFromDb.Tagset = tagsetFromDb;
                            context.Update(tagsetFromDb);
                            context.Update(tagFromDb);
                            context.SaveChanges();
                        }

                        //Adding tag to cube object with FileName:
                        CubeObject cubeObjectFromDb = context.CubeObjects
                                                      .Where(co => co.Photo.FileName.Equals(fileName))
                                                      .Include(co => co.Photo)
                                                      .Include(co => co.ObjectTagRelations)
                                                      .FirstOrDefault();

                        if (cubeObjectFromDb == null)
                        {
                            File.AppendAllText(pathToErrorLogFile, "File " + fileName + " was not found while parsing line " + lineCount);
                            //throw new Exception("Expected cubeobject to be in the DB already, but it isn't!");
                        }
                        else
                        {
                            if (cubeObjectFromDb.ObjectTagRelations
                                .FirstOrDefault(otr => otr.TagId == tagFromDb.Id) == null) //If Cubeobject does not already have tag asscociated with it, add it
                            {
                                ObjectTagRelation newObjectTagRelation = DomainClassFactory.NewObjectTagRelation(tagFromDb, cubeObjectFromDb);
                                context.ObjectTagRelations.Add(newObjectTagRelation);
                                context.SaveChanges();
                            }
                        }
                    }
                }
            }
        }
示例#5
0
 public static void AddTagToTagset(Tag tag, Tagset tagset)
 {
     tag.Tagset = tagset;
     tagset.Tags.Add(tag);
 }