/// <summary> /// Traverse the HCL CodeRef hierarchy tree and write each CodeRef starting from /// the given ICodeRefMutableObject object. /// </summary> /// <param name="item"> /// The root ICategoryMutableObject object /// </param> private void TraverseCodeRefTree(ICodeRefMutableObject item) { var parents = new Stack<ICodeRefMutableObject>(); var open = new Dictionary<ICodeRefMutableObject, Queue<ICodeRefMutableObject>>(); this.WriteCodeRef(item); parents.Push(item); open.Add(item, new Queue<ICodeRefMutableObject>(item.CodeRefs)); while (parents.Count > 0) { ICodeRefMutableObject parent = parents.Peek(); Queue<ICodeRefMutableObject> remainingCodeRef = open[parent]; while (remainingCodeRef.Count > 0) { ICodeRefMutableObject current = remainingCodeRef.Dequeue(); this.WriteCodeRef(current); parents.Push(current); remainingCodeRef = new Queue<ICodeRefMutableObject>(current.CodeRefs); open.Add(current, remainingCodeRef); // parent = parents.Peek(); } parent = parents.Pop(); this.WriteCodeRefTrailingElements(parent); this.WriteEndElement(); } }
/// <summary> /// The write code ref trailing elements. /// </summary> /// <param name="codeRef"> /// The code ref. /// </param> private void WriteCodeRefTrailingElements(ICodeRefMutableObject codeRef) { var level = string.IsNullOrEmpty(codeRef.LevelReference) ? null : codeRef.LevelReference.Split('.').Last(); this.TryToWriteElement(this.DefaultNS, ElementNameTable.LevelRef, level); this.TryToWriteElement(this.DefaultNS, ElementNameTable.NodeAliasID, codeRef.CodeId); ////TryToWriteElement(this.DefaultNS, ElementNameTable.Version, codeRef.Version); this.TryToWriteElement(this.DefaultNS, ElementNameTable.ValidFrom, codeRef.ValidFrom); this.TryToWriteElement(this.DefaultNS, ElementNameTable.ValidTo, codeRef.ValidTo); }
/// <summary> /// The write code ref. /// </summary> /// <param name="codeRef"> /// The code ref. /// </param> private void WriteCodeRef(ICodeRefMutableObject codeRef) { this.WriteStartElement(this.DefaultNS, ElementNameTable.CodeRef); // this.TryToWriteElement(this.DefaultNS, ElementNameTable.URN, codeRef.Urn); this.TryToWriteElement(this.DefaultNS, ElementNameTable.CodelistAliasRef, codeRef.CodelistAliasRef); this.TryToWriteElement(this.DefaultNS, ElementNameTable.CodeID, codeRef.Id); }
/// <summary> /// The add hierarchical code. /// </summary> /// <param name="codeRef"> /// The code ref. /// </param> public void AddHierarchicalCode(ICodeRefMutableObject codeRef) { this._codeRefs.Add(codeRef); }
/// <summary> /// The add code ref. /// </summary> /// <param name="codeRef"> /// The code ref. /// </param> public virtual void AddCodeRef(ICodeRefMutableObject codeRef) { this._codeRefs.Add(codeRef); }
/// <summary> /// Handles the CodeRef type element text only child elements /// </summary> /// <param name="parent"> /// The parent ICodeRefMutableObject object /// </param> /// <param name="localName"> /// The name of the current xml element /// </param> private void HandleTextChildElement(ICodeRefMutableObject parent, object localName) { if (NameTableCache.IsElement(localName, ElementNameTable.CodelistAliasRef)) { parent.CodelistAliasRef = this.Text; } else if (NameTableCache.IsElement(localName, ElementNameTable.CodeID)) { parent.CodeId = this.Text; } else if (NameTableCache.IsElement(localName, ElementNameTable.LevelRef)) { parent.LevelReference = this.Text; } else if (NameTableCache.IsElement(localName, ElementNameTable.NodeAliasID)) { parent.Id = this.Text; } else if (NameTableCache.IsElement(localName, ElementNameTable.ValidFrom)) { parent.ValidFrom = XmlConvert.ToDateTime(this.Text, XmlDateTimeSerializationMode.RoundtripKind); } else if (NameTableCache.IsElement(localName, ElementNameTable.ValidTo)) { parent.ValidTo = XmlConvert.ToDateTime(this.Text, XmlDateTimeSerializationMode.RoundtripKind); } }
/// <summary> /// Handles the CodeRef type element child elements /// </summary> /// <param name="parent"> /// The parent ICodeRefMutableObject object /// </param> /// <param name="localName"> /// The name of the current xml element /// </param> /// <returns> /// The <see cref="StructureReaderBaseV20.ElementActions"/>. /// </returns> private ElementActions HandleChildElements(ICodeRefMutableObject parent, object localName) { ElementActions actions = null; if (NameTableCache.IsElement(localName, ElementNameTable.CodeRef)) { var cr = new CodeRefMutableCore(); ParseAttributes(cr, this.Attributes); parent.AddCodeRef(cr); actions = this.BuildElementActions(cr, HandleChildElements, this.HandleTextChildElement); } return actions; }
/// <summary> /// Retrieve and populate the <see cref="IHierarchyMutableObject.HierarchicalCodeObjects"/> for the HIERARCHY with specified Primary KEY /// </summary> /// <param name="artefact"> /// The <see cref="IHierarchyMutableObject"/> instance to populate /// </param> /// <param name="hid"> /// The Hierarchy Mapping store Primary KEY /// </param> private void FillCodeRef(IHierarchyMutableObject artefact, long hid) { IDictionary <long, ICodeRefMutableObject> allItems = new ListDictionary <long, ICodeRefMutableObject>(); var childItems = new Dictionary <long, long>(); using (DbCommand command = this._itemCommandBuilder.Build(new ItemSqlQuery(this._codeRefQueryInfo, hid))) { using (IDataReader dataReader = this.MappingStoreDb.ExecuteReader(command)) { int sysIdIdx = dataReader.GetOrdinal("SYSID"); int idIdx = dataReader.GetOrdinal("CODE_ID"); int parentIdx = dataReader.GetOrdinal("PARENT"); int nodeAliasIdIdx = dataReader.GetOrdinal("NodeAliasID"); //// TODO check how to set the version (it seems supported in v2.1) int levelRefIdx = dataReader.GetOrdinal("LEVEL_REF"); int clidIdx = dataReader.GetOrdinal("CLID"); int clversionIdx = dataReader.GetOrdinal("CLVERSION"); int clagencyIdx = dataReader.GetOrdinal("CLAGENCY"); int validFromIdx = dataReader.GetOrdinal("VALID_FROM"); int validToIdx = dataReader.GetOrdinal("VALID_TO"); while (dataReader.Read()) { var codeRef = new CodeRefMutableCore { CodelistAliasRef = BuildCodelistRefAlias( DataReaderHelper.GetString(dataReader, clidIdx), DataReaderHelper.GetString(dataReader, clagencyIdx), DataReaderHelper.GetString(dataReader, clversionIdx)), Id = DataReaderHelper.GetString(dataReader, nodeAliasIdIdx), CodeId = DataReaderHelper.GetString(dataReader, idIdx) }; if (artefact.ChildLevel != null) { var levelRef = DataReaderHelper.GetString(dataReader, levelRefIdx); ILevelMutableObject currentLevel = artefact.ChildLevel; var normalizedLevel = GetNormalizedLevel(currentLevel, levelRef); codeRef.LevelReference = normalizedLevel; } if (string.IsNullOrWhiteSpace(codeRef.Id)) { codeRef.Id = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", codeRef.CodelistAliasRef.Replace('@', '_'), '_', codeRef.CodeId); } DateTime?validFrom = DataReaderHelper.GetStringDate(dataReader, validFromIdx); if (validFrom != null) { codeRef.ValidFrom = validFrom.Value; } DateTime?validTo = DataReaderHelper.GetStringDate(dataReader, validToIdx); if (validTo != null) { codeRef.ValidTo = validTo.Value; } long sysId = DataReaderHelper.GetInt64(dataReader, sysIdIdx); long parentItemId = DataReaderHelper.GetInt64(dataReader, parentIdx); if (parentItemId > long.MinValue) { childItems.Add(sysId, parentItemId); } allItems.Add(sysId, codeRef); } } } this._hierarchalCodeAnnotationRetrieverEngine.RetrieveAnnotations(hid, allItems); ICollection <KeyValuePair <long, ICodeRefMutableObject> > collection = allItems; foreach (KeyValuePair <long, ICodeRefMutableObject> item in collection) { long sysId = item.Key; ICodeRefMutableObject codeRef = item.Value; long parentItemId; if (childItems.TryGetValue(sysId, out parentItemId)) { // has parent ICodeRefMutableObject parent = allItems[parentItemId]; parent.AddCodeRef(codeRef); } else { // add only root elements artefact.AddHierarchicalCode(codeRef); } } }