public void Set(string id, string contentType)
        {
            var contentIdentity = new ContentIdentity(id);

            _contentTypes[contentIdentity] = contentType;
            _allIdentitiesForImport.Add(contentIdentity);
            _allIdentitiesForImportStatus[contentIdentity] = false;
        }
        /// <summary>
        /// Lookup for a content item based on a <see cref="ContentIdentity"/>. If multiple
        /// resolvers can give a result, the one with the highest priority is used. As soon as
        /// only one content item is returned from resolvers, it is returned as the result.
        /// </summary>
        /// <param name="contentIdentity">The <see cref="ContentIdentity"/> instance to lookup</param>
        /// <returns>The <see cref="ContentItem"/> instance represented by the identity object.</returns>
        public ContentItem ResolveIdentity(ContentIdentity contentIdentity)
        {
            var resolvers = _identityResolverSelectors.Value
                            .Select(x => x.GetResolver(contentIdentity))
                            .Where(x => x != null)
                            .OrderByDescending(x => x.Priority);

            if (!resolvers.Any())
            {
                return(null);
            }

            IEnumerable <ContentItem> contentItems = null;

            foreach (var resolver in resolvers)
            {
                var resolved = resolver.Resolve(contentIdentity).ToArray();

                // first pass
                if (contentItems == null)
                {
                    contentItems = resolved;
                }
                else   // subsquent passes means we need to intersect
                {
                    contentItems = contentItems.Intersect(resolved).ToArray();
                }

                if (contentItems.Count() == 1)
                {
                    return(contentItems.First());
                }
            }

            return(contentItems.FirstOrDefault());
        }
        public ContentItem Get(string id, VersionOptions versionOptions, string contentTypeHint = null)
        {
            var contentIdentity = new ContentIdentity(id);

            // lookup in local cache
            if (_identities.ContainsKey(contentIdentity))
            {
                if (_draftVersionRecordIds.ContainsKey(_identities[contentIdentity]))
                {
                    //draft was previously created. Recall.
                    versionOptions = VersionOptions.VersionRecord(_draftVersionRecordIds[_identities[contentIdentity]]);
                }
                var result = _contentManager.Get(_identities[contentIdentity], versionOptions);

                // 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);
                }
            }

            ContentItem existingItem = _contentManager.ResolveIdentity(contentIdentity);

            //ensure we have the correct version
            if (existingItem != null)
            {
                existingItem = _contentManager.Get(existingItem.Id, versionOptions);
            }

            if (existingItem == null && _identities.ContainsKey(contentIdentity))
            {
                existingItem = _contentManager.Get(_identities[contentIdentity], versionOptions);
            }

            if (existingItem != null)
            {
                _identities[contentIdentity] = existingItem.Id;
                if (versionOptions.IsDraftRequired)
                {
                    _draftVersionRecordIds[existingItem.Id] = existingItem.VersionRecord.Id;
                }
                return(existingItem);
            }

            //create item if not found and draft was requested, or it is found later in the import queue
            if (versionOptions.IsDraftRequired || _allIdentitiesForImportStatus.ContainsKey(contentIdentity))
            {
                var contentType = _contentTypes.ContainsKey(contentIdentity) ? _contentTypes[contentIdentity] : contentTypeHint;

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

                var contentItem = _contentManager.Create(contentType, VersionOptions.Draft);

                _identities[contentIdentity] = contentItem.Id;

                //store versionrecordid in case a draft is requested again
                _draftVersionRecordIds[contentItem.Id] = contentItem.VersionRecord.Id;

                //add the requested item as a dependency if it is not the currently running item
                if (_allIdentitiesForImportStatus.ContainsKey(contentIdentity) &&
                    !_allIdentitiesForImportStatus[contentIdentity])
                {
                    _dependencyIdentities.Enqueue(contentIdentity);
                }

                return(contentItem);
            }

            return(null);
        }
Example #4
0
 public ContentItemMetadata()
 {
     Identity = new ContentIdentity();
 }