void WithQueryHints(QueryHints hints)
        {
            if (hints == QueryHints.Empty)
            {
                return;
            }

            //var contentItemVersionCriteria = BindItemVersionCriteria();
            var contentItemCriteria = BindItemCriteria();

            var contentItemMetadata = _session.SessionFactory.GetClassMetadata(typeof(ContentItemRecord));

            // break apart and group hints by their first segment
            var hintDictionary = hints.Records
                                 .Select(hint => new { Hint = hint, Segments = hint.Split('.') })
                                 .GroupBy(item => item.Segments.FirstOrDefault())
                                 .ToDictionary(grouping => grouping.Key, StringComparer.InvariantCultureIgnoreCase);

            // locate hints that match properties in the ContentItemVersionRecord
            //foreach (var hit in contentItemVersionMetadata.PropertyNames.Where(hintDictionary.ContainsKey).SelectMany(key => hintDictionary[key]))
            //{
            //    contentItemVersionCriteria.SetFetchMode(hit.Hint, FetchMode.Eager);
            //    hit.Segments.Take(hit.Segments.Count() - 1).Aggregate(contentItemVersionCriteria, ExtendCriteria);
            //}

            // locate hints that match properties in the ContentItemRecord
            //foreach (var hit in contentItemMetadata.PropertyNames.Where(hintDictionary.ContainsKey).SelectMany(key => hintDictionary[key]))
            //{
            //    contentItemVersionCriteria.SetFetchMode("ContentItemRecord." + hit.Hint, FetchMode.Eager);
            //    hit.Segments.Take(hit.Segments.Count() - 1).Aggregate(contentItemCriteria, ExtendCriteria);
            //}

            //if (hintDictionary.SelectMany(x => x.Value).Any(x => x.Segments.Count() > 1))
            //    contentItemVersionCriteria.SetResultTransformer(new DistinctRootEntityResultTransformer());
        }
        private IEnumerable <ContentItemVersionRecord> GetManyImplementation(QueryHints hints, Action <ICriteria, ICriteria> predicate)
        {
            var session = _sessionLocator.Value.For(typeof(ContentItemRecord));
            var contentItemVersionCriteria = session.CreateCriteria(typeof(ContentItemVersionRecord));
            var contentItemCriteria        = contentItemVersionCriteria.CreateCriteria("ContentItemRecord");

            predicate(contentItemCriteria, contentItemVersionCriteria);

            var contentItemMetadata        = session.SessionFactory.GetClassMetadata(typeof(ContentItemRecord));
            var contentItemVersionMetadata = session.SessionFactory.GetClassMetadata(typeof(ContentItemVersionRecord));

            if (hints != QueryHints.Empty)
            {
                // break apart and group hints by their first segment
                var hintDictionary = hints.Records
                                     .Select(hint => new { Hint = hint, Segments = hint.Split('.') })
                                     .GroupBy(item => item.Segments.FirstOrDefault())
                                     .ToDictionary(grouping => grouping.Key, StringComparer.InvariantCultureIgnoreCase);

                // locate hints that match properties in the ContentItemVersionRecord
                foreach (var hit in contentItemVersionMetadata.PropertyNames.Where(hintDictionary.ContainsKey).SelectMany(key => hintDictionary[key]))
                {
                    contentItemVersionCriteria.SetFetchMode(hit.Hint, FetchMode.Eager);
                    hit.Segments.Take(hit.Segments.Count() - 1).Aggregate(contentItemVersionCriteria, ExtendCriteria);
                }

                // locate hints that match properties in the ContentItemRecord
                foreach (var hit in contentItemMetadata.PropertyNames.Where(hintDictionary.ContainsKey).SelectMany(key => hintDictionary[key]))
                {
                    contentItemVersionCriteria.SetFetchMode("ContentItemRecord." + hit.Hint, FetchMode.Eager);
                    hit.Segments.Take(hit.Segments.Count() - 1).Aggregate(contentItemCriteria, ExtendCriteria);
                }

                if (hintDictionary.SelectMany(x => x.Value).Any(x => x.Segments.Count() > 1))
                {
                    contentItemVersionCriteria.SetResultTransformer(new DistinctRootEntityResultTransformer());
                }
            }

            contentItemCriteria.SetCacheable(true);

            return(contentItemVersionCriteria.List <ContentItemVersionRecord>());
        }
 public IEnumerable <T> GetManyByVersionId <T>(IEnumerable <int> versionRecordIds, QueryHints hints) where T : class, IContent
 {
     return(GetManyByVersionId(versionRecordIds, hints).AsPart <T>());
 }
        public IEnumerable <ContentItem> GetManyByVersionId(IEnumerable <int> versionRecordIds, QueryHints hints)
        {
            var contentItemVersionRecords = GetManyImplementation(hints, (contentItemCriteria, contentItemVersionCriteria) =>
                                                                  contentItemVersionCriteria.Add(Restrictions.In("Id", versionRecordIds.ToArray())));

            var itemsById = contentItemVersionRecords
                            .Select(r => Get(r.ContentItemRecord.Id, VersionOptions.VersionRecord(r.Id)))
                            .GroupBy(ci => ci.VersionRecord.Id)
                            .ToDictionary(g => g.Key);

            return(versionRecordIds.SelectMany(id => {
                IGrouping <int, ContentItem> values;
                return itemsById.TryGetValue(id, out values) ? values : Enumerable.Empty <ContentItem>();
            }).ToArray());
        }
        public IEnumerable <T> GetMany <T>(IEnumerable <int> ids, VersionOptions options, QueryHints hints) where T : class, IContent
        {
            var contentItemVersionRecords = GetManyImplementation(hints, (contentItemCriteria, contentItemVersionCriteria) => {
                contentItemCriteria.Add(Restrictions.In("Id", ids.ToArray()));
                if (options.IsPublished)
                {
                    contentItemVersionCriteria.Add(Restrictions.Eq("Published", true));
                }
                else if (options.IsLatest)
                {
                    contentItemVersionCriteria.Add(Restrictions.Eq("Latest", true));
                }
                else if (options.IsDraft && !options.IsDraftRequired)
                {
                    contentItemVersionCriteria.Add(
                        Restrictions.And(Restrictions.Eq("Published", false),
                                         Restrictions.Eq("Latest", true)));
                }
                else if (options.IsDraft || options.IsDraftRequired)
                {
                    contentItemVersionCriteria.Add(Restrictions.Eq("Latest", true));
                }
            });

            var itemsById = contentItemVersionRecords
                            .Select(r => Get(r.ContentItemRecord.Id, options.IsDraftRequired ? options : VersionOptions.VersionRecord(r.Id)))
                            .GroupBy(ci => ci.Id)
                            .ToDictionary(g => g.Key);

            return(ids.SelectMany(id => {
                IGrouping <int, ContentItem> values;
                return itemsById.TryGetValue(id, out values) ? values : Enumerable.Empty <ContentItem>();
            }).AsPart <T>().ToArray());
        }
        public virtual ContentItem Get(int id, VersionOptions options, QueryHints hints)
        {
            var         session = _contentManagerSession();
            ContentItem contentItem;

            ContentItemVersionRecord versionRecord = null;

            // obtain the root records based on version options
            if (options.VersionRecordId != 0)
            {
                // short-circuit if item held in session
                if (session.RecallVersionRecordId(options.VersionRecordId, out contentItem))
                {
                    return(contentItem);
                }

                versionRecord = _contentItemVersionRepository.Get(options.VersionRecordId);
            }
            else if (session.RecallContentRecordId(id, out contentItem))
            {
                // try to reload a previously loaded published content item

                if (options.IsPublished)
                {
                    return(contentItem);
                }

                versionRecord = contentItem.VersionRecord;
            }
            else
            {
                // do a query to load the records in case Get is called directly
                var contentItemVersionRecords = GetManyImplementation(hints,
                                                                      (contentItemCriteria, contentItemVersionCriteria) => {
                    contentItemCriteria.Add(Restrictions.Eq("Id", id));
                    if (options.IsPublished)
                    {
                        contentItemVersionCriteria.Add(Restrictions.Eq("Published", true));
                    }
                    else if (options.IsLatest)
                    {
                        contentItemVersionCriteria.Add(Restrictions.Eq("Latest", true));
                    }
                    else if (options.IsDraft && !options.IsDraftRequired)
                    {
                        contentItemVersionCriteria.Add(
                            Restrictions.And(Restrictions.Eq("Published", false),
                                             Restrictions.Eq("Latest", true)));
                    }
                    else if (options.IsDraft || options.IsDraftRequired)
                    {
                        contentItemVersionCriteria.Add(Restrictions.Eq("Latest", true));
                    }

                    contentItemVersionCriteria.SetFetchMode("ContentItemRecord", FetchMode.Eager);
                    contentItemVersionCriteria.SetFetchMode("ContentItemRecord.ContentType", FetchMode.Eager);
                    //contentItemVersionCriteria.SetMaxResults(1);
                });


                if (options.VersionNumber != 0)
                {
                    versionRecord = contentItemVersionRecords.FirstOrDefault(
                        x => x.Number == options.VersionNumber) ??
                                    _contentItemVersionRepository.Get(
                        x => x.ContentItemRecord.Id == id && x.Number == options.VersionNumber);
                }
                else
                {
                    versionRecord = contentItemVersionRecords.FirstOrDefault();
                }
            }

            // no record means content item is not in db
            if (versionRecord == null)
            {
                // check in memory
                var record = _contentItemRepository.Get(id);
                if (record == null)
                {
                    return(null);
                }

                versionRecord = GetVersionRecord(options, record);

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

            // return item if obtained earlier in session
            if (session.RecallVersionRecordId(versionRecord.Id, out contentItem))
            {
                if (options.IsDraftRequired && versionRecord.Published)
                {
                    return(BuildNewVersion(contentItem));
                }
                return(contentItem);
            }

            // allocate instance and set record property
            contentItem = New(versionRecord.ContentItemRecord.ContentType.Name);
            contentItem.VersionRecord = versionRecord;

            // store in session prior to loading to avoid some problems with simple circular dependencies
            session.Store(contentItem);

            // create a context with a new instance to load
            var context = new LoadContentContext(contentItem);

            // invoke handlers to acquire state, or at least establish lazy loading callbacks
            Handlers.Invoke(handler => handler.Loading(context), Logger);
            Handlers.Invoke(handler => handler.Loaded(context), Logger);

            // when draft is required and latest is published a new version is appended
            if (options.IsDraftRequired && versionRecord.Published)
            {
                contentItem = BuildNewVersion(context.ContentItem);
            }

            return(contentItem);
        }
 IContentQuery <T> IContentQuery <T> .WithQueryHints(QueryHints hints)
 {
     _query.WithQueryHints(hints);
     return(this);
 }
示例#8
0
        public static T Get <T>(this IContentManager manager, int id, VersionOptions options, QueryHints hints) where T : class, IContent
        {
            var contentItem = manager.Get(id, options, hints);

            return(contentItem == null ? null : contentItem.Get <T>());
        }
        public static IEnumerable <T> GetTooMany <T>(this IContentManager contentManager, IEnumerable <int> ids, VersionOptions versionOptions, QueryHints queryHints) where T : class, IContent
        {
            if (ids == null)
            {
                return(null);
            }
            var result     = new List <T>();
            var nextIdList = new List <int>();

            var pageSize     = MaxPageSize;
            var maxPageIndex = Math.Floor((double)ids.Count() / MaxPageSize);

            for (var page = 0; page <= maxPageIndex; page++)
            {
                if (maxPageIndex == page)
                {
                    pageSize = ids.Count() % MaxPageSize;
                }
                if (pageSize > 0)
                {
                    result.AddRange(contentManager.GetMany <T>(ids.Skip(2000 * page).Take(pageSize), versionOptions, queryHints));
                }
            }
            return(result);
        }
        public IEnumerable<ContentItem> GetManyByVersionId(IEnumerable<int> versionRecordIds, QueryHints hints) {
            if (!versionRecordIds.Any()) {
                // since there are no ids, I have to get no item, so it makes
                // sense to not do anything at all
                return Enumerable.Empty<ContentItem>();
            }
            var contentItemVersionRecords = GetManyImplementation(hints, (contentItemCriteria, contentItemVersionCriteria) =>
                contentItemVersionCriteria.Add(Restrictions.In("Id", versionRecordIds.ToArray())));

            var itemsById = contentItemVersionRecords
                .Select(r => Get(r.ContentItemRecord.Id, VersionOptions.VersionRecord(r.Id)))
                .Where(ci => ci != null)
                .GroupBy(ci => ci.VersionRecord.Id)
                .ToDictionary(g => g.Key);

            return versionRecordIds.SelectMany(id => {
                IGrouping<int, ContentItem> values;
                return itemsById.TryGetValue(id, out values) ? values : Enumerable.Empty<ContentItem>();
            }).ToArray();
        }