Ejemplo n.º 1
0
        public EntityView GetFormView(string pocoName)
        {
            EntityView scrud = new EntityView();
            List<EntityColumn> columns = new List<EntityColumn>();

            object poco = PocoHelper.GetInstanceOf(pocoName);
            if (poco == null)
            {
                return null;
            }

            bool hasAccess = Service.HasAccess(poco.GetType().FullName, AccessTypeEnum.Read, this.UserId);

            if (!hasAccess)
            {
                throw new MixERPException(Warnings.AccessIsDenied);
            }

            PrimaryKeyAttribute primaryKey =
                poco.GetType().GetAttributeValue((PrimaryKeyAttribute attribute) => attribute);

            scrud.PrimaryKey = String.Empty;

            if (primaryKey != null)
            {
                scrud.PrimaryKey = primaryKey.Value;
            }

            Type type = poco.GetType();
            foreach (PropertyInfo info in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                EntityColumn column = new EntityColumn();

                column.PropertyName = info.Name;
                column.DataType = info.PropertyType.ToString();

                if (column.DataType.StartsWith("System.Nullable`1["))
                {
                    column.DataType = column.DataType.Replace("System.Nullable`1[", "").Replace("]", "");
                }

                column.ColumnName =
                    info.GetCustomAttributes(typeof(ColumnAttribute), false)
                        .Cast<ColumnAttribute>()
                        .Select(c => c.Name)
                        .FirstOrDefault();

                ColumnDbType dbType =
                    info.GetCustomAttributes(typeof(ColumnDbType), false).Cast<ColumnDbType>().FirstOrDefault();

                if (dbType != null)
                {
                    column.IsNullable = dbType.IsNullable;
                    column.DbDataType = dbType.Name;
                    column.Value = dbType.DefaultValue;
                    column.MaxLength = dbType.MaxLength;

                    if (column.Value.StartsWith("nextval"))
                    {
                        column.Value = "";
                    }
                }

                if (column.ColumnName != null)
                {
                    column.IsPrimaryKey =
                        column.ColumnName.ToUpperInvariant().Equals(scrud.PrimaryKey.ToUpperInvariant());
                }

                if (column.IsPrimaryKey)
                {
                    column.IsSerial = primaryKey.autoIncrement;
                }

                columns.Add(column);
            }

            scrud.Columns = columns;
            return scrud;
        }
Ejemplo n.º 2
0
        public static List <object> Import <T>(string catalog, T poco, List <dynamic> entities, EntityView entityView, int userId)
        {
            string         primaryKeyName = PocoHelper.GetKeyName(poco);
            string         tableName      = PocoHelper.GetTableName(poco);
            List <dynamic> items          = new List <dynamic>();
            List <object>  result         = new List <object>();

            int line = 0;

            foreach (dynamic entity in entities)
            {
                if (entity == null)
                {
                    continue;
                }

                ExpandoObject item = new ExpandoObject();


                foreach (dynamic o in entity)
                {
                    string key   = o.Key;
                    object value = o.Value;

                    EntityColumn column = entityView.Columns.FirstOrDefault(c => c.ColumnName.Equals(key));

                    if (column != null)
                    {
                        bool isNullable = column.IsNullable || primaryKeyName.Equals(key);

                        AddProperty(ref item, column.DataType, isNullable, key, value);
                    }
                }

                if (((ICollection <KeyValuePair <string, object> >)item).Count > 1)
                {
                    line++;

                    if (((IDictionary <string, object>)item).ContainsKey("entered_by"))
                    {
                        ((IDictionary <string, object>)item)["entered_by"] = userId;
                    }

                    ((IDictionary <string, object>)item)["audit_user_id"] = userId;
                    ((IDictionary <string, object>)item)["audit_ts"]      = DateTime.UtcNow;

                    items.Add(item);
                }
            }


            line = 0;

            try
            {
                using (Database db = new Database(Factory.GetConnectionString(catalog), Factory.ProviderName))
                {
                    using (Transaction transaction = db.GetTransaction())
                    {
                        foreach (ExpandoObject item in items)
                        {
                            line++;

                            object primaryKeyValue = ((IDictionary <string, object>)item)[primaryKeyName];

                            if (primaryKeyValue != null)
                            {
                                db.Update(tableName, primaryKeyName, item, primaryKeyValue);
                            }
                            else
                            {
                                primaryKeyValue = db.Insert(tableName, primaryKeyName, item);
                            }

                            result.Add(primaryKeyValue);
                        }

                        transaction.Complete();
                    }
                }
            }
            catch (NpgsqlException ex)
            {
                string errorMessage = string.Format(CultureManager.GetCurrent(), "Error on line {0}.", line);

                if (ex.Code.StartsWith("P"))
                {
                    errorMessage += Factory.GetDBErrorResource(ex);

                    throw new MixERPException(errorMessage, ex);
                }

                errorMessage += ex.Message;
                throw new MixERPException(errorMessage, ex);
            }
            catch (Exception ex)
            {
                string errorMessage = string.Format(CultureManager.GetCurrent(), "Error on line {0}.", line);
                throw new MixERPException(errorMessage, ex);
            }

            return(result);
        }