public void ConstructAndSaveEntity(EntityElement entityElement)
        {
            StoreEntity storeEntityObject;
            try
            {
                storeEntityObject = InternalConstructEntity(entityElement);
            }
            catch (Exception ex)
            {
                throw new ConstructException(ex.Message, ex);
            }

            if (storeEntityObject.Entity == null)
                throw new ConstructException("Cannot create entity, unknown error.");

            if (storeEntityObject.CanSave)
            {
                ISession session = _session;
                bool needCloseSession = false;
                if (session == null)
                {
                    session = _factory.OpenSession();
                    needCloseSession = true;
                }

                try
                {
                    if (storeEntityObject.Stored)
                        session.Update(storeEntityObject.Entity);
                    else
                        session.Save(storeEntityObject.Entity);
                    session.Flush();
                }
                catch (Exception ex)
                {
                    var className = storeEntityObject.Entity.GetType().Name;
                    throw new ConstructException(
                        String.Format("[{0}] {1}",
                            className +
                            (String.IsNullOrWhiteSpace(storeEntityObject.RefName)
                                ? ""
                                : " -> " + storeEntityObject.RefName),
                            ex.Message),
                        ex);
                }
                finally
                {
                    if (needCloseSession)
                        session.Close();
                }
            }
        }
예제 #2
0
 /// <summary>
 /// Constructor for double type with value
 /// </summary>
 /// <param name="parent">entity element</param>
 /// <param name="name">property name</param>
 /// <param name="value">property value</param>
 public EntityProperty(EntityElement parent, string name, double value)
     : this(parent, name, ValueTypes.Double, value.ToString(InvariantFormatProvider))
 {
     _objValue = value;
 }
예제 #3
0
 /// <summary>
 /// Constructor for int type with value
 /// </summary>
 /// <param name="parent">entity element</param>
 /// <param name="name">property name</param>
 /// <param name="value">property value</param>
 public EntityProperty(EntityElement parent, string name, int value)
     : this(parent, name, ValueTypes.Int, value.ToString())
 {
     _objValue = value;
 }
예제 #4
0
 /// <summary>
 /// Constructor for string type with value
 /// </summary>
 /// <param name="parent">entity element</param>
 /// <param name="name">property name</param>
 /// <param name="value">property value</param>
 public EntityProperty(EntityElement parent, string name, string value)
     : this(parent, name, ValueTypes.String, value)
 {
     _objValue = value;
 }
예제 #5
0
 /// <summary>
 /// Constructor for string type
 /// </summary>
 /// <param name="parent">entity element</param>
 /// <param name="name">property name</param>
 public EntityProperty(EntityElement parent, string name)
     : this(parent, name, ValueTypes.String, "")
 {
 }
예제 #6
0
        /// <summary>
        /// Factory method
        /// </summary>
        /// <returns>instance of EntityProperty</returns>
        internal static EntityProperty CreateProperty(EntityElement parent, string name, string type, string value)
        {
            // string
            if (String.IsNullOrEmpty(type) || ValueTypes.String.Name == type)
                return new EntityProperty(parent, name, value);

            // value types
            try
            {
                if (ValueTypes.Int.Name.Equals(type, StringComparison.InvariantCultureIgnoreCase) || "int" == type ||
                    "integer" == type)
                    return new EntityProperty(parent, name, Int32.Parse(value));
                if (ValueTypes.Bool.Name.Equals(type, StringComparison.InvariantCultureIgnoreCase) || "bool" == type)
                    return new EntityProperty(parent, name, Boolean.Parse(value));
                if (ValueTypes.Byte.Name.Equals(type, StringComparison.InvariantCultureIgnoreCase))
                    return new EntityProperty(parent, name, Byte.Parse(value, InvariantFormatProvider));
                if (ValueTypes.Decimal.Name.Equals(type, StringComparison.InvariantCultureIgnoreCase))
                    return new EntityProperty(parent, name, Decimal.Parse(value, InvariantFormatProvider));
                if (ValueTypes.Double.Name.Equals(type, StringComparison.InvariantCultureIgnoreCase))
                    return new EntityProperty(parent, name, Double.Parse(value, InvariantFormatProvider));
                if (ValueTypes.DateTime.Name.Equals(type, StringComparison.InvariantCultureIgnoreCase))
                    return new EntityProperty(parent, name, DateTime.Parse(value, InvariantFormatProvider));
                if (ValueTypes.Binary.Name.Equals(type, StringComparison.InvariantCultureIgnoreCase))
                    return new EntityProperty(parent, name, value, Convert.FromBase64String(value));
            }
            catch(Exception ex)
            {
                throw new ParseException(String.Format("Can not convert string value '{0}' to type '{1}'. See element:\r\n{2}",
                        value, type, parent.ToXmlString()), parent.ToXmlString(), ex);
            }

            // reference
            if (ValueTypes.Reference.Name.Equals(type, StringComparison.InvariantCultureIgnoreCase))
                return new EntityProperty(parent, name, new Reference(value));

            // enum
            if (type.StartsWith("Enum."))
            {
                Type enumType = TypeResolver.FindType(parent.RootConfig.Assembly,
                    parent.RootConfig.Namespace + "." + type.Substring("Enum.".Length, type.Length - "Enum.".Length));
                if (enumType != null)
                    return new EntityProperty(parent, name, (Enum)Enum.Parse(enumType, value));
                else
                    throw new ParseException(String.Format("Can not convert string value '{0}' to type '{1}'.\r\nType not found.\r\nSee element:\r\n{2}",
                        value, type, parent.ToXmlString()), parent.ToXmlString());
            }

            return null;
        }
예제 #7
0
 /// <summary>
 /// Constructor for binary type
 /// </summary>
 /// <param name="parent">entity element</param>
 /// <param name="name">property name</param>
 /// <param name="value">property value</param>
 /// <param name="data">describes reference</param>
 public EntityProperty(EntityElement parent, string name, string value, byte[] data)
 {
     _parent = parent;
     _name = name;
     _valueType = ValueTypes.Binary;
     _value = value;
     _objValue = data;
 }
예제 #8
0
 /// <summary>
 /// Constructor for given type with value
 /// </summary>
 /// <param name="parent">entity element</param>
 /// <param name="name">property name</param>
 /// <param name="reference">describes reference</param>
 public EntityProperty(EntityElement parent, string name, Reference reference)
 {
     _parent = parent;
     _name = name;
     _valueType = ValueTypes.Reference;
     _value = reference.RefName;
 }
예제 #9
0
 /// <summary>
 /// Constructor for given type with value
 /// </summary>
 /// <param name="parent">entity element</param>
 /// <param name="name">property name</param>
 /// <param name="type">property type</param>
 /// <param name="value">property value</param>
 public EntityProperty(EntityElement parent, string name, Type type, string value)
 {
     _parent = parent;
     _name = name;
     _valueType = type;
     _value = value;
     _objValue = value;
 }
예제 #10
0
 /// <summary>
 /// Constructor for enum type with value
 /// </summary>
 /// <param name="parent">entity element</param>
 /// <param name="name">property name</param>
 /// <param name="value">property value</param>
 public EntityProperty(EntityElement parent, string name, Enum value)
     : this(parent, name, value.GetType(), value.ToString())
 {
     _objValue = value;
 }
예제 #11
0
 /// <summary>
 /// Constructor for DateTime type with value
 /// </summary>
 /// <param name="parent">entity element</param>
 /// <param name="name">property name</param>
 /// <param name="value">property value</param>
 public EntityProperty(EntityElement parent, string name, DateTime value)
     : this(parent, name, ValueTypes.DateTime, value.ToUniversalTime().ToString("s"))
 {
     _objValue = value;
 }
예제 #12
0
 public void CreateTestContent()
 {
     var entity = new EntityElement(this, "Bonitos", "bonitos1");
     entity.CreateStringProperty("Name", "Bob");
     entity.CreateBoolProperty("Disabled", true);
     entity.CreateDecimalProperty("Price", 125.2m);
 }
        private StoreEntity InternalConstructEntity(EntityElement entityElement)
        {
            Type cType = TypeResolver.FindType(entityElement.RootConfig.Assembly,
                entityElement.RootConfig.Namespace + "." + entityElement.ClassName);
            if (cType == null)
                throw new ConstructException(String.Format("Type of '{0}' not found.",
                    entityElement.RootConfig.Namespace + "." + entityElement.ClassName));

            // create instance of type cType or get from reference dictionary
            StoreEntity storeEntityObject = !String.IsNullOrEmpty(entityElement.RefName) &&
                                            _references.ContainsKey(entityElement.RefName)
                ? new StoreEntity(true, _references[entityElement.RefName], entityElement.RefName)
                : new StoreEntity(false, Activator.CreateInstance(cType), entityElement.RefName);
            // check flag 'save'
            if (!entityElement.Save)
            {
                // do not need to save entity!
                storeEntityObject.CanSave = false;
                // set id to empty
                var idPropInfo = cType.GetProperty("Id");
                if (idPropInfo != null)
                    idPropInfo.SetValue(storeEntityObject.Entity, Guid.Empty, null);
            }
            // check for similar types
            if (storeEntityObject.Entity.GetType() != cType)
                throw new ConstructException(String.Format("Previously created instance of the same name '{0}' has a different type ({1}) than that ({2}).",
                            entityElement.RefName, storeEntityObject.Entity.GetType().Name, cType.Name));
            // store to reference dictionary
            if (!String.IsNullOrEmpty(entityElement.RefName))
                _references[entityElement.RefName] = storeEntityObject.Entity;
            // set properties
            foreach (EntityProperty property in entityElement.Properties)
            {
                if (property.ValueType == EntityProperty.ValueTypes.Reference)
                {
                    string subRefProperty = ""; // sub reference property name
                    // if the specified property of the entity reference then extract property name
                    if (property.Value.Contains("."))
                    {
                        string[] refParts = property.Value.Split(new[] {'.'}, 2);
                        property.Value = refParts[0];   // correct reference name
                        subRefProperty = refParts[1];   // property of reference
                    }
                    if (!_references.ContainsKey(property.Value))
                        throw new ConstructException(String.Format( "[{0}.{1}] The entity reference name was not found: {2}",
                            entityElement.ClassName, property.Name, property.Value));

                    if (String.IsNullOrEmpty(subRefProperty))
                        property.ObjectValue = _references[property.Value];
                    else
                    {
                        // get property Info from reference
                        var subRefPropInfo = _references[property.Value].GetType().GetProperty(subRefProperty);
                        if (subRefPropInfo == null)
                            throw new ConstructException(
                                String.Format("[{0}.{1}] The property of the entity reference name was not found: {2}.{3}",
                                    entityElement.ClassName, property.Name, property.Value, subRefProperty));
                        // get property Value from reference and remember
                        property.ObjectValue = subRefPropInfo.GetValue(_references[property.Value], null);
                    }
                }

                // find a property in our entity
                var propInfo = cType.GetProperty(property.Name);
                try
                {
                    if (propInfo == null)
                        throw new ConstructException(
                            String.Format("[{0}.{1}] The property of the entity was not found: {2}",
                                entityElement.ClassName, property.Name, property.Name));

                    object oldPropValue = propInfo.GetValue(storeEntityObject.Entity, null);
                    // if old value of the property is not null and is IList
                    if (oldPropValue is IList)
                    {
                        IList pList = (IList)oldPropValue;
                        pList.Add(property.ObjectValue);
                    }
                    // else if generic type like ICollection<T>
                    else if (oldPropValue != null && oldPropValue.GetType().IsGenericType && oldPropValue is IEnumerable)
                    {
                        var methodAdd = oldPropValue.GetType().GetMethod("Add", BindingFlags.Instance | BindingFlags.Public);
                        if (methodAdd == null)
                            throw new ConstructException(
                                String.Format("[{0}.{1}] The collection does not have 'Add' method: {2}",
                                    entityElement.ClassName, property.Name, property.Name));
                        methodAdd.Invoke(oldPropValue, new[] { property.ObjectValue });
                    }
                    else if (oldPropValue == null && propInfo.PropertyType.IsGenericType && typeof(IEnumerable).IsAssignableFrom(propInfo.PropertyType))
                    {
                        throw new ConstructException(
                                String.Format("[{0}.{1}] The collection can not be null.",
                                entityElement.ClassName, property.Name));
                    }
                    else
                        // else set the value to the property of the object
                        propInfo.SetValue(storeEntityObject.Entity, property.ObjectValue, null);
                }
                catch (Exception ex)
                {
                    throw new ConstructException(String.Format("Can not set the value '{0}' to the property '{1}'.\r\n{2}\r\nSee element:\r\n{3}",
                        property.Value, property.Name, ex.Message, entityElement.ToXmlString()), ex);
                }
            }

            return storeEntityObject;
        }