예제 #1
0
        public ActionResult AddRemoveCode(string site, string accession, string type, string isTouch = "")
        {
            Regex rgx    = new Regex("[^a-zA-Z0-9,]");
            var   result = db.AccessionCodes                                           //all existing accession codes
                           .Select(o => o.Code);

            List <String> siteCodes = db.AccLoc.Where(o => o.LocCode == site).Select(o => o.AccCode).ToList();
            List <String> touchList = db.AccessionCodes.Where(o => o.IsTouch == "Y").Select(o => o.Code).ToList();

            String[]         codes = rgx.Replace(accession.Trim(), "").Split(',');
            HashSet <String> set   = new HashSet <string>();

            foreach (String s in codes)
            {
                set.Add(s);
            }
            String errorlog = "";

            if (type == "add")
            {
                foreach (String c in set)
                {
                    if (touchList.Contains(c) && isTouch == "")
                    {
                        errorlog += c + " ";
                    }
                }
                if (errorlog == "")
                {
                    string added = "";
                    foreach (String c in set)
                    {
                        added += c + " ";
                        if (!result.Contains(c))
                        {
                            AccessionCodes ac = new AccessionCodes(db);                    //new regular code only if it does not exist
                            ac.Code = c;
                            if (isTouch != "")
                            {
                                ac.IsTouch = "Y";
                            }
                            else
                            {
                                ac.IsTouch = "N";
                            }
                            ac.CreatedAt = DateTime.Now;
                            ac.UpdatedAt = DateTime.Now;
                            //ac.LockVersion = 0;
                            db.AccessionCodes.Add(ac);
                            db.SaveChanges();

                            AccLoc al = new AccLoc(db);                                             //location and code mapping
                            al.LocCode   = site.Trim();
                            al.AccCode   = c;
                            al.CreatedAt = DateTime.Now;
                            al.UpdatedAt = DateTime.Now;
                            //al.LockVersion = 0;
                            db.AccLoc.Add(al);
                            db.SaveChanges();
                        }
                        else if (siteCodes.Contains(c))
                        {
                            if (isTouch == "y")
                            {
                                if (!touchList.Contains(c))
                                {
                                    AccessionCodes acode = db.AccessionCodes.Where(o => o.Code == c).First();
                                    acode.IsTouch = "Y";
                                    db.SaveChanges();
                                }
                            }
                        }
                        else if (!siteCodes.Contains(c))
                        {
                            if (isTouch == "y")
                            {
                                if (!touchList.Contains(c))
                                {
                                    AccessionCodes acode = db.AccessionCodes.Where(o => o.Code == c).First();
                                    acode.IsTouch = "Y";
                                    db.SaveChanges();
                                }
                            }
                            AccLoc al = new AccLoc(db);                                             //location and code mapping
                            al.LocCode   = site.Trim();
                            al.AccCode   = c;
                            al.CreatedAt = DateTime.Now;
                            al.UpdatedAt = DateTime.Now;
                            //al.LockVersion = 0;
                            db.AccLoc.Add(al);
                            db.SaveChanges();
                        }
                    }


                    added = String.Join(",", added.Trim().Split(" "));
                    UserLogs u = new UserLogs(db);
                    u.LogDetails(currentUserID, IPAddress, "Added accession codes: " + added + " at location : " + site);
                }
                else
                {
                    errorlog = String.Join(",", errorlog.Trim().Split(" "));
                    TempData["accession"] = rgx.Replace(accession.Trim(), "");
                    TempData["site"]      = site.Trim();
                    List <Location> lc = db.Location.ToList();
                    TempData["locations"] = lc;
                    ModelState.AddModelError("CustomError", "The following codes are touch codes and must be added as touch codes for this location" + "\n" + errorlog);
                    return(View("AddRemove"));
                }
            }
            else //remove
            {
                string removed = "";
                foreach (String c in set)
                {
                    if (siteCodes.Contains(c))
                    {
                        AccLoc acl = db.AccLoc.Where(o => o.LocCode == site && o.AccCode == c).First();
                        db.Remove(acl);
                        db.SaveChanges();
                        removed += c + " ";
                    }
                }

                removed = String.Join(",", removed.Trim().Split(" "));
                UserLogs u = new UserLogs(db);
                u.LogDetails(currentUserID, IPAddress, "Removed accession codes: " + removed + " at location : " + site);

                //removing the unused codes    NOW HANDLED BY TRIGGER
                //List<String> accLocLeft = db.AccLoc.Select(o => o.AccCode).ToList();
                //List<AccessionCodes> toBeDel = db.AccessionCodes.Where(o => !accLocLeft.Contains(o.Code)).ToList();
                //foreach (AccessionCodes ac in toBeDel)
                //{
                //    db.AccessionCodes.Remove(ac);
                //    db.SaveChanges();
                //}
            }

            TempData["msg"] = "Success";
            return(RedirectToAction("Index", "Home"));
        }
        public ActionResult AddRemoveUser(string userid, string type)
        {
            Regex rgx    = new Regex("[^a-zA-Z0-9,]");
            var   result = db.Users                                           //all existing user ids
                           .Select(o => o.UserId);

            String[]         userids = rgx.Replace(userid.Trim(), "").Split(',');
            HashSet <String> set     = new HashSet <string>();

            foreach (String s in userids)
            {
                set.Add(s);
            }
            String errorlog = "";

            if (type == "add")
            {
                string added = "";
                foreach (String c in set)
                {
                    if (result.Contains(c))
                    {
                        errorlog += c + " ";
                    }
                }
                if (errorlog == "")
                {
                    foreach (String c in set)
                    {
                        Users u = new Users(db);
                        u.UserId    = c;
                        u.CreatedAt = DateTime.Now;
                        added      += c + " ";
                        db.Users.Add(u);
                        db.SaveChanges();
                    }
                    added = String.Join(",", added.Trim().Split(" "));
                    UserLogs ul = new UserLogs(db);
                    ul.LogDetails(currentUserID, IPAddress, "Added Users: " + added);
                }
                else
                {
                    errorlog            = String.Join(",", errorlog.Trim().Split(" "));
                    TempData["userids"] = rgx.Replace(userid.Trim(), "");

                    List <Users> lc = db.Users.ToList();
                    TempData["users"] = lc;
                    ModelState.AddModelError("CustomError", "The following users are already registered" + "\n" + errorlog);
                    return(View("ManageForm"));
                }
            }
            else //remove
            {
                string removed = "";
                foreach (String c in set)
                {
                    if (result.Contains(c))
                    {
                        Users u = db.Users.Where(o => o.UserId == c).First();
                        db.Remove(u);
                        db.SaveChanges();
                        removed += c + " ";
                    }
                }
                removed = String.Join(",", removed.Trim().Split(" "));
                UserLogs ul = new UserLogs(db);
                ul.LogDetails(currentUserID, IPAddress, "Removed Users: " + removed);

                //removing the unused codes    NOW HANDLED BY TRIGGER
                //List<String> accLocLeft = db.AccLoc.Select(o => o.AccCode).ToList();
                //List<AccessionCodes> toBeDel = db.AccessionCodes.Where(o => !accLocLeft.Contains(o.Code)).ToList();
                //foreach (AccessionCodes ac in toBeDel)
                //{
                //    db.AccessionCodes.Remove(ac);
                //    db.SaveChanges();
                //}
            }

            TempData["msg"] = "Success";
            return(RedirectToAction("Index", "Home"));
        }