// GET api/<controller>
        public IEnumerable GetAllDirectory()
        {
            List <string> text = new List <string>();

            //string labeling = DataAccessBase.GetLabeling();
            string        labeling = Constant.currentDatabase;
            DirectoryInfo dir      = null;

            if (labeling != string.Empty)
            {
                dir = new DirectoryInfo(labeling);
            }
            else
            {
                var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content      = new StringContent("no dir created!"),
                    ReasonPhrase = "labeling is empty"
                };
                throw new HttpResponseException(resp);
            }


            List <Class.Directory> listdir = new List <Class.Directory>();
            int i = 0;

            foreach (DirectoryInfo d in dir.GetDirectories())
            {
                Class.Directory dirprova = new Class.Directory();
                dirprova.NameDirectory = d.Name;
                i++;
                dirprova.IdDirectory = i;


                dirprova.SubDirectory = new List <Class.Directory>();
                text.Add("\n");
                foreach (DirectoryInfo d1 in d.GetDirectories())
                {
                    Class.Directory sub = new Class.Directory();
                    sub.NameDirectory = d1.Name;
                    sub.IdDirectory   = i;
                    dirprova.SubDirectory.Add(sub);
                    text.Add(d1.Name);
                }

                listdir.Add(dirprova);
            }


            // writeFile(text);
            return(listdir);
        }
        public IEnumerable GetDirectorybyNameSubDir(string id)
        {
            List <string> text = new List <string>();

            string[] s     = id.Split('!');
            bool     found = false;

            //string labeling = DataAccessBase.GetLabeling();
            string        labeling = Constant.currentDatabase;
            DirectoryInfo dir      = null;

            if (labeling != string.Empty)
            {
                dir = new DirectoryInfo(labeling);
            }


            List <Class.Directory> listdir = new List <Class.Directory>();


            foreach (DirectoryInfo d in dir.GetDirectories())
            {
                Class.Directory dirprova = new Class.Directory();
                dirprova.NameDirectory = d.Name;
                dirprova.SubDirectory  = new List <Class.Directory>();

                text.Add("\n");
                foreach (DirectoryInfo d1 in d.GetDirectories())
                {
                    LabelingFramework.Class.Directory sub = new Class.Directory();
                    sub.NameDirectory = d1.Name;
                    dirprova.SubDirectory.Add(sub);
                    text.Add(d1.Name);
                }

                for (int i = 0; i < s.Length - 1; i++)
                {
                    if (d.GetDirectories().Where(p => p.Name.ToUpper() == s[i].ToString().ToUpper()).Any())
                    {
                        found = true;
                    }
                    else
                    {
                        found = false;
                        break;
                    }
                }



                if ((found) || (s[0] == string.Empty))
                {
                    listdir.Add(dirprova);
                }
            }


            // writeFile(text);

            return(listdir);
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Class.Group group    = new Class.Group();
            string      labeling = Constant.currentDatabase;
            //string labeling = DataAccessBase.GetLabeling();

            int numberOfImages = 0;

            group.IsPatientChosen = Convert.ToBoolean(HiddenFieldIsPatientChosen.Value);

            // - Martin - split at '|' (no regular directory symbol)
            string[] path      = HiddenFieldPathSel.Value.Split('|');
            string   reference = HiddenFieldReferenceName.Value;

            group.ImagesPerPatient = Int32.Parse(hddNumberImagePatient.Value);

            if (reference != "Choose a Reference")
            {
                group.GroupHasReference = true;
            }
            bool isNotLocal = false;

            // definition of variables used later
            if ((reference.ToUpper() == "GLOBAL") || (reference.ToUpper() == "CHOOSE A REFERENCE"))
            {
                isNotLocal = true;
            }

            if ((reference.ToUpper() == "GLOBAL"))
            {
                group.ReferenceIsGlobal = true;
            }

            for (int i = 0; i < path.Length - 1; i++)
            {
                numberOfImages++;
                Class.Directory dir = new Class.Directory();
                dir.Path = path[i];

                if (!isNotLocal)
                {
                    if (path[i].ToUpper().Contains(reference.ToUpper()))
                    {
                        dir.IsReference     = true;
                        group.ReferenceName = path[i].Split('/')[1];
                    }
                    else
                    {
                        dir.IsReference = false;
                    }

                    group.LoopOver += path[i].Replace("/", "_") + System.Environment.NewLine;
                }
                else
                {
                    group.LoopOver += path[i].Replace("/", "_") + System.Environment.NewLine;

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


                group.SubDirectory.Add(dir);
            }
            group.PageStyle = Int64.Parse(hddImagesPerPage.Value);
            int images = 0;     // max amount of images to be shown on a a single page

            switch (group.PageStyle)
            {
            case 0: { group.ImagesPerPage = -1; images = group.ImagesPerPatient; break; }

            // case 1: { group.ImagesPerPage = 1; break; }
            case 1: { try { group.ImagesPerPage = Int32.Parse(txtmImagesPerPage.Text); group.ImagesPerPage = (group.ImagesPerPage < 0) ? -1 : group.ImagesPerPage; } catch (Exception) { group.ImagesPerPage = -1; }; break; }

            default: { group.ImagesPerPage = -1; break; }
            }


            // check if value is within codomain [1, number of selected images]
            if (group.ImagesPerPatient == 0)
            {
                group.ImagesPerPatient = 1;
            }

            if (0 < group.ImagesPerPage)
            {
                images = group.ImagesPerPage;
            }

            if (group.ImagesPerPatient < group.ImagesPerPage)
            {
                images = group.ImagesPerPatient;
            }


            string patient          = "";
            int    numberOfPatients = 0;

            for (int i = 0; i < path.Length; i++)
            {
                if ((path[i] != "") && (path[i].Substring(0, path[i].IndexOf("/")) != patient))
                {
                    patient = path[i].Substring(0, path[i].IndexOf("/"));
                    numberOfPatients++;
                }
            }

            if (group.GroupHasReference && !group.ReferenceIsGlobal)
            {
                group.ImagesPerPatient--;
            }

            group.PageStyleDescription = getText(group.PageStyle, images);
            group.OverallNumber        = numberOfImages;
            double numberOfPages = ((double)group.ImagesPerPatient / images);

            group.NumberOfPages = (int)Math.Ceiling(numberOfPages) * numberOfPatients;



            listAddedGroups.Add(group);
            listGroup.Add(group);


            // to avoid adding the same group again after a postback
            if (listGroup.Count > 1)
            {
                if ((listGroup[listGroup.Count - 1].GroupHasReference == listGroup[listGroup.Count - 2].GroupHasReference) && (listGroup[listGroup.Count - 1].LoopOver == listGroup[listGroup.Count - 2].LoopOver) && (listGroup[listGroup.Count - 1].ReferenceName == listGroup[listGroup.Count - 2].ReferenceName))
                {
                    listGroup.RemoveAt(listGroup.Count() - 1);
                }
            }



            if (listGroup[listGroup.Count() - 1].LoopOver == null)
            {
                listGroup.RemoveAt(listGroup.Count() - 1); // remove last entry in case of an added empty group
            }
            else
            {
                group.Name               = (listGroup.Count()).ToString();
                Session["Group"]         = listGroup;
                HiddenFieldPathSel.Value = "";
                gvGroupName.DataSource   = listGroup;
                gvGroupName.DataBind();
            }
            //for (int i = 0; i < ddlDatabase.Items.Count; i++)
            //{
            //    if (ddlDatabase.Items[i].Selected)
            //    {
            //        Constant.currentDatabase = ddlDatabase.Items[i].Text;
            //        break;
            //    }
            //}
            ScriptManager.RegisterStartupScript(this, GetType(), "fUpdateTable", "fUpdateTable();", true);
        }