예제 #1
0
        void Member_New(Member member, NewEventArgs e)
        {
            Type typeMemberType = MemberTypeManager.GetMemberTypeType(member.ContentType.Alias);

            foreach (PropertyInfo propInfo in typeMemberType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                MemberTypePropertyAttribute propAttr = Util.GetAttribute <MemberTypePropertyAttribute>(propInfo);
                if (propAttr == null)
                {
                    continue; // skip this property - not part of a Member type
                }

                string propertyName;
                string propertyAlias;
                MemberTypeManager.ReadPropertyNameAndAlias(propInfo, propAttr, out propertyName, out propertyAlias);

                if (propAttr.DefaultValue != null)
                {
                    try
                    {
                        umbraco.cms.businesslogic.property.Property property = member.getProperty(propertyAlias);
                        property.Value = Convert.ChangeType(propAttr.DefaultValue, propInfo.PropertyType);
                    }
                    catch (Exception exc)
                    {
                        throw new Exception(string.Format("Cannot set default value ('{0}') for property {1}.{2}. Error: {3}",
                                                          propAttr.DefaultValue, typeMemberType.Name, propInfo.Name, exc.Message), exc);
                    }
                }
            }
        }
예제 #2
0
        public static void SynchronizeMemberTypes()
        {
#if DEBUG
            Stopwatch timer = new Stopwatch();
            timer.Start();
#endif
            MemberTypeManager memberTypeManager = new MemberTypeManager();
            memberTypeManager.Synchronize();

#if DEBUG
            timer.Stop();
            StopwatchLogger.AddToLog(string.Format("Total elapsed time for method 'SynchronizeMemberTypes': {0}ms.", timer.ElapsedMilliseconds));
            timer.Restart();
#endif
        }
예제 #3
0
        public virtual T CreateAndPopulateTypedInstance <T>(Member member) where T : MemberTypeBase
        {
            T retVal = null;

            if (member != null)
            {
                Type typeMemberType = MemberTypeManager.GetMemberTypeType(member.ContentType.Alias);
                T    typedMember    = (T)CreateInstance(typeMemberType, new[] { member.LoginName, member.Email, member.Password });
                if (MemberHelper.PopuplateInstance <T>(member, typeMemberType, typedMember))
                {
                    retVal = typedMember;
                }
            }

            return(retVal);
        }
        public void Intercept(IInvocation invocation)
        {
            MemberTypeBase memberType = (MemberTypeBase)invocation.InvocationTarget;

            string propertyName = invocation.Method.GetPropertyName();

            if (invocation.Method.IsGetter())
            {
                if (memberType[propertyName] == null)
                {
                    Type         typeDocType = MemberTypeManager.GetMemberTypeType(memberType.Source.ContentType.Alias);
                    PropertyInfo propInfo    = typeDocType.GetProperty(propertyName);

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

                invocation.ReturnValue = memberType[propertyName];
                if (invocation.ReturnValue == null && invocation.Method.ReturnType == typeof(bool))
                {
                    invocation.ReturnValue = false;
                }
            }
            else
            {
                memberType[propertyName] = invocation.Arguments[0];
            }
        }
예제 #5
0
        private void SynchronizeMemberType(Type typeMemberType, Type basetypeMemberType)
        {
            MemberTypeAttribute memberTypeAttr = this.GetMemberTypeAttribute(typeMemberType);

            string memberTypeName  = string.IsNullOrEmpty(memberTypeAttr.Name) ? typeMemberType.Name : memberTypeAttr.Name;
            string memberTypeAlias = MemberTypeManager.GetMemberTypeAlias(typeMemberType);

            try
            {
                this.AddToSynchronized(typeMemberType.Name, memberTypeAlias, typeMemberType);
            }
            catch (ArgumentException exc)
            {
                throw new Exception(string.Format("Member type with alias '{0}' already exists! Please use unique class names as class name is used as alias. Member type causing the problem: '{1}' (assembly: '{2}'). Error message: {3}",
                                                  memberTypeAlias, typeMemberType.FullName, typeMemberType.Assembly.FullName, exc.Message));
            }


            _memberTypes.Add(memberTypeAlias, typeMemberType);

            MemberType memberType = MemberType.GetByAlias(memberTypeAlias);

            if (memberType == null)
            {
                memberType = MemberType.MakeNew(new umbraco.BusinessLogic.User(0), memberTypeAttr.Name);
            }

            memberType.Alias       = memberTypeAlias;
            memberType.IconUrl     = memberTypeAttr.IconUrl;
            memberType.Thumbnail   = memberTypeAttr.Thumbnail;
            memberType.Description = memberTypeAttr.Description;

            SynchronizeMemberTypeProperties(typeMemberType, memberType);

            memberType.Save();
        }
예제 #6
0
        internal static object GetPropertyValue(Member member, PropertyInfo propInfo, MemberTypePropertyAttribute propAttr)
        {
            object value = null;

            string propertyName;
            string propertyAlias;

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

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

            umbraco.cms.businesslogic.property.Property property = member.getProperty(propertyAlias);

            if (property == null)
            {
                value = null;
            }
            else if (propInfo.PropertyType.Equals(typeof(System.Boolean)))
            {
                if (property.Value == null || String.IsNullOrEmpty(Convert.ToString(property.Value)) ||
                    Convert.ToString(property.Value) == "0")
                {
                    value = false;
                }
                else
                {
                    value = true;
                }
            }
            else if (ContentHelper.PropertyConvertors.ContainsKey(propInfo.PropertyType))
            {
                // will be transformed later. TODO: move transformation here
                value = property.Value;
            }
            else if (propInfo.PropertyType.IsGenericType &&
                     propInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                if (String.IsNullOrEmpty(Convert.ToString(property.Value)))
                {
                    value = null;
                }
                else
                {
                    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
            {
                value = Convert.ChangeType(property.Value, propInfo.PropertyType);
            }

            if (ContentHelper.PropertyConvertors.ContainsKey(propInfo.PropertyType))
            {
                value = ContentHelper.PropertyConvertors[propInfo.PropertyType].ConvertValueWhenRead(value);
            }

            return(value);
        }
예제 #7
0
        /// <summary>
        /// Updates or adds the member. If member already exists, it updates it.
        /// If member doesn't exists, it creates new member.
        /// </summary>
        /// <param name="member">Member to update/add</param>
        /// <param name="user">User used for add or updating the content</param>
        private static void Save(MemberTypeBase member, User user)
        {
            if (user == null)
            {
                throw new Exception("User cannot be null!");
            }

            if (string.IsNullOrEmpty(member.LoginName))
            {
                throw new Exception("Member Login Name cannot be empty");
            }

            MemberType memberType = MemberTypeManager.GetMemberType(member.GetType());

            Member umember;

            if (member.Id == 0) // member is new so create Member
            {
                umember = Member.MakeNew(member.LoginName, member.Email, memberType, user);

                // reload
                umember          = Member.GetMemberFromLoginName(member.LoginName);
                umember.Password = member.Password;
                member.Id        = umember.Id;
            }
            else // member already exists, so load it
            {
                umember = new Member(member.Id);
            }

            umember.Email     = member.Email;
            umember.LoginName = member.LoginName;

            foreach (PropertyInfo propInfo in member.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                DocumentTypePropertyAttribute propAttr = Util.GetAttribute <DocumentTypePropertyAttribute>(propInfo);
                if (propAttr == null)
                {
                    continue; // skip this property - not part of a Document Type
                }

                string propertyName;
                string propertyAlias;
                MemberTypeManager.ReadPropertyNameAndAlias(propInfo, propAttr, out propertyName, out propertyAlias);

                umbraco.cms.businesslogic.property.Property property = umember.getProperty(propertyAlias);
                if (property == null)
                {
                    throw new Exception(string.Format("Property '{0}' not found in this node: {1}. Content type: {2}.",
                                                      propertyAlias, member.Id, memberType.Alias));
                }

                if (ContentHelper.PropertyConvertors.ContainsKey(propInfo.PropertyType))
                {
                    property.Value = ContentHelper.PropertyConvertors[propInfo.PropertyType].ConvertValueWhenWrite(propInfo.GetValue(member, null));
                }
                else
                {
                    property.Value = propInfo.GetValue(member, null);
                }
            }

            umember.Save();
        }
예제 #8
0
 public static MemberType GetMemberType(Type typeMemberType)
 {
     return(MemberType.GetByAlias(MemberTypeManager.GetMemberTypeAlias(typeMemberType)));
 }