public void ItemsSetAndBatchInitialisedReturnsBatchedItems()
        {
            var importContentSession = new ImportContentSession(_contentManager.Object);

            importContentSession.Set("/Id=One", "TestType");
            importContentSession.Set("/Id=Two", "TestType");
            importContentSession.Set("/Id=Three", "TestType");
            importContentSession.Set("/Id=Four", "TestType");
            importContentSession.Set("/Id=Five", "TestType");

            importContentSession.InitializeBatch(1, 2);


            var comparer = new ContentIdentity.ContentIdentityEqualityComparer();

            Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Two")));
            Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Three")));
            Assert.That(importContentSession.GetNextInBatch(), Is.Null);

            importContentSession.InitializeBatch(2, 5);

            //item with "/Id=Three" should not be returned twice in the same session
            Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Four")));
            Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Five")));
            Assert.That(importContentSession.GetNextInBatch(), Is.Null);
        }
        public void ContentIdentitiesWithKeysAddedInDifferentOrderAreEqual() {
            var comparer = new ContentIdentity.ContentIdentityEqualityComparer();

            var identity1 = new ContentIdentity("/foo=bar");
            Assert.That(comparer.Equals(identity1, new ContentIdentity(identity1.ToString())));

            var identity2 = new ContentIdentity(@"/foo=bar/abaz=quux\/fr\\ed=foo/yarg=yiu=foo");
            Assert.That(comparer.Equals(identity2, new ContentIdentity(identity2.ToString())));
        }
        public void ContentIdentitiesWithKeysAddedInDifferentOrderAreEqual()
        {
            var comparer = new ContentIdentity.ContentIdentityEqualityComparer();

            var identity1 = new ContentIdentity("/foo=bar");

            Assert.That(comparer.Equals(identity1, new ContentIdentity(identity1.ToString())));

            var identity2 = new ContentIdentity(@"/foo=bar/abaz=quux\/fr\\ed=foo/yarg=yiu=foo");

            Assert.That(comparer.Equals(identity2, new ContentIdentity(identity2.ToString())));
        }
        public void ItemsSetAndUninitializedReturnsAllItems() {
            var importContentSession = new ImportContentSession(_contentManager.Object);

            importContentSession.Set("/Id=One", "TestType");
            importContentSession.Set("/Id=Two", "TestType");
            importContentSession.Set("/Id=Three", "TestType");

            var comparer = new ContentIdentity.ContentIdentityEqualityComparer();
            Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=One")));
            Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Two")));
            Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Three")));
            Assert.That(importContentSession.GetNextInBatch(), Is.Null);
        }
        public void ItemsSetAndUninitializedReturnsAllItems()
        {
            var importContentSession = new ImportContentSession(_contentManager.Object);

            importContentSession.Set("/Id=One", "TestType");
            importContentSession.Set("/Id=Two", "TestType");
            importContentSession.Set("/Id=Three", "TestType");

            var comparer = new ContentIdentity.ContentIdentityEqualityComparer();

            Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=One")));
            Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Two")));
            Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Three")));
            Assert.That(importContentSession.GetNextInBatch(), Is.Null);
        }
        public ContentItem Get(string id) {
            var contentIdentity = new ContentIdentity(id);

            // lookup in local cache
            if (_identities.ContainsKey(contentIdentity))
                return _contentManager.Get(_identities[contentIdentity], VersionOptions.DraftRequired);

            // no result ? then check if there are some more content items to load from the db
            if(_lastIndex != int.MaxValue) {
                
                var equalityComparer = new ContentIdentity.ContentIdentityEqualityComparer();
                IEnumerable<ContentItem> block;
            
                // load identities in blocks
                while ((block = _contentManager.HqlQuery()
                    .ForVersion(VersionOptions.Latest)
                    .OrderBy(x => x.ContentItemVersion(), x => x.Asc("Id"))
                    .Slice(_lastIndex, BulkPage)).Any()) {

                        foreach (var item in block) {
                            _lastIndex++;

                            // ignore content item if it has already been imported
                            if (_contentItemIds.ContainsKey(item.Id)) {
                                continue;
                            }

                            var identity = _contentManager.GetItemMetadata(item).Identity;

                            // ignore content item if the same identity is already present
                            if (_identities.ContainsKey(identity)) {
                                continue;
                            }

                            _identities.Add(identity, item.Id);
                            _contentItemIds.Add(item.Id, identity);
                        
                            if (equalityComparer.Equals(identity, contentIdentity)) {
                                return _contentManager.Get(item.Id, VersionOptions.DraftRequired);
                            }
                        }

                    _contentManager.Flush();
                    _contentManager.Clear();
                }
            }

            _lastIndex = int.MaxValue;

            if(!_contentTypes.ContainsKey(contentIdentity)) {
                throw new ArgumentException("Unknown content type for " + id);
                
            }

            var contentItem = _contentManager.Create(_contentTypes[contentIdentity], VersionOptions.Draft);
            _identities.Add(contentIdentity, contentItem.Id);
            _contentItemIds.Add(contentItem.Id, contentIdentity);

            return contentItem;
        }
        public void GetNextInBatchInitialisedTwoBatchesReturnsItemsOnceEach()
        {
            var session = new ImportContentSession(_contentManager.Object);

            session.Set(_testItemIdentity1.ToString(), "TestContentType");
            session.Set(_testItemIdentity2.ToString(), "TestContentType");
            session.Set(_testItemIdentity3.ToString(), "TestContentType");
            session.Set(_testItemIdentity4.ToString(), "TestContentType");
            session.Set(_testItemIdentity5.ToString(), "TestContentType");

            session.InitializeBatch(0, 2);
            var firstIdentity = session.GetNextInBatch();
            //get later item as dependency
            var dependencyItem     = session.Get(_testItemIdentity5.ToString(), VersionOptions.Latest);
            var dependencyIdentity = session.GetNextInBatch();
            var secondIdentity     = session.GetNextInBatch();
            var afterBatch1        = session.GetNextInBatch();

            session.InitializeBatch(2, 2);
            var thirdIdentity = session.GetNextInBatch();
            var fourthdentity = session.GetNextInBatch();
            var afterBatch2   = session.GetNextInBatch();

            session.InitializeBatch(4, 2);
            var fifthIdentity = session.GetNextInBatch();
            var afterBatch3   = session.GetNextInBatch();

            var comparer = new ContentIdentity.ContentIdentityEqualityComparer();

            Assert.That(comparer.Equals(_testItemIdentity1, firstIdentity));
            Assert.That(comparer.Equals(_testItemIdentity5, dependencyIdentity));
            Assert.That(comparer.Equals(_testItemIdentity2, secondIdentity));
            Assert.That(afterBatch1, Is.Null);

            Assert.That(comparer.Equals(_testItemIdentity3, thirdIdentity));
            Assert.That(comparer.Equals(_testItemIdentity4, fourthdentity));
            Assert.That(afterBatch2, Is.Null);

            Assert.That(fifthIdentity, Is.Null); //already processed as dependency
            Assert.That(afterBatch3, Is.Null);
        }
        public void GetNextInBatchInitialisedWithOneItemReturnsOneItemThenNull()
        {
            var session = new ImportContentSession(_contentManager.Object);

            session.Set(_testItemIdentity1.ToString(), "TestContentType");
            session.InitializeBatch(0, 1);
            var firstIdentity  = session.GetNextInBatch();
            var secondIdentity = session.GetNextInBatch();

            var comparer = new ContentIdentity.ContentIdentityEqualityComparer();

            Assert.That(comparer.Equals(_testItemIdentity1, firstIdentity));
            Assert.That(secondIdentity, Is.Null);
        }
        private IEnumerable<ContentItem> ResolveIdentity(ContentIdentity identity) {
            var identifier = identity.Get("User.UserName");

            if (identifier == null) {
                return null;
            }

            var comparer = new ContentIdentity.ContentIdentityEqualityComparer();
            return _contentManager
                .Query<UserPart, UserPartRecord>()
                .Where(p => p.UserName == identifier)
                .List<ContentItem>()
                .Where(c => comparer.Equals(identity, _contentManager.GetItemMetadata(c).Identity));
        }
        public ContentItem Get(string id) {
            var contentIdentity = new ContentIdentity(id);

            // lookup in local cache
            if (_identities.ContainsKey(contentIdentity))
                return _identities[contentIdentity];

            // no result ? then check if there are some more content items to load from the db

            if(_lastIndex == int.MaxValue) {
                // everything has already been loaded from db
                return null;
            }

            var equalityComparer = new ContentIdentity.ContentIdentityEqualityComparer();
            IEnumerable<ContentItem> block;
            
            // load identities in blocks
            while ((block = _contentManager.HqlQuery()
                .ForVersion(VersionOptions.Latest)
                .OrderBy(x => x.ContentItemVersion(), x => x.Asc("Id"))
                .Slice(_lastIndex, BulkPage)).Any()) {

                    foreach (var item in block) {
                        _lastIndex++;

                        // ignore content item if it has already been imported
                        if (_contentItemIds.ContainsKey(item.Id)) {
                            continue;
                        }

                        var identity = _contentManager.GetItemMetadata(item).Identity;

                        // ignore content item if the same identity is already present
                        if (_identities.ContainsKey(identity)) {
                            continue;
                        }

                        _identities.Add(identity, item);
                        _contentItemIds.Add(item.Id, identity);
                        
                        if (equalityComparer.Equals(identity, contentIdentity)) {
                            return item;
                        }
                    }
            }

            _lastIndex = int.MaxValue;
            return null;
        }
        private IEnumerable <ContentItem> ResolveIdentity(ContentIdentity identity)
        {
            var identifier = identity.Get("User.UserName");

            if (identifier == null)
            {
                return(null);
            }

            var comparer = new ContentIdentity.ContentIdentityEqualityComparer();

            return(_contentManager
                   .Query <UserPart, UserPartRecord>()
                   .Where(p => p.UserName == identifier)
                   .List <ContentItem>()
                   .Where(c => comparer.Equals(identity, _contentManager.GetItemMetadata(c).Identity)));
        }
        public ContentItem Get(string id) {
            var contentIdentity = new ContentIdentity(id);

            if (_dictionary.ContainsKey(contentIdentity))
                return _dictionary[contentIdentity];

            foreach (var item in _contentManager.Query(VersionOptions.Latest).List()) {
                var identity = _contentManager.GetItemMetadata(item).Identity;
                var equalityComparer = new ContentIdentity.ContentIdentityEqualityComparer();
                if (equalityComparer.Equals(identity, contentIdentity)) {
                    _dictionary.Add(identity, item);
                    return item;
                }
            }

            return null;
        }
        public void ItemsSetAndBatchInitialisedReturnsBatchedItems() {
            var importContentSession = new ImportContentSession(_contentManager.Object);

            importContentSession.Set("/Id=One", "TestType");
            importContentSession.Set("/Id=Two", "TestType");
            importContentSession.Set("/Id=Three", "TestType");
            importContentSession.Set("/Id=Four", "TestType");
            importContentSession.Set("/Id=Five", "TestType");

            importContentSession.InitializeBatch(1, 2);


            var comparer = new ContentIdentity.ContentIdentityEqualityComparer();
            Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Two")));
            Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Three")));
            Assert.That(importContentSession.GetNextInBatch(), Is.Null);

            importContentSession.InitializeBatch(2, 5);

            //item with "/Id=Three" should not be returned twice in the same session
            Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Four")));
            Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Five")));
            Assert.That(importContentSession.GetNextInBatch(), Is.Null);
        }
        public void GetNextInBatchInitialisedTwoBatchesReturnsItemsOnceEach() {
            var session = new ImportContentSession(_contentManager.Object);
            session.Set(_testItemIdentity1.ToString(), "TestContentType");
            session.Set(_testItemIdentity2.ToString(), "TestContentType");
            session.Set(_testItemIdentity3.ToString(), "TestContentType");
            session.Set(_testItemIdentity4.ToString(), "TestContentType");
            session.Set(_testItemIdentity5.ToString(), "TestContentType");

            session.InitializeBatch(0, 2);
            var firstIdentity = session.GetNextInBatch();
            //get later item as dependency
            var dependencyItem = session.Get(_testItemIdentity5.ToString(), VersionOptions.Latest);
            var dependencyIdentity = session.GetNextInBatch();
            var secondIdentity = session.GetNextInBatch();
            var afterBatch1 = session.GetNextInBatch();

            session.InitializeBatch(2, 2);
            var thirdIdentity = session.GetNextInBatch();
            var fourthdentity = session.GetNextInBatch();
            var afterBatch2 = session.GetNextInBatch();

            session.InitializeBatch(4, 2);
            var fifthIdentity = session.GetNextInBatch();
            var afterBatch3 = session.GetNextInBatch();

            var comparer = new ContentIdentity.ContentIdentityEqualityComparer();
            Assert.That(comparer.Equals(_testItemIdentity1, firstIdentity));
            Assert.That(comparer.Equals(_testItemIdentity5, dependencyIdentity));
            Assert.That(comparer.Equals(_testItemIdentity2, secondIdentity));
            Assert.That(afterBatch1, Is.Null);

            Assert.That(comparer.Equals(_testItemIdentity3, thirdIdentity));
            Assert.That(comparer.Equals(_testItemIdentity4, fourthdentity));
            Assert.That(afterBatch2, Is.Null);

            Assert.That(fifthIdentity, Is.Null); //already processed as dependency
            Assert.That(afterBatch3, Is.Null);
        }
        public void GetNextInBatchInitialisedWithOneItemReturnsOneItemThenNull() {
            var session = new ImportContentSession(_contentManager.Object);
            session.Set(_testItemIdentity1.ToString(), "TestContentType");
            session.InitializeBatch(0, 1);
            var firstIdentity = session.GetNextInBatch();
            var secondIdentity = session.GetNextInBatch();

            var comparer = new ContentIdentity.ContentIdentityEqualityComparer();
            Assert.That(comparer.Equals(_testItemIdentity1, firstIdentity));
            Assert.That(secondIdentity, Is.Null);
        }
        public ContentItem Get(string id, string contentTypeHint = null) {
            var contentIdentity = new ContentIdentity(id);

            // lookup in local cache
            if (_identities.ContainsKey(contentIdentity)) {
                var result = _contentManager.Get(_identities[contentIdentity], VersionOptions.DraftRequired);

                // if two identities are conflicting, then ensure that there types are the same
                // e.g., importing a blog as home page (alias=) and the current home page is a page, the blog
                // won't be imported, and blog posts will be attached to the page
                if (contentTypeHint == null || result.ContentType == contentTypeHint) {
                    return result;
                }
            }

            // no result ? then check if there are some more content items to load from the db
            if(_lastIndex != int.MaxValue) {
                
                var equalityComparer = new ContentIdentity.ContentIdentityEqualityComparer();
                IEnumerable<ContentItem> block;
            
                // load identities in blocks
                while ((block = _contentManager.HqlQuery()
                    .ForVersion(VersionOptions.Latest)
                    .OrderBy(x => x.ContentItemVersion(), x => x.Asc("Id"))
                    .Slice(_lastIndex, BulkPage)).Any()) {

                        foreach (var item in block) {
                            _lastIndex++;

                            // ignore content item if it has already been imported
                            if (_contentItemIds.ContainsKey(item.Id)) {
                                continue;
                            }

                            var identity = _contentManager.GetItemMetadata(item).Identity;

                            // ignore content item if the same identity is already present
                            if (_identities.ContainsKey(identity)) {
                                continue;
                            }

                            _identities.Add(identity, item.Id);
                            _contentItemIds.Add(item.Id, identity);
                        
                            if (equalityComparer.Equals(identity, contentIdentity)) {
                                return _contentManager.Get(item.Id, VersionOptions.DraftRequired);
                            }
                        }

                    _contentManager.Flush();
                    _contentManager.Clear();
                }
            }

            _lastIndex = int.MaxValue;

            if(!_contentTypes.ContainsKey(contentIdentity)) {
                throw new ArgumentException("Unknown content type for " + id);
                
            }

            var contentItem = _contentManager.Create(_contentTypes[contentIdentity], VersionOptions.Draft);
            _identities[contentIdentity] = contentItem.Id;
            _contentItemIds[contentItem.Id] = contentIdentity;

            return contentItem;
        }