private void FormDisplayFields_Load(object sender, EventArgs e)
 {
     _listOrthoChartTabs     = OrthoChartTabs.GetDeepCopy(true);
     _listOrthoChartTabLinks = OrthoChartTabLinks.GetDeepCopy();
     LoadDisplayFields();
     FillComboOrthoChartTabs();
     FillGrids();
 }
Ejemplo n.º 2
0
        private void butOK_Click(object sender, EventArgs e)
        {
            OrthoChartTabFields orphanedTab = _listTabDisplayFields.Find(x => x.OrthoChartTab == null);

            //No need to do anything if nothing changed and there are no 'orphaned' display fields to delete.
            if (!changed && (orphanedTab != null && orphanedTab.ListDisplayFields.All(x => x.DisplayFieldNum == 0)))
            {
                DialogResult = DialogResult.OK;
                return;
            }
            //Get all fields associated to a tab in order to sync with the database later.
            List <DisplayField> listAllFields = GetAllFields(false);

            if (listAllFields.Count(x => x.InternalName == "Signature") > 1)
            {
                MessageBox.Show(Lan.g(this, "Only one display field can be a signature field.  Fields that have the signature field checkbox checked:") + " "
                                + string.Join(", ", listAllFields.FindAll(x => x.InternalName == "Signature").Select(x => x.Description)));
                return;
            }
            //Ensure all new displayfields have a primary key so that tab links can be created below.  Update existing displayfields.
            foreach (DisplayField df in listAllFields)
            {
                if (df.DisplayFieldNum == 0)               //New displayfield
                {
                    DisplayFields.Insert(df);
                }
                else                  //Existing displayfield.
                {
                    DisplayFields.Update(df);
                }
            }
            DataValid.SetInvalid(InvalidType.DisplayFields);
            //Remove tab links which no longer exist.  Update tab link item order for tab links which still belong to the same tab.
            List <OrthoChartTabLink> listOrthoChartTabLinks = OrthoChartTabLinks.GetDeepCopy();

            for (int i = listOrthoChartTabLinks.Count - 1; i >= 0; i--)
            {
                OrthoChartTabLink   orthoChartTabLink   = listOrthoChartTabLinks[i];
                OrthoChartTabFields orthoChartTabFields = _listTabDisplayFields.FirstOrDefault(
                    x => x.OrthoChartTab != null && x.OrthoChartTab.OrthoChartTabNum == orthoChartTabLink.OrthoChartTabNum);
                if (orthoChartTabFields == null)
                {
                    continue;                    //The tab was hidden and we are going to leave the tab links alone.
                }
                DisplayField df = orthoChartTabFields.ListDisplayFields.FirstOrDefault(x => x.DisplayFieldNum == orthoChartTabLink.DisplayFieldNum);
                if (df == null)               //The tab link no longer exists (was removed).
                {
                    listOrthoChartTabLinks.RemoveAt(i);
                }
                else                  //The tab link still exists.  Update the link with any changes.
                {
                    orthoChartTabLink.ItemOrder = orthoChartTabFields.ListDisplayFields.IndexOf(df);
                }
            }
            //Add new tab links which were just created.
            foreach (OrthoChartTabFields orthoChartTabFields in _listTabDisplayFields)
            {
                //Skip "orphaned" fields that just show in the available fields list.
                if (orthoChartTabFields.OrthoChartTab == null)
                {
                    continue;
                }
                foreach (DisplayField df in orthoChartTabFields.ListDisplayFields)
                {
                    OrthoChartTabLink orthoChartTabLink = listOrthoChartTabLinks.FirstOrDefault(
                        x => x.OrthoChartTabNum == orthoChartTabFields.OrthoChartTab.OrthoChartTabNum && x.DisplayFieldNum == df.DisplayFieldNum);
                    if (orthoChartTabLink != null)
                    {
                        continue;
                    }
                    orthoChartTabLink                  = new OrthoChartTabLink();
                    orthoChartTabLink.ItemOrder        = orthoChartTabFields.ListDisplayFields.IndexOf(df);
                    orthoChartTabLink.OrthoChartTabNum = orthoChartTabFields.OrthoChartTab.OrthoChartTabNum;
                    orthoChartTabLink.DisplayFieldNum  = df.DisplayFieldNum;
                    listOrthoChartTabLinks.Add(orthoChartTabLink);
                }
            }
            //Delete any display fields that have a valid PK and are in the "orphaned" list.
            //This is fine to do because the field will show back up in the available list of display fields if a patient is still using the field.
            //This is because we link the ortho chart display fields by their name instead of by their PK.
            if (orphanedTab != null)           //An orphaned list actually exists.
            //Look for any display fields that have a valid PK (this means the user removed this field from every tab and we need to delete it).
            {
                List <DisplayField> listFieldsToDelete = orphanedTab.ListDisplayFields.FindAll(x => x.DisplayFieldNum != 0);
                listFieldsToDelete.ForEach(x => DisplayFields.Delete(x.DisplayFieldNum));
            }
            OrthoChartTabLinks.Sync(listOrthoChartTabLinks, OrthoChartTabLinks.GetDeepCopy());
            DataValid.SetInvalid(InvalidType.OrthoChartTabs);
            DialogResult = DialogResult.OK;
        }