Esempio n. 1
0
        public EbookDTO Update(IndexUnit indexUnit)
        {
            var ebookFromDb = _ebookProvider.GetAll().Where(book => book.Filename == indexUnit.Filename).SingleOrDefault();

            ebookFromDb.Title  = indexUnit.Title;
            ebookFromDb.Author = indexUnit.Author;
            var keyword = string.Empty;

            foreach (var item in indexUnit.Keywords)
            {
                keyword += item + " ";
            }
            if (keyword.Length >= 120)
            {
                keyword = keyword.Substring(0, 110);
            }
            ebookFromDb.Keywords   = keyword;
            ebookFromDb.LanguageId = _languageManager.GetByName(indexUnit.Language).LanguageId;
            ebookFromDb.CategoryId = indexUnit.Category;
            indexUnit.FileDate     = ebookFromDb.PublicationYear.ToString();

            _indexManager.Delete(indexUnit.Filename);
            indexUnit.Text = GetDocumentText(indexUnit.Filename);
            _indexManager.Index(indexUnit);
            int ebookId;

            ebookId = _ebookProvider.Update(ebookFromDb);
            return(ConverterExtension.ToDTO(_ebookProvider.GetById(ebookId)));
        }
Esempio n. 2
0
        public EbookDTO Create(IndexUnit unit)
        {
            _indexManager.Index(unit);
            var keywords = string.Empty;

            foreach (var item in unit.Keywords)
            {
                keywords += item + " ";
            }
            keywords = keywords.Trim();
            var language = _languageManager.GetByName(unit.Language);
            var ebook    = new Ebook()
            {
                Author          = unit.Author,
                CategoryId      = unit.Category,
                Filename        = unit.Filename,
                Keywords        = keywords,
                MIME            = "application/pdf",
                PublicationYear = int.Parse(unit.FileDate),
                Title           = unit.Title,
                UserId          = 1,
                LanguageId      = language.LanguageId
            };
            int newEbookId = 0;

            newEbookId = _ebookProvider.Create(ebook);
            return(ConverterExtension.ToDTO(_ebookProvider.GetById(newEbookId)));
        }
Esempio n. 3
0
        public IActionResult AddEbook(IndexUnit indexUnit)
        {
            if (indexUnit == null)
            {
                return(BadRequest());
            }

            var ebook = _ebookManager.Create(indexUnit);

            return(Created(new Uri("http://localhost:12621/api/ebook/" + ebook.EbookId), ebook));
        }
Esempio n. 4
0
        public IActionResult UpdateEbook(string filename, [FromBody] IndexUnit indexUnit)
        {
            if (indexUnit == null)
            {
                return(BadRequest());
            }

            var ebookFromDb = _ebookManager.Update(indexUnit);

            return(Ok(ebookFromDb));
        }
Esempio n. 5
0
        public IndexUnit GetIndexUnit(string fileName)
        {
            IndexUnit unit = new IndexUnit();

            try
            {
                var filePath = ConfigurationManager.FileDir + fileName;
                Console.Out.WriteLine(filePath);
                PdfReader reader = new PdfReader(filePath);
                Dictionary <string, string> dict = reader.Info;

                string author = string.Empty;
                if (dict.Keys.Contains("Author"))
                {
                    author += reader.Info["Author"];
                }
                unit.Author = author;
                string title = string.Empty;
                if (dict.Keys.Contains("Title"))
                {
                    title += reader.Info["Title"];
                }
                unit.Title    = title;
                unit.Keywords = new List <string>();
                if (dict.Keys.Contains("Keywords"))
                {
                    string   keywords         = "" + reader.Info["Keywords"];
                    string[] splittedKeywords = keywords.Split(" ");

                    foreach (var item in splittedKeywords)
                    {
                        unit.Keywords.Add(item);
                    }
                }

                unit.FileDate = DateTools.DateToString(DateTime.Now.Date, DateTools.Resolution.DAY);
                var text = string.Empty;
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                    text += PdfTextExtractor.GetTextFromPage(reader, i, strategy);
                }
                unit.Text = text.Replace('\n', ' ').Trim();

                reader.Close();
                reader.Dispose();
                return(unit);
            }
            catch (Exception e) { Console.Out.WriteLine(e.Message); return(null); }
        }
Esempio n. 6
0
        public void Index(IndexUnit unit)
        {
            Document document = new Document();

            document.Add(new TextField("title", unit.Title, Field.Store.YES));

            foreach (var keyword in unit.Keywords)
            {
                document.Add(new TextField("keyword", keyword, Field.Store.YES));
            }

            document.Add(new StringField("filename", unit.Filename, Field.Store.YES));
            document.Add(new TextField("text", unit.Text, Field.Store.NO));
            document.Add(new TextField("filedate", unit.FileDate, Field.Store.YES));
            document.Add(new TextField("author", unit.Author, Field.Store.YES));
            document.Add(new TextField("category", unit.Category.ToString(), Field.Store.YES));
            document.Add(new TextField("language", unit.Language, Field.Store.YES));
            indexWriter.AddDocument(document);
            indexWriter.Commit();
        }
Esempio n. 7
0
        public IActionResult GetEbook(int id)
        {
            var ebook = _ebookManager.GetById(id);

            if (ebook == null)
            {
                return(NotFound());
            }
            IndexUnit indexUnit = new IndexUnit()
            {
                Title    = ebook.Title,
                Category = ebook.CategoryId,
                Author   = ebook.Author,
                FileDate = ebook.PublicationYear.ToString(),
                Filename = ebook.Filename,
                Language = _languageManager.GetById(ebook.LanguageId).Name,
                Keywords = ebook.Keywords.Trim().Split(" ").ToList()
            };

            return(Ok(indexUnit));
        }
 protected override void RemoveFromParent(Node node)
 {
     if (!node.Parent.Equals(Address.Empty))
     {
         Node parent = Cache.LookupNode(node.Parent);
         NodeEntry entryToRemove = null;
         foreach (NodeEntry entry in parent.NodeEntries)
             if (entry.Child.Equals(node.Address))
                 entryToRemove = entry;
         IndexUnit indexUnit = new IndexUnit(parent.Address, node.Address, entryToRemove.MinimumBoundingBox, Operation.Delete);
         NodeTranslationTable.Add(indexUnit);
     }
     else
         Root = Address.Empty;
     Cache.DeletePageData(node);
 }
 protected virtual void InsertRecord(BufferItem operation)
 {
     Leaf leafToInsertInto = ChooseLeaf(Cache.LookupRecord(operation.Entry.Child));
     IndexUnit indexUnit = new IndexUnit(leafToInsertInto.Address, operation.Entry.Child, operation.Entry.MinimumBoundingBox, operation.Operation);
     NodeTranslationTable.Add(indexUnit);
     leafToInsertInto.AddNodeEntry(operation.Entry);
     if (leafToInsertInto.NodeEntries.Count > Constants.MAXIMUM_ENTRIES_PER_NODE)
     {
         List<Node> splitNodes = Split(leafToInsertInto);
         RemoveFromParent(leafToInsertInto);
         AdjustTree(splitNodes[0], splitNodes[1]);
     }
     else
         AdjustTree(leafToInsertInto);
 }
 protected override void Insert(Node newNode, Node node)
 {
     MinimumBoundingBox mbb = newNode.CalculateMinimumBoundingBox();
     IndexUnit indexUnit = new IndexUnit(node.Address, newNode.Address, mbb, Operation.Insert);
     NodeTranslationTable.Add(indexUnit);
     node.AddNodeEntry(new NodeEntry(mbb, newNode.Address));
     newNode.Parent = node.Address;
     Cache.WritePageData(newNode);
 }
 protected virtual void FlushBuffer()
 {
     foreach (BufferItem operation in Buffer.Buffer)
     {
         if (operation.Operation == Operation.Insert)
         {
             if (operation.Entry is LeafEntry)
             {
                 InsertRecord(operation);
             }
         }
         else if (operation.Operation == Operation.Delete)
         {
             Record recordToDelete = Cache.LookupRecord(operation.Entry.Child);
             Leaf leafWithRecord = FindLeaf(recordToDelete, Cache.LookupNode(Root));
             if (leafWithRecord == null)
                 continue;
             LeafEntry entryToRemove = null;
             foreach (LeafEntry entry in leafWithRecord.NodeEntries)
                 if (entry.Child.Equals(recordToDelete.Address))
                     entryToRemove = entry;
             IndexUnit indexUnit = new IndexUnit(leafWithRecord.Address, operation.Entry.Child, operation.Entry.MinimumBoundingBox, operation.Operation);
             NodeTranslationTable.Add(indexUnit);
             leafWithRecord.RemoveNodeEntry(operation.Entry);
             CondenseTree(leafWithRecord);
             Node rootNode = Cache.LookupNode(Root);
             if (rootNode.NodeEntries.Count == 1)
             {
                 Node newRoot = Cache.LookupNode(rootNode.NodeEntries[0].Child);
                 newRoot.Parent = Address.Empty;
                 Root = newRoot.Address;
                 Cache.DeletePageData(rootNode);
                 Cache.WritePageData(newRoot);
             }
             Cache.DeletePageData(recordToDelete);
         }
         else
             throw new Exception();
     }
     Buffer.Clear();
     NodeTranslationTable.FlushIndexUnits();
 }
 protected override void AdjustTree(Node node1, Node node2)
 {
     if (node1.Address.Equals(Root))
         return;
     if (Root.Equals(Address.Empty))
     {
         Type childType = node1 is Leaf ? typeof(Leaf) : typeof(Node);
         Node rootNode = new Node(Address.Empty, childType);
         Root = rootNode.Address;
         node1.Parent = Root;
         node2.Parent = Root;
         rootNode.AddNodeEntry(new NodeEntry(node1.CalculateMinimumBoundingBox(), node1.Address));
         rootNode.AddNodeEntry(new NodeEntry(node2.CalculateMinimumBoundingBox(), node2.Address));
         Cache.WritePageData(rootNode);
         //Node temp = Cache.LookupNode(rootNode.Address);
         Cache.WritePageData(node1);
         Cache.WritePageData(node2);
         TreeHeight++;
         return;
     }
     Node parent = Cache.LookupNode(node1.Parent);
     NodeEntry entryToUpdate = null;
     foreach (NodeEntry entry in parent.NodeEntries)
         if (entry.Child.Equals(node1.Address))
             entryToUpdate = entry;
     if (entryToUpdate == null)
     {
         MinimumBoundingBox mbb = node1.CalculateMinimumBoundingBox();
         IndexUnit indexUnit = new IndexUnit(parent.Address, node1.Address, mbb, Operation.Insert);
         NodeTranslationTable.Add(indexUnit);
         parent.AddNodeEntry(new NodeEntry(mbb, node1.Address));
     }
     else
     {
         MinimumBoundingBox mbb = node1.CalculateMinimumBoundingBox();
         IndexUnit indexUnit = new IndexUnit(parent.Address, entryToUpdate.Child, mbb, Operation.Update);
         NodeTranslationTable.Add(indexUnit);
         entryToUpdate.MinimumBoundingBox = mbb;
     }
     if (node2 != null)
     {
         MinimumBoundingBox mbb = node2.CalculateMinimumBoundingBox();
         IndexUnit indexUnit = new IndexUnit(parent.Address, node2.Address, mbb, Operation.Insert);
         NodeTranslationTable.Add(indexUnit);
         parent.AddNodeEntry(new NodeEntry(mbb, node2.Address));
         Cache.WritePageData(node2);
         Cache.WritePageData(node1);
         if (parent.NodeEntries.Count > Constants.MAXIMUM_ENTRIES_PER_NODE)
         {
             List<Node> splitNodes = Split(parent);
             if (parent.Address.Equals(Root))
                 Root = Address.Empty;
             RemoveFromParent(parent);
             AdjustTree(splitNodes[0], splitNodes[1]);
             return;
         }
     }
     AdjustTree(parent, null);
 }
Esempio n. 13
0
 public virtual void RemoveIndexUnit(IndexUnit indexUnit)
 {
     IndexUnits.Remove(indexUnit);
 }
Esempio n. 14
0
 public virtual void AddIndexUnit(IndexUnit indexUnit)
 {
     IndexUnits.Add(indexUnit);
 }