protected void Page_Load(object sender, EventArgs e)
        {
            Class.User user = Session["User"] as Class.User;
            // - Martin
            long idTestCase = 0;

            if (HttpContext.Current.Session["IdTestCase"] != null)
            {
                idTestCase = Convert.ToInt64(HttpContext.Current.Session["IdTestCase"]);
            }
            // infos die immer da sind
            Class.TestCase tc = DataAccess.DataAccessTestCase.getTestCase().Where(p => p.IDTestcase == idTestCase).FirstOrDefault();  /* original:  Class.TestCase tc = DataAccess.DataAccessTestCase.getTestCase(Convert.ToInt64(Session["IdTestCase"]));*/
            // initialize manage object with all needed content
            managerView = new manageImg(tc, user);

            // spawn a thread to create the image packages
            ThreadStart thread = new ThreadStart(managerView.ThreadRun);

            task = new Thread(thread);

            task.Start();

            if (task.IsAlive) // creation of everything finished => all necessary information are in Session["managerView"]
            {
                Session["managerView"] = managerView;
                Response.Redirect("~/managementImage/ViewImage_.aspx");
            }
        }
        public void writeConfigFile(long idtestcase)
        {
            Class.TestCase    tc             = DataAccess.DataAccessTestCase.getTestCase(idtestcase);
            List <GroupImage> listgroupImage = DataAccessTestCase.getAllImagesOfTestcase(idtestcase);
            string            path           = Constant.pathConfigFileTestcase;


            string listOfUsertypes         = "";
            List <Class.TypUser> Usertypes = DataAccessUser.GetTypeUserForTestCase(idtestcase);

            foreach (Class.TypUser user in Usertypes)
            {
                listOfUsertypes += user.ID_Typ + ",";
            }


            try
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(path + "\\" + tc.NameTestCase + "_" + idtestcase + ".cfg"))
                {
                    string activeLearning      = "off";
                    string scale               = "continuous";
                    int    totalAmountOfImages = 0;

                    if (tc.DiskreteScale)
                    {
                        scale = "discrete";
                    }

                    if (tc.ActiveLearning)
                    {
                        activeLearning = "on";
                    }

                    file.WriteLine("Id:" + idtestcase + "; name:" + tc.NameTestCase + "; general info:" + tc.GeneralInfo + "; test question:" + tc.TestQuestion + "; scale type:" + scale
                                   + "; active learning:" + activeLearning + "; initial threshold:" + tc.initialThreshold + "; user interval:" + tc.userThreshold + "; database path:" + tc.dbPath + "; eligible user types: " + listOfUsertypes);
                    file.WriteLine("IdGroup; Path; IsReference; IdGroupHasImage; PatientName");

                    foreach (GroupImage image in listgroupImage)
                    {
                        file.WriteLine(image.Idgroup + "; " + image.Path + "; " + image.IsReference + "; " + image.IdGroupHasImage + "; " + image.PatienteName + ";");
                        totalAmountOfImages++;
                    }
                    file.WriteLine("total amount of images in testcase: " + totalAmountOfImages);
                }
            }
            catch (Exception) { }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (HttpContext.Current.Session["User"] == null)
                {
                    Response.Redirect("~/Account/Login.aspx");
                }
                else
                {
                    listGroup         = new List <Class.Group>();
                    listAddedGroups   = new List <Group>();
                    listRemovedGroups = new List <long>();

                    // check for duplicate descriptions, add the version to the list and remove duplicates
                    for (int i = 0; i < tsc.Count(); i++)
                    {
                        tsc[i].scaleVersions = new List <string>();
                        tsc[i].scaleVersions.Add(tsc[i].verScaleCont + "");

                        for (int c = i + 1; c < tsc.Count(); c++)
                        {
                            if (tsc[i].DescriptionScaleContinuous == tsc[c].DescriptionScaleContinuous)
                            {
                                tsc[i].scaleVersions.Add(tsc[c].verScaleCont + "");
                                tsc.RemoveAt(c);
                                c--;
                            }
                        }

                        tsc[i].ddlContinuous.DataSource = tsc[i].scaleVersions;
                        tsc[i].ddlContinuous.DataBind();
                    }


                    PopulateTypeUser();            // insert in the drop down list all existing users
                    PopulateTypeScaleContinuous(); // insert in the drop down list all existing scale continous
                    PopulateTypeScaleDiscrete();
                    PopulateDatabases();

                    cbxDiscreteScale.Attributes.Add("style", "display:none");

                    string labeling = listDatabase[0].DatabasePath;
                    Constant.currentDatabase = labeling;

                    long?id_TestCase = null;
                    if (Session["IdTestCase"] != null)
                    {
                        id_TestCase = Convert.ToInt64(Session["IdTestCase"]);
                    }
                    if (!IsPostBack)
                    {
                        listGroup = new List <Class.Group>();
                        // if admin wants to change a test case from view test case
                        if (id_TestCase != null)// es gibt schon ein id
                        {
                            //listGroup = new List<Class.Group>();
                            long                 idTestCase = (long)id_TestCase;
                            Class.TestCase       tc         = DataAccess.DataAccessTestCase.getTestCase().Where(p => p.IDTestcase == idTestCase).FirstOrDefault();
                            List <Class.TypUser> listtu     = DataAccess.DataAccessUser.GetTypeUserForTestCase(idTestCase);
                            listGroup_ = DataAccess.DataAccessTestCase.getGroupFromIdTestCase(idTestCase);
                            // name test case and testquestion cant be changed
                            txtNameTestCase.Text      = tc.NameTestCase;
                            txtNameTestCase.Enabled   = false;
                            txtTestQuestion.Text      = tc.TestQuestion;
                            txtTestQuestion.Enabled   = false;
                            txtGeneralInfo.Text       = tc.GeneralInfo;
                            cbxActiveLearning.Checked = tc.ActiveLearning;

                            if (tc.ActiveLearning)
                            {
                                txtActiveUserThreshold.Text    = tc.userThreshold + "";
                                txtActiveInitialThreshold.Text = tc.initialThreshold + "";
                            }

                            hddTCSeed.Value = Convert.ToString(tc.iSeed);

                            // schaut welche skala ausgesucht ist
                            if (tc.DiskreteScale)
                            {
                                rblTypeScale.SelectedValue = "0";
                            }
                            else
                            {
                                rblTypeScale.SelectedValue = "1";
                            }

                            rblTypeScale.Enabled = false;

                            btnInsertTestCase.Visible = false;// no button insert, but instead later button change

                            // takes all the selected type of user
                            foreach (Class.TypUser tu in listtu)
                            {
                                ListItem app = rblTypeUserAssigned.Items.FindByValue(tu.ID_Typ.ToString());

                                int id = rblTypeUserAssigned.Items.IndexOf(app);

                                rblTypeUserAssigned.Items[id].Selected = true;
                            }

                            // insert all the already created images groups
                            foreach (Class.Group g in listGroup_)
                            {
                                List <Class.GroupImage> listGroupImage = DataAccess.DataAccessTestCase.GetGrouphasImageFromGroup(g.IdGroup, 0);
                                Class.Group             appgroup       = new Class.Group();
                                appgroup.Name              = g.Name;
                                appgroup.GroupExistDB      = true;
                                appgroup.IdGroup           = g.IdGroup;
                                appgroup.IdTestCase        = g.IdTestCase;
                                appgroup.ReferenceIsGlobal = g.ReferenceIsGlobal;
                                appgroup.IsPatientChosen   = g.IsPatientChosen;
                                appgroup.PageStyle         = g.PageStyle;
                                appgroup.GroupHasReference = g.GroupHasReference;
                                appgroup.ImagesPerPage     = g.ImagesPerPage;



                                if (g.ReferenceIsGlobal)
                                {
                                    appgroup.ReferenceName = "GLOBAL";
                                }
                                else
                                {
                                    appgroup.ReferenceName = "";
                                }

                                int numberOfImages = 0;
                                g.ImagesPerPage = 0;
                                string Patient = "";
                                for (int i = 0; i < listGroupImage.Count; i++)
                                {
                                    if (i == 0)
                                    {
                                        Patient = listGroupImage[0].PatienteName;
                                    }

                                    if (listGroupImage[i].PatienteName == Patient)
                                    {
                                        g.ImagesPerPage++;
                                    }

                                    appgroup.LoopOver += listGroupImage[i].Path.Replace("/", "_") + System.Environment.NewLine;
                                    numberOfImages++;
                                    if (listGroupImage[i].IsReference)
                                    {
                                        appgroup.ReferenceName = listGroupImage[i].Path.Split('/')[1];
                                    }
                                }

                                if (g.PageStyle > 0)
                                {
                                    g.ImagesPerPage = Convert.ToInt32(g.PageStyle);
                                }

                                if (g.ImagesPerPage < 1)
                                {
                                    g.ImagesPerPage = -1;
                                }

                                appgroup.PageStyleDescription = getText(g.PageStyle, numberOfImages);
                                appgroup.OverallNumber        = numberOfImages;
                                double numberOfPages = (numberOfImages / g.ImagesPerPage);
                                appgroup.NumberOfPages = (int)Math.Ceiling(numberOfPages);


                                listGroup.Add(appgroup);
                            }

                            Session["Group"]       = listGroup;
                            gvGroupName.DataSource = listGroup;
                            gvGroupName.DataBind();
                            EnabledSelectedTypeUser();
                            btnUpdateTestCase.Visible = true;
                        }
                        else
                        {
                            btnUpdateTestCase.Visible = false;// es wurde kein id gefunden und desshalb muessen keine infos geladen werden
                        }
                    }
                }

                gvContinuous.DataSource = tsc;
                gvContinuous.DataBind();
                gvContinuous.Visible = false;
                ScriptManager.RegisterStartupScript(this, GetType(), "fUpdateTable", "fUpdateTable();", true);
            }
            if (Session["Group"] != null)
            {
                listGroup = (List <Class.Group>)Session["Group"];
            }

            if (Session["typeckeched"] != null)
            {
                listtypechecked = (List <string>)Session["typeckeched"];
            }
        }
        // click button
        protected void btnInsertTestCase_Click(object sender, EventArgs e)
        {
            try
            {
                List <long> listScales = GetSelectedTypeScaleContinuous();
                if (Page.IsValid)
                {
                    // takes infos from textboxes
                    Class.TestCase tc = new Class.TestCase();
                    tc.NameTestCase  = txtNameTestCase.Text;
                    tc.DiskreteScale = rblTypeScale.SelectedValue == "0" ? true : false;
                    tc.GeneralInfo   = txtGeneralInfo.Text;
                    tc.State         = (int)LabelingFramework.Utility.Enum.StatoTestCase.towork;
                    tc.TestQuestion  = txtTestQuestion.Text;
                    if (txtminvalue.Text != string.Empty)
                    {
                        tc.MinDiskreteScale = Convert.ToInt32(txtminvalue.Text);
                    }
                    else
                    {
                        tc.MinDiskreteScale = 0;
                    }
                    if (txtmaxvalue.Text != string.Empty)
                    {
                        tc.MaxDiskreteScale = Convert.ToInt32(txtmaxvalue.Text);
                    }
                    else
                    {
                        tc.MaxDiskreteScale = 5;
                    }

                    tc.dbPath         = Constant.currentDatabase; // - Martin
                    tc.ActiveLearning = cbxActiveLearning.Checked;


                    if (tc.ActiveLearning)
                    {      // only first group is added in case of active learning
                        while (listGroup.Count > 1)
                        {
                            listGroup.RemoveAt(listGroup.Count - 1);
                        }
                    }


                    tc.userThreshold    = Int32.Parse(txtActiveUserThreshold.Text);
                    tc.initialThreshold = Int32.Parse(txtActiveInitialThreshold.Text);
                    tc.iSeed            = Guid.NewGuid().GetHashCode();


                    //List<long> listScales;
                    if (tc.DiskreteScale)
                    {
                        listScales = new List <long>();
                        listScales.Add(GetSelectedScaleDiscrete());
                    }
                    else
                    {
                        listScales = GetSelectedTypeScaleContinuous();
                    }


                    Int64 IdTestCase = 0;



                    // checks is everything is fine between mysql, testcase ,repository und bilder , wenn alles funktioniert dann wird das testcase gespeichert sonst nicht
                    using (TransactionScope scope = new TransactionScope())
                    {
                        DataAccess.DataAccessTestCase.InsertTestCase(tc, ref IdTestCase);
                        DataAccess.DataAccessTestCase.InsertRelationshipTestTypeUser(IdTestCase, GetSelectedTypeUser());
                        DataAccess.DataAccessTestCase.InsertRelationshipTestScale(IdTestCase, listScales, tc.DiskreteScale);
                        DataAccess.DataAccessTestCase.InsertGroup(IdTestCase, listGroup);
                        writeConfigFile(IdTestCase);
                        scope.Complete();

                        DivSuccess.Visible = true;
                        DivError.Visible   = false;
                    }


                    if (Constant.sendNotificationMailTC)
                    {
                        List <Class.User> users = DataAccess.DataAccessTestCase.getUsersFromTestCaseID(IdTestCase);
                        //string subject = "New test case '" + tc.NameTestCase +  "' available on labeling framework website";
                        MyEmail mail = new MyEmail();


                        foreach (Class.User user in users)
                        {
                            //string messageText = "Dear " + user.title + " " + user.Surname + ",\n a new test case is available for labeling on: " + "<a href=''" + Constant.webSiteAddress + "''>" + Constant.webSiteAddress + "</a> ";

                            List <string> message = Constant.testCaseNotificationMail(user, tc);

                            mail.sendEmail(user.Email, message[0], message[1], false);
                        }
                    }

                    ScriptManager.RegisterStartupScript(this, GetType(), "fVersionScale", "fVersionScale();", true);
                    Response.Redirect("~/TestCase/ViewTestCase.aspx"); // return to previous page ('ViewTestCase')
                }
                else
                {
                    DivError.Visible   = true;
                    lblError.Text      = "Pleasse check if all fields are filled out properly";
                    DivSuccess.Visible = false;
                }
            }
            catch (Exception ex)
            {
                int line = (new StackTrace(ex, true)).GetFrame(0).GetFileLineNumber();
                DivError.Visible   = true;
                lblError.Text      = ex.Message + " Error occurred in C# (\"InsertTestCase.aspx\" line: " + line + ")";
                DivSuccess.Visible = false;
            }
        }