/// <summary>
        /// Sets the publishing values for names and properties.
        /// </summary>
        /// <param name="content"></param>
        /// <param name="impact"></param>
        /// <returns>A value indicating whether it was possible to publish the names and values for the specified
        /// culture(s). The method may fail if required names are not set, but it does NOT validate property data</returns>
        public static bool PublishCulture(this IContent content, CultureImpact impact)
        {
            if (impact == null)
            {
                throw new ArgumentNullException(nameof(impact));
            }

            // the variation should be supported by the content type properties
            //  if the content type is invariant, only '*' and 'null' is ok
            //  if the content type varies, everything is ok because some properties may be invariant
            if (!content.ContentType.SupportsPropertyVariation(impact.Culture, "*", true))
            {
                throw new NotSupportedException($"Culture \"{impact.Culture}\" is not supported by content type \"{content.ContentType.Alias}\" with variation \"{content.ContentType.Variations}\".");
            }

            // set names
            if (impact.ImpactsAllCultures)
            {
                foreach (var c in content.AvailableCultures) // does NOT contain the invariant culture
                {
                    var name = content.GetCultureName(c);
                    if (string.IsNullOrWhiteSpace(name))
                    {
                        return(false);
                    }
                    content.SetPublishInfo(c, name, DateTime.Now);
                }
            }
            else if (impact.ImpactsOnlyInvariantCulture)
            {
                if (string.IsNullOrWhiteSpace(content.Name))
                {
                    return(false);
                }
                // PublishName set by repository - nothing to do here
            }
            else if (impact.ImpactsExplicitCulture)
            {
                var name = content.GetCultureName(impact.Culture);
                if (string.IsNullOrWhiteSpace(name))
                {
                    return(false);
                }
                content.SetPublishInfo(impact.Culture, name, DateTime.Now);
            }

            // set values
            // property.PublishValues only publishes what is valid, variation-wise,
            // but accepts any culture arg: null, all, specific
            foreach (var property in content.Properties)
            {
                // for the specified culture (null or all or specific)
                property.PublishValues(impact.Culture);

                // maybe the specified culture did not impact the invariant culture, so PublishValues
                // above would skip it, yet it *also* impacts invariant properties
                if (impact.ImpactsAlsoInvariantProperties)
                {
                    property.PublishValues(null);
                }
            }

            content.PublishedState = PublishedState.Publishing;
            return(true);
        }
Пример #2
0
        /// <summary>
        /// Tries to create an impact instance representing the impact of a culture set,
        /// in the context of a content item variation.
        /// </summary>
        /// <param name="culture">The culture code.</param>
        /// <param name="isDefault">A value indicating whether the culture is the default culture.</param>
        /// <param name="variation">A content variation.</param>
        /// <param name="throwOnFail">A value indicating whether to throw if the impact cannot be created.</param>
        /// <param name="impact">The impact if it could be created, otherwise null.</param>
        /// <returns>A value indicating whether the impact could be created.</returns>
        /// <remarks>
        /// <para>Validates that the culture is compatible with the variation.</para>
        /// </remarks>
        internal static bool TryCreate(string culture, bool isDefault, ContentVariation variation, bool throwOnFail, out CultureImpact impact)
        {
            impact = null;

            // if culture is invariant...
            if (culture == null)
            {
                // ... then variation must not vary by culture ...
                if (variation.VariesByCulture())
                {
                    if (throwOnFail)
                    {
                        throw new InvalidOperationException("The invariant culture is not compatible with a varying variation.");
                    }
                    return(false);
                }

                // ... and it cannot be default
                if (isDefault)
                {
                    if (throwOnFail)
                    {
                        throw new InvalidOperationException("The invariant culture can not be the default culture.");
                    }
                    return(false);
                }

                impact = Invariant;
                return(true);
            }

            // if culture is 'all'...
            if (culture == "*")
            {
                // ... it cannot be default
                if (isDefault)
                {
                    if (throwOnFail)
                    {
                        throw new InvalidOperationException("The 'all' culture can not be the default culture.");
                    }
                    return(false);
                }

                // if variation does not vary by culture, then impact is invariant
                impact = variation.VariesByCulture() ? All : Invariant;
                return(true);
            }

            // neither null nor "*" - cannot be the empty string
            if (culture.IsNullOrWhiteSpace())
            {
                if (throwOnFail)
                {
                    throw new ArgumentException("Cannot be the empty string.", nameof(culture));
                }
                return(false);
            }

            // if culture is specific, then variation must vary
            if (!variation.VariesByCulture())
            {
                if (throwOnFail)
                {
                    throw new InvalidOperationException($"The variant culture {culture} is not compatible with an invariant variation.");
                }
                return(false);
            }

            // return specific impact
            impact = new CultureImpact(culture, isDefault);
            return(true);
        }