Exemplo n.º 1
0
        public static DynamicDictionary PushValidationErros(DynamicDictionary soruce, DynamicDictionary destination)
        {
            foreach (string s in soruce.KeyList)
            {
                if (destination.ContainsKey(s))
                {
                    List <string> msgs = new List <string>();
                    if (destination.GetValue(s).GetType() == typeof(List <string>))
                    {
                        msgs = (List <string>)destination.GetValue(s); //soruce
                    }
                    else
                    {
                        msgs.Add(destination.GetValue(s).ToString());
                    }

                    if (soruce.GetValue(s).GetType() == typeof(List <string>))
                    {
                        List <string> n = (List <string>)soruce.GetValue(s);
                        msgs.AddRange(n);
                    }
                    else
                    {
                        msgs.Add(soruce.GetValue(s).ToString());
                    }
                    destination.SetValue(s, msgs);
                }
                else
                {
                    destination.Add(s, soruce.GetValue(s));
                }
            }
            return(destination);
        }
Exemplo n.º 2
0
        public override ResponseModel Insert(DbConnect con, DynamicDictionary item)
        {
            AssignedUserRightsModel model = new AssignedUserRightsModel();

            if (model.GetType().GetProperty("client_id") != null)
            {
                object      rights_user = item.GetValue("assigned_right_ids");
                IEnumerable enumerable  = rights_user as IEnumerable;
                if (enumerable != null)
                {
                    foreach (object element in enumerable)
                    {
                        object aa = element;
                        if (item.GetValueAsInt("client_id") == null)
                        {
                            item.Add("client_id", SessionData.client_id);
                        }
                        else
                        {
                            item.Add("client_id", item.GetValueAsInt("client_id"));
                        }
                        item.Add("assigned_right_id", aa);
                        base.Insert(con, item);
                    }
                }
            }
            ResponseModel Assigned_right_user = new ResponseModel();

            Assigned_right_user.success = true;
            Assigned_right_user.message = "Data Successfully Added.";
            return(Assigned_right_user);
        }
Exemplo n.º 3
0
        protected virtual ResponseModel BatchInsert(DbConnect con, List <DynamicDictionary> items, TKey id, SaveCallBackDelegate fun = null)
        {
            ResponseModel resp = new ResponseModel(true, "OK");

            if (items?.Count > 0)
            {
                for (int i = 0, len = items.Count; i < len; i++)
                {
                    //adding parent key before saving.
                    items[i].SetValue(ParentColumnName, ParentColumnValue);
                    resp = Service.Insert(con, items[i]);
                    if (!resp.success)
                    {
                        return(resp);
                    }
                    else
                    {
                        if (fun != null)
                        {
                            DynamicDictionary dd = (DynamicDictionary)resp.data;
                            resp = fun(con, items[i], dd.GetValue <TKey>("id"));
                            if (!resp.success)
                            {
                                return(resp);
                            }
                        }
                    }
                }
            }
            return(resp);
        }
Exemplo n.º 4
0
        protected virtual ResponseModel BatchUpdate(DbConnect con, List <DynamicDictionary> items, TKey id, SaveCallBackDelegate fun = null)
        {
            ResponseModel resp = new ResponseModel(true, "OK");

            if (items?.Count > 0)
            {
                for (int i = 0, len = items.Count; i < len; i++)
                {
                    TModel mdl = new TModel();
                    mdl.LoadFromDynamicDictionary(items[i]);
                    TKey item_id = items[i].GetValue <TKey>(mdl.GetKeyPropertyName());
                    resp = Service.Update(con, item_id, items[i]);
                    if (!resp.success)
                    {
                        return(resp);
                    }
                    else
                    {
                        if (fun != null)
                        {
                            DynamicDictionary dd = Service.GetAsDictionary(con, item_id);
                            //DynamicDictionary dd = (DynamicDictionary)resp.data;
                            resp = fun(con, items[i], dd.GetValue <TKey>("id"));
                            if (!resp.success)
                            {
                                return(resp);
                            }
                        }
                    }
                }
            }
            return(resp);
        }
Exemplo n.º 5
0
        public void LoadFromDynamicDictionary(DynamicDictionary item)
        {
            ModelBase           model = this;
            PropertyInfo        prop;
            List <PropertyInfo> FieldList = model.GetFieldList();

            for (int i = 0, len = FieldList.Count; i < len; i++)
            {
                prop = FieldList[i];
                if (item.ContainsKey(prop.Name.Trim().ToLower()))
                {
                    if (item[prop.Name.Trim().ToLower()] == null)
                    {
                        prop.SetValue(model, null);
                    }
                    else
                    {
                        try
                        {
                            //prop.SetValue(model, ModelService.ChangeType(item[prop.Name.Trim().ToLower()], prop.PropertyType));
                            prop.SetValue(model, ModelService.ChangeType(item.GetValue(prop.Name.Trim().ToLower()), prop.PropertyType));
                        }
                        catch (Exception ex)
                        {
                            prop.SetValue(model, null);
                        }
                    }
                }
            }
        }
Exemplo n.º 6
0
 public DynamicDictionary PushErrors(DynamicDictionary errorList)
 {
     foreach (string s in errorList.KeyList)
     {
         errors.Add(s, errorList.GetValue(s));
     }
     return(errors);
 }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        protected virtual ResponseModel BatchDelete(DbConnect con, List <DynamicDictionary> items, SaveCallBackDelegate fun = null)
        {
            ResponseModel resp = new ResponseModel(true, "OK");

            if (items?.Count > 0)
            {
                for (int i = 0, len = items.Count; i < len; i++)
                {
                    TModel mdl = new TModel();
                    mdl.LoadFromDynamicDictionary(items[i]);
                    TKey del_id = items[i].GetValue <TKey>(mdl.GetKeyPropertyName());
                    if (del_id?.ToString().Length > 0)
                    {
                        ResponseBase bs = Service.Delete(con, del_id);
                        resp.errors  = bs.errors;
                        resp.success = bs.success;
                        resp.message = bs.message;
                    }
                    if (!resp.success)
                    {
                        return(resp);
                    }
                    else
                    {
                        if (fun != null)
                        {
                            //DynamicDictionary dd = Service.GetAsDictionary(con, del_id);
                            DynamicDictionary dd = (DynamicDictionary)resp.data;
                            if (dd == null)//added for voucher detail while deleting the voucher detail grid on editing
                            {
                                resp = fun(con, items[i], del_id);
                            }
                            else
                            {
                                resp = fun(con, items[i], dd.GetValue <TKey>("id"));
                            }
                            if (!resp.success)
                            {
                                return(resp);
                            }
                        }
                    }
                }
            }
            return(resp);
        }
Exemplo n.º 9
0
        public string GetLang(string file, string key)
        {
            string            val  = "";
            DynamicDictionary temp = null;

            try
            {
                temp = (DynamicDictionary)Lang.GetValue(file);
                if (temp != null)
                {
                    if (temp.ContainsKey(key.ToLower()))
                    {
                        val = temp.GetValue(key.ToLower()).ToString();
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return(val);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Performs validation of unique-constraints. Detail on the failure are stored in UniqueErrors Property
        /// It will use the help of TModel for performing the unique-constraint check.
        /// </summary>
        /// <param name="item"></param>
        /// <returns>Returns the status of the validation.</returns>
        public virtual bool ValidateUniqueValue(DbConnect con, DynamicDictionary item, IModel validatorModel, bool skipFieldsNotProvided = false)
        {
            /**
             * TODO
             * 1) disable null check or check if unique constraint is not composite constraint
             * 2) check unique constraint in edit mode, load all data
             **/
            PropertyInfo key = validatorModel.GetKeyPropertyInfo();
            TKey         id  = item.GetValue <TKey>(key.Name);

            //if (Conversion.ToInt32(id.ToString()) > 0)
            //    skipFieldsNotProvided = false; //if edit mode checke

            if (validatorModel.GetType().GetProperty("deleted_uq_code") != null) //tod not equal..
            {
                item["deleted_uq_code"] = 1;
            }
            BangoCommand cmd   = new BangoCommand(commandType: MyroCommandTypes.StringBuilder);
            string       union = string.Empty;
            //List<PropertyInfo> uniqueFields = new List<PropertyInfo>();
            DictionaryFx <string, PropertyInfo> uniqueFields = new DictionaryFx <string, PropertyInfo>();
            //preparing sql
            DynamicDictionary data_param = null;

            Bango.Models.Attributes.TableDetailAttribute tabelDetail = validatorModel.GetTableDetail();

            foreach (KeyValuePair <string, UniqueConstraint> unique in validatorModel.UniqueFields)
            {
                if (unique.Value.Fields.Count == 0)
                {
                    continue;
                }

                bool value_not_provided = false;

                foreach (string fld in unique.Value.Fields)
                {
                    if (!item.ContainsKey(fld))
                    {
                        //1) disable null check or check if unique constraint is not composite constraint
                        ///if (unique.Value.Fields.Count <= 1)
                        if (unique.Value.Fields.Count <= 1) //TODO:Shivashwor modify... for client duplicate data insert OFF...
                        {
                            value_not_provided = true;
                            break;
                        }

                        if (!skipFieldsNotProvided)
                        {
                            //If fld name not exists in validatorModel then
                            if (validatorModel.GetValue(fld) == null)
                            {
                                ///item.Add(fld, null);
                                value_not_provided = true;
                            }
                            else
                            {
                                Type t = validatorModel.GetType().GetProperty(fld).PropertyType;

                                if (t.IsValueType)
                                {
                                    item.Add(fld, Activator.CreateInstance(t));
                                }
                                else
                                {
                                    item.Add(fld, null);
                                }
                            }
                        }
                        else
                        {
                            //TODO:Shivashwor modify... for client duplicate data insert OFF...
                            value_not_provided = true;
                        }
                        break;
                    }
                }
                if (value_not_provided)
                {
                    continue;
                }

                data_param = (DynamicDictionary)item.Clone();
                ///TODO:SHIVASHWOR 15 nov 2015 for Unique value is empty or not...
                object data_val = data_param.GetValue(unique.Key);
                if (data_val != null)
                {
                    if (data_val.ToString().Trim().Length == 0)
                    {
                        continue;
                    }
                }

                if (union.Length > 0)
                {
                    cmd.SqlString.AppendLine(union);
                }
                string and = string.Empty;
                cmd.SqlString.AppendLine(String.Format("SELECT distinct '{0}' unique_constraint, '{2}' error_message FROM {1} {3} WHERE 1=1 "
                                                       , DbServiceUtility.SafeDBString(unique.Value.Name), tabelDetail.Name
                                                       , DbServiceUtility.SafeDBString(unique.Value.ErrorMessage)
                                                       , tabelDetail.Alias));
                //CHECKING In if client_id exists in the model for adding the client_id in unique contraint check if the developer has forgot to added
                PropertyInfo prop_client_id = validatorModel.GetType().GetProperty("client_id");
                if (prop_client_id != null)
                {
                    if (!unique.Value.Fields.Contains("client_id"))
                    {
                        unique.Value.Fields.Add("client_id");
                    }
                }
                foreach (string fld in unique.Value.Fields)
                {
                    if (validatorModel.GetType().GetProperty(fld) != null)
                    {
                        DbServiceUtility.BindParameter(cmd, fld, data_param, validatorModel.GetType().GetProperty(fld).PropertyType, tabelDetail.Alias, SearchTypes.Equal | SearchTypes.CaseSensetive, string.Empty, true, validatorModel.GetType().GetProperty(fld));
                    }
                    //cmd.SqlString.AppendFormat(" {1} {0} = @{0}", fld, and);//uniqueFields[fld] = validatorModel.GetType().GetProperty(fld);
                }


                if (key.Name.Trim().Length > 0)//PRIMARY KEY Check if n
                {
                    if (id != null)
                    {
                        //var obj_updateBy = data_param.GetValue("updated_by");

                        //if (obj_updateBy!= null)
                        DbServiceUtility.BindParameter(cmd, key.Name, data_param, System.Data.DbType.Int32, tabelDetail.Alias, SearchTypes.NotEqual, string.Empty, true, key);
                    }
                }

                union = " UNION ALL";
            }

            string finalSql           = cmd.FinalSql;
            IEnumerable <dynamic> lst = null;

            if (finalSql.Length > 0)
            {
                try
                {
                    lst = con.DB.Query <dynamic>(finalSql, cmd.FinalParameters);
                }
                catch (NpgsqlException ex)
                {
                    Errors.Add(ex.ToString());
                    LogTrace.WriteErrorLog(ex.ToString());
                    LogTrace.WriteDebugLog(string.Format("sql which gave exception:\r{0}", ex.Routine));
                    return(false);
                }
                catch (Exception ex)
                {
                }



                //checking for the unique constraint
                if (lst.Count() > 0)
                {
                    foreach (DapperRow dr in lst)
                    {
                        DynamicDictionary dic = Conversion.ToDynamicDictionary(dr);
                        DynamicDictionary err = new DynamicDictionary();
                        err.Add(dic.GetValueAsString("unique_constraint"), dic.GetValue("error_message"));
                        ModelService.PushValidationErros(err, ValidationErrors);
                    }
                    return(false);
                }
            }
            else
            {
                //TODO:Shivashwor 01 Nov 2015/
                //if edit mode nothing changed after save data occurs
                //  throw new NoSqlStringProvidedException();
            }
            return(true);
        }
Exemplo n.º 11
0
        public override ResponseModel Update(int?id, DynamicDictionary item)
        {
            string        message = string.Empty;
            bool          success = false;
            object        data    = item;
            ResponseModel resp    = new ResponseModel();
            UserModel     Model   = new UserModel();

            LoadItemAfterSave = true;
            using (DbConnect con = new DbConnect())
            {
                con.DB.BeginTransaction();
                try
                {
                    string new_file_name  = item.GetValueAsString("new_file_name");
                    string user_file_name = item.GetValueAsString("userfilename");
                    string relative_path  = "temp/";
                    if (user_file_name == null)
                    {
                        if (new_file_name != null)
                        {
                            item.SetValue("photo_path", relative_path + new_file_name);
                        }
                    }
                    else
                    {
                        if (new_file_name != "")
                        {
                            item.SetValue("photo_path", relative_path + new_file_name);
                        }
                        else
                        {
                            item.SetValue("photo_path", "");
                            string filePath = FileBox.GetWebAppRoot();
                            if (System.IO.File.Exists(filePath + user_file_name)) //if file exists than delete.
                            {
                                System.IO.File.Delete(filePath + user_file_name);
                            }
                        }
                    }
                    if (SessionData.client_id == 1)
                    {
                        CheckClientID = false;
                    }

                    resp = base.Update(id, item);

                    #region User Profile Window
                    if (resp.success && item.GetValueAsString("userProfile") == "true")
                    {
                        string file_name = item.GetValueAsString("new_file_name");
                        string filePath  = FileBox.GetWebAppRoot() + "temp/";
                        int?   photo_id  = item.GetValueAsInt("photo_id");
                        if (file_name == "")
                        {
                            var photoPath = item.GetValueAsString("user_file_name");
                            if (photoPath == "")
                            {
                                return(null);
                            }
                            if (System.IO.File.Exists(filePath + new_file_name)) //if file exists than delete.
                            {
                                System.IO.File.Delete(filePath + new_file_name);
                            }

                            int?tax_photo_id = item.GetValueAsInt("photo_id");
                            item.SetValue("photo_path", "");
                            resp = base.Update(id, item);
                        }
                        else
                        {
                            if (photo_id == 0)
                            {
                                item.SetValue("photo_path", relative_path + new_file_name);
                            }
                            else
                            {
                                item.SetValue("photo_path", relative_path + new_file_name);
                            }
                            resp = base.Update(id, item);
                        }
                    }
                    #endregion
                    else
                    {
                    }
                    if (resp.success)
                    {
                        con.DB.CommitTransaction();
                        message = "Data added successfully.";
                        success = true;
                        if (resp.success)
                        {
                            DynamicDictionary respdata        = (DynamicDictionary)resp.data;
                            string            confirmpassword = respdata.GetValueAsString("confirmpassword");
                            if (item.GetValueAsString("userProfile") == "" || item.GetValueAsString("userProfile") == null)
                            {
                                if (confirmpassword == "" || confirmpassword == null)
                                {
                                    int user_id = (int)((DynamicDictionary)resp.data).GetValueAsInt("id");
                                    resp.data = user_id;
                                    return(resp);
                                }
                            }
                        }
                        return(resp);
                    }
                    else
                    {
                        con.DB.RollbackTransaction();

                        if (resp.validation_errors.GetCount() > 0)
                        {
                            message = string.Join(",", resp.error_code);
                        }
                        else
                        {
                            message = "Data add failed, please try again later.";
                        }
                    }
                }
                catch (Exception)
                {
                    con.DB.RollbackTransaction();
                    message = "Data add failed, Rollback Transaction.";
                }
            }
            return(new ResponseModel(success, item.GetValue("photo_id"), message));
        }
        public bool CheckChangeChanges(TModel oldData, DynamicDictionary newData)
        {
            HasChanges = false;
            var s = Snapshotter.Start(oldData);

            //start observer
            Diff          = new DynamicParameters();
            ChangedValues = new ExpandoObject();
            //merge new data into old data
            ModelService srvc         = new ModelService();
            TModel       oldDataClone = new TModel();

            srvc.CopyProperty(oldData, oldDataClone);
            List <string> ignoreFields = GetIgnoreFields(ActivityType);
            //bool status = srvc.Merge<TModel>(newData, oldData, GetIgnoreFields(ActivityType));
            //changes
            //Diff = s.Diff();
            List <Dictionary <string, object> > changed_values = new List <Dictionary <string, object> >();

            try
            {
                foreach (PropertyInfo prop in oldData.GetType().GetProperties())
                {
                    if (ignoreFields.Contains <string>(prop.Name))
                    {
                        continue;
                    }
                    if (!newData.ContainsKey(prop.Name))
                    {
                        continue;
                    }
                    string newValue = newData.GetValue(prop.Name) == null ? string.Empty : newData.GetValue(prop.Name).ToString();
                    string oldValue = prop.GetValue(oldData) == null ? string.Empty : prop.GetValue(oldData).ToString();

                    if (newValue != oldValue)
                    {
                        //setting the changed value in property for saving
                        //storing data for saving in change history.
                        HasChanges = true;
                        Dictionary <string, object> row = new Dictionary <string, object>();
                        row.Add("field_name", prop.Name);
                        row.Add("old_value", oldDataClone.GetType().GetProperty(prop.Name).GetValue(oldDataClone));
                        //row.Add("new_value", newData.GetType().GetProperty(prop.Name).GetValue(newData));
                        row.Add("new_value", newData.GetValue(prop.Name));
                        changed_values.Add(row);
                        prop.SetValue(oldData, ModelService.ChangeType(newData.GetValue(prop.Name), prop.PropertyType));
                    }
                }
            }
            catch (Exception ex)
            {
                Bango.Base.Log.LogTrace.WriteErrorLog(ex.ToString());
                throw new Exception("Error while tracking changes.");
            }

            if (HasChanges)
            {
                Changes.table_name        = ModelService.GetTableName(oldData);
                Changes.changed_values    = JsonConvert.SerializeObject(changed_values);
                Changes.activity_datetime = DateTime.Now;
                Changes.user_id           = SessionData.user_id;
                Changes.activity_type     = ActivityType.ToString();
                Changes.os_computer_name  = HttpRequestHelper.GetClientHostName();
                Changes.os_ipaddress      = HttpRequestHelper.GetClientIpAddress();
                Changes.os_useragent      = HttpRequestHelper.GetClientBrowser();
                // Changes.pkey_name = Changes.GetKeyPropertyInfo().Name;
            }
            Diff = s.Diff();
            return(HasChanges);
        }