Exemplo n.º 1
0
        public AddFileViewModel()
        {
            CmdOK = new Cmd(ExecuteOK);
            CmdCancel = new Cmd(ExecuteCancel);
            PickFile = new Cmd(PickFileCmd);

            Param = new NewItem()
            {
                Name = "ATMega16",
                Uri = "http://www.atmel.com/images/doc2466.pdf",
                KeepLocalCopy = false,
                Tags = "ATMega16 Atmel ATMega"

                //Name = "Name",
                //Uri = string.Empty,
                //KeepLocalCopy = false,
                //Tags = string.Empty,
            };
        }
Exemplo n.º 2
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();
            }
        }