protected void btnUpdateCode_Click(object sender, EventArgs e)
        {
            // Get Input field
            string code_id = txt_id_update.Text.Trim();
            IList<ITextTypeWrapperMutableObject> code_names = AddTextName_update.TextObjectList;
            IList<ITextTypeWrapperMutableObject> code_descs = AddTextDescription_update.TextObjectList;
            string code_parent_id = txt_parentid_update.Text.Trim();
            string code_order_str = txt_order_update.Text.Trim();

            // Get Current Object Session
            ICodelistMutableObject cl = cl = GetCodeListFromSession();
            IEnumerable<ICodeMutableObject> _rc = (from x in cl.Items where x.Id == code_id select x).OfType<ICodeMutableObject>();
            if (_rc.Count() == 0) return;

            ICodeMutableObject code = _rc.First();

            ICodeMutableObject _bCode = new CodeMutableCore();

            int indexCode = cl.Items.IndexOf(code);
            int indexOrder=0;
            try
            {

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

                #region CODE NAMES
                if (code_names != null)
                {
                    foreach (var tmpName in code_names)
                    {
                        _bCode.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= '#codes';");
                    return;
                }
                #endregion

                #region CODE DESCRIPTIONS
                if (code_descs != null)
                {
                    foreach (var tmpDescription in code_descs)
                    {
                        _bCode.AddDescription(tmpDescription.Locale, tmpDescription.Value);
                    }
                }
                #endregion

                #region PARANT ID

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

                if (!code_parent_id.Equals(string.Empty) && ValidationUtils.CheckIdFormat(code_id))
                {
                    IEnumerable<ICodeMutableObject> parentCode = (from c in cl.Items where c.Id == code_parent_id select c).OfType<ICodeMutableObject>();
                    if (parentCode.Count() > 0)
                        _bCode.ParentCode = code_parent_id;
                    else
                    {
                        lblErrorOnUpdate.Text = Resources.Messages.err_parent_id_not_found;
                        Utils.AppendScript( "openPopUp('df-Dimension-update', 600 );" );
                        Utils.AppendScript("location.href= '#codes';");
                        return;
                    }
                }
                #endregion

                #region CODE ORDER
                int tmpOrder = 0;
                if (!code_order_str.Equals(string.Empty) && !int.TryParse( code_order_str, out tmpOrder ) )
                {
                    lblErrorOnUpdate.Text = Resources.Messages.err_order_format_invalid;
                    Utils.AppendScript( "openPopUp('df-Dimension-update', 600);" );
                    Utils.AppendScript("location.href= '#codes';");
                    return;
                }
                else
                {
                    if ( tmpOrder < 0 )
                    {
                        lblErrorOnUpdate.Text = Resources.Messages.err_order_less_than_zero;
                        Utils.AppendScript( "openPopUp('df-Dimension-update', 600);" );
                        Utils.AppendScript("location.href= '#codes';");
                        return;
                    }
                }
                #endregion
            #region ANNOTATIONS

                foreach ( IAnnotationMutableObject annotation in cl.Items.ElementAt(indexCode).Annotations )
                {
                    _bCode.AddAnnotation( annotation );
                }

            #endregion
                if (!int.TryParse(code_order_str, out indexOrder))
                    indexOrder = cl.Items.Count + 1;
                else { indexOrder--; }
                if (indexOrder < 0
                    || indexOrder >= cl.Items.Count) indexOrder = cl.Items.Count - 1;

                cl.Items.RemoveAt(indexCode);
                cl.Items.Insert(indexOrder, _bCode);

                // Ultimo controllo se ottengo Immutable istanze validazione completa
                var canRead = cl.ImmutableInstance;

            }
            catch (Exception ex)
            {
                cl.Items.RemoveAt(indexOrder);
                cl.Items.Insert(indexCode, code);
                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_code_update;
                    Utils.AppendScript( "openPopUp('df-Dimension-update', 600);" );
                }
                Utils.AppendScript("location.href='#codes';");
                return;
            }

            if (!SaveInMemory(cl))
                return;

            BindData();
            Utils.AppendScript("location.href='#codes';");
        }
        private ICodelistMutableObject InsertCodeInCodelist(ICodelistMutableObject cl)
        {
            if (cl == null) return null;

            ICodeMutableObject code = new CodeMutableCore();

            string code_id = txt_id_new.Text.Trim();

            IList<ITextTypeWrapperMutableObject> code_names = AddTextName_new.TextObjectList;
            IList<ITextTypeWrapperMutableObject> code_descs = AddTextDescription_new.TextObjectList;
            string code_parent_id = txt_parentid_new.Text.Trim();
            string code_order_str = txt_order_new.Text.Trim();

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

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

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

            #region CODE DESCRIPTIONS
            if (code_descs != null)
            {
                foreach (var tmpDescription in code_descs)
                {
                    code.AddDescription(tmpDescription.Locale, tmpDescription.Value);
                }
            }
            #endregion

            #region PARANT ID

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

            if (!code_parent_id.Equals(string.Empty) && ValidationUtils.CheckIdFormat(code_id))
            {
                IEnumerable<ICodeMutableObject> parentCode = (from c in cl.Items where c.Id == code_parent_id select c).OfType<ICodeMutableObject>();
                if (parentCode.Count() > 0)
                    code.ParentCode = code_parent_id;
                else
                {
                    lblErrorOnNewInsert.Text = Resources.Messages.err_parent_id_not_found;
                    Utils.AppendScript( "openPopUp('df-Dimension', 600);" );
                    Utils.AppendScript("location.href= '#codes';");
                    return null;
                }
            }
            #endregion

            #region CODE ORDER
            int tmpOrder = 0;
            if (!code_order_str.Equals(string.Empty) && !int.TryParse( code_order_str, out tmpOrder ) )
            {
                lblErrorOnNewInsert.Text = Resources.Messages.err_order_format_invalid;
                Utils.AppendScript( "openPopUp('df-Dimension', 600);" );
                Utils.AppendScript("location.href= '#codes';");
                return null;
            }
            else
            {
                if ( tmpOrder < 0 )
                {
                    lblErrorOnNewInsert.Text = Resources.Messages.err_order_less_than_zero;
                    Utils.AppendScript( "openPopUp('df-Dimension', 600);" );
                    Utils.AppendScript("location.href= '#codes';");
                    return null;
                }
            }
            #endregion

            int indexOrder;
            if (!int.TryParse(code_order_str, out indexOrder))
                indexOrder = cl.Items.Count + 1;
            else { indexOrder--; }
            if (indexOrder < 0
                || indexOrder > cl.Items.Count) indexOrder = cl.Items.Count;

            cl.Items.Insert(indexOrder, code);

            try
            {
                // Ultimo controllo se ottengo Immutable istanze validazione completa
                var canRead = cl.ImmutableInstance;
            }
            catch (Exception ex)
            {
                cl.Items.RemoveAt(indexOrder);
                return null;
            }
            return cl;
        }
        protected void btnImportFromCsv_Click(object sender, EventArgs e)
        {
            if ( !csvFile.HasFile )
            {
                Utils.AppendScript( "openPopUp( 'importCsv' );" );
                Utils.AppendScript( "location.href='#codes'" );
                Utils.AppendScript( string.Format( "alert( '{0}' );", Resources.Messages.err_no_file_uploaded ) );
                return;
            }

            ICodelistMutableObject cl = GetCodeListFromSession();

            if (cl == null) return;

            List<csvCode> codes = new List<csvCode>();
            bool errorInUploading = false;
            StreamReader reader = null;
            StreamWriter logWriter = null;
            string wrongRowsMessage = string.Empty;
            string wrongRowsMessageForUser = string.Empty;
            string wrongFileLines = string.Empty;

            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_codelists_files/") + filename);

                reader = new StreamReader(Server.MapPath("~/csv_codelists_files/") + filename);
                logWriter = new StreamWriter(Server.MapPath("~/csv_codelists_import_logs/") + logFilename, true);
                logWriter.WriteLine(string.Format("LOG RELATIVO A CARICAMENTO DELLA CODELIST [ ID => \"{0}\"  AGENCY_ID => \"{1}\"  VERSION => \"{2}\" ]  |  LINGUA SELEZIONATA: {3}\n", cl.Id.ToString(), cl.AgencyId.ToString(), cl.Version.ToString(), cmbLanguageForCsv.SelectedValue.ToString()));
                logWriter.WriteLine("-----------------------------------------------------------------------------------------------------------------------------\n");
                reader.ReadLine();
                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;
                    }
                    codes.Add(new csvCode(fields[0].ToString().Replace("\"", ""), fields[1].ToString().Replace("\"", ""), fields[2].ToString().Replace("\"", ""), fields[3].ToString().Replace("\"", "")));
                    currentRow++;
                }

            }
            catch (Exception ex)
            {
                Utils.AppendScript(string.Format("Upload status: The file could not be uploaded. The following error occured: {0}", ex.Message));
            }

            foreach (csvCode code in codes)
            {
                if ( !code.parentCode.Trim().Equals( string.Empty ) )
                {
                    int cont = (from myCode in cl.Items
                                where myCode.Id.Equals( code.parentCode )
                                select myCode).Count();
                    if ( cont == 0 )
                    {
                        errorInUploading = true;
                        wrongRowsMessageForUser += string.Format(Resources.Messages.err_csv_import_parent_code_error, code.parentCode, code.code, code.code );
                        continue;
                    }
                }
                ICodeMutableObject tmpCode = cl.GetCodeById(code.code);
                if (tmpCode == null)
                {
                    tmpCode = new CodeMutableCore();
                    tmpCode.Id = code.code;
                    tmpCode.ParentCode = code.parentCode;
                    tmpCode.AddName(cmbLanguageForCsv.SelectedValue.ToString(), code.name);
                    tmpCode.AddDescription(cmbLanguageForCsv.SelectedValue.ToString(), code.description);
                    cl.AddItem(tmpCode);
                }
                else
                {
                    tmpCode.Id = code.code;
                    tmpCode.ParentCode = code.parentCode;
                    tmpCode.AddName(cmbLanguageForCsv.SelectedValue.ToString(), code.name);
                    tmpCode.AddDescription(cmbLanguageForCsv.SelectedValue.ToString(), code.description);
                }
            }

            if ( errorInUploading )
            {
                lblImportCsvErrors.Text = wrongRowsMessageForUser;
                lblImportCsvWrongLines.Text = wrongFileLines;
                Utils.AppendScript("openP('importCsvErrors',500);");
            }

            logWriter.Close();
            reader.Close();

            if (!SaveInMemory(cl)) return;

            BindData();
            if ( !errorInUploading )
            {
                Utils.ShowDialog( Resources.Messages.succ_operation );
            }
            Utils.AppendScript("location.href='#codes'");
        }
        private void ManageCode(csvCode code)
        {
            switch (_structureType)
            {
                case SdmxStructureEnumType.CategoryScheme:
                    break;
                case SdmxStructureEnumType.CodeList:
                    ICodeMutableObject tmpClCode = ucCodelist.GetCodeById(code.code);

                    if (tmpClCode == null)
                    {
                        tmpClCode = new CodeMutableCore();
                        tmpClCode.Id = code.code;
                        tmpClCode.ParentCode = code.parentCode;
                        tmpClCode.AddName(cmbLanguageForCsv.SelectedValue.ToString(), code.name);
                        tmpClCode.AddDescription(cmbLanguageForCsv.SelectedValue.ToString(), code.description);
                        ucCodelist.AddItem(tmpClCode);
                    }
                    else
                    {
                        tmpClCode.Id = code.code;
                        tmpClCode.ParentCode = code.parentCode;
                        tmpClCode.AddName(cmbLanguageForCsv.SelectedValue.ToString(), code.name);
                        tmpClCode.AddDescription(cmbLanguageForCsv.SelectedValue.ToString(), code.description);
                    }

                    break;
                case SdmxStructureEnumType.ConceptScheme:
                    break;
            }
        }