protected void btnAddAttribute_Click(object sender, EventArgs e)
        {
            if (!ValidateAttributeData())
            {
                OpenAddAttributePopUp();
                return;
            }

            /*
             * Creo l'attribute
             * ID
             * Concept
             * Eventuale Codelist
             * Assignment status
             * Attachmentlevel
             *      DimensionGroup
             *          Attachment Dimension
             *      Group
             *          Attached Group ID
             * */

            IAttributeMutableObject attr = new AttributeMutableCore();
            attr.Id = txtAttributeID.Text;

            #region "***** Concept Reference ******"

            ArtefactIdentity csIdentity = new ArtefactIdentity();
            string[] conceptData = txtAttributeConcept.Text.Split(',');

            if (conceptData.Length > 0)
            {
                csIdentity.ID = conceptData[0];
                csIdentity.Agency = conceptData[1];
                csIdentity.Version = conceptData[2].Substring(0, conceptData[2].IndexOf(' '));

                string conceptID = conceptData[2].Substring(conceptData[2].LastIndexOf(' ') + 1);

                IStructureReference conceptRef = new StructureReferenceImpl(csIdentity.Agency,
                                                                            csIdentity.ID,
                                                                            csIdentity.Version,
                                                                            SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Concept),
                                                                            new string[] { conceptID });

                attr.ConceptRef = conceptRef;
            }

            #endregion

            #region "***** Codelist Reference ******"

            if (txtAttributeCodelist.Text != string.Empty)
            {
                ArtefactIdentity clIdentity = new ArtefactIdentity();
                string[] clData = txtAttributeCodelist.Text.Split(',');

                clIdentity.ID = clData[0];
                clIdentity.Agency = clData[1];
                clIdentity.Version = clData[2];

                IStructureReference codelistRef = new StructureReferenceImpl(clIdentity.Agency,
                                                                                clIdentity.ID,
                                                                                clIdentity.Version,
                                                                                SdmxStructureType.GetFromEnum(SdmxStructureEnumType.CodeList),
                                                                                null);

                IRepresentationMutableObject representationRef = new RepresentationMutableCore();
                representationRef.Representation = codelistRef;

                attr.Representation = representationRef;
            }

            #endregion

            attr.AssignmentStatus = cmbAssignmentStatus.SelectedValue;
            attr.AttachmentLevel = (AttributeAttachmentLevel)Enum.Parse(typeof(AttributeAttachmentLevel), cmbAttachLevel.SelectedValue);

            switch (attr.AttachmentLevel)
            {
                case AttributeAttachmentLevel.DataSet:
                    break;
                case AttributeAttachmentLevel.DimensionGroup:
                    // Aggiungo le dimensioni selezionate nella list box
                    IList<string> lDim = new List<string>();

                    foreach (ListItem li in lbAttachmentDimension.Items)
                    {
                        if (li.Selected)
                            attr.DimensionReferences.Add(li.Value);
                    }

                    break;
                case AttributeAttachmentLevel.Group:
                    // Aggiungo il gruppo selezionato nella combo
                    attr.AttachmentGroup = cmbAttachedGroupID.SelectedValue;
                    break;
                case AttributeAttachmentLevel.Null:
                    break;
                case AttributeAttachmentLevel.Observation:
                    break;
                default:
                    break;
            }

            try
            {
                if (hdnEditAttribute.Value == "true")
                {
                    foreach (IAttributeMutableObject attrDel in _dsdMutable.Attributes)
                    {
                        if (attrDel.Id == txtAttributeID.Text)
                        {
                            _dsdMutable.Attributes.Remove(attrDel);
                            break;
                        }
                    }
                }

                _dsdMutable.AddAttribute(attr);
                SetDsdToSession();
                SetAttributeTab(_dsdMutable.ImmutableInstance);

                txtAttributeID.Text = "";
                txtAttributeConcept.Text = "";
                txtAttributeCodelist.Text = "";
                cmbAssignmentStatus.SelectedIndex = 0;
                cmbAttachLevel.SelectedIndex = 0;

                lbAttachmentDimension.ClearSelection();
                if (cmbAttachedGroupID.Items.Count >0)
                    cmbAttachedGroupID.SelectedIndex = 0;
                pnlAttachedGroupID.Visible = false;
                pnlAttachmentDimension.Visible = false;

                Utils.ForceBlackClosing();
            }
            catch (Exception ex)
            {
                _dsdMutable.Attributes.Remove(attr);
                OpenAddAttributePopUp();
                Utils.ShowDialog(ex.Message, 600, Resources.Messages.err_add_attribute);
            }
        }
        public void TestDataWriterEngineAllDimensions(DataEnumType format)
        {
            string outfile = string.Format("{0}-alldim.xml", format);
            DataType fromEnum = DataType.GetFromEnum(format);
            using (Stream writer = File.Create(outfile))
            {
                IConceptSchemeMutableObject conceptScheme = new ConceptSchemeMutableCore { Id = "CONCEPTS_TEST", AgencyId = "TEST" };
                conceptScheme.AddName("en", "Dummy concept scheme build for this tests");

                ////conceptScheme.Annotations.Add(new AnnotationMutableCore() { Id = "ANNOTABLETEST", Title = "Test", Type = "ATYPE" });
                conceptScheme.FinalStructure = TertiaryBool.GetFromEnum(TertiaryBoolEnumType.True);

                IConceptSchemeObject parent = conceptScheme.ImmutableInstance;

                IConceptObject freqConcept = CreateConcept(parent, "FREQ", "Frequency");

                IConceptObject adjustmentConcept = CreateConcept(parent, "ADJUSTMENT", "The Adjustment");

                IConceptObject activityConcpet = CreateConcept(parent, "STS_ACTIVITY", "Name of activity ");

                IConceptObject timeDimensionConcpet = CreateConcept(parent, "TIME_PERIOD", "Name of  Time Period");
                IConceptObject decimalsConcept = CreateConcept(parent, "DECIMALS", "Name of concept");

                IConceptObject obsConcept = CreateConcept(parent, "OBS_VALUE", "Name of  observation value");

                ICodelistObject freqCl = CreateCodelist("CL_FREQ", "Freq codelist", "Q", "A", "M");
                ICodelistObject adjCl = CreateCodelist("CL_ADJUSTMENT", "Adjustment codelist", "N", "S", "W");
                ICodelistObject actCl = CreateCodelist("CL_ACTIVITY", "Activity codelist", "A", "B", "C");
                ICodelistObject deciCl = CreateCodelist("CL_DECIMALS", "DECIMALS codelist", "1", "2", "0");

                IDataStructureMutableObject mutable = new DataStructureMutableCore { Id = "TEST_DSD", AgencyId = "TEST" };
                mutable.AddName("en", "FOO BAR");
                mutable.AddDimension(
                    new DimensionMutableCore { ConceptRef = freqConcept.AsReference, FrequencyDimension = true, Representation = new RepresentationMutableCore { Representation = freqCl.AsReference } });
                mutable.AddDimension(new DimensionMutableCore { ConceptRef = adjustmentConcept.AsReference, Representation = new RepresentationMutableCore { Representation = adjCl.AsReference } });
                mutable.AddDimension(new DimensionMutableCore { ConceptRef = activityConcpet.AsReference, Representation = new RepresentationMutableCore { Representation = actCl.AsReference } });
                mutable.AddDimension(new DimensionMutableCore { ConceptRef = timeDimensionConcpet.AsReference, TimeDimension = true });

                IList<string> dimList = new List<string> { freqConcept.Id, adjustmentConcept.Id, adjustmentConcept.Id };
                var attributeMutableCore = new AttributeMutableCore
                                               {
                                                   ConceptRef = decimalsConcept.AsReference, 
                                                   Representation = new RepresentationMutableCore { Representation = deciCl.AsReference }, 
                                                   AttachmentLevel = AttributeAttachmentLevel.DimensionGroup, 
                                                   AssignmentStatus = AttributeAssignmentStatus.Mandatory.ToString()
                                               };
                attributeMutableCore.DimensionReferences.AddAll(dimList);
                mutable.AddAttribute(attributeMutableCore);

                mutable.PrimaryMeasure = new PrimaryMeasureMutableCore { ConceptRef = obsConcept.AsReference };

                var manager = new DataWriterManager();
                IDataWriterEngine dataWriter = manager.GetDataWriterEngine(new SdmxDataFormatCore(fromEnum), writer);

                IDataStructureObject dataStructureObject = mutable.ImmutableInstance;
                IList<IDatasetStructureReference> structures = new List<IDatasetStructureReference>
                                                                   {
                                                                       new DatasetStructureReferenceCore(
                                                                           null, 
                                                                           dataStructureObject.AsReference, 
                                                                           null, 
                                                                           null, 
                                                                           DatasetStructureReference.AllDimensions)
                                                                   };
                IList<IParty> receiver = new List<IParty> { new PartyCore(new List<ITextTypeWrapper>(), "ZZ9", new List<IContact>(), null) };
                var sender = new PartyCore(new List<ITextTypeWrapper> { new TextTypeWrapperImpl("en", "TEST SENDER", null) }, "ZZ1", null, null);
                IHeader header = new HeaderImpl(
                    null, 
                    structures, 
                    null, 
                    DatasetAction.GetFromEnum(DatasetActionEnumType.Information), 
                    "TEST_DATAFLOW", 
                    "DATASET_ID", 
                    null, 
                    DateTime.Now, 
                    DateTime.Now, 
                    null, 
                    null, 
                    new List<ITextTypeWrapper> { new TextTypeWrapperImpl("en", "test header name", null) }, 
                    new List<ITextTypeWrapper> { new TextTypeWrapperImpl("en", "source 1", null) }, 
                    receiver, 
                    sender, 
                    true);
                dataWriter.WriteHeader(header);

                dataWriter.StartDataset(null, dataStructureObject, new DatasetHeaderCore(null, DatasetAction.GetFromEnum(DatasetActionEnumType.Information), structures.First()));
                var sw = new Stopwatch();
                sw.Start();
                var series = from f in freqCl.Items from ad in adjCl.Items from ac in actCl.Items select new { f, ad, ac };

                sw.Stop();
                Trace.WriteLine(sw.Elapsed);
                sw.Reset();

                var startTime = new DateTime(2005, 1, 1);
                sw.Start();
                foreach (var key in series)
                {
                    dataWriter.StartSeries();
                    dataWriter.WriteSeriesKeyValue(freqConcept.Id, key.f.Id);
                    dataWriter.WriteSeriesKeyValue(adjustmentConcept.Id, key.ad.Id);
                    dataWriter.WriteSeriesKeyValue(activityConcpet.Id, key.ac.Id);

                    Func<int, string> getPeriod = null;
                    switch (key.f.Id)
                    {
                        case "Q":
                            getPeriod = i =>
                                {
                                    DateTime months = startTime.AddMonths(3 * i);
                                    return DateUtil.FormatDate(months, TimeFormatEnumType.QuarterOfYear);
                                };
                            dataWriter.WriteAttributeValue(decimalsConcept.Id, "1");
                            break;
                        case "A":
                            getPeriod = i =>
                                {
                                    DateTime months = startTime.AddMonths(12 * i);
                                    return DateUtil.FormatDate(months, TimeFormatEnumType.Year);
                                };
                            dataWriter.WriteAttributeValue(decimalsConcept.Id, "0");
                            break;
                        case "M":
                            getPeriod = i =>
                                {
                                    DateTime months = startTime.AddMonths(i + 1);
                                    return DateUtil.FormatDate(months, TimeFormatEnumType.Month);
                                };
                            dataWriter.WriteAttributeValue(decimalsConcept.Id, "2");
                            break;
                        default:
                            Assert.Fail("Test bug. Check CL_FREQ codes");
                            break;
                    }

                    for (int i = 0; i < 100; i++)
                    {
                        string period = getPeriod(i);
                        dataWriter.WriteObservation(DimensionObject.TimeDimensionFixedId, period, i.ToString(CultureInfo.InvariantCulture));
                    }
                }

                dataWriter.Close();
                sw.Stop();
                Trace.WriteLine(sw.Elapsed);
            }

            if (fromEnum.BaseDataFormat.EnumType == BaseDataFormatEnumType.Generic)
            {
                var fileReadableDataLocation = new FileReadableDataLocation(outfile);

                XMLParser.ValidateXml(fileReadableDataLocation, fromEnum.SchemaVersion);
            }
        }
        public void TestDataWriterEngineDimensionAtObs(DataEnumType format, string dimAtObs)
        {
            string outfile = string.Format("{0}-{1}.xml", format, dimAtObs);
            DataType fromEnum = DataType.GetFromEnum(format);
            using (Stream writer = File.Create(outfile))
            {
                IConceptSchemeMutableObject conceptScheme = new ConceptSchemeMutableCore { Id = "CONCEPTS_TEST", AgencyId = "TEST" };
                conceptScheme.AddName("en", "Dummy concept scheme build for this tests");

                ////conceptScheme.Annotations.Add(new AnnotationMutableCore() { Id = "ANNOTABLETEST", Title = "Test", Type = "ATYPE" });
                conceptScheme.FinalStructure = TertiaryBool.GetFromEnum(TertiaryBoolEnumType.True);

                IConceptSchemeObject parent = conceptScheme.ImmutableInstance;

                IConceptObject freqConcept = CreateConcept(parent, "FREQ", "Frequency");

                IConceptObject adjustmentConcept = CreateConcept(parent, "ADJUSTMENT", "The Adjustment");

                IConceptObject activityConcpet = CreateConcept(parent, "STS_ACTIVITY", "Name of activity ");

                IConceptObject timeDimensionConcpet = CreateConcept(parent, "TIME_PERIOD", "Name of  Time Period");
                IConceptObject decimalsConcept = CreateConcept(parent, "DECIMALS", "Name of concept");

                IConceptObject obsConcept = CreateConcept(parent, "OBS_VALUE", "Name of  observation value");

                ICodelistObject freqCl = CreateCodelist("CL_FREQ", "Freq codelist", "Q", "A", "M");
                ICodelistObject adjCl = CreateCodelist("CL_ADJUSTMENT", "Adjustment codelist", "N", "S", "W");
                ICodelistObject actCl = CreateCodelist("CL_ACTIVITY", "Activity codelist", "A", "B", "C");
                ICodelistObject deciCl = CreateCodelist("CL_DECIMALS", "DECIMALS codelist", "1", "2", "0");

                IDataStructureMutableObject mutable = new DataStructureMutableCore { Id = "TEST_DSD", AgencyId = "TEST" };
                mutable.AddName("en", "FOO BAR");
                mutable.AddDimension(
                    new DimensionMutableCore { ConceptRef = freqConcept.AsReference, FrequencyDimension = true, Representation = new RepresentationMutableCore { Representation = freqCl.AsReference } });
                mutable.AddDimension(new DimensionMutableCore { ConceptRef = adjustmentConcept.AsReference, Representation = new RepresentationMutableCore { Representation = adjCl.AsReference } });
                mutable.AddDimension(new DimensionMutableCore { ConceptRef = activityConcpet.AsReference, Representation = new RepresentationMutableCore { Representation = actCl.AsReference } });
                mutable.AddDimension(new DimensionMutableCore { ConceptRef = timeDimensionConcpet.AsReference, TimeDimension = true });

                IList<string> dimList = new List<string> { freqConcept.Id, adjustmentConcept.Id, adjustmentConcept.Id };
                var attributeMutableCore = new AttributeMutableCore
                                               {
                                                   ConceptRef = decimalsConcept.AsReference, 
                                                   Representation = new RepresentationMutableCore { Representation = deciCl.AsReference }, 
                                                   AttachmentLevel = AttributeAttachmentLevel.DimensionGroup, 
                                                   AssignmentStatus = AttributeAssignmentStatus.Mandatory.ToString()
                                               };
                attributeMutableCore.DimensionReferences.AddAll(dimList);
                mutable.AddAttribute(attributeMutableCore);

                mutable.PrimaryMeasure = new PrimaryMeasureMutableCore { ConceptRef = obsConcept.AsReference };

                var manager = new DataWriterManager();
                IDataWriterEngine dataWriter = manager.GetDataWriterEngine(new SdmxDataFormatCore(fromEnum), writer);

                IDataStructureObject dataStructureObject = mutable.ImmutableInstance;
                IList<IDatasetStructureReference> structures = new List<IDatasetStructureReference> { new DatasetStructureReferenceCore(null, dataStructureObject.AsReference, null, null, dimAtObs) };
                IList<IParty> receiver = new List<IParty> { new PartyCore(new List<ITextTypeWrapper>(), "ZZ9", new List<IContact>(), null) };
                var sender = new PartyCore(new List<ITextTypeWrapper> { new TextTypeWrapperImpl("en", "TEST SENDER", null) }, "ZZ1", null, null);
                IHeader header = new HeaderImpl(
                    null, 
                    structures, 
                    null, 
                    DatasetAction.GetFromEnum(DatasetActionEnumType.Information), 
                    "TEST_DATAFLOW", 
                    "DATASET_ID", 
                    null, 
                    DateTime.Now, 
                    DateTime.Now, 
                    null, 
                    null, 
                    new List<ITextTypeWrapper> { new TextTypeWrapperImpl("en", "test header name", null) }, 
                    new List<ITextTypeWrapper> { new TextTypeWrapperImpl("en", "source 1", null) }, 
                    receiver, 
                    sender, 
                    true);
                dataWriter.WriteHeader(header);

                dataWriter.StartDataset(null, dataStructureObject, new DatasetHeaderCore(null, DatasetAction.GetFromEnum(DatasetActionEnumType.Information), structures.First()));
                var sw = new Stopwatch();
                sw.Start();

                var startTime = new DateTime(2005, 1, 1);

                var series = (from f in freqCl.Items
                              from ad in adjCl.Items
                              from ac in actCl.Items
                              from t in Enumerable.Range(0, 100)
                              select new { Freq = f.Id, Adj = ad.Id, Activity = ac.Id, Time = BuildPeriodResolver(f.Id, startTime)(t) }).OrderBy(
                                  arg => BuildOrderBy(arg.Freq, arg.Adj, arg.Activity, arg.Time, dimAtObs)).ToArray();

                sw.Stop();
                Trace.WriteLine(sw.Elapsed);
                sw.Reset();

                sw.Start();
                string lastKey = null;
                int i = 0;
                foreach (var key in series)
                {
                    var currentKey = new List<string>();
                    string crossValue = null;
                    if (!string.Equals(freqConcept.Id, dimAtObs))
                    {
                        currentKey.Add(key.Freq);
                    }
                    else
                    {
                        crossValue = key.Freq;
                    }

                    if (!string.Equals(adjustmentConcept.Id, dimAtObs))
                    {
                        currentKey.Add(key.Adj);
                    }
                    else
                    {
                        crossValue = key.Adj;
                    }

                    if (!string.Equals(activityConcpet.Id, dimAtObs))
                    {
                        currentKey.Add(key.Activity);
                    }
                    else
                    {
                        crossValue = key.Activity;
                    }

                    if (!string.Equals(timeDimensionConcpet.Id, dimAtObs))
                    {
                        currentKey.Add(key.Time);
                    }
                    else
                    {
                        crossValue = key.Time;
                    }

                    var currentKeyValues = string.Join(",", currentKey);

                    if (lastKey == null || !lastKey.Equals(currentKeyValues))
                    {
                        lastKey = currentKeyValues;
                        i = 0;

                        dataWriter.StartSeries();
                        if (!string.Equals(freqConcept.Id, dimAtObs))
                        {
                            dataWriter.WriteSeriesKeyValue(freqConcept.Id, key.Freq);
                        }

                        if (!string.Equals(adjustmentConcept.Id, dimAtObs))
                        {
                            dataWriter.WriteSeriesKeyValue(adjustmentConcept.Id, key.Adj);
                        }

                        if (!string.Equals(activityConcpet.Id, dimAtObs))
                        {
                            dataWriter.WriteSeriesKeyValue(activityConcpet.Id, key.Activity);
                        }

                        if (!string.Equals(timeDimensionConcpet.Id, dimAtObs))
                        {
                            dataWriter.WriteSeriesKeyValue(timeDimensionConcpet.Id, key.Time);
                        }
                    }

                    dataWriter.WriteObservation(crossValue, i.ToString(CultureInfo.InvariantCulture));
                    i++;
                }

                dataWriter.Close();
                sw.Stop();
                Trace.WriteLine(sw.Elapsed);
            }

            if (fromEnum.BaseDataFormat.EnumType == BaseDataFormatEnumType.Generic)
            {
                var fileReadableDataLocation = new FileReadableDataLocation(outfile);

                XMLParser.ValidateXml(fileReadableDataLocation, fromEnum.SchemaVersion);
            }
        }
        /// <summary>
        /// Handles the KeyFamily element child elements
        /// </summary>
        /// <param name="parent">
        /// The parent IDataStructureMutableObject object
        /// </param>
        /// <param name="localName">
        /// The name of the current xml element
        /// </param>
        /// <returns>
        /// The <see cref="StructureReaderBaseV20.ElementActions"/>.
        /// </returns>
        private ElementActions HandleChildElements(ICrossSectionalDataStructureMutableObject parent, object localName)
        {
            ElementActions actions = null;
            if (NameTableCache.IsElement(localName, ElementNameTable.Dimension))
            {
                var dimension = new DimensionMutableCore();
                ParseAttributes(parent, dimension, this.Attributes);
                parent.AddDimension(dimension);
                actions = this.BuildElementActions(dimension, this.HandleChildElements, DoNothing);
            }
            else if (NameTableCache.IsElement(localName, ElementNameTable.TimeDimension))
            {
                var timeDimension = new DimensionMutableCore { TimeDimension = true, Id = DimensionObject.TimeDimensionFixedId };
                ParseAttributes(parent, timeDimension, this.Attributes);
                parent.AddDimension(timeDimension);
                actions = this.BuildElementActions(timeDimension, this.HandleChildElements, DoNothing);
            }
            else if (NameTableCache.IsElement(localName, ElementNameTable.PrimaryMeasure))
            {
                var measure = new PrimaryMeasureMutableCore();
                ParseComponentBaseAttributes(parent, measure, this.Attributes);
                parent.PrimaryMeasure = measure;
                actions = this.BuildElementActions(measure, this.HandleChildElements, DoNothing);
            }
            else if (NameTableCache.IsElement(localName, ElementNameTable.CrossSectionalMeasure))
            {
                var measure = new CrossSectionalMeasureMutableCore();
                ParseAttributes(parent, measure, this.Attributes);
                parent.AddCrossSectionalMeasures(measure);
                actions = this.BuildElementActions(measure, this.HandleChildElements, DoNothing);
            }
            else if (NameTableCache.IsElement(localName, ElementNameTable.Attribute))
            {
                var attribute = new AttributeMutableCore();
                ParseAttributes(parent, attribute, this.Attributes);
                parent.AddAttribute(attribute);
                actions = this.BuildElementActions(attribute, this.HandleChildElements, this.HandleTextChildElement);
            }
            else if (NameTableCache.IsElement(localName, ElementNameTable.Components))
            {
                //// Component element contains the rest of elements so we use the same action again.
                actions = this.BuildElementActions(parent, this.HandleChildElements, DoNothing);
            }
            else if (NameTableCache.IsElement(localName, ElementNameTable.Group))
            {
                var groupMutableCore = new GroupMutableCore();
                ParseAttributes(groupMutableCore, this.Attributes);
                parent.AddGroup(groupMutableCore);
                actions = this.BuildElementActions(groupMutableCore, this.HandleAnnotableChildElements, this.HandleTextChildElement);
            }

            return actions;
        }