Example #1
0
        public static void DeleteEntity <T>(string catalog, T poco, object primaryKeyValue)
        {
            string primaryKeyName = PocoHelper.GetKeyName(poco);
            string tableName      = PocoHelper.GetTableName(poco);

            Data.Service.Delete(catalog, tableName, primaryKeyName, primaryKeyValue);
        }
Example #2
0
        public static T GetViewForEdit <T>(string catalog, T poco, object primaryKeyValue)
        {
            string primaryKeyName = PocoHelper.GetKeyName(poco);
            string tableName      = PocoHelper.GetTableName(poco);

            return(Data.Service.GetViewForEdit <T>(catalog, tableName, primaryKeyName, primaryKeyValue));
        }
Example #3
0
        public static IEnumerable <T> ForDownloadTemplate <T>(string catalog, T poco, bool byOffice, int officeId,
                                                              bool includeData)
        {
            string tableName = PocoHelper.GetTableName(poco);
            string keyName   = PocoHelper.GetKeyName(poco);

            IEnumerable <T> view = Data.Service.ForDownloadTemplate(catalog, poco, tableName, keyName, byOffice, officeId,
                                                                    includeData);

            return(view);
        }
Example #4
0
        public static IEnumerable <T> GetView <T>(string catalog, T poco, long page, List <Filter> filters, bool byOffice,
                                                  int officeId, bool showall,
                                                  long pageSize)
        {
            string tableName = PocoHelper.GetTableName(poco);
            string keyName   = PocoHelper.GetKeyName(poco);

            IEnumerable <T> view = Data.Service.GetView(catalog, poco, tableName, keyName, page, filters, byOffice,
                                                        officeId, showall, pageSize);

            return(view);
        }
Example #5
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);
        }