private void ontology_LanguageAdded(object sender, string language, string defaultLanguageCodeSet)
 {
     CODE_PHRASE cp = new CODE_PHRASE();
     cp.code_string = language;
     
     if (defaultLanguageCodeSet != "")
     {                
         TERMINOLOGY_ID terminologyId = new TERMINOLOGY_ID();
         terminologyId.value = defaultLanguageCodeSet;
         cp.terminology_id = terminologyId;                
     }
     
     AddDescriptionItem(cp);
 }
        protected override TermBindingSet[] CloneTermBindingSet(EiffelSoftware.Library.Base.structures.table.HASH_TABLE_REFERENCE_REFERENCE currentObject)
        {
            List<TermBindingSet> termBindingSets = new List<TermBindingSet>();

            if (currentObject != null)
            {
                if (currentObject.count() > 0)
                {
                    currentObject.start();

                    // 0..* items TERM_BINDING_ITEM                    
                    for (int i = 1; i <= currentObject.count(); i++)
                    {
                        TermBindingSet termBindingSet = new TermBindingSet();

                        //1 terminology string
                        termBindingSet.terminology = currentObject.key_for_iteration().ToString();

                        //terms HASH table
                        EiffelStructures.table.Impl.HASH_TABLE_REFERENCE_REFERENCE adlTerms;
                        adlTerms = currentObject.item_for_iteration() as EiffelStructures.table.Impl.HASH_TABLE_REFERENCE_REFERENCE;
                        SortedList<string, TERM_BINDING_ITEM> localTerms = new SortedList<string, TERM_BINDING_ITEM>();

                        adlTerms.start();

                        if (adlTerms.count() > 0)
                        {
                            for (int j = 1; j <= adlTerms.count(); j++)
                            {
                                openehr.openehr.rm.data_types.text.CODE_PHRASE term = adlTerms.item_for_iteration() as openehr.openehr.rm.data_types.text.CODE_PHRASE;

                                if (term != null)
                                {
                                    TERM_BINDING_ITEM localTerm = new TERM_BINDING_ITEM();

                                    // 1 code string
                                    localTerm.code = adlTerms.key_for_iteration().ToString();

                                    // 1 value CODE_PHRASE (CODE_PHRASE to HASH TABLE)                                
                                    CODE_PHRASE codePhrase = new CODE_PHRASE();

                                    // 1 code_string string
                                    codePhrase.code_string = term.code_string().ToString();

                                    // 1 terminology_id TERMINOLOGY_ID                                
                                    if (term.code_string() != null)
                                    {
                                        TERMINOLOGY_ID terminologyId = new TERMINOLOGY_ID();
                                        terminologyId.value = term.terminology_id().value().ToString();
                                        codePhrase.terminology_id = terminologyId;
                                    }

                                    localTerm.value = codePhrase;
                                    localTerms.Add(localTerm.code, localTerm);
                                }

                                adlTerms.forth();
                            }

                            termBindingSet.items = new TERM_BINDING_ITEM[localTerms.Count];
                            localTerms.Values.CopyTo(termBindingSet.items, 0);
                        }

                        if (termBindingSet.items != null)
                        {
                            termBindingSets.Add(termBindingSet);
                        }

                        currentObject.forth();
                    }
                }
            }

            if (termBindingSets.Count > 0)
            {
                TermBindingSet[] termBindingSetArray = new TermBindingSet[termBindingSets.Count];
                termBindingSets.CopyTo(termBindingSetArray);
                return termBindingSetArray;
            }
            else
            {
                return null;
            }
        }
        protected virtual TermBindingSet[] CloneTermBindingSet(EiffelStructures.table.HASH_TABLE_REFERENCE_REFERENCE o)
        {
            TermBindingSet[] result = null;

            if (o != null && o.count() > 0)
            {
                result = new TermBindingSet[o.count()];
                o.start();

                for (int i = 1; i <= o.count(); i++)
                {
                    TermBindingSet termBindingSet = new TermBindingSet();
                    termBindingSet.terminology = o.key_for_iteration().ToString();

                    EiffelStructures.table.Impl.HASH_TABLE_REFERENCE_REFERENCE adlTerms = o.item_for_iteration() as EiffelStructures.table.Impl.HASH_TABLE_REFERENCE_REFERENCE;
                    SortedList<string, TERM_BINDING_ITEM> localTerms = new SortedList<string, TERM_BINDING_ITEM>();
                    adlTerms.start();

                    for (int j = 1; j <= adlTerms.count(); j++)
                    {
                        openehr.openehr.rm.data_types.text.CODE_PHRASE term = adlTerms.item_for_iteration() as openehr.openehr.rm.data_types.text.CODE_PHRASE;

                        if (term != null)
                        {
                            TERM_BINDING_ITEM localTerm = new TERM_BINDING_ITEM();
                            localTerm.code = adlTerms.key_for_iteration().ToString();
                            CODE_PHRASE codePhrase = new CODE_PHRASE();
                            codePhrase.code_string = term.code_string().ToString();

                            if (term.code_string() != null)
                            {
                                TERMINOLOGY_ID terminologyId = new TERMINOLOGY_ID();
                                terminologyId.value = term.terminology_id().value().ToString();
                                codePhrase.terminology_id = terminologyId;
                            }

                            localTerm.value = codePhrase;
                            localTerms.Add(localTerm.code, localTerm);
                        }

                        adlTerms.forth();
                    }

                    termBindingSet.items = new TERM_BINDING_ITEM[localTerms.Count];
                    localTerms.Values.CopyTo(termBindingSet.items, 0);
                    result[i - 1] = termBindingSet;
                    o.forth();
                }
            }

            return result;
        }
        protected virtual CODE_PHRASE CloneCodePhrase(openehr.openehr.rm.data_types.text.CODE_PHRASE o)
        {
            CODE_PHRASE result = null;

            if (o != null)
            {
                result = new CODE_PHRASE();

                if (o.code_string() != null)
                    result.code_string = o.code_string().ToString();
                else
                    result.code_string = "";

                if (o.terminology_id() != null)
                {
                    TERMINOLOGY_ID terminologyId = new TERMINOLOGY_ID();
                    terminologyId.value = o.terminology_id().value().ToString();
                    result.terminology_id = terminologyId;
                }
            }

            return result;
        }
        public void AddTranslation(CODE_PHRASE a_language)
        {
            TRANSLATION_DETAILS td = new TRANSLATION_DETAILS();
            TRANSLATION_DETAILS[] translationDetails;
            td.language = a_language;

            if (_archetype.translations == null)
            {
                //create an array
                translationDetails = Array.CreateInstance(typeof(TRANSLATION_DETAILS), 1) as TRANSLATION_DETAILS[];
                translationDetails[0] = td;
            }
            else
            {
                //resize the array
                translationDetails = _archetype.translations;
                int i = translationDetails.Length;
                Array.Resize(ref translationDetails, i + 1);
                translationDetails[i] = td;
            }

            _archetype.translations = translationDetails;    
        }
        internal void AddDescriptionItem(CODE_PHRASE a_language)
        {
            RESOURCE_DESCRIPTION_ITEM rdi = new RESOURCE_DESCRIPTION_ITEM();
            RESOURCE_DESCRIPTION_ITEM[] descriptionItems;
            rdi.language = a_language;

            if (_archetype.description.details == null)
            {
                //create an array
                descriptionItems = Array.CreateInstance(typeof(RESOURCE_DESCRIPTION_ITEM), 1) as RESOURCE_DESCRIPTION_ITEM[];
                descriptionItems[0] = rdi;
            }
            else
            {
                //resize the array
                descriptionItems = _archetype.description.details;
                int i = descriptionItems.Length;
                Array.Resize(ref descriptionItems, i + 1);
                descriptionItems[i] = rdi;

                foreach (RESOURCE_DESCRIPTION_ITEM descItem in _archetype.description.details)
                {
                    //copy from the primary language
                    string originalLanguage = _archetype.original_language.code_string;

                    if (descItem.language.code_string == originalLanguage)
                    {
                        rdi.purpose = string.Format("{0}{1}({2})", "*", descItem.purpose, originalLanguage);

                        if (rdi.use != null && rdi.use != "")
                        {
                            rdi.use = string.Format("{0}{1}({2})", "*", descItem.use, originalLanguage);
                        }

                        if (rdi.misuse != null && rdi.misuse != "")
                        {
                            rdi.misuse = string.Format("{0}{1}({2})", "*", descItem.misuse, originalLanguage);
                        }

                        if (rdi.original_resource_uri != null && rdi.original_resource_uri.ToString() != "")
                        {                            
                            rdi.original_resource_uri = descItem.original_resource_uri;
                        }
                    }
                }
            }

            _archetype.description.details = descriptionItems;
        }