Beispiel #1
0
        /// <summary>
        /// Resolves the item.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="database">The database.</param>
        /// <returns>
        /// Item.
        /// </returns>
        /// <exception cref="System.NotSupportedException">You can not save a class that does not contain a property that represents the item ID. Ensure that at least one property has been marked to contain the Sitecore ID.
        /// or
        /// Cannot get ID for item</exception>
        public Item ResolveItem(object target, Database database)
        {
            if (target == null)
            {
                return(null);
            }

            ID       id;
            Language language      = null;
            int      versionNumber = -1;

            if (ItemConfig != null)
            {
                var item = ItemConfig.PropertyGetter(target) as Item;
                if (item != null)
                {
                    return(item);
                }
            }

            if (IdConfig == null)
            {
                throw new NotSupportedException(
                          "You can not save a class that does not contain a property that represents the item ID. Ensure that at least one property has been marked to contain the Sitecore ID. Type: {0}".Formatted(target.GetType().FullName));
            }

            id = GetId(target);

            language = GetLanguage(target);

            if (VersionConfig != null)
            {
                var valueInt = VersionConfig.PropertyInfo.GetValue(target, null);
                if (valueInt is int)
                {
                    versionNumber = (int)valueInt;
                }
                else if (valueInt is string)
                {
                    int.TryParse(valueInt as string, out versionNumber);
                }
            }

            if (language != null && versionNumber > 0)
            {
                return(database.GetItem(id, language, new global::Sitecore.Data.Version(versionNumber)));
            }
            else if (language != null)
            {
                return(database.GetItem(id, language));
            }
            else
            {
                return(database.GetItem(id));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Resolves the item.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="database">The database.</param>
        /// <returns>
        /// Item.
        /// </returns>
        /// <exception cref="System.NotSupportedException">You can not save a class that does not contain a property that represents the item ID. Ensure that at least one property has been marked to contain the Sitecore ID.
        /// or
        /// Cannot get ID for item</exception>
        public Item ResolveItem(object target, Database database)
        {
            if (target == null)
            {
                return(null);
            }

            ID       id;
            Language language      = null;
            int      versionNumber = -1;

            if (ItemConfig != null)
            {
                var item = ItemConfig.PropertyGetter(target) as Item;
                if (item != null)
                {
                    return(item);
                }
            }

            if (IdConfig == null && ItemUriConfig == null)
            {
                string message =
                    "Failed item resolve - You cannot save a class that does not contain a property that represents the item ID. Ensure that at least one property has been marked to contain the Sitecore ID. Type: {0}"
                    .Formatted(target.GetType().FullName);
                Sitecore.Diagnostics.Log.Error(message, this);

                LogDumpConfiguration();

                throw new NotSupportedException(message);
            }

            id            = GetId(target);
            language      = GetLanguage(target);
            versionNumber = GetVersion(target);

            if (language != null && versionNumber > 0)
            {
                return(database.GetItem(id, language, global::Sitecore.Data.Version.Parse(versionNumber)));
            }
            else if (language != null)
            {
                return(database.GetItem(id, language));
            }
            else
            {
                return(database.GetItem(id));
            }
        }