Example #1
0
        public void After(Core.Services.tmp.EntityOperation operation, EntityOperationContext context, EntityOperationResult result)
        {
            if (!result.Success)
            {
                return;
            }

            var update = operation as EntityUpdate;

            if (operation.IsEntity(EntityConsts.BibliographicListQuery) && update != null && update.ContainsProperty("Status") && update.Get <QueryStatus>("Status") == QueryStatus.Completed)
            {
                var q = new EntityQuery2(EntityConsts.BibliographicListQuery, update.Id.Value)
                {
                    AllProperties = true
                };
                q.Include(User.ENTITY, Roles.Customer);
                var    biblListQuery = _repository.Read(q);
                var    user = new User(biblListQuery.GetSingleRelation(User.ENTITY, Roles.Customer).Entity);
                var    template = _templateService.Get(new Guid(NotificationTemplates.QUERY_COMPLETED));
                string subject = null, body = null;
                Dictionary <string, Entity> templateContext = new Dictionary <string, Entity>(StringComparer.InvariantCultureIgnoreCase);
                templateContext.Add("Customer", user);
                templateContext.Add("Query", biblListQuery);

                _templateService.Render(template, templateContext, out subject, out body);
                var withEmail = biblListQuery.GetData <ReplyMethods>("ReplyMethod") == ReplyMethods.ByEmail;
                _notificationService.SendNotification(withEmail, new User[] { user }, subject, body, null, new Relation[] { new Relation(Notification.ROLE, biblListQuery) });
            }
            else if (operation.IsEntity(Payment.ENTITY) && update != null && update.ContainsProperty("Status") && update.Get <PaymentStatus>("Status") == PaymentStatus.Paid)
            {
                var q = new EntityQuery2(EntityConsts.BibliographicListQuery);
                q.AddProperties("Number");
                q.WhereRelated(new RelationQuery(Payment.ENTITY, Roles.Payment, update.Id.Value));
                q.Include(User.ENTITY, Roles.Customer);
                q.Include(File.ENTITY, Roles.File);
                var biblListQuery = _repository.Read(q);
                if (biblListQuery != null)
                {
                    var file = new File(biblListQuery.GetSingleRelation(File.ENTITY, Roles.File).Entity);
                    var user = new User(biblListQuery.GetSingleRelation(User.ENTITY, Roles.Customer).Entity);

                    var template = _templateService.Get(new Guid(NotificationTemplates.PAYMENT_COMPLETED));

                    string subject = null, body = null;
                    Dictionary <string, Entity> templateContext = new Dictionary <string, Entity>(StringComparer.InvariantCultureIgnoreCase);
                    templateContext.Add("Customer", user);
                    templateContext.Add("Query", biblListQuery);

                    _templateService.Render(template, templateContext, out subject, out body);

                    var withEmail = biblListQuery.GetData <ReplyMethods>("ReplyMethod") == ReplyMethods.ByEmail;
                    _notificationService.SendNotification(withEmail, new User[] { user }, subject, body, new File[] { file }, new Relation[] { new Relation(Notification.ROLE, biblListQuery) });
                    //_fileService.GrantAccess(file.Id, FileAccessType.Read, new User(biblQuery.GetSingleRelation(User.ENTITY, Roles.Customer).Entity));
                }
            }
        }
Example #2
0
        public System.IO.Stream GetFileContent(int fileId, Guid?token = null)
        {
            var q = new EntityQuery2(File.ENTITY, fileId);

            q.AddProperties("ContentPath");
            q.Include(User.ENTITY, Roles.Access);
            var file = new File(_repository.Read(q));

            if (HasAccessInternal(_securityService.CurrentUser, file.Access, token))
            {
                return(new System.IO.FileStream(System.IO.Path.Combine(_permPath, file.ContentPath), System.IO.FileMode.Open));
            }
            else
            {
                throw new UnauthorizedAccessException("You don't have permissions to access this file.");
            }
        }
Example #3
0
        public void After(Core.Services.tmp.EntityOperation operation, EntityOperationContext context, EntityOperationResult result)
        {
            if (!result.Success)
                return;

            var update = operation as EntityUpdate;
            if (operation.IsEntity(EntityConsts.BibliographicListQuery) && update != null && update.ContainsProperty("Status") && update.Get<QueryStatus>("Status") == QueryStatus.Completed)
            {
                var q = new EntityQuery2(EntityConsts.BibliographicListQuery, update.Id.Value) { AllProperties = true };
                q.Include(User.ENTITY, Roles.Customer);
                var biblListQuery = _repository.Read(q);
                var user = new User(biblListQuery.GetSingleRelation(User.ENTITY, Roles.Customer).Entity);
                var template = _templateService.Get(new Guid(NotificationTemplates.QUERY_COMPLETED));
                string subject = null, body = null;
                Dictionary<string, Entity> templateContext = new Dictionary<string, Entity>(StringComparer.InvariantCultureIgnoreCase);
                templateContext.Add("Customer", user);
                templateContext.Add("Query", biblListQuery);

                _templateService.Render(template, templateContext, out subject, out body);
                var withEmail = biblListQuery.GetData<ReplyMethods>("ReplyMethod") == ReplyMethods.ByEmail;
                _notificationService.SendNotification(withEmail, new User[] { user }, subject, body, null, new Relation[] { new Relation(Notification.ROLE, biblListQuery) });
            }
            else if (operation.IsEntity(Payment.ENTITY) && update != null && update.ContainsProperty("Status") && update.Get<PaymentStatus>("Status") == PaymentStatus.Paid)
            {
                var q = new EntityQuery2(EntityConsts.BibliographicListQuery);
                q.AddProperties("Number");
                q.WhereRelated(new RelationQuery(Payment.ENTITY, Roles.Payment, update.Id.Value));
                q.Include(User.ENTITY, Roles.Customer);
                q.Include(File.ENTITY, Roles.File);
                var biblListQuery = _repository.Read(q);
                if (biblListQuery != null)
                {
                    var file = new File(biblListQuery.GetSingleRelation(File.ENTITY, Roles.File).Entity);
                    var user = new User(biblListQuery.GetSingleRelation(User.ENTITY, Roles.Customer).Entity);

                    var template = _templateService.Get(new Guid(NotificationTemplates.PAYMENT_COMPLETED));

                    string subject = null, body = null;
                    Dictionary<string, Entity> templateContext = new Dictionary<string, Entity>(StringComparer.InvariantCultureIgnoreCase);
                    templateContext.Add("Customer", user);
                    templateContext.Add("Query", biblListQuery);

                    _templateService.Render(template, templateContext, out subject, out body);

                    var withEmail = biblListQuery.GetData<ReplyMethods>("ReplyMethod") == ReplyMethods.ByEmail;
                    _notificationService.SendNotification(withEmail, new User[] { user }, subject, body, new File[] { file }, new Relation[] { new Relation(Notification.ROLE, biblListQuery) });
                    //_fileService.GrantAccess(file.Id, FileAccessType.Read, new User(biblQuery.GetSingleRelation(User.ENTITY, Roles.Customer).Entity));

                }
            }
        }
Example #4
0
 public System.IO.Stream GetFileContent(int fileId, Guid? token = null)
 {
     var q = new EntityQuery2(File.ENTITY, fileId);
     q.AddProperties("ContentPath");
     q.Include(User.ENTITY, Roles.Access);
     var file = new File(_repository.Read(q));
     if (HasAccessInternal(_securityService.CurrentUser, file.Access, token))
     {
         return new System.IO.FileStream(System.IO.Path.Combine(_permPath, file.ContentPath), System.IO.FileMode.Open);
     }
     else
         throw new UnauthorizedAccessException("You don't have permissions to access this file.");
 }
Example #5
0
        public void Test_EntityRepo_Count()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                var jb1 = new Book()
                {
                    Title = "The Shadow is Rising",
                    Price = 21.15m
                };
                var jb2 = new Book()
                {
                    Title = "The Eye of the World",
                    Price = 25.80m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Create(jb1);
                repository.Create(jb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));
                repository.Attach(jordan, new Relation("author", jb1));
                repository.Attach(jordan, new Relation("author", jb2));
                #endregion

                var query = new EntityQuery2("author");
                query.AddProperties("firstname", "lastname", "born");
                var res = repository.Search(query);

                Assert.AreEqual(2, res.Count());
                Assert.AreEqual(2, repository.Count(query));

                //greater then
                EntityQuery2 q = new EntityQuery2("book");
                q.WhereGreaterThen("price", 19.0m);
                Assert.AreEqual(3, repository.Search(q).Count());
                Assert.AreEqual(3, repository.Count(q));

                //less then
                q = new EntityQuery2("book");
                q.WhereLessThen("price", 20.0m);
                Assert.AreEqual(2, repository.Search(q).Count());
                Assert.AreEqual(2, repository.Count(q));

                //is boolean
                q = new EntityQuery2("author");
                q.WhereIs("isalive", false);
                var r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                //is string (ignore case)
                q = new EntityQuery2("author");
                q.WhereIs("lastname", "jordan");
                r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                //starts with
                q = new EntityQuery2("author");
                q.WhereStartsWith("firstname", "ra");
                r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                //ends with
                q = new EntityQuery2("book");
                q.WhereEndsWith("title", "world");
                r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                //less then
                q = new EntityQuery2("book");
                q.WhereAnyOf("id", new object[] { fb1.Id, jb1.Id, jb2.Id });
                Assert.AreEqual(3, repository.Search(q).Count());
                Assert.AreEqual(3, repository.Count(q));

                //between decimal
                q = new EntityQuery2("book");
                q.WhereBetween("price", 19.0m, 22.0m);
                Assert.AreEqual(2, repository.Search(q).Count());
                Assert.AreEqual(2, repository.Count(q));

                //between datetime
                q = new EntityQuery2("author");
                q.WhereBetween("born", new DateTime(1948, 1, 1), new DateTime(1949, 1, 1));
                r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                q = new EntityQuery2("author");
                q.WhereBetween("born", new DateTime(1948, 1, 1), new DateTime(1949, 1, 1));
                q.WhereIs("isalive", true);
                Assert.AreEqual(0, repository.Search(q).Count());
                Assert.AreEqual(0, repository.Count(q));

                q = new EntityQuery2("author");
                q.WhereBetween("born", new DateTime(1960, 1, 1), new DateTime(1970, 1, 1));
                q.WhereIs("isalive", true);
                q.WhereStartsWith("firstname", "ra");
                Assert.AreEqual(1, repository.Search(q).Count());
                Assert.AreEqual(1, repository.Count(q));
            }
        }
Example #6
0
        public void Test_EntityRepo_AttachDetach()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);

                #endregion

                repository.Attach(feist, new Relation("author", fb1));
                var rel2 = new Relation("author", fb2);
                var writtenOn = new DateTime(1996, 4, 25);
                rel2.SetData<DateTime>("WrittenOn", writtenOn);
                repository.Attach(feist, rel2);

                var q = new EntityQuery2("author", feist.Id);
                q.AddProperties("FirstName", "lastname", "isalive", "born", "rating");
                q.Include("book", "author");
                var e = repository.Read(q);
                Assert.AreEqual(2, e.GetManyRelations("book", "author").Count());

                var bq = new EntityQuery2("book");
                bq.Include("author", "author");
                var bes = repository.Search(bq);
                foreach (var be in bes)
                {
                    Assert.AreEqual(1, be.RelationsData.Count);
                    Assert.AreEqual(feist.Id, be.GetSingleRelation("author", "author").Entity.Id);
                    if (be.Id == fb2.Id)
                        Assert.AreEqual(writtenOn, be.GetSingleRelation("author", "author").GetData<DateTime>("writtenon"));
                }

                repository.Detach(feist, new Relation("author", fb1));
                e = repository.Read(q);
                Assert.AreEqual(1, e.GetManyRelations("book", "author").Count());

                repository.Attach(fb1, new Relation("author", feist));
                e = repository.Read(q);
                Assert.AreEqual(2, e.GetManyRelations("book", "author").Count());
                repository.Detach(fb1, new Relation("author", feist));
                e = repository.Read(q);
                Assert.AreEqual(1, e.GetManyRelations("book", "author").Count());

                bool ex = false;
                try { repository.Attach(fb2, new Relation("author", jordan)); }
                catch (Exception) { ex = true; }
                Assert.IsTrue(ex, "Exception not thrown when attaching two authors to single book");
            }
        }
Example #7
0
        public void Test_EntityOperation_Update()
        {
            var dbService = new TestDatabaseService();
            var repo = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            IEntityOperationService svc = new EntityOperationService(repo, dbService, new IEntityOperationInspector[] { new Inspector() }, new IEntityQueryInspector[] { new Inspector() }, new IEntityOperationLogic[] { new Logic() });
            EntityUpdate update = new EntityUpdate("Author");
            update.Set("FirstName", "John");
            update.Set("LastName", "Tolkin");
            update.Set("Numberofawards", 2);
            update.Set("IsAlive", false);

            EntityUpdate book = new EntityUpdate("book");
            book.Set("Title", "The Eye of the World");
            book.Set("genre", Genre.Fantasy);

            svc.Update(book);
            update.Attach("Book", "author", book.Id.Value);

            var result = svc.Update(update);
            Assert.AreEqual(true, result.Success);

            EntityQuery2 query = new EntityQuery2("Author");
            query.AddProperties("FirstName", "LastName", "IsAlive", "CreatedOn");
            query.Include("book", "author");
            var res = repo.Search(query);
            Assert.AreEqual(1, res.Count());
            var a = res.Single();
            Assert.AreEqual("John", a.GetData<string>("Firstname"));
            Assert.AreEqual("Tolkin", a.GetData<string>("LastName"));
            var created = a.GetData<DateTime>("createdon");
            Assert.AreEqual(DateTime.Now.Date, created.Date);
            var books = a.GetManyRelations("book", "author");
            Assert.AreEqual(1, books.Count());
            var b = books.Single().Entity;
            Assert.AreEqual("The Eye of the World", b.GetData<string>("title"));
            Assert.AreEqual(Genre.Fantasy, b.GetData<Genre>("genre"));
            created = b.GetData<DateTime>("createdon");
            Assert.AreEqual(DateTime.Now.Date, created.Date);

            repo.Delete(a, true);
            repo.Delete(b);
        }
Example #8
0
        public void Test_EntityRepo_Update()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));
                #endregion

                jordan.LastName += "EDIT";
                jordan.IsAlive = true;
                jordan.Born = jordan.Born.AddDays(2.0);
                jordan.Rating -= 0.5m;

                repository.Update(jordan);
                var q = new EntityQuery2("author", jordan.Id);
                q.AddProperties("lastname", "isalive", "born", "rating");
                var e = repository.Read(q);
                var updated = new Author(e);
                Assert.AreEqual(jordan.LastName, updated.LastName);
                Assert.AreEqual(jordan.Born, updated.Born);
                Assert.AreEqual(jordan.IsAlive, updated.IsAlive);
                Assert.AreEqual(jordan.Rating, updated.Rating);
            }
        }
Example #9
0
        public void Test_EntityRepo_SearchWithRels()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));

                var query = new EntityQuery2("author");
                query.AddProperties("firstname", "lastname", "born");
                query.Include("book", "author");
                var res = repository.Search(query);

                Assert.AreEqual(2, res.Count());
                var rf = res.Single(e => e.Id == feist.Id);
                var rj = res.Single(e => e.Id == jordan.Id);

                Assert.AreEqual(1, rf.RelationsData.Count);
                var books = rf.GetManyRelations("book", "author");
                Assert.AreEqual(2, books.Count());
                foreach (var r in books)
                {
                    var orig = r.Entity.Id == fb1.Id ? fb1 : fb2;
                    foreach (var pm in dms.Domain.Entities["book"].Properties)
                    {
                        if (orig.Data.ContainsKey(pm.Name))
                            Assert.AreEqual(orig.Data[pm.Name], r.Entity.Data[pm.Name]);
                    }
                }

                Assert.AreEqual(0, rj.RelationsData.Count);
                Assert.AreEqual(0, rj.GetManyRelations("book", "author").Count());

                //TODO: rules!

                //repository.Complete();
            }
        }
Example #10
0
        public void Test_EntityRepo_ReadWithRel()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));

                EntityQuery2 q = new EntityQuery2("book", fb1.Id);
                q.AddProperties("title", "price");
                q.Include("author", "author");
                var e = repository.Read(q);
                Assert.AreEqual(2, e.Data.Count);
                foreach (var p in q.Properties)
                {
                    Assert.AreEqual(fb1.Data[p], e.Data[p]);
                }

                Assert.AreEqual(1, e.RelationsData.Count);
                var authorRel = e.GetSingleRelation("author", "author");
                foreach (var d in feist.Data)
                {
                    Assert.AreEqual(d.Value, authorRel.Entity.Data[d.Key]);
                }

                //repository.Complete();
            }
        }
Example #11
0
        public void Test_EntityRepo_Read()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                jordan.Id = repository.Create(jordan);
                Assert.IsTrue(jordan.Id > 0);
                feist.Id = repository.Create(feist);
                Assert.IsTrue(feist.Id > 0);

                EntityQuery2 q = new EntityQuery2("author", jordan.Id);
                q.AddProperties("FirstName", "lastname", "isalive", "born", "rating");
                var e = repository.Read(q);
                Assert.AreEqual(5, e.Data.Count);
                foreach (var p in q.Properties)
                {
                    Assert.AreEqual(jordan.Data[p], e.Data[p]);
                }
            }
        }
Example #12
0
        //[TestMethod]
        public void Test_EntityRepo_Perf()
        {
            var r = new Random();
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                for (int i = 0; i < 1000; i++)
                {
                    EntityUpdate update = new EntityUpdate("author");
                    update.Set("firstname", "Robert" + i);
                    update.Set("lastname", "Jordan");
                    update.Set("isalive", false);
                    update.Set("Born", new DateTime(1948, 10, 17));
                    update.Set("Rating", 5.5m + r.Next(4));
                    var id = repository.Create(update.ToEntity());
                    Assert.IsTrue(id > 0);

                    EntityQuery2 q = new EntityQuery2("author", id);
                    q.AddProperties("FirstName", "lastname", "isalive", "born", "rating");
                    var e = repository.Read(q);
                    foreach (var pu in update.PropertyUpdates)
                    {
                        Assert.AreEqual(pu.Value, e.Data[pu.Key]);
                    }

                    EntityUpdate update2 = new EntityUpdate(e.Name, e.Id);
                    update2.Set("rating", 5.5m + r.Next(4));
                    update2.Set("lastname", e.Data["lastname"] + "_EDIT");

                    repository.Update(update2.ToEntity());
                    e = repository.Read(q);
                    foreach (var pu in update2.PropertyUpdates)
                    {
                        Assert.AreEqual(pu.Value, e.Data[pu.Key]);
                    }
                    foreach (var pu in update.PropertyUpdates)
                    {
                        if (!pu.Key.Equals("rating", StringComparison.InvariantCultureIgnoreCase) && !pu.Key.Equals("lastname", StringComparison.InvariantCultureIgnoreCase))
                            Assert.AreEqual(pu.Value, e.Data[pu.Key]);
                    }

                }

                ctx.Complete();
            }

            using (var ctx = dbService.GetDatabaseContext(true))
            {
                var qAll = new EntityQuery2("Author");
                var all = repository.Search(qAll);
                Assert.AreEqual(1000, all.Count());
                foreach (var a in all)
                    repository.Delete(a);
                Assert.AreEqual(0, repository.Search(qAll).Count());
            }
        }
Example #13
0
        public void Test_EntityRepo_Paging()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data

                int aCnt = 20;
                int bCnt = 3;
                for (int i = 0; i < aCnt; i++)
                {
                    var a = new Author()
                    {
                        FirstName = "Fname" + i,
                        LastName = "Lname" + i,
                        Born = DateTime.Now.AddYears(-20).AddDays(i),
                        NumberOfAwards = i / 3
                    };
                    repository.Create(a);
                    for (int j = 0; j < bCnt; j++)
                    {
                        var b = new Book()
                        {
                            Title = string.Format("Book_{0}_{1}", i, j),
                            Genre = Genre.SciFi,
                            Price = 10.0m + j,
                            ISBN = string.Format("{0}_{1}", a.LastName, j)
                        };
                        repository.Create(b);
                        repository.Attach(b, new Relation("author", a));
                    }
                }

                Assert.AreEqual(aCnt, repository.Search(new EntityQuery2("author")).Count());
                Assert.AreEqual(aCnt * bCnt, repository.Search(new EntityQuery2("book")).Count());

                #endregion

                var query = new EntityQuery2("Author");
                query.AddProperties("FirstName", "LastName");
                query.Include("book", "author");
                query.Paging = new Paging(1, 10);
                var res = repository.Search(query);
                Assert.AreEqual(10, res.Count());
                int idx = 0;
                foreach (var r in res)
                {
                    Assert.AreEqual("Fname" + idx, r.GetData<string>("firstname"));
                    var books = r.GetManyRelations("book", "author");
                    int bidx = 0;
                    foreach (var b in books)
                    {
                        Assert.AreEqual(string.Format("Book_{0}_{1}", idx, bidx++), b.Entity.GetData<string>("title"));
                    }
                    idx++;
                }

                //assert second page
                query.Paging.Page++;
                res = repository.Search(query);
                Assert.AreEqual(10, res.Count());
                foreach (var r in res)
                {
                    Assert.AreEqual("Fname" + idx, r.GetData<string>("firstname"));
                    var books = r.GetManyRelations("book", "author");
                    int bidx = 0;
                    foreach (var b in books)
                    {
                        Assert.AreEqual(string.Format("Book_{0}_{1}", idx, bidx++), b.Entity.GetData<string>("title"));
                    }
                    idx++;
                }
            }
        }
Example #14
0
        public void Test_EntityRepo_Create()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m,
                    Genre = Genre.Fantasy
                };
                #endregion

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));
                fb1.Genre = Genre.Mistery; //default value
                feist.IsAlive = true;//default value

                var q = new EntityQuery2("author", feist.Id);
                q.AddProperties("FirstName", "lastname", "isalive", "born", "rating");
                q.Include("book", "author");
                var e = repository.Read(q);
                var created = new Author(e);
                Assert.AreEqual(feist.FirstName, created.FirstName);
                Assert.AreEqual(feist.LastName, created.LastName);
                Assert.AreEqual(feist.Born, created.Born);
                Assert.AreEqual(feist.IsAlive, created.IsAlive);
                Assert.AreEqual(feist.Rating, created.Rating);
                Assert.AreEqual(2, e.GetManyRelations("book", "author").Count());//repository.Detach(feist, new Relation("author", fb1));
                var eb1 = e.GetManyRelations("book", "author").First();
                Book b = new Book(eb1.Entity);
                Assert.AreEqual(fb1.Genre, b.Genre);
            }
        }
Example #15
0
        public InspectionResult Inspect(EntityOperation operation)
        {
            if (operation.IsEntity("Inquery"))
            {
                if (_securityService.HasModulePermission(_securityService.CurrentUser, AskTheLibModule.Id, Permissions.Use))
                {
                    if (_securityService.CurrentUser.UserType == UserTypes.Librarian)
                        return InspectionResult.Allow;
                    else if (_securityService.CurrentUser.UserType == UserTypes.Customer)
                    {
                        if (operation is EntityUpdate && (operation as EntityUpdate).IsCreate())
                            return InspectionResult.Allow;
                        else if (operation is EntityUpdate)
                        {
                            var update = operation as EntityUpdate;
                            if (update.ContainsRelation(User.ENTITY, RelationConsts.Customer))
                                return InspectionResult.Deny;

                            var q = new EntityQuery2(Inquery.EntityType, update.Id.Value);
                            q.AddProperties("Status");
                            q.Include(User.ENTITY, RelationConsts.Customer);
                            var inquery = _repository.Read(q);

                            if (inquery.GetData<QueryStatus>("Status") != QueryStatus.New)
                                return InspectionResult.Deny;

                            if (update.ContainsProperty("Status")
                                && update.Get<QueryStatus>("Status") != QueryStatus.Canceled)
                                return InspectionResult.Deny;

                            var customer = inquery.GetSingleRelation(User.ENTITY, RelationConsts.Customer);
                            if (customer != null && customer.Entity.Id == _securityService.CurrentUser.Id)
                                return InspectionResult.Allow;
                        }
                    }
                }
            }
            else if (operation.IsEntity(Notification.ENTITY) && operation is EntityUpdate)
            {
                var update = operation as EntityUpdate;
                if (update.IsCreate()
                    && update.ContainsRelation(Inquery.EntityType, RelationConsts.Inquery)
                    && _securityService.HasModulePermission(_securityService.CurrentUser, AskTheLibModule.Id, Permissions.Use)
                    && _securityService.CurrentUser.UserType == UserTypes.Librarian)
                {
                    return InspectionResult.Allow;
                }
            }
            return InspectionResult.None;
        }
Example #16
0
        public InspectionResult Inspect(EntityOperation operation)
        {
            if (operation.IsEntity("Inquery"))
            {
                if (_securityService.HasModulePermission(_securityService.CurrentUser, AskTheLibModule.Id, Permissions.Use))
                {
                    if (_securityService.CurrentUser.UserType == UserTypes.Librarian)
                    {
                        return(InspectionResult.Allow);
                    }
                    else if (_securityService.CurrentUser.UserType == UserTypes.Customer)
                    {
                        if (operation is EntityUpdate && (operation as EntityUpdate).IsCreate())
                        {
                            return(InspectionResult.Allow);
                        }
                        else if (operation is EntityUpdate)
                        {
                            var update = operation as EntityUpdate;
                            if (update.ContainsRelation(User.ENTITY, RelationConsts.Customer))
                            {
                                return(InspectionResult.Deny);
                            }

                            var q = new EntityQuery2(Inquery.EntityType, update.Id.Value);
                            q.AddProperties("Status");
                            q.Include(User.ENTITY, RelationConsts.Customer);
                            var inquery = _repository.Read(q);

                            if (inquery.GetData <QueryStatus>("Status") != QueryStatus.New)
                            {
                                return(InspectionResult.Deny);
                            }

                            if (update.ContainsProperty("Status") &&
                                update.Get <QueryStatus>("Status") != QueryStatus.Canceled)
                            {
                                return(InspectionResult.Deny);
                            }

                            var customer = inquery.GetSingleRelation(User.ENTITY, RelationConsts.Customer);
                            if (customer != null && customer.Entity.Id == _securityService.CurrentUser.Id)
                            {
                                return(InspectionResult.Allow);
                            }
                        }
                    }
                }
            }
            else if (operation.IsEntity(Notification.ENTITY) && operation is EntityUpdate)
            {
                var update = operation as EntityUpdate;
                if (update.IsCreate() &&
                    update.ContainsRelation(Inquery.EntityType, RelationConsts.Inquery) &&
                    _securityService.HasModulePermission(_securityService.CurrentUser, AskTheLibModule.Id, Permissions.Use) &&
                    _securityService.CurrentUser.UserType == UserTypes.Librarian)
                {
                    return(InspectionResult.Allow);
                }
            }
            return(InspectionResult.None);
        }