public ActionResult Edit(Guid? id, Guid? contractId)
 {
     ViewData["selectedTab"] = (int)0;
     if (id.HasValue)
     {
         if (id != Guid.Empty)
             return View(db.UserContractUris.FirstOrDefault(c => c.Id == id.Value));
         else
         {
             var contract = db.UserContracts.FirstOrDefault(u => u.Id == contractId.Value);
             var model = new UserContractUri();
             model.UserContract = contract;
             model.Enabled = true;
             return View(model);
         }
     }
     else
     {
         var contract = db.UserContracts.First(u => u.Id == contractId.Value);
         var model = new UserContractUri();
         model.UserContract = contract;
         model.Enabled = true;
         return View(model);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="browser"></param>
        public static UserContractRedirect ValidateUri(UserContractUri uri, HttpBrowserCapabilitiesBase browser)
        {
            if (uri == null) return null;

            if (uri.UserContractRedirects == null)
            {
                uri.RuleActive = false;
            }

            if (uri.UserContractRedirects != null)
                foreach (var redirect in uri.UserContractRedirects)
                {
                    if (redirect.Enabled == true)
                    {
                        redirect.RuleActive = ValidateRedirect(redirect, browser);
                        if (redirect.RuleActive && !uri.RuleActive)
                        {
                            uri.CalculatedUri = uri.Uri;
                            uri.RuleActive = true;

                            //now check the randomizer
                            ValidateRandomizer(uri);
                        }
                    }
                    else
                        redirect.RuleActive = false;
                }

            return null;
        }
        public ActionResult AddRandomUri(Guid userContractId, string uri)
        {
            if (userContractId == null)
            {
                return BreadCrum.RedirectToPreviousAction(Session, ControllerName);
            }

            var userContract = db.UserContracts.Find(userContractId);
            var userContractUri = new UserContractUri()
            {
                Id = Guid.NewGuid(),
                UserContractId = userContract.Id,
                Uri = uri,
                Name = "noname",
                Enabled = false,
                RandomFunction = 0,
            };
            userContract.UserContractUris.Add(userContractUri);
            var randomUri = new RandomUri()
            {
                Id = Guid.NewGuid(),
                Uri = uri,
                UserContractUriId = userContractUri.Id,
                RandomParameter = 1,
            };
            userContractUri.RandomUris.Add(randomUri);
            db.SaveChanges();
            return RedirectToAction("Edit", new {id = userContract.Id});
        }
        public ActionResult Create(UserContractUri usercontracturi)
        {
            if (ModelState.IsValid)
            {
                usercontracturi.Id = Guid.NewGuid();
                db.UserContractUris.Add(usercontracturi);
                db.SaveChanges();
                //return RedirectToAction("Index");
                return BreadCrum.RedirectToPreviousAction(Session, ControllerName);

            }

            ViewBag.UserContractId = new SelectList(db.UserContracts, "Id", "Uri", usercontracturi.UserContractId);
            return View(usercontracturi);
        }
        public ActionResult DeleteConfirmed(UserContractUri userContractUri, FormCollection collection)
        {
            try
            {
                var contract = db.UserContractUris.FirstOrDefault(c => c.Id == userContractUri.Id);
                var redirectId = contract.UserContract.Id;
                var redirectUserId = contract.UserContract.User.Id;
                if (contract != null)
                {
                    db.UserContractUris.Remove(contract);
                    db.SaveChanges();
                }
                //return RedirectToAction("Edit", "Contract", new { id = redirectId, userId = redirectUserId });
                return BreadCrum.RedirectToPreviousAction(Session, ControllerName);

            }
            catch (Exception ex)
            {
                return BreadCrum.RedirectToPreviousAction(Session, ControllerName);
            }
        }
 public ActionResult Edit(UserContractUri usercontracturi)
 {
     if (ModelState.IsValid)
     {
         db.Entry(usercontracturi).State = EntityState.Modified;
         db.SaveChanges();
         return BreadCrum.RedirectToPreviousAction(Session, ControllerName);
     }
     ViewBag.UserContractId = new SelectList(db.UserContracts, "Id", "Uri", usercontracturi.UserContractId);
     return View(usercontracturi);
 }
Exemplo n.º 7
0
        private static void ValidateRandomizer(UserContractUri redirect)
        {
            if (redirect.RandomFunction == 0) return;

            if (redirect.RandomUris.Count == 0) return;

            //%x;  x percent of clicks will have a different url
            if (redirect.RandomFunction == 1)
            {
                var total = redirect.UserContractLogs.Count;

                if (total == 0)
                {
                    redirect.CalculatedUri = redirect.Uri;
                    return;

                }

                //get the standard urls
                var standard = redirect.UserContractLogs.Count(row => row.ResultUri == redirect.Uri);

                var custom = redirect.RandomUris.FirstOrDefault();
                if (custom != null)
                {
                    var percent = custom.RandomParameter;

                    redirect.CalculatedUri = standard/(decimal)total*100 > percent ? custom.Uri : redirect.Uri;
                }
            }

            //1/x; 1 in x clicks will have a different url
            if (redirect.RandomFunction == 2)
            {
                var total = redirect.UserContractLogs.Count;

                if (total == 0)
                {
                    redirect.CalculatedUri = redirect.Uri;
                    return;

                }

                var custom = redirect.RandomUris.FirstOrDefault();
                if (custom != null)
                {
                    var click = Convert.ToInt16(custom.RandomParameter);

                    if (click == 0) return;
                    var logs = redirect.UserContractLogs.OrderByDescending(row => row.DateTime).Take(click);
                    redirect.CalculatedUri = logs.All(row => row.ResultUri != custom.Uri) ? custom.Uri : redirect.Uri;
                }

            }

            //a1.x1, a2.x2, a3.x3… where x1+x2+x3=x total number of clicks and a1, a2, a3… ratios for each x1, x2, x3…. In human language there are weights given to a set of urls.
            if (redirect.RandomFunction == 3)
            {
                var total = redirect.UserContractLogs.Count;

                if (total == 0)//take a first RandomUri
                {
                    var custom = redirect.RandomUris.FirstOrDefault();
                    if (custom != null)
                    {
                        redirect.CalculatedUri = custom.Uri;
                        return;
                    }
                }

                var customs = redirect.RandomUris;
                var allWeights = redirect.RandomUris.Sum(row => row.RandomParameter);

                if (allWeights == 0) return;

                var standardDistance = (decimal)0;//used to see that the weighting is also random made, with other words pick some uri if the distance of the last Uri is closer
                foreach (var custom in customs)
                {
                    var weight = custom.RandomParameter/allWeights;//this is to normalize the weight. a1=10, a2=50, a3=40 => na1=10/100, na2=50/100, na3=40/100

                    if (weight == 0) continue;

                    var count = redirect.UserContractLogs.Count(row => row.ResultUri == custom.Uri);// get the number of occurences for this Uri.

                    if (count/(decimal) total <= weight) //is this a candidate?
                    {
                        var customDistance = Math.Abs(count/(decimal) total - weight);
                        if (standardDistance <= customDistance)
                            //it means the calculated distance is high, we should promote this one
                        {
                            standardDistance = customDistance;//write it down, so we may know it has been a distance used.
                            redirect.CalculatedUri = custom.Uri;
                        }
                    }
                }

            }

            if (redirect.RandomFunction == 4)
            {
                var custom = redirect.RandomUris.OrderBy(row => Guid.NewGuid()).FirstOrDefault();
                if (custom != null)
                {
                    redirect.CalculatedUri = custom.Uri;
                }
            }
        }
        public ActionResult Edit(Guid? id, Guid? contractId, FormCollection collection, UserContractUri modelUri)
        {
            //Save Action
            //If id == null then insert
            if (!ModelState.IsValid)
            {
                return View(modelUri);
            }
            try
            {
                ViewData["selectedTab"] = int.Parse(collection.GetValue("selectedTab").AttemptedValue);
                collection.Remove("selectedTab");
                bool addNewUserContractUri = false;
                if (id.HasValue)
                {
                    if (id.Value == Guid.Empty)
                        addNewUserContractUri = true;
                }
                else
                    addNewUserContractUri = true;

                if (!addNewUserContractUri)
                {
                    var model = db.UserContractUris.FirstOrDefault(u => u.Id == id);
                    model.Enabled = modelUri.Actief;
                    model.Name = modelUri.Name;
                    model.Uri = modelUri.Uri;
                    model.RandomFunction = modelUri.RandomFunction;

                    db.SaveChanges();
                    return View(model);
                }
                else
                {
                    UserContractUri model = new UserContractUri();
                    var contract = db.UserContracts.First(c => c.Id == contractId);
                    model.Id = Guid.NewGuid();
                    model.UserContractId = contract.Id;
                    model.Enabled = modelUri.Actief;
                    model.Name = modelUri.Name;
                    model.Uri = modelUri.Uri;
                    model.RandomFunction = modelUri.RandomFunction;

                    db.UserContractUris.Add(model);
                    db.SaveChanges();
                    //return RedirectToAction("Edit", new {id = model.Id, contractId = model.UserContractId});
                    //return RedirectToAction("Edit", "Contract", new { id = contract.Id, userId = contract.UserId });
                    return BreadCrum.RedirectToPreviousAction(Session, ControllerName);

                }
            }
            catch(Exception ex)
            {
                //return RedirectToAction("Edit", new { id = Guid.Empty, contractId = contractId });
                return BreadCrum.RedirectToPreviousAction(Session, ControllerName);

            }
        }
        private void WriteLog(Dictionary<string, string> userAgent, string resultUri, UserContract userContract, UserContractUri userContractUri = null,
                              UserContractRedirect userContractRedirect = null)
        {
            var host = Request.UserHostAddress;
            var userContractLog = new UserContractLog
                {
                    Id = Guid.NewGuid(),
                    DateTime = DateTime.Now,
                    Host = host,
                    ResultUri = resultUri,
                    UserContractId = userContract.Id,
                    UserAgent = userAgent.Aggregate("", (current, key) => current + string.Format("{0}:{1};", key.Key, key.Value))
                };

            if (userContractUri != null)
            {
                userContractLog.UserContractUri = userContractUri.Id;
            }

            if (userContractRedirect != null)
            {
                userContractLog.UserContractRedirect = userContractRedirect.Id;
            }

            db.UserContractLogs.Add(userContractLog);
            db.SaveChanges();
        }
 private void BuildRecipeByWeekDayPro(UserContract userContract)
 {
     for (var i = 1; i <= 7; i++)
     {
         var userContractUri = new UserContractUri()
         {
             Id = Guid.NewGuid(),
             UserContractId = userContract.Id,
             Uri = userContract.Uri,
             Name = "noname",
             Enabled = false,
             RandomFunction = 0,
         };
         userContract.UserContractUris.Add(userContractUri);
         var userContractRedirect = new UserContractRedirect()
         {
             Id = Guid.NewGuid(),
             UserContractUri = userContractUri.Id,
             Enabled = true
         };
         userContractRedirect.DayOfTheWeekValue = i;
         userContractUri.UserContractRedirects.Add(userContractRedirect);
     }
 }
 private void BuildRecipeByTime(UserContract userContract)
 {
     var userContractUri = new UserContractUri()
     {
         Id = Guid.NewGuid(),
         UserContractId = userContract.Id,
         Uri = userContract.Uri,
         Name = "noname",
         Enabled = false,
         RandomFunction = 0,
     };
     userContract.UserContractUris.Add(userContractUri);
     var userContractRedirect = new UserContractRedirect()
     {
         Id = Guid.NewGuid(),
         UserContractUri = userContractUri.Id,
         Enabled = true
     };
     userContractUri.UserContractRedirects.Add(userContractRedirect);
 }
 private void BuildRecipeByRandom4(UserContract userContract)
 {
     var userContractUri = new UserContractUri()
     {
         Id = Guid.NewGuid(),
         UserContractId = userContract.Id,
         Uri = userContract.Uri,
         Name = "noname",
         Enabled = false,
         RandomFunction = 3,
     };
     userContract.UserContractUris.Add(userContractUri);
     var userContractRedirect = new UserContractRedirect
     {
         Id = Guid.NewGuid(),
         UserContractUri = userContractUri.Id,
         Enabled = true,
         DateTimeValueStart = userContract.StartDate,
         DateTimeValueStop = userContract.EndDate
     };
     userContractUri.UserContractRedirects.Add(userContractRedirect);
     var randomUri = new RandomUri()
     {
         Id = Guid.NewGuid(),
         Uri = userContract.Uri,
         UserContractUriId = userContractUri.Id,
         RandomParameter = 1,
     };
     userContractUri.RandomUris.Add(randomUri);
 }
        public ActionResult AddUri(Guid userContractId, DateTime? StartEndDate, string uri)
        {
            if (userContractId == null)
            {
                return BreadCrum.RedirectToPreviousAction(Session, ControllerName);
            }

            var userContract = db.UserContracts.Find(userContractId);
            var userContractUri = new UserContractUri()
            {
                Id = Guid.NewGuid(),
                UserContractId = userContract.Id,
                Uri = uri,
                Name = "noname",
                Enabled = false,
                RandomFunction = 0,
            };
            userContract.UserContractUris.Add(userContractUri);
            var userContractRedirect = new UserContractRedirect()
            {
                Id = Guid.NewGuid(),
                UserContractUri = userContractUri.Id,
                Enabled = true
            };
            userContractUri.UserContractRedirects.Add(userContractRedirect);
            db.SaveChanges();
            return RedirectToAction("Edit", new {id = userContract.Id});
        }
 private void BuildRecipeByDate(UserContract userContract)
 {
     var userContractUri = new UserContractUri()
     {
         Id = Guid.NewGuid(),
         UserContractId = userContract.Id,
         Uri = userContract.Uri,
         Name = "noname",
         Enabled = false,
         RandomFunction = 0,
     };
     userContract.UserContractUris.Add(userContractUri);
     var userContractRedirect = new UserContractRedirect()
     {
         Id = Guid.NewGuid(),
         UserContractUri = userContractUri.Id,
         Enabled = true
     };
     userContractRedirect.DateTimeValueStart = DateTime.Now;
     userContractUri.UserContractRedirects.Add(userContractRedirect);
     db.SaveChanges();
 }
 public ActionResult AddUserContractUri(Guid userContractId, int weekDay)
 {
     var userContract = db.UserContracts.Find(userContractId);
     var userContractUri = new UserContractUri()
     {
         Id = Guid.NewGuid(),
         UserContractId = userContract.Id,
         Uri = userContract.Uri,
         Name = "noname",
         Enabled = false,
         RandomFunction = 0,
     };
     userContract.UserContractUris.Add(userContractUri);
     var userContractRedirect = new UserContractRedirect()
     {
         Id = Guid.NewGuid(),
         UserContractUri = userContractUri.Id,
         Enabled = true
     };
     userContractRedirect.DayOfTheWeekValue = weekDay;
     userContractUri.UserContractRedirects.Add(userContractRedirect);
     db.SaveChanges();
     return RedirectToAction("Edit", new {id = userContract.Id});
 }