Beispiel #1
0
        public ActionResult AddWd(string query, string dataType, string name, int period, int focus)
        {
            using (HlidacStatu.Lib.Data.DbEntities db = new HlidacStatu.Lib.Data.DbEntities())
            {
                if (string.IsNullOrEmpty(query))
                {
                    return(Json("0", JsonRequestBehavior.AllowGet));
                }
                string id = this.User.Identity.GetUserId();

                var dt = dataType;

                HlidacStatu.Lib.Data.WatchDog wd = new HlidacStatu.Lib.Data.WatchDog();
                wd.Created    = DateTime.Now;
                wd.UserId     = id;
                wd.StatusId   = 1;
                wd.SearchTerm = query;
                wd.PeriodId   = period;
                wd.FocusId    = focus;
                wd.Name       = Devmasters.TextUtil.ShortenText(name, 50);
                if (dt.ToLower() == typeof(Smlouva).Name.ToLower())
                {
                    wd.dataType = typeof(Smlouva).Name;
                }
                else if (dt.ToLower() == typeof(VerejnaZakazka).Name.ToLower())
                {
                    wd.dataType = typeof(VerejnaZakazka).Name;
                }
                else if (dt.ToLower() == typeof(Lib.Data.Insolvence.Rizeni).Name.ToLower())
                {
                    wd.dataType = typeof(Lib.Data.Insolvence.Rizeni).Name;
                }
                else if (dt.ToLower().StartsWith(typeof(HlidacStatu.Lib.Data.External.DataSets.DataSet).Name.ToLower()))
                {
                    var dataSetId = dt.Replace("DataSet.", "");
                    if (HlidacStatu.Lib.Data.External.DataSets.DataSet.ExistsDataset(dataSetId) == false)
                    {
                        HlidacStatu.Util.Consts.Logger.Error("AddWd - try to hack, wrong dataType = " + dataType + "." + dataSetId);
                        throw new ArgumentOutOfRangeException("AddWd - try to hack, wrong dataType = " + dataType + "." + dataSetId);
                    }
                    wd.dataType = typeof(HlidacStatu.Lib.Data.External.DataSets.DataSet).Name + "." + dataSetId;
                }
                else if (dt == WatchDog.AllDbDataType)
                {
                    wd.dataType = dt;
                }
                else
                {
                    HlidacStatu.Util.Consts.Logger.Error("AddWd - try to hack, wrong dataType = " + dataType);
                    throw new ArgumentOutOfRangeException("AddWd - try to hack, wrong dataType = " + dataType);
                }
                if (!db.WatchDogs
                    .Any(m => m.dataType == wd.dataType && m.UserId == id && m.SearchTerm == query))
                {
                    wd.Save();
                }


                //var lastOrder = HlidacStatu.Lib.Data.Invoices.GetCurrentOrderForUser(this.User.Identity.GetUserId());
                //if (lastOrder.HasValue == false && focus == 1)
                //    return Json("2", JsonRequestBehavior.AllowGet);

                return(Json("1", JsonRequestBehavior.AllowGet));
            }
        }
        public ActionResult Watchdog(string _id, string _dataid, string dataType = "VerejnaZakazka", string query = null, string expiration = null)
        {
            string id     = _id;
            string dataid = _dataid;

            id = id.ToLower();
            var apiAuth = Framework.ApiAuth.IsApiAuth(this,
                                                      parameters: new Framework.ApiCall.CallParameter[] {
                new Framework.ApiCall.CallParameter("id", id),
                new Framework.ApiCall.CallParameter("query", query),
                new Framework.ApiCall.CallParameter("expiration", expiration)
            });

            if (!apiAuth.Authentificated)
            {
                //Response.StatusCode = 401;
                return(Json(ApiResponseStatus.ApiUnauthorizedAccess, JsonRequestBehavior.AllowGet));
            }
            else
            {
                if (apiAuth.ApiCall.User != "*****@*****.**" &&
                    apiAuth.ApiCall.User != "*****@*****.**"
                    )
                {
                    return(Json(ApiResponseStatus.ApiUnauthorizedAccess, JsonRequestBehavior.AllowGet));
                }

                var wdName = WatchDog.APIID_Prefix + dataid;
                using (HlidacStatu.Lib.Data.DbEntities db = new HlidacStatu.Lib.Data.DbEntities())
                {
                    switch (id.ToLower())
                    {
                    case "add":
                        var expirDate = Devmasters.DT.Util.ToDateTime(expiration, "yyyy-MM-ddTHH:mm:ss");
                        if (string.IsNullOrEmpty(query))
                        {
                            return(Json(ApiResponseStatus.Error(-99, "No query"), JsonRequestBehavior.AllowGet));
                        }

                        var wd2 = db.WatchDogs.AsNoTracking().Where(m => m.Name == wdName).FirstOrDefault();
                        if (wd2 != null)
                        {
                            wd2.SearchTerm = query;
                            wd2.Expires    = expirDate;
                            wd2.Save();
                        }
                        else
                        {
                            var dt = dataType;

                            HlidacStatu.Lib.Data.WatchDog wd = new HlidacStatu.Lib.Data.WatchDog();
                            wd.Created         = DateTime.Now;
                            wd.UserId          = apiAuth.ApiCall.UserId;
                            wd.StatusId        = 1;
                            wd.SearchTerm      = query;
                            wd.PeriodId        = 2; //daily
                            wd.FocusId         = 0;
                            wd.Name            = wdName;
                            wd.Expires         = expirDate;
                            wd.SpecificContact = "HTTPPOSTBACK";
                            if (dt.ToLower() == typeof(Smlouva).Name.ToLower())
                            {
                                wd.dataType = typeof(Smlouva).Name;
                            }
                            else if (dt.ToLower() == typeof(VerejnaZakazka).Name.ToLower())
                            {
                                wd.dataType = typeof(VerejnaZakazka).Name;
                            }
                            else if (dt.ToLower().StartsWith(typeof(HlidacStatu.Lib.Data.External.DataSets.DataSet).Name.ToLower()))
                            {
                                var dataSetId = dt.Replace("DataSet.", "");
                                if (HlidacStatu.Lib.Data.External.DataSets.DataSet.ExistsDataset(dataSetId) == false)
                                {
                                    HlidacStatu.Util.Consts.Logger.Error("AddWd - try to hack, wrong dataType = " + dataType + "." + dataSetId);
                                    throw new ArgumentOutOfRangeException("AddWd - try to hack, wrong dataType = " + dataType + "." + dataSetId);
                                }
                                wd.dataType = typeof(HlidacStatu.Lib.Data.External.DataSets.DataSet).Name + "." + dataSetId;
                            }
                            else if (dt == WatchDog.AllDbDataType)
                            {
                                wd.dataType = dt;
                            }
                            else
                            {
                                HlidacStatu.Util.Consts.Logger.Error("AddWd - try to hack, wrong dataType = " + dataType);
                                throw new ArgumentOutOfRangeException("AddWd - try to hack, wrong dataType = " + dataType);
                            }

                            wd.Save();
                        }
                        break;

                    case "delete":
                    case "disable":
                    case "get":
                    case "enable":
                        var wd1 = db.WatchDogs.AsNoTracking().Where(m => m.Name == wdName).FirstOrDefault();
                        if (wd1 == null)
                        {
                            return(Json(ApiResponseStatus.Error(-404, "Watchdog not found"), JsonRequestBehavior.AllowGet));
                        }

                        if (id == "delete")
                        {
                            wd1.Delete();
                            return(Json(new { Ok = true }, JsonRequestBehavior.AllowGet));
                        }
                        if (id == "disable")
                        {
                            wd1.StatusId = 0;
                        }
                        if (id == "delete")
                        {
                            wd1.StatusId = 1;
                        }
                        if (id == "get")
                        {
                            return(Content(Newtonsoft.Json.JsonConvert.SerializeObject(new { id = wd1.Name.Replace("APIID:", ""), expiration = wd1.Expires, query = wd1.SearchTerm }), "text/json"));
                        }

                        wd1.Save();
                        break;

                    default:
                        break;
                    }
                }


                return(Json(new { Ok = true }, JsonRequestBehavior.AllowGet));
            }
        }