//    public virtual ContentItem Clone(ContentItem contentItem) {
        //        // Mostly taken from: http://orchard.codeplex.com/discussions/396664
        //        var importContentSession = new ImportContentSession(this);

        //        var element = Export(contentItem);

        //        // If a handler prevents this element from being exported, it can't be cloned
        //        if (element == null) {
        //            throw new InvalidOperationException("The content item couldn't be cloned because a handler prevented it from being exported.");
        //        }

        //        var elementId = element.Attribute("Id");
        //        var copyId = elementId.Value + "-copy";
        //        elementId.SetValue(copyId);
        //        var status = element.Attribute("Status");
        //        if (status != null) status.SetValue("Draft"); // So the copy is always a draft.

        //        importContentSession.Set(copyId, element.Name.LocalName);

        //        Import(element, importContentSession);

        //        return importContentSession.Get(copyId, element.Name.LocalName);
        //    }

        //    public virtual ContentItem Restore(ContentItem contentItem, VersionOptions options) {
        //        // Invoke handlers.
        //        Handlers.Invoke(handler => handler.Restoring(new RestoreContentContext(contentItem, options)), Logger);

        //        // Get the latest version.
        //        var versions = contentItem.Record.Versions.OrderBy(x => x.Number).ToArray();
        //        var latestVersionRecord = versions.SingleOrDefault(x => x.Latest) ?? versions.Last();

        //        // Get the specified version.
        //        var specifiedVersionContentItem =
        //            contentItem.VersionRecord.Number == options.VersionNumber || contentItem.VersionRecord.Id == options.VersionRecordId
        //            ? contentItem
        //            : Get(contentItem.Id, options);

        //        // Create a new version record based on the specified version record.
        //        var rolledBackContentItem = BuildNewVersion(specifiedVersionContentItem);
        //        rolledBackContentItem.VersionRecord.Published = options.IsPublished;

        //        // Invoke handlers.
        //        Handlers.Invoke(handler => handler.Restored(new RestoreContentContext(rolledBackContentItem, options)), Logger);

        //        if (options.IsPublished) {
        //            // Unpublish the latest version.
        //            latestVersionRecord.Published = false;

        //            var publishContext = new PublishContentContext(rolledBackContentItem, previousItemVersionRecord: latestVersionRecord);

        //            Handlers.Invoke(handler => handler.Publishing(publishContext), Logger);
        //            Handlers.Invoke(handler => handler.Published(publishContext), Logger);
        //        }

        //        return rolledBackContentItem;
        //    }

        //    /// <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 ContentItemMetadata GetItemMetadata(IContent content) {
        //        var context = new GetContentItemMetadataContext {
        //            ContentItem = content.ContentItem,
        //            Metadata = new ContentItemMetadata()
        //        };

        //        Handlers.Invoke(handler => handler.GetContentItemMetadata(context), Logger);

        //        return context.Metadata;
        //    }

        //    public IEnumerable<GroupInfo> GetEditorGroupInfos(IContent content) {
        //        var metadata = GetItemMetadata(content);
        //        return metadata.EditorGroupInfo
        //            .GroupBy(groupInfo => groupInfo.Id)
        //            .Select(grouping => grouping.OrderBy(groupInfo => groupInfo.Position, new FlatPositionComparer()).FirstOrDefault());
        //    }

        //    public IEnumerable<GroupInfo> GetDisplayGroupInfos(IContent content) {
        //        var metadata = GetItemMetadata(content);
        //        return metadata.DisplayGroupInfo
        //            .GroupBy(groupInfo => groupInfo.Id)
        //            .Select(grouping => grouping.OrderBy(groupInfo => groupInfo.Position, new FlatPositionComparer()).FirstOrDefault());
        //    }

        //    public GroupInfo GetEditorGroupInfo(IContent content, string groupInfoId) {
        //        return GetEditorGroupInfos(content).FirstOrDefault(gi => string.Equals(gi.Id, groupInfoId, StringComparison.OrdinalIgnoreCase));
        //    }

        //    public GroupInfo GetDisplayGroupInfo(IContent content, string groupInfoId) {
        //        return GetDisplayGroupInfos(content).FirstOrDefault(gi => string.Equals(gi.Id, groupInfoId, StringComparison.OrdinalIgnoreCase));
        //    }

        //    public dynamic BuildDisplay(IContent content, string displayType = "", string groupId = "") {
        //        return _contentDisplay.Value.BuildDisplay(content, displayType, groupId);
        //    }

        //    public dynamic BuildEditor(IContent content, string groupId = "") {
        //        return _contentDisplay.Value.BuildEditor(content, groupId);
        //    }

        //    public dynamic UpdateEditor(IContent content, IUpdateModel updater, string groupId = "") {
        //        var context = new UpdateContentContext(content.ContentItem);

        //        Handlers.Invoke(handler => handler.Updating(context), Logger);

        //        var result = _contentDisplay.Value.UpdateEditor(content, updater, groupId);

        //        Handlers.Invoke(handler => handler.Updated(context), Logger);

        //        return result;
        //    }

        //    public void Clear() {
        //    }

        //    public IContentQuery<ContentItem> Query() {
        //        var query = _context.Resolve<IContentQuery>(TypedParameter.From<IContentManager>(this));
        //        return query.ForPart<ContentItem>();
        //    }

        //    public IHqlQuery HqlQuery() {
        //        return new DefaultHqlQuery(this, _sessionLocator.Value.For(typeof(ContentItemVersionRecord)), _sqlStatementProviders.Value, _shellSettings);
        //    }

        //    // Insert or Update imported data into the content manager.
        //    // Call content item handlers.
        //    public void Import(XElement element, ImportContentSession importContentSession) {
        //        var elementId = element.Attribute("Id");
        //        if (elementId == null) {
        //            return;
        //        }

        //        var identity = elementId.Value;

        //        if (String.IsNullOrWhiteSpace(identity)) {
        //            return;
        //        }

        //        var status = element.Attribute("Status");

        //        var item = importContentSession.Get(identity, VersionOptions.Latest, XmlConvert.DecodeName(element.Name.LocalName));
        //        if (item == null) {
        //            item = New(XmlConvert.DecodeName(element.Name.LocalName));
        //            if (status != null && status.Value == "Draft") {
        //                Create(item, VersionOptions.Draft);
        //            }
        //            else {
        //                Create(item);
        //            }
        //        }

        //        // create a version record if import handlers need it
        //        if(item.VersionRecord == null) {
        //            item.VersionRecord = new ContentItemVersionRecord {
        //                ContentItemRecord = new ContentItemRecord {
        //                    ContentType = AcquireContentTypeRecord(item.ContentType)
        //                },
        //                Number = 1,
        //                Latest = true,
        //                Published = true
        //            };
        //        }

        //        var context = new ImportContentContext(item, element, importContentSession);
        //        foreach (var contentHandler in Handlers) {
        //            contentHandler.Importing(context);
        //        }

        //        foreach (var contentHandler in Handlers) {
        //            contentHandler.Imported(context);
        //        }

        //        var savedItem = Get(item.Id, VersionOptions.Latest);

        //        // the item has been pre-created in the first pass of the import, create it in db
        //        if(savedItem == null) {
        //            if (status != null && status.Value == "Draft") {
        //                Create(item, VersionOptions.Draft);
        //            }
        //            else {
        //                Create(item);
        //            }
        //        }

        //        if (status == null || status.Value == Published) {
        //            Publish(item);
        //        }
        //    }

        //    public XElement Export(ContentItem contentItem) {
        //        var context = new ExportContentContext(contentItem, new XElement(XmlConvert.EncodeLocalName(contentItem.ContentType)));

        //        foreach (var contentHandler in Handlers) {
        //            contentHandler.Exporting(context);
        //        }

        //        foreach (var contentHandler in Handlers) {
        //            contentHandler.Exported(context);
        //        }

        //        if (context.Exclude) {
        //            return null;
        //        }

        //        context.Data.SetAttributeValue("Id", GetItemMetadata(contentItem).Identity.ToString());
        //        if (contentItem.IsPublished()) {
        //            context.Data.SetAttributeValue("Status", Published);
        //        }
        //        else {
        //            context.Data.SetAttributeValue("Status", Draft);
        //        }

        //        return context.Data;
        //    }

        private ContentTypeRecord AcquireContentTypeRecord(string contentType)
        {
            var contentTypeRecord = _contentStorageProvider
                                    .Query <ContentTypeRecord>(x => x.Name == contentType)
                                    .FirstOrDefault();

            if (contentTypeRecord == null)
            {
                //TEMP: this is not safe... ContentItem types could be created concurrently?
                contentTypeRecord = new ContentTypeRecord {
                    Name = contentType
                };
                _contentStorageProvider.Store(contentTypeRecord);
            }

            var contentTypeId = contentTypeRecord.Id;

            // There is a case when a content type record is created locally but the transaction is actually
            // cancelled. In this case we are caching an Id which is none existent, or might represent another
            // content type. Thus we need to ensure that the cache is valid, or invalidate it and retrieve it
            // another time.

            var result = _contentStorageProvider
                         .Query <ContentTypeRecord>(x => x.Id == contentTypeId)
                         .FirstOrDefault();

            if (result != null && result.Name.Equals(contentType, StringComparison.OrdinalIgnoreCase))
            {
                return(result);
            }

            // invalidate the cache entry and load it again
            return(AcquireContentTypeRecord(contentType));
        }
        private ContentTypeRecord AcquireContentTypeRecord(string contentType)
        {
            var contentTypeId = _cacheManager.Get(contentType + "_Record", ctx => {
                ctx.Monitor(_signals.When(contentType + "_Record"));

                var contentTypeRecord = _contentTypeRepository.Get(x => x.Name == contentType);

                if (contentTypeRecord == null)
                {
                    //TEMP: this is not safe... ContentItem types could be created concurrently?
                    contentTypeRecord = new ContentTypeRecord {
                        Name = contentType
                    };
                    _contentTypeRepository.Create(contentTypeRecord);
                }

                return(contentTypeRecord.Id);
            });

            // There is a case when a content type record is created locally but the transaction is actually
            // cancelled. In this case we are caching an Id which is none existent, or might represent another
            // content type. Thus we need to ensure that the cache is valid, or invalidate it and retrieve it
            // another time.

            var result = _contentTypeRepository.Get(contentTypeId);

            if (result != null && result.Name.Equals(contentType, StringComparison.OrdinalIgnoreCase))
            {
                return(result);
            }

            // invalidate the cache entry and load it again
            _signals.Trigger(contentType + "_Record");
            return(AcquireContentTypeRecord(contentType));
        }
Exemple #3
0
        private ContentTypeRecord AcquireContentTypeRecord(string contentType)
        {
            var contentTypeRecord = _contentTypeRepository.Get(x => x.Name == contentType);

            if (contentTypeRecord == null)
            {
                //TEMP: this is not safe... ContentItem types could be created concurrently?
                contentTypeRecord = new ContentTypeRecord {
                    Name = contentType
                };
                _contentTypeRepository.Create(contentTypeRecord);
            }
            return(contentTypeRecord);
        }
Exemple #4
0
        private int GetContentTypeRecordId(string contentType)
        {
            return(_cacheManager.Get(contentType + "_Record", true, ctx => {
                ctx.Monitor(_signals.When(contentType + "_Record"));

                var contentTypeRecord = _contentTypeRepository.Get(x => x.Name == contentType);

                if (contentTypeRecord == null)
                {
                    //TEMP: this is not safe... ContentItem types could be created concurrently?
                    contentTypeRecord = new ContentTypeRecord {
                        Name = contentType
                    };
                    _contentTypeRepository.Create(contentTypeRecord);
                }

                return contentTypeRecord.Id;
            }));
        }
Exemple #5
0
 protected override void OnContentTypeAdded(ContentTypeRecord contentType)
 {
     base.OnContentTypeAdded(contentType);
     FirstImport = true;
 }