コード例 #1
0
        public TableDetailAttribute GetTableDetail()
        {
            Type typ = this.GetType();

            if (_tableDetail != null)
            {
                return(_tableDetail);
            }
            _tableDetail = typ.GetCustomAttribute <TableDetailAttribute>();
            if (_tableDetail == null)
            {
                _tableDetail = new TableDetailAttribute(this.GetType().Name);
            }
            if (_tableDetail.DeleteFlagField?.Length == 0)
            {
                _tableDetail.DeleteFlagField = "is_deleted";
            }
            if (_tableDetail.OrderByField?.Length == 0)
            {
                //string tmp = "name_" + SessionData.language;

                //if(typ.GetProperty("code") != null){
                //    //_tableDetail.OrderByField = "code";
                //    _tableDetail.OrderByField = "id";
                //}
                //else if (typ.GetProperty(tmp) != null)
                //{
                //    _tableDetail.OrderByField = tmp;
                //}
                //else {
                //    _tableDetail.OrderByField = GetKeyPropertyName();
                //}
            }
            return(_tableDetail);
        }
コード例 #2
0
        public static PropertyInfo GetDeleteFieldProperty(IModel model)
        {
            TableDetailAttribute attrib = model.GetTableDetail();

            if (attrib.DeleteFlagField.Trim().Length > 0)
            {
                return(model.GetType().GetProperty(attrib.DeleteFlagField));
            }
            return(null);
        }
コード例 #3
0
        public virtual BangoCommand GetSearchCommand(SearchScenario scenario, DbConnect con, BangoCommand cmd, DynamicDictionary data_param, string selectClause, string orderByClause, int page = -1, int pageSize = 20, bool count = false, string tableAlias = null, string scenarioOthers = null)
        {
            TableDetailAttribute tableDetail = _model.GetTableDetail();
            //clear the params whic are empty or null
            List <string> keys = new List <string>(data_param.KeyList.Cast <String>());

            foreach (string key in keys)
            {
                object value = data_param.GetValue(key);
                if (value == null || data_param.GetValueAsString(key).Length == 0)
                {
                    data_param.Remove(key);
                }
            }

            //BangoCommand cmd = GetSearchCommandTemplate(selectClause, count, tableAlias);
            //cmd.Sql.AppendLine("FROM " + model.GetTableName());
            IDbExpression dbExp = App.Container.GetInstance <IDbExpression>();

            if (data_param.GetCount() == 0)
            {
                return(cmd);
            }

            string append = DbServiceUtility.GetTableAliasForColumn(tableAlias);

            if (!(scenario == SearchScenario.TreeNode && count == false))
            {
                //check & adding delete flag check sql
                DbServiceUtility.BindDeleteParameter(cmd, _model, tableAlias);

                if (CheckClientID)
                {
                    DbServiceUtility.BindClientIdParameter(cmd, _model, tableAlias, DisplayMasterDataFromSystem);
                }

                //add remaining default search criteria

                cmd = BeforeBindingParameter(scenario, con, cmd, data_param, count, tableAlias);
                cmd = DbServiceUtility.BindParameters(cmd, _model, data_param, tableAlias);
                cmd = AfterBindingParameter(scenario, con, cmd, data_param, count, tableAlias);

                //check & adding order by clause
                if (count == false)
                {
                    cmd = BeforeBindingOrderBy(scenario, con, cmd, data_param, count, tableAlias);
                    cmd = DbServiceUtility.BindOrderBy(cmd, orderByClause);
                    cmd = AfterBindingOrderBy(scenario, con, cmd, data_param, count, tableAlias);
                    cmd = DbServiceUtility.BindPagination(cmd, page, pageSize);
                }
            }
            return(cmd);
        }
コード例 #4
0
        public virtual BangoCommand GetSearchItemsCommand(DbConnect con, DynamicDictionary data_param, int page, int pageSize, string sort_by = null, bool count = false)
        {
            sort_by = sort_by == null ? string.Empty : sort_by;
            TableDetailAttribute tableDetail = _model.GetTableDetail();

            if (sort_by.Trim().Length == 0 && tableDetail.OrderByField != null && tableDetail.OrderByField.Length > 0)
            {
                sort_by = DbServiceUtility.SetColumnAlias(tableDetail.Alias, tableDetail.OrderByField);
            }

            BangoCommand cmd = GetSearchCommandTemplate(_model.GetAllFields(TableDetail.Alias, false), count, TableDetail.Alias);

            return(GetSearchCommand(SearchScenario.Search, con, cmd, data_param, GetAllFields()
                                    , sort_by, page, pageSize, count, TableDetail.Alias));
        }
コード例 #5
0
        public virtual TModel GetAsModel(DbConnect con, TKey id)
        {
            TModel item;
            ITable <TModel, TKey> tbl = con.GetModelTable <TModel, TKey>();

            item = tbl.Get(id);
            //checking if the item is already deleted (soft deleted)
            if (item != null)
            {
                TableDetailAttribute tableDetail = item.GetTableDetail();
                PropertyInfo         is_deleted  = item.GetType().GetProperty(tableDetail.DeleteFlagField);

                if (is_deleted != null)
                {
                    bool dele = Convert.ToBoolean(is_deleted.GetValue(item));
                    if (dele == true)
                    {
                        Errors.Add("Record already deleted.");
                        return(null);
                    }
                }

                if (CheckClientID)
                {
                    PropertyInfo client_id = item.GetType().GetProperty("client_id");
                    if (client_id != null)
                    {
                        if ((int?)client_id.GetValue(item) != SessionData.client_id)
                        {
                            Errors.Add("Record doesnot belong.");
                            return(null);
                        }
                    }
                }
            }
            return(item);
        }
コード例 #6
0
        public virtual bool HardDelete(DbConnect con, TKey id)
        {
            //pull old data
            TModel oldData = new TModel();
            TableDetailAttribute tableDatail = oldData.GetTableDetail();
            //data = new TModel();
            DynamicDictionary data_param = new DynamicDictionary();
            // ChangeHistoryHelper<TModel> chngHlpr = null;
            ITable <TModel, int?> tbl = con.GetModelTable <TModel>();
            bool status = false;

            //chngHlpr.Diff.Add("id", id, DbType.Int32, ParameterDirection.Input);
            DynamicParameters where = new DynamicParameters();
            where.Add("id", id);
            if (CheckClientID)
            {
                PropertyInfo client_id = oldData.GetType().GetProperty("client_id");
                if (client_id != null)
                {
                    where.Add("client_id", SessionData.client_id);
                }
            }
            try
            {
                //int? pk = (int?)Convert.ChangeType(id, typeof(int?));
                status = tbl.Delete((int?)Base.Conversion.ToInt32(id));
            }
            catch (Npgsql.NpgsqlException ex)
            {
                LogTrace.WriteErrorLog(ex.ToString());
                LogTrace.WriteDebugLog(string.Format("SQL which gave exception:\r{0}", ex.Routine));
                throw ex;
            }

            return(status);
        }
コード例 #7
0
 public SearchRepo()
 {
     TableDetail = _model.GetTableDetail();
 }
コード例 #8
0
        public virtual bool SoftDelete(DbConnect con, TKey id)
        {
            Is_Child_Records_Exists = false; //Default child record off
            //pull old data
            TModel oldData            = new TModel();
            ITable <TModel, TKey> tbl = con.GetModelTable <TModel, TKey>();

            oldData = tbl.Get(id);
            //checking if the data is editable by current login or not
            if (CheckClientID)
            {
                if (ValidateForClientData(oldData) == false)
                {
                    return(false);
                }
            }
            TableDetailAttribute tableDatail = oldData.GetTableDetail();

            //09 feb 2016  Foreign key refrence table data exists or not checke..
            #region todo:shivahwor
            StringBuilder Sql = new StringBuilder();
            Sql.AppendFormat(@"
                    SELECT 
                --tc.table_schema, tc.constraint_name, 
                tc.table_name, kcu.column_name
                --,ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name
                    FROM information_schema.table_constraints tc
                    JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name
                    JOIN information_schema.constraint_column_usage ccu ON ccu.constraint_name = tc.constraint_name
                    WHERE constraint_type = 'FOREIGN KEY'
                    AND ccu.table_name='{0}' ", tableDatail.Name);

            IEnumerable <DynamicDictionary> foreignkey_Table = DbServiceUtility.ExecuteList(con, Sql.ToString());

            Sql.Length = 0;
            DynamicDictionary rec = null;
            if (foreignkey_Table != null)
            {
                foreach (DynamicDictionary item in foreignkey_Table)
                {
                    object tbl_Name = item.GetValue("table_name");
                    object col_name = item.GetValue("column_name");

                    if (Sql.Length > 0)
                    {
                        Sql.AppendLine("\n\t UNION ALL ");
                    }

                    Sql.AppendFormat(" SELECT 1 from {0} Where {1}={2}  AND is_deleted ='F' ", tbl_Name.ToString(), col_name.ToString(), id);
                }
                rec = DbServiceUtility.ExecuteItem(con, Sql.ToString());
            }

            if (rec != null)
            {
                if (rec.KeyList.Count > 0)
                {
                    Is_Child_Records_Exists = true;     //if child records exists than do not remove...
                    return(false);
                }
            }

            #endregion

            //data = new TModel();
            DynamicDictionary            data_param = new DynamicDictionary();
            ChangeHistoryHelper <TModel> chngHlpr   = null;
            chngHlpr = new ChangeHistoryHelper <TModel>(AuditActivityTypes.SOFTDELETE);
            //if CREATED_BY, CREATED_on field exists then update those fields
            PropertyInfo by = oldData.GetType().GetProperty("deleted_by");
            if (by != null)
            {
                data_param.SetValue("deleted_by", SessionData.user_id);
                //by.SetValue(data, Bango.GetCurrentUserId());
            }
            PropertyInfo on = oldData.GetType().GetProperty("deleted_on");
            if (on != null)
            {
                //on.SetValue(data, DateTime.Now);
                data_param.SetValue("deleted_on", DateTime.Now);
            }

            PropertyInfo uq_code = oldData.GetType().GetProperty("deleted_uq_code");
            if (on != null)
            {
                //on.SetValue(data, DateTime.Now);

                data_param.SetValue("deleted_uq_code", DateTime.Now.ToString("MMddHHmmss"));
            }

            PropertyInfo is_deleted = oldData.GetType().GetProperty(tableDatail.DeleteFlagField);
            if (is_deleted != null)
            {
                //is_deleted.SetValue(data, true);
                data_param.SetValue(tableDatail.DeleteFlagField, true);
            }

            chngHlpr.CheckChangeChanges(oldData, data_param);
            //chngHlpr.Diff.Add("id", id, DbType.Int32, ParameterDirection.Input);
            DynamicParameters where = new DynamicParameters();
            where.Add("id", id);
            if (CheckClientID)
            {
                PropertyInfo client_id = oldData.GetType().GetProperty("client_id");
                if (client_id != null)
                {
                    where.Add("client_id", SessionData.client_id);
                }
            }
            try
            {
                int?savedId = tbl.Update(where, chngHlpr.Diff);

                if (TrackChanges)
                {
                    //save the changes
                    chngHlpr.LogChanges(con);
                }
            }
            catch (Npgsql.NpgsqlException ex)
            {
                LogTrace.WriteErrorLog(ex.ToString());
                LogTrace.WriteDebugLog(string.Format("SQL which gave exception:\r{0}", ex.Routine));
                throw ex;
            }

            return(true);
        }
コード例 #9
0
        public static string GetDeleteFieldName(IModel model)
        {
            TableDetailAttribute attrib = model.GetTableDetail();

            return(attrib.DeleteFlagField);
        }