Exemple #1
0
        public void Intercept(IInvocation invocation)
        {
            ContentTypeBase docType = (ContentTypeBase)invocation.InvocationTarget;

            string propertyName = invocation.Method.GetPropertyName();

            if (invocation.Method.IsGetter())
            {
                if (docType[propertyName] == null)
                {
                    // Type typeDocType = DocumentTypeManager.GetDocumentTypeType(docType.Source.NodeTypeAlias);
                    var typeDocType = ProxyUtil.GetUnproxiedType(docType);

                    PropertyInfo propInfo = typeDocType.GetProperty(propertyName);

                    object value = null;
                    try
                    {
                        value = ContentHelper.GetPropertyValueOrMixin(docType, propInfo);
                        docType[propertyName] = value;
                    }
                    catch (Exception exc)
                    {
                        throw new Exception(string.Format("Cannot set the value of a document type property {0}.{1} (document type: {2}) to value: '{3}' (value type: {4}). Error: {5}",
                                                          typeDocType.Name, propInfo.Name, propInfo.PropertyType.FullName,
                                                          value, value != null ? value.GetType().FullName : "", exc.Message));
                    }
                }

                invocation.ReturnValue = docType[propertyName];
                if (invocation.ReturnValue == null && invocation.Method.ReturnType == typeof(bool))
                {
                    invocation.ReturnValue = false;
                }
            }
            else
            {
                docType[propertyName] = invocation.Arguments[0];
            }
        }
Exemple #2
0
        /// <summary>
        /// Gets all ancestor nodes of a given type from a given node id.
        /// (parent's path back to root)
        /// </summary>
        /// <typeparam name="T">Strongly typed content item</typeparam>
        /// <param name="nodeId">Id of node for which to retrieve node ancestors</param>
        public static IEnumerable <T> GetAncestors <T>(int nodeId)
            where T : DocumentTypeBase, new()
        {
            Node parentNode = uQuery.GetNode(nodeId);

            string docTypeAlias = DocumentTypeManager.GetDocumentTypeAlias(typeof(T));

            IEnumerable <Node> ancestorNodes = parentNode.GetAncestorNodes();

            foreach (Node childNode in ancestorNodes)
            {
                // Check if this childNode is of a given document type and if not deleted
                if (docTypeAlias != childNode.NodeTypeAlias)
                {
                    continue;
                }

                var d = ContentHelper.GetByNode <T>(childNode);
                if (d != null)
                {
                    yield return(d);
                }
            }
        }
Exemple #3
0
 /// <summary>
 /// Gets all children nodes from a given node id.
 /// Note: This method returns only first level children - it doesn't return children's children.
 /// </summary>
 /// <param name="parentId">Parent node id of all children to get</param>
 public static IEnumerable <DocumentTypeBase> GetChildren(int parentId)
 {
     return(ContentHelper.GetChildren(parentId, false));
 }
Exemple #4
0
        internal static object GetPropertyValue(Node sourceNode, PropertyInfo propInfo, DocumentTypePropertyAttribute propAttr)
        {
            string propertyName;
            string propertyAlias;
            object value = null;

            if (propAttr == null)
            {
                propAttr = Util.GetAttribute <DocumentTypePropertyAttribute>(propInfo);
            }

            DocumentTypeManager.ReadPropertyNameAndAlias(propInfo, propAttr, out propertyName, out propertyAlias);

            var property = sourceNode.GetProperty(propertyAlias);


            if (property == null)
            {
                value = null;
            }

            else if (propInfo.PropertyType.Equals(typeof(Boolean)))
            {
                if (String.IsNullOrEmpty(property.Value) || property.Value == "0")
                {
                    value = false;
                }
                else
                {
                    value = true;
                }
            }
            else if (propAttr.CustomTypeConverter != null)
            {
                value = ((ICustomTypeConvertor)Activator.Current.GetInstance(propAttr.CustomTypeConverter)).ConvertValueWhenRead(property.Value);
            }
            else if (ContentHelper.PropertyConvertors.ContainsKey(propInfo.PropertyType))
            {
                // will be transformed later. TODO: move transformation here
                //value = ContentHelper.GetInnerXml(node.Id.ToString(), propertyAlias);

                value = PropertyConvertors[propInfo.PropertyType].ConvertValueWhenRead(property.Value);
            }
            else if (String.IsNullOrEmpty(property.Value))
            {
                // if property type is string or if it's some custom type, try to get the inner xml of this property within a node.
                if (propInfo.PropertyType == typeof(string) ||
                    ContentHelper.PropertyConvertors.ContainsKey(propInfo.PropertyType))
                {
                    value = ContentHelper.GetInnerXml(sourceNode.Id.ToString(), propertyAlias);
                    if (value == null && propInfo.PropertyType == typeof(string))
                    {
                        value = string.Empty;
                    }
                }
                else
                {
                    value = null;
                }
            }
            else if (propInfo.PropertyType.IsGenericType &&
                     propInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                value = Convert.ChangeType(property.Value, Nullable.GetUnderlyingType(propInfo.PropertyType));

                // TODO: If data type is DateTime and is nullable and is less than 1.1.1000 than set it to NULL
            }
            else if (propInfo.PropertyType.Equals(typeof(HtmlString)))
            {
                value = new HtmlString(property.Value);
            }
            else
            {
                value = Convert.ChangeType(property.Value, propInfo.PropertyType);
            }

            return(value);
        }
Exemple #5
0
 /// <summary>
 /// Updates or adds the content item. If content item already exists, it updates it.
 /// NOTE: Set the ParentId property of this item.
 /// If content item doesn't exists, it creates new content item (in that case contentItem.Id will be set to newly created id).
 /// </summary>
 /// <param name="user">User used for add or updating the content</param>
 /// <param name="publish">If set to <c>true</c> it contentItem will be published as well.</param>
 public void Save(User user, bool publish)
 {
     ContentHelper.Save(this, user, publish);
 }
Exemple #6
0
 /// <summary>
 /// Gets all children nodes of a given type from a given node id.
 /// Note: This method returns only first level children - it doesn't return children's children.
 /// </summary>
 /// <typeparam name="T">Strongly typed content item</typeparam>
 /// <param name="parentId">Parent node id of all children to get</param>
 public static IEnumerable <T> GetChildren <T>(int parentId)
     where T : DocumentTypeBase, new()
 {
     return(ContentHelper.GetChildren <T>(parentId, false));
 }
Exemple #7
0
 /// <summary>
 /// Updates or adds the content item using current user. If content item already exists, it updates it.
 /// If content item doesn't exists, it creates new content item.
 /// NOTE: Set the ParentId property of this item.
 /// </summary>
 /// <param name="publish">If set to <c>true</c> it contentItem will be published as well.</param>
 public void Save(bool publish)
 {
     ContentHelper.Save(this, publish);
 }
Exemple #8
0
 /// <summary>
 /// Updates or adds the content item using current user. If content item already exists, it updates it.
 /// NOTE: Set the ParentId property of this item.
 /// If content item doesn't exists, it creates new content item.
 /// </summary>
 public void Save()
 {
     ContentHelper.Save(this);
 }
Exemple #9
0
 /// <summary>
 /// Gets all children nodes from a given node id.
 /// </summary>
 /// <param name="deepGet">if set to <c>true</c> method will return children's children (complete tree).</param>
 /// <returns></returns>
 public IEnumerable <DocumentTypeBase> GetChildren(bool deepGet)
 {
     return(ContentHelper.GetChildren(this.Id, deepGet));
 }
Exemple #10
0
 /// <summary>
 /// Gets all children nodes from a given node id.
 /// Note: This method returns only first level children - it doesn't return children's children.
 /// </summary>
 public IEnumerable <DocumentTypeBase> GetChildren()
 {
     return(ContentHelper.GetChildren(this.Id));
 }
Exemple #11
0
 /// <summary>
 /// Gets all children nodes of a given type from a given node id.
 /// Note: This method returns only first level children - it doesn't return children's children.
 /// </summary>
 /// <typeparam name="T">Strongly typed content item</typeparam>
 public IEnumerable <T> GetChildren <T>()
     where T : DocumentTypeBase, new()
 {
     return(ContentHelper.GetChildren <T>(this.Id));
 }
Exemple #12
0
 /// <summary>
 /// Gets all ancestor nodes from a given node.
 /// </summary>
 public IEnumerable <DocumentTypeBase> GetAncestors()
 {
     return(ContentHelper.GetAncestors(this.Id));
 }
Exemple #13
0
 public static IEnumerable <TType> As <TType>(this IEnumerable <IPublishedContent> nodes)
     where TType : DocumentTypeBase, new()
 {
     return(nodes.Select(n => ContentHelper.GetByNodeId <TType>(n.Id)));
 }