Exemplo n.º 1
0
        public void CreateMetaBo(string userName)
        {
            userName = "******" + userName;
            MetaBoRepo   borepo = new MetaBoRepo();
            VersionsRepo vrepo  = new VersionsRepo();

            META_BO mbo = new META_BO
            {
                BO_NAME    = BoName,
                BO_DB_NAME = BoName,
                TYPE       = "form"
            };

            mbo = borepo.Create(mbo, userName);
            db.META_BO.Add(mbo);

            VERSIONS version = vrepo.Create(mbo.META_BO_ID, userName);

            db.VERSIONS.Add(version);

            // db.SaveChanges();

            foreach (var f in Fields)
            {
                db.META_FIELD.Add(new MetaFieldRepo().Create(f.MetaField(mbo.META_BO_ID, userName), userName));
            }
            db.SaveChanges();

            Task.Run(async() => await vrepo.CommitAsync(version.VERSIONS_ID, userName));
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> Insert(int id, Dictionary <string, object> Items)
        {
            Logger.Info("POST Crud: " + id);
            var model = new CrudModel()
            {
                MetaBoID = id,
                Items    = Items
            };
            // var meta = await db.META_BO.FindAsync(model.MetaBoID);
            var meta = db.META_BO.Find(model.MetaBoID);

            /* ACCESS RIGHTS */
            try
            {
                UserRoleManager.Instance.VerifyWrite(meta.BO_DB_NAME);
            }
            catch (UnauthorizedAccessException ex)
            {
                return(Content(HttpStatusCode.Forbidden, ex.Message));
            }
            /* FIN ACCESS RIGHTS */

            BO bo_model = new BO()
            {
                CREATED_BY   = User.Identity.Name,
                CREATED_DATE = DateTime.Now,
                UPDATED_BY   = User.Identity.Name,
                UPDATED_DATE = DateTime.Now,
                STATUS       = "1",
                BO_TYPE      = model.MetaBoID.ToString(),
                VERSION      = meta.VERSION
            };

            db.BO.Add(bo_model);
            await db.SaveChangesAsync();

            int id_ = (int)bo_model.BO_ID;

            BO_Insert_return = id_;
            model.MetaBO     = meta;
            model.BO_ID      = id_;
            model.Items.Add("BO_ID", model.BO_ID);

            //return Ok(model.FormatInsert());
            string insert = model.Insert();

            // Workflow executer begin
            var s = new SGBD();

            var db_workflow = s.Cmd(" select * from WORKFLOW CROSS APPLY OPENJSON(ITEMS) with(type varchar(50) '$.type',   " +
                                    "  precedent varchar(50) '$.precedent',     [index] int '$.index',    val nvarchar(500) '$.value.value') as jsonValues where ACTIVE = 1 and jsonValues.val = " + id +
                                    "and jsonValues.type = 'bo'");
            dynamic _JSON;

            var where = "";
            foreach (DataRow item in db_workflow.Rows)
            {
                _JSON = System.Web.Helpers.Json.Decode(item["ITEMS"].ToString());
                int level = 0;

                for (var i = (int)item["index"] + 1; i < _JSON.Length; i++)
                {
                    var elm = _JSON[i];
                    if (elm["status"] != "deleted")
                    {
                        where = "";
                        level++;
                        if (elm["type"] == "validation")
                        {
                            foreach (var rule in elm.value.rules)
                            {
                                var value = rule["value"];
                                if (!"int,float,decimal,nvarchar(MAX)".Contains(rule["field"]["DB_TYPE"]))
                                {
                                    value = "'" + value + "'";
                                }
                                where += " " + rule["logic"] + " " + rule["field"]["DB_NAME"] + " " + rule["condition"] + " " + value;
                            }

                            var check_validation = s.Cmd("select * from " + meta.BO_DB_NAME + " where BO_ID=" + model.BO_ID + "  " + where);


                            if (check_validation.Rows.Count > 0)
                            {
                                TASK valid = new TASK()
                                {
                                    BO_ID        = id_,
                                    JSON_DATA    = System.Web.Helpers.Json.Encode(elm.value.validators),
                                    CREATED_BY   = User.Identity.Name,
                                    CREATED_DATE = DateTime.Now,
                                    STATUS       = elm.value.status,
                                    ETAT         = 0,
                                    TASK_LEVEL   = level,
                                    TASK_TYPE    = "VALIDATION"
                                };

                                db.TASK.Add(valid);

                                foreach (var _validator in elm.value.validators)
                                {
                                    NOTIF notification = new NOTIF()
                                    {
                                        VALIDATOR    = _validator["email"],
                                        CREATED_DATE = DateTime.Now,
                                        ETAT         = 0
                                    };
                                    db.NOTIF.Add(notification);
                                }

                                db.SaveChanges();
                            }
                        }
                        else if (elm["type"] == "bo")
                        {
                            TASK valid = new TASK()
                            {
                                BO_ID        = id_,
                                JSON_DATA    = System.Web.Helpers.Json.Encode(elm.value),
                                CREATED_BY   = User.Identity.Name,
                                CREATED_DATE = DateTime.Now,
                                ETAT         = 0,
                                TASK_LEVEL   = level,
                                TASK_TYPE    = "BO"
                            };
                            db.TASK.Add(valid);

                            db.SaveChanges();
                            var task_id = (int)valid.TASK_ID;
                            // await Insert_Bo_Using_Mapping(id, meta.BO_DB_NAME, model.BO_ID, task_id);
                        }
                    }
                }
            }

            if (insert == "")
            {
                BO_Insert_return = id_;
                return(Ok(model));
            }
            else
            {
                return(InternalServerError(new Exception(insert)));
            }
        }