protected void btnImportFromCsv_Click(object sender, EventArgs e)
        {
            if ( !csvFile.HasFile )
            {
                Utils.AppendScript( "openPopUp( 'importCsv' );" );
                Utils.AppendScript( "location.href='#organizationunits'" );
                Utils.AppendScript( string.Format( "alert( '{0}' );", Resources.Messages.err_no_file_uploaded ) );
                return;
            }

            IOrganisationUnitSchemeMutableObject ous = GetOrganizationUnitSchemeFromSession();

            if (ous == null) return;

            List<csvOrganizationUnit> organizationUnits = new List<csvOrganizationUnit>();
            bool errorInUploading = false;
            try
            {
                string filenameWithoutExtension = string.Format("{0}_{1}_{2}", Path.GetFileName(csvFile.FileName).Substring(0, csvFile.FileName.Length - 4), Session.SessionID, DateTime.Now.ToString().Replace('/', '_').Replace(':', '_').Replace(' ', '_'));
                string filename = string.Format("{0}.csv", filenameWithoutExtension);
                string logFilename = string.Format("{0}.log", filenameWithoutExtension);
                csvFile.SaveAs(Server.MapPath("~/csv_organizationunitschemes_files/") + filename);

                StreamReader reader = new StreamReader(Server.MapPath("~/csv_organizationunitschemes_files/") + filename);
                StreamWriter logWriter = new StreamWriter(Server.MapPath("~/csv_organizationunitschemes_import_logs/") + logFilename, true);
                logWriter.WriteLine(string.Format("LOG RELATIVO A CARICAMENTO DELL\'ORGANIZATION UNIT SCHEME [ ID => \"{0}\"  AGENCY_ID => \"{1}\"  VERSION => \"{2}\" ]  |  LINGUA SELEZIONATA: {3}\n", ous.Id.ToString(), ous.AgencyId.ToString(), ous.Version.ToString(), cmbLanguageForCsv.SelectedValue.ToString()));
                logWriter.WriteLine("-----------------------------------------------------------------------------------------------------------------------------\n");
                reader.ReadLine();
                string wrongRowsMessage = string.Empty;
                string wrongRowsMessageForUser = string.Empty;
                string wrongFileLines = string.Empty;
                int currentRow = 1;

                char separator = txtSeparator.Text.Trim().Equals( string.Empty ) ? ';' : txtSeparator.Text.Trim().ElementAt( 0 );

                while (!reader.EndOfStream)
                {
                    string  currentFileLine = reader.ReadLine();
                    string[] fields = currentFileLine.Split( separator );
                    if (fields.Length != 4)
                    {
                        errorInUploading = true;
                        wrongRowsMessage += string.Format( Resources.Messages.err_csv_import_line_bad_format, currentRow + 1);
                        wrongRowsMessageForUser += string.Format(Resources.Messages.err_csv_import_line_bad_format_gui, currentRow + 1);
                        wrongFileLines += string.Format( "{0}\n", currentFileLine );
                        logWriter.WriteLine(string.Format(Resources.Messages.err_csv_import_line_bad_format, currentRow + 1));
                        logWriter.Flush();
                        currentRow++;
                        continue;
                    }
                    if (fields[0].Trim().Equals("\"\"") || fields[0].Trim().Equals(string.Empty))
                    {
                        errorInUploading = true;
                        wrongRowsMessage += string.Format(Resources.Messages.err_csv_import_id_missing, currentRow + 1);
                        wrongRowsMessageForUser += string.Format(Resources.Messages.err_csv_import_id_missing_gui, currentRow + 1);
                        wrongFileLines += string.Format( "{0}\n", currentFileLine );
                        logWriter.WriteLine(string.Format(Resources.Messages.err_csv_import_id_missing, currentRow + 1));
                        logWriter.Flush();
                        currentRow++;
                        continue;
                    }
                    if (fields[1].Trim().Equals("\"\"") || fields[1].Trim().Equals(string.Empty))
                    {
                        errorInUploading = true;
                        wrongRowsMessage += string.Format(Resources.Messages.err_csv_import_name_missing, currentRow + 1);
                        wrongRowsMessageForUser += string.Format(Resources.Messages.err_csv_import_name_missing_gui, currentRow + 1);
                        wrongFileLines += string.Format( "{0}\n", currentFileLine );
                        logWriter.WriteLine(string.Format(Resources.Messages.err_csv_import_name_missing, currentRow + 1));
                        logWriter.Flush();
                        currentRow++;
                        continue;
                    }
                    organizationUnits.Add(new csvOrganizationUnit(fields[0].ToString().Replace( "\"", "" ), fields[1].ToString().Replace( "\"", "" ), fields[2].ToString().Replace( "\"", "" ), fields[3].ToString().Replace( "\"", "" )));
                    currentRow++;
                }
                if ( !errorInUploading )
                {
                    logWriter.WriteLine("Andato tutto a buon fine con questo file!");
                }
                else
                {
                    lblImportCsvErrors.Text = wrongRowsMessageForUser;
                    lblImportCsvWrongLines.Text = wrongFileLines;
                    Utils.AppendScript("openP('importCsvErrors',500);");
                }
                logWriter.Close();
                reader.Close();
            }
            catch (Exception ex)
            {
                Utils.AppendScript(string.Format("Upload status: The file could not be uploaded. The following error occured: {0}", ex.Message));
            }

            foreach (csvOrganizationUnit organizationUnit in organizationUnits)
            {

                IEnumerable<IOrganisationUnitMutableObject> tmpOrganisationUnits = (from conc in ous.Items where conc.Id == organizationUnit.organizationUnit select conc).OfType<IOrganisationUnitMutableObject>();

                IOrganisationUnitMutableObject tmpOrganizationUnit;

                if (!(tmpOrganisationUnits.Count() > 0))
                {
                    tmpOrganizationUnit = new OrganisationUnitMutableCore();
                    tmpOrganizationUnit.Id = organizationUnit.organizationUnit;
                    tmpOrganizationUnit.ParentUnit = organizationUnit.parentOrganizationUnit;
                    tmpOrganizationUnit.AddName(cmbLanguageForCsv.SelectedValue.ToString(), organizationUnit.name);
                    tmpOrganizationUnit.AddDescription(cmbLanguageForCsv.SelectedValue.ToString(), organizationUnit.description);

                    ous.AddItem(tmpOrganizationUnit);
                }
                else
                {
                    tmpOrganizationUnit = tmpOrganisationUnits.First();
                    tmpOrganizationUnit.Id = organizationUnit.organizationUnit;
                    tmpOrganizationUnit.ParentUnit = organizationUnit.parentOrganizationUnit;
                    tmpOrganizationUnit.AddName(cmbLanguageForCsv.SelectedValue.ToString(), organizationUnit.name);
                    tmpOrganizationUnit.AddDescription(cmbLanguageForCsv.SelectedValue.ToString(), organizationUnit.description);
                }
            }

            if (!SaveInMemory(ous))
                return;

            BindData();
            if ( !errorInUploading )
            {
                Utils.ShowDialog( Resources.Messages.succ_operation );
            }
            Utils.AppendScript("location.href='#organizationunits';");
        }
        protected void btnUpdateOrganizationUnit_Click(object sender, EventArgs e)
        {
            // Get Input field
            string organization_unit_id = txt_id_update.Text.Trim();
            IList<ITextTypeWrapperMutableObject> organization_unit_names = AddTextName_Update.TextObjectList;
            IList<ITextTypeWrapperMutableObject> organization_unit_descs = AddTextDescription_Update.TextObjectList;
            string organization_unit_parent_id = txt_parentid_update.Text.Trim();
            // string code_order_str = txtUpdateCodeOrder.Text.Trim();

            // Get Current Object Session
            IOrganisationUnitSchemeMutableObject ous = GetOrganizationUnitSchemeFromSession();
            IEnumerable<IOrganisationUnitMutableObject> _rc = (from x in ous.Items where x.Id == organization_unit_id select x).OfType<IOrganisationUnitMutableObject>();
            if (_rc.Count() == 0) return;

            IOrganisationUnitMutableObject organizationUnit = _rc.First();

            IOrganisationUnitMutableObject _bOrganizationUnit = new OrganisationUnitMutableCore();

            int indexOrganisationUnit = ous.Items.IndexOf(organizationUnit);
            int indexOrder = 0;
            try
            {

                #region ORGANIZATION UNIT ID
                if (!organization_unit_id.Equals(string.Empty) && ValidationUtils.CheckIdFormat(organization_unit_id))
                {
                    _bOrganizationUnit.Id = organization_unit_id;
                }
                else
                {
                    lblErrorOnUpdate.Text = Resources.Messages.err_id_format;
                    Utils.AppendScript( "openPopUp('df-Dimension-update', 600 );" );
                    Utils.AppendScript("location.href= '#organizationunits';");
                    return;
                }
                #endregion

                #region CONCEPT NAMES
                if (organization_unit_names != null)
                {
                    foreach (var tmpName in organization_unit_names)
                    {
                        _bOrganizationUnit.AddName(tmpName.Locale, tmpName.Value);
                    }
                }
                else
                {
                    lblErrorOnUpdate.Text = Resources.Messages.err_list_name_format;
                    Utils.AppendScript( "openPopUp('df-Dimension-update', 600 );" );
                    Utils.AppendScript("location.href= '#organizationunits';");
                    return;
                }
                #endregion

                #region ORGANIZATION UNIT DESCRIPTIONS
                if (organization_unit_descs != null)
                {
                    foreach (var tmpDescription in organization_unit_descs)
                    {
                        _bOrganizationUnit.AddDescription(tmpDescription.Locale, tmpDescription.Value);
                    }
                }
                #endregion

                #region PARANT ID

                if ( organization_unit_id.Equals( organization_unit_parent_id ) )
                {
                    lblErrorOnUpdate.Text = Resources.Messages.err_parent_id_same_value;
                    Utils.AppendScript( "openPopUp('df-Dimension-update', 600 );" );
                    Utils.AppendScript("location.href= '#organizationunits';");
                    return;
                }

                if (!organization_unit_parent_id.Equals(string.Empty) && ValidationUtils.CheckIdFormat(organization_unit_id))
                {
                    //IEnumerable<ICodeMutableObject> parentCode = (from c in cl.Items where c.Id == code_parent_id select c).OfType<ICodeMutableObject>();

                    IEnumerable<IOrganisationUnitMutableObject> parentConcept = (from c in ous.Items where c.Id == organization_unit_parent_id select c).OfType<IOrganisationUnitMutableObject>();
                    if (parentConcept.Count() > 0)
                        _bOrganizationUnit.ParentUnit = organization_unit_parent_id;
                    else
                    {

                        lblErrorOnUpdate.Text = Resources.Messages.err_parent_id_not_found;
                        Utils.AppendScript( "openPopUp('df-Dimension-update', 600 );" );
                        Utils.AppendScript("location.href= '#organizationunits';");
                        return;
                    }
                }
                #endregion

                ous.Items.Remove(organizationUnit);
                ous.Items.Insert(indexOrganisationUnit, _bOrganizationUnit);

                var canRead = ous.ImmutableInstance;

            }
            catch (Exception ex) // ERRORE GENERICO!
            {
                ous.Items.Remove(_bOrganizationUnit);
                ous.Items.Insert(indexOrganisationUnit, organizationUnit);

                if ( ex.Message.Contains( "- 706 -" ) )
                {
                    lblErrorOnUpdate.Text = Resources.Messages.err_parent_item_is_child;
                    Utils.AppendScript( "openPopUp('df-Dimension-update', 600);" );
                 }
                else
                {
                    lblErrorOnUpdate.Text = Resources.Messages.err_organization_unit_update;
                    Utils.AppendScript( "openPopUp('df-Dimension-update', 600);" );
                }
                Utils.AppendScript("location.href='#organizationunits';");
                return;
            }

            if (!SaveInMemory(ous))
                return;

            BindData();
            lblErrorOnUpdate.Text = string.Empty;
            Utils.AppendScript("location.href='#organizationunits';");
        }
        private IOrganisationUnitSchemeMutableObject InsertOrganizationUnitInOrganizationUnitscheme(IOrganisationUnitSchemeMutableObject ous)
        {
            if (ous == null) return null;

            IOrganisationUnitMutableObject organizationUnit = new OrganisationUnitMutableCore();
            string organization_unit_id = txt_id_new.Text.Trim();

            IList<ITextTypeWrapperMutableObject> organization_unit_names = AddTextName_new.TextObjectList;
            IList<ITextTypeWrapperMutableObject> organization_unit_descs = AddTextDescription_new.TextObjectList;
            string organization_unit_parent_id = txt_parentid_new.Text.Trim();
            // string code_order_str = txtOrderNewCode.Text.Trim();     ----- ORDINE

            #region CONCEPT ID
            if (ValidationUtils.CheckIdFormat(organization_unit_id))
            {
                organizationUnit.Id = organization_unit_id;
            }
            else
            {
                lblErrorOnNewInsert.Text = Resources.Messages.err_id_format;
                Utils.AppendScript( "openPopUp('df-Dimension', 600);" );
                Utils.AppendScript("location.href= '#organizationunits';");
                return null;
            }

            IEnumerable<IOrganisationUnitMutableObject> organizationunits = (from ou in ous.Items where ou.Id == organization_unit_id select ou).OfType<IOrganisationUnitMutableObject>();
            if ( organizationunits.Count() > 0 )
            {
                lblErrorOnNewInsert.Text = Resources.Messages.err_id_exist;
                Utils.AppendScript( "openPopUp('df-Dimension', 600);" );
                Utils.AppendScript("location.href= '#organizationunits';");
                return null;
            }
            #endregion

            #region ORGANIZATION UNIT NAMES
            if (organization_unit_names != null)
            {
                foreach (var tmpName in organization_unit_names)
                {
                    organizationUnit.AddName(tmpName.Locale, tmpName.Value);
                }
            }
            else
            {
                lblErrorOnNewInsert.Text = Resources.Messages.err_list_name_format;
                Utils.AppendScript( "openPopUp('df-Dimension', 600);" );
                Utils.AppendScript("location.href= '#organizationunits';");
                return null;
            }
            #endregion

            #region ORGANIZATION UNIT DESCRIPTIONS
            if (organization_unit_descs != null)
            {
                foreach (var tmpDescription in organization_unit_descs)
                {
                    organizationUnit.AddDescription(tmpDescription.Locale, tmpDescription.Value);
                }
            }
            #endregion

            #region PARANT ID

            if ( organization_unit_id.Equals( organization_unit_parent_id ) )
            {
                lblErrorOnNewInsert.Text = Resources.Messages.err_parent_id_same_value;
                Utils.AppendScript( "openPopUp('df-Dimension-update', 600 );" );
                Utils.AppendScript("location.href= '#organizationunits';");
                return null;
            }

            if (!organization_unit_parent_id.Equals(string.Empty) && ValidationUtils.CheckIdFormat(organization_unit_id))
            {
                IEnumerable<IOrganisationUnitMutableObject> parentOrganizationUnit = (from ou in ous.Items where ou.Id == organization_unit_parent_id select ou).OfType<IOrganisationUnitMutableObject>();
                if (parentOrganizationUnit.Count() > 0)
                    organizationUnit.ParentUnit = organization_unit_parent_id;
                else
                {
                    lblErrorOnNewInsert.Text = Resources.Messages.err_parent_id_not_found;
                    Utils.AppendScript( "openPopUp('df-Dimension', 600);" );
                    Utils.AppendScript("location.href= '#concepts';");
                    return null;
                }
            }
            #endregion

            ous.Items.Add( organizationUnit );

            try
            {
                // Ultimo controllo se ottengo Immutable istanze validazione completa
                var canRead = ous.ImmutableInstance;
            }
            catch (Exception ex)
            {
                ous.Items.Remove(organizationUnit);

                return null;

            }

            return ous;
        }