///////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////BUILD FROM MUTABLE OBJECTS              //////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////////////
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="AttributeObjectCore"/> class.
        /// </summary>
        /// <param name="attribute">
        /// The attributeObject. 
        /// </param>
        /// <param name="parent">
        /// The parent. 
        /// </param>
        /// <exception cref="SdmxSemmanticException">
        /// Throws Validate exception.
        /// </exception>
        public AttributeObjectCore(IAttributeMutableObject attribute, IAttributeList parent)
            : base(attribute, parent)
        {
            this.dimensionReferences = new List<string>();
            this.conceptRoles = new List<ICrossReference>();
            try
            {
                this.attachmentLevel = attribute.AttachmentLevel;
                this.assignmentStatus = attribute.AssignmentStatus;
                this.attachmentGroup = attribute.AttachmentGroup;
                if (attribute.DimensionReferences != null)
                {
                    this.dimensionReferences = new List<string>(attribute.DimensionReferences);
                }

                if (attribute.ConceptRoles != null)
                {
                    foreach (IStructureReference currentConceptRole in attribute.ConceptRoles)
                    {
                        this.conceptRoles.Add(new CrossReferenceImpl(this, currentConceptRole));
                    }
                }

                this.primaryMeasureReference = attribute.PrimaryMeasureReference;
                this.ValidateAttributeAttributes();
            }
            catch (Exception th)
            {
                throw new SdmxSemmanticException("IsError creating structure: " + this.ToString(), th);
            }
        }
 /// <summary>
 /// Adds the specifiedd attribute.
 /// </summary>
 /// <param name="attribute">
 /// The attribute. 
 /// </param>
 public void AddAttribute(IAttributeMutableObject attribute)
 {
     this._attributes.Add(attribute);
 }
        /// <summary>
        /// Write a KeyFamily/Components/Attribute element from a IAttributeMutableObject object
        /// </summary>
        /// <param name="attribute">
        /// The IAttributeMutableObject object to write
        /// </param>
        /// <param name="helper">
        /// The helper.
        /// </param>
        private void WriteComponent(IAttributeMutableObject attribute, CrossSectionalDsdHelper helper)
        {
            this.WriteStartElement(this.DefaultPrefix, ElementNameTable.Attribute);
            this.WriteComponentAttributes(attribute);
            this.WriteRepresentation(attribute.Representation);
            helper.WriteCrossComponentAttributes(attribute, this.TryWriteAttribute);

            /* 
                  <xs:attribute name="attachmentLevel" type="structure:AttachmentLevelType" use="required"/>
                <xs:attribute name="assignmentStatus" type="structure:AssignmentStatusType" use="required"/>
                <xs:attribute name="isTimeFormat" type="xs:boolean" default="false"/>
                <xs:attribute name="isEntityAttribute" type="xs:boolean" default="false"/>
                <xs:attribute name="isNonObservationalTimeAttribute" type="xs:boolean" default="false"/>
                <xs:attribute name="isCountAttribute" type="xs:boolean" default="false"/>
                <xs:attribute name="isFrequencyAttribute" type="xs:boolean" default="false"/>
                <xs:attribute name="isIdentityAttribute" type="xs:boolean" default="false"/>
             */
            this.TryWriteAttribute(
                AttributeNameTable.attachmentLevel, Helper.GetAttachmentLevelV2(attribute.AttachmentLevel));
            this.TryWriteAttribute(AttributeNameTable.assignmentStatus, attribute.AssignmentStatus);
            this.TryWriteAttribute(AttributeNameTable.isTimeFormat, "TimeFormat".Equals(attribute.Id));

            //////this.TryWriteAttribute(AttributeNameTable.isEntityAttribute, attribute.IsEntityAttribute);
            //////this.TryWriteAttribute(
            //////    AttributeNameTable.isNonObservationalTimeAttribute, attribute.IsNonObservationalTimeAttribute);
            //////this.TryWriteAttribute(AttributeNameTable.isCountAttribute, attribute.IsCountAttribute);
            //////this.TryWriteAttribute(AttributeNameTable.isFrequencyAttribute, attribute.IsFrequencyAttribute);
            //////this.TryWriteAttribute(AttributeNameTable.isIdentityAttribute, attribute.IsIdentityAttribute);
            this.WriteComponentContent(attribute);

            /*
             * <xs:element name="AttachmentGroup" type="common:IDType" minOccurs="0" maxOccurs="unbounded"/>
            <xs:element name="AttachmentMeasure" type="common:IDType" minOccurs="0" maxOccurs="unbounded"/>
             */
            this.TryToWriteElement(this.DefaultNS, ElementNameTable.AttachmentGroup, attribute.AttachmentGroup);

            helper.WriteAttachementMeasures(
                attachmentMeasure =>
                this.TryToWriteElement(this.DefaultNS, ElementNameTable.AttachmentMeasure, attachmentMeasure), 
                attribute.ConceptRef.ChildReference.Id);

            this.WriteEndElement();
        }
        private IDataStructureMutableObject ReOrderAttribute(IDataStructureMutableObject dsMut, IAttributeMutableObject attMut, int inputOrder)
        {
            var sortedAttributes = dsMut.AttributeList.Attributes.OrderBy(
            o =>
            {
                var corder = o.Annotations.FirstOrDefault(mutableObject => string.Equals(mutableObject.Type, "@ORDER@"));
                return corder != null ? int.Parse(corder.Text[0].Value) : 0;
            }).ToArray();

            dsMut.Attributes.Clear();

            int orderID = 1;

            IAnnotationMutableObject annOrder;

            foreach (IAttributeMutableObject att in sortedAttributes)
            {
                if (orderID == inputOrder)
                {
                    annOrder = attMut.Annotations.Where(a => a.Type == "@ORDER@").FirstOrDefault();

                    if (annOrder != null)
                        attMut.Annotations.Remove(annOrder);

                    attMut.Annotations.Add(CreateAnnotation("order" + orderID.ToString(),
                                                        "",
                                                        "@ORDER@",
                                                        "", new List<ITextTypeWrapperMutableObject>(){
                                                            new TextTypeWrapperMutableCore("en",orderID.ToString()) }
                                                         )
                                        );
                    dsMut.AddAttribute(attMut);

                    ++orderID;
                }

                annOrder = att.Annotations.Where(a => a.Type == "@ORDER@").FirstOrDefault();

                if (annOrder != null)
                    att.Annotations.Remove(annOrder);

                att.Annotations.Add(CreateAnnotation("order" + orderID.ToString(),
                                                    "",
                                                    "@ORDER@",
                                                    "", new List<ITextTypeWrapperMutableObject>(){
                                                            new TextTypeWrapperMutableCore("en",orderID.ToString()) }
                                                     )
                                    );
                dsMut.AddAttribute(att);

                ++orderID;
            }

            if (orderID <= inputOrder)
            {
                annOrder = attMut.Annotations.Where(a => a.Type == "@ORDER@").FirstOrDefault();

                if (annOrder != null)
                    attMut.Annotations.Remove(annOrder);

                attMut.Annotations.Add(CreateAnnotation("order" + orderID.ToString(),
                                                    "",
                                                    "@ORDER@",
                                                    "", new List<ITextTypeWrapperMutableObject>(){
                                                            new TextTypeWrapperMutableCore("en",orderID.ToString()) }
                                                     )
                                    );
                dsMut.AddAttribute(attMut);
            }

            return dsMut;
        }
        /////// <summary>
        ///////     Initialize handlers based on parent type for Elements and element text
        /////// </summary>
        ////private void InitializeTypeSwitch()
        ////{
        ////    // add element text handlers
        ////    this.AddHandleText<IAttributeMutableObject>(this.HandleTextChildElement);
        ////    this.AddHandleText<IGroupMutableObject>(this.HandleTextChildElement);
        ////    this.AddHandleText<IAnnotationMutableObject>(this.HandleTextChildElement);
        ////    this.AddHandleText<INameableMutableObject>(
        ////        (parent, localName) => this.HandleCommonTextChildElement(parent, localName));

        ////    // add element handlers
        ////    this.AddHandleElement<IComponentMutableObject>(this.HandleChildElements);
        ////    this.AddHandleElement<ICrossSectionalDataStructureMutableObject>(this.HandleChildElements);
        ////    this.AddHandleElement<ICollection<IAnnotationMutableObject>>(HandleChildElements);
        ////    this.AddHandleElement<IdentifiableMutableCore>(HandleChildElements); // super type move last
        ////    this.AddHandleElement<IMutableObjects>(this.HandleTopLevel);
        ////}

        /// <summary>
        /// Populates the properties of the given IAttributeMutableObject object from the given xml attributes dictionary
        /// </summary>
        /// <param name="parent">
        /// The parent.
        /// </param>
        /// <param name="component">
        /// The given IAttributeMutableObject object
        /// </param>
        /// <param name="attributes">
        /// The dictionary contains the attributes of the element
        /// </param>
        private void ParseAttributes(ICrossSectionalDataStructureMutableObject parent, IAttributeMutableObject component, IDictionary<string, string> attributes)
        {
            ParseComponentBaseAttributes(parent, component, attributes);
            string attachmentLevel = Helper.TrySetFromAttribute(attributes, AttributeNameTable.attachmentLevel, string.Empty);
            if (attachmentLevel == "Series")
            {
                component.AttachmentLevel = AttributeAttachmentLevel.DimensionGroup;
                foreach (IDimensionMutableObject dimension in this._currentDsd.Dimensions)
                {
                    if (!dimension.TimeDimension)
                    {
                        component.DimensionReferences.Add(dimension.ConceptRef.ChildReference.Id);
                    }
                }
            }
            else
            {
                AttributeAttachmentLevel modelAttachementLevel;

                if (Enum.TryParse(attachmentLevel, out modelAttachementLevel))
                {
                    component.AttachmentLevel = modelAttachementLevel;
                }
            }

            component.AssignmentStatus = Helper.TrySetFromAttribute(attributes, AttributeNameTable.assignmentStatus, component.AssignmentStatus);

            //// TODO JAVA 0.9.9 missing TimeFormat property from IAttributeMutableObject.
            bool timeFormat = Helper.TrySetFromAttribute(attributes, AttributeNameTable.isTimeFormat, false);
            if (timeFormat)
            {
                //// TODO FIXME HACK because of missing TimeFormat property from java 0.9.9
                component.Id = "TIME_FORMAT";
            }

            //// TODO java not supported by the Common API as in Java version 0.9.9
            ////component.IsEntityAttribute = Helper.TrySetFromAttribute(
            ////    attributes, AttributeNameTable.isEntityAttribute, component.IsEntityAttribute);
            ////component.IsNonObservationalTimeAttribute = Helper.TrySetFromAttribute(
            ////    attributes, AttributeNameTable.isNonObservationalTimeAttribute, component.IsNonObservationalTimeAttribute);
            ////component.IsCountAttribute = Helper.TrySetFromAttribute(
            ////    attributes, AttributeNameTable.isCountAttribute, component.IsCountAttribute);
            ////component.IsFrequencyAttribute = Helper.TrySetFromAttribute(
            ////    attributes, AttributeNameTable.isFrequencyAttribute, component.IsFrequencyAttribute);
            ////component.IsIdentityAttribute = Helper.TrySetFromAttribute(
            ////    attributes, AttributeNameTable.isIdentityAttribute, component.IsIdentityAttribute);
        }
        /// <summary>
        /// Handles the Attribute element child elements
        /// </summary>
        /// <param name="parent">
        /// The parent IAttributeMutableObject object
        /// </param>
        /// <param name="localName">
        /// The name of the current xml element
        /// </param>
        private void HandleTextChildElement(IAttributeMutableObject parent, object localName)
        {
            if (NameTableCache.IsElement(localName, ElementNameTable.AttachmentGroup))
            {
                if (string.IsNullOrWhiteSpace(parent.AttachmentGroup))
                {
                    parent.AttachmentGroup = this.Text;
                }
            }
            else if (NameTableCache.IsElement(localName, ElementNameTable.AttachmentMeasure))
            {
                IList<string> measureList;
                string key = parent.ConceptRef.ChildReference.Id;
                if (!this._currentDsd.AttributeToMeasureMap.TryGetValue(key, out measureList))
                {
                    measureList = new List<string>();
                    this._currentDsd.AttributeToMeasureMap.Add(key, measureList);
                }

                measureList.Add(this.Text);
            }
        }