Beispiel #1
0
        public void TestNewItem()
        {
            using (var db = new AppDataBase())
            {
                db.Items.Add(new DocItem()
                {
                    FileName = "filename",
                    LocalCopy = false,
                    Name = "name",
                    Uri = "file://filename",
                    HashCode = 0
                });

                db.SaveChanges();
            }
        }
Beispiel #2
0
        public void TestDbConnection()
        {
            using(var db = new AppDataBase())
            {
                var g = (from i in db.Tags
                        select i).ToArray();

                var d = from i in db.Items
                        select i;

                var f = (from i in db.Favorites
                         select i).ToArray();

                foreach(var i in d)
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("{0} {1}", i.Name, i.Uri));
                }
            }
        }
Beispiel #3
0
        public void CmdSearch(string searchString)
        {
            if (!string.IsNullOrWhiteSpace(searchString))
            {
                var searchStringLow = searchString.ToLower();

                using(var db = new AppDataBase())
                {
                    var result = from i in db.Items
                                 let tags = i.Tags.Select(x => x.Name.ToLower())
                                 where tags.Contains(searchStringLow) || i.Name.ToLower().Contains(searchStringLow)
                                 orderby i.Rating descending
                                 select new DocItemView() { Name = i.Name, TagArray = i.Tags.Select(x => x.Name), Id = i.DocItemId, Rating = i.Rating };

                    SearchResult = result.Take(10).ToArray();
                }

                RiseSearchResultChanged();
            }
        }
Beispiel #4
0
        private void UpdateGroups()
        {
            using (var db = new AppDataBase())
            {
                var result = from i in db.Favorites
                             select i.Value;

                Groups = result.ToArray();
            }

            RiseGroupsChanged();
        }
Beispiel #5
0
        void OpenDocument(DocItemView itemView)
        {
            DocItem item = null;

            using (var db = new AppDataBase())
            {
                item = (from i in db.Items
                        where i.DocItemId == itemView.Id
                       select i).First();
            }

            if (item != null)
            {
                if (Storage.Default.IsFileExist(item) == false)
                {
                    string fileName = Storage.Default.GetFullPath(item);

                    WindowAsyncOperation.RunAsync(Properties.Resources.MsgDownload, (ReportProgress progress) =>
                    {
                        WebDownload d = new WebDownload(item.Uri, progress);
                        d.Save(fileName);
                    });
                }

                Storage.Default.OpenFile(item);
            }
        }
Beispiel #6
0
        private void ChangeRateImpl(DocItemView obj)
        {
            if (obj != null)
            {
                using(var db = new AppDataBase())
                {
                    var item = (from i in db.Items
                                where i.DocItemId == obj.Id
                               select i).Single();

                    item.Rating = obj.Rating;

                    db.SaveChanges();
                }
            }
        }
Beispiel #7
0
        public static void AddItem(NewItem param, ReportProgress progressReport)
        {
            // 0%
            ReportProgress(progressReport, 0);

            string[] sc = {";", " ", ","};

            string[] tags = param.Tags.Split(sc, StringSplitOptions.RemoveEmptyEntries);

            // check uri
            bool isUriLocal = IsUriLocal(param.Uri);

            /*
             * Local URI
             *  [keep local] store to database
             *  [not keep] only link
             *
             * Web file
             *  [keep local] store to database
             *  [not keep] only link
            */

            var item = new DocItem()
            {
                Uri = param.Uri,
                Name = param.Name,
                Rating = 3
            };

            // Download file
            // 90%
            if (isUriLocal)
            {
                // local filesystem

                item.FileName = Path.GetFileName(param.Uri);
                item.HashCode = HashCalc.GetHash(param.Uri);
            }
            else
            {
                var download = new WebDownload(param.Uri, (int p, string d) =>
                {
                    int percentage = p * 90;
                    percentage /= 100;
                    ReportProgress(progressReport, percentage);
                });

                item.FileName = download.FileName;
                item.HashCode = HashCalc.GetHash(download.Content);
            }
            ReportProgress(progressReport, 90);

            // add DB record
            // 100%
            using (var db = new AppDataBase())
            using(var transaction = db.Database.BeginTransaction())
            {
                // try to select existing tags
                //var registeredTags = db.Tags.Select(x => x.Name).Intersect(tags, StringComparer.InvariantCultureIgnoreCase).ToArray();

                var registeredTags = from i in db.Tags
                                     where tags.Contains(i.Name.ToLower())
                                     select i;

                // save new tags
                var newTags = tags.Except(registeredTags.Select(x => x.Name), StringComparer.InvariantCultureIgnoreCase);

                item.Tags = new List<Tag>();

                // add link to existing tags
                foreach(var i in registeredTags)
                {
                    item.Tags.Add(i);
                }

                // add new tags and link with document
                foreach(var i in newTags)
                {
                    var newTagRecord = db.Tags.Add(new Tag()
                    {
                        Name = i
                    });

                    item.Tags.Add(newTagRecord);
                }

                // save tags
                db.SaveChanges();

                // add item
                db.Items.Add(item);

                // save item
                db.SaveChanges();

                transaction.Commit();
            }
        }
Beispiel #8
0
        public void Add(IEnumerable<DocItem> items, string commonTags, ReportProgress progressReport)
        {
            string[] sc = { ";", " ", "," };

            string[] tags = commonTags.Split(sc, StringSplitOptions.RemoveEmptyEntries);

            using(var db = new AppDataBase())
            {
                var registeredTags = from i in db.Tags
                                     where tags.Contains(i.Name.ToLower())
                                     select i;

                List<Tag> allTags = new List<Tag>(registeredTags);

                if (progressReport != null)
                {
                    progressReport(-1, Properties.Resources.AddTags);
                }

                using (var transaction = db.Database.BeginTransaction())
                {
                    var newTags = tags.Except(registeredTags.Select(x => x.Name), StringComparer.InvariantCultureIgnoreCase);

                    // add new tags and link with document
                    foreach (var i in newTags)
                    {
                        var newTagRecord = db.Tags.Add(new Tag()
                        {
                            Name = i
                        });

                        allTags.Add(newTagRecord);
                    }

                    db.SaveChanges();

                    transaction.Commit();
                }

                using (var transaction = db.Database.BeginTransaction())
                {
                    int totalItems = items.Count();
                    int c = 0;

                    foreach (var i in items)
                    {
                        c++;

                        i.Tags = allTags;
                        db.Items.Add(i);

                        if (progressReport != null)
                        {
                            int percentage = c * 100;
                            percentage /= totalItems;

                            progressReport(percentage, string.Format(Properties.Resources.FormatAddFile, i.FileName));
                        }
                    }

                    progressReport(-1, Properties.Resources.MsgCommitingChanges);

                    db.SaveChanges();

                    transaction.Commit();
                }
            }
        }
Beispiel #9
0
        private void ProcessSingleFile(string fileName)
        {
            var hash = HashCalc.GetHash(fileName);

            using(var db = new AppDataBase())
            {
                var isFileNotRegistered = (from i in db.Items
                                where i.HashCode == hash
                                select i).Count() == 0;

                if (isFileNotRegistered)
                {
                    // file not registered

                    var file = Path.GetFileNameWithoutExtension(fileName);

                    var item = new DocItem()
                    {
                        Uri = string.Format(@"file://{0}", fileName),
                        FileName = file,
                        Name = file,
                        HashCode = hash,
                        LocalCopy = false,
                        Rating = 3,
                    };

                    lock(NewItems)
                    {
                        NewItems.Add(item);
                    }
                }
            }
        }