コード例 #1
0
        public KontentPublishingModule() : base(MODULE_NAME)
        {
            _settings = new SyncSettings();
            _settings.LoadFromConfig();

            if (_settings.IsValid())
            {
                _assetSync       = new AssetSync(_settings);
                _languageSync    = new LanguageSync(_settings);
                _contentTypeSync = new ContentTypeSync(_settings);
                _pageSync        = new PageSync(_settings, _assetSync, _contentTypeSync);
                _taxonomySync    = new TaxonomySync(_settings);
            }
        }
コード例 #2
0
        private IEnumerable <object> GetContentTypeElements(DataClassInfo contentType)
        {
            var    items           = GetItemsToSync(contentType.ClassName);
            object pendingCategory = null;

            var classGuid = contentType.ClassGUID;

            var fieldItems = items.SelectMany(item =>
            {
                if (item is FormCategoryInfo category)
                {
                    pendingCategory = new
                    {
                        external_id   = $"group|{classGuid}|{ValidationHelper.GetCodeName(category.CategoryName)}",
                        type          = "guidelines",
                        guidelines    = $"<h2>{category.CategoryName}</h2>",
                        content_group = new ExternalIdReference()
                        {
                            ExternalId = GetGroupExternalId(classGuid, CONTENT_GUID)
                        }
                    };
                    return(Array.Empty <object>());
                }

                if (item is FormFieldInfo field)
                {
                    var isRequired = !field.AllowEmpty &&
                                     // Exception - CM-13044 File field cannot be required because when a new document under workflow is created, it creates a document which appears published for a moment
                                     // but the attachment is not present at that time so publishing of such document would fail immediately
                                     (field.DataType != FieldDataType.File) &&
                                     // Exception - Allow empty value for root document name
                                     ((field.Name.ToLowerInvariant() != "documentname") || (contentType.ClassName.ToLowerInvariant() != "cms.root"));

                    var fieldGuid = field.Guid;

                    var element = new
                    {
                        external_id = GetFieldExternalId(classGuid, fieldGuid),
                        name        = GetElementName(field).LimitedTo(ELEMENT_MAXLENGTH),
                        codename    = field.Name.ToLower().LimitedTo(ELEMENT_MAXLENGTH),
                        guidelines  = GetElementGuidelines(field),
                        is_required = isRequired,
                        type        = GetElementType(field.DataType),
                        mode        = "single",
                        options     = (field.DataType == FieldDataType.Boolean)
                            ? new[] {
                            new MultipleChoiceElementOption {
                                Name       = "True",
                                Codename   = "true",
                                ExternalId = GetMultipleChoiceOptionExternalId(classGuid, fieldGuid, "true"),
                            },
                            new MultipleChoiceElementOption {
                                Name       = "False",
                                Codename   = "false",
                                ExternalId = GetMultipleChoiceOptionExternalId(classGuid, fieldGuid, "false"),
                            }
                        }
                            : null,
                        content_group = new ExternalIdReference()
                        {
                            ExternalId = GetGroupExternalId(classGuid, CONTENT_GUID)
                        }
                    };

                    if (pendingCategory != null)
                    {
                        // Only add category if there are some elements inside it
                        var elementWithCategory = new[] { pendingCategory, element };
                        pendingCategory         = null;
                        return(elementWithCategory);
                    }
                    return(new[] { element });
                }

                return(null);
            }).Where(x => x != null).ToList();

            var unsortedAttachmentsElement = new
            {
                external_id   = GetFieldExternalId(classGuid, UNSORTED_ATTACHMENTS_GUID),
                name          = UNSORTED_ATTACHMENTS_NAME,
                codename      = UNSORTED_ATTACHMENTS.ToLower(),
                type          = "asset",
                content_group = new ExternalIdReference()
                {
                    ExternalId = GetGroupExternalId(classGuid, UNSORTED_ATTACHMENTS_GUID)
                },
            };
            var relatedPagesElement = new
            {
                external_id = GetFieldExternalId(classGuid, RELATED_PAGES_GUID),
                name        = RELATED_PAGES_NAME,
                codename    = RELATED_PAGES.ToLower(),
                type        = "snippet",
                snippet     = new ExternalIdReference()
                {
                    ExternalId = GetSnippetExternalId(RELATED_PAGES_GUID)
                },
                content_group = new ExternalIdReference()
                {
                    ExternalId = GetGroupExternalId(classGuid, RELATED_PAGES_GUID)
                },
            };
            var categoriesElement = new
            {
                external_id    = GetFieldExternalId(classGuid, TaxonomySync.CATEGORIES_GUID),
                name           = TaxonomySync.CATEGORIES,
                codename       = TaxonomySync.CATEGORIES.ToLower(),
                type           = "taxonomy",
                taxonomy_group = new ExternalIdReference()
                {
                    ExternalId = TaxonomySync.GetTaxonomyExternalId(TaxonomySync.CATEGORIES_GUID)
                },
                content_group = new ExternalIdReference()
                {
                    ExternalId = GetGroupExternalId(classGuid, TaxonomySync.CATEGORIES_GUID)
                },
            };

            var allElements = fieldItems
                              .AsEnumerable()
                              .Concat(new object[] {
                unsortedAttachmentsElement,
                relatedPagesElement,
                categoriesElement,
            })
                              .ToList();

            return(allElements);
        }
コード例 #3
0
        private async Task UpsertVariant(TreeNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            var externalId = GetPageExternalId(node.NodeGUID);
            var endpoint   = GetVariantEndpoint(node);

            var contentType = DataClassInfoProvider.GetDataClassInfo(node.NodeClassName);

            if (contentType == null)
            {
                throw new InvalidOperationException($"Content type {node.NodeClassName} not found.");
            }

            var formInfo = FormHelper.GetFormInfo(node.ClassName, false);

            if (formInfo == null)
            {
                throw new InvalidOperationException($"Form info for {node.NodeClassName} not found.");
            }

            var fieldsToSync  = ContentTypeSync.GetItemsToSync(node.NodeClassName).OfType <FormFieldInfo>();
            var fieldElements = await Task.WhenAll(
                fieldsToSync.Select(async(field) => new
            {
                element = new
                {
                    external_id = ContentTypeSync.GetFieldExternalId(contentType.ClassGUID, field.Guid)
                },
                value = await GetElementValue(node, field)
            })
                );

            var unsortedAttachmentsElement = new
            {
                element = new
                {
                    external_id = ContentTypeSync.GetFieldExternalId(contentType.ClassGUID, ContentTypeSync.UNSORTED_ATTACHMENTS_GUID)
                },
                value = (object)GetAttachmentGuids(node, null).Select(guid => new {
                    external_id = AssetSync.GetAttachmentExternalId(guid)
                })
            };
            var categoriesElement = new
            {
                element = new
                {
                    external_id = ContentTypeSync.GetFieldExternalId(contentType.ClassGUID, TaxonomySync.CATEGORIES_GUID)
                },
                value = (object)GetCategoryGuids(node).Select(guid => new {
                    external_id = TaxonomySync.GetCategoryTermExternalId(guid)
                }).ToList()
            };

            var relationshipElements = GetRelationshipElements(node);

            var payload = new
            {
                elements = fieldElements
                           .Concat(new[] {
                    unsortedAttachmentsElement,
                    categoriesElement,
                })
                           .Concat(relationshipElements)
                           .ToList()
            };

            await ExecuteWithoutResponse(endpoint, HttpMethod.Put, payload, true);
        }