public void Deserialize_SingleRequest() { var xml = @"<!DOCTYPE TagElevationRequestCollection [ <!ELEMENT TagElevationRequestCollection (TagElevationRequest*)> <!ELEMENT TagElevationRequest (ColumnName,ElevationPathway,Conditional?)> <!ELEMENT ColumnName (#PCDATA)> <!ELEMENT ElevationPathway (#PCDATA)> <!ELEMENT Conditional (ConditionalPathway,ConditionalRegex)> <!ELEMENT ConditionalPathway (#PCDATA)> <!ELEMENT ConditionalRegex (#PCDATA)> ]> <TagElevationRequestCollection> <TagElevationRequest> <ColumnName>ContentSequenceDescriptions</ColumnName> <ElevationPathway>ContentSequence->TextValue</ElevationPathway> <Conditional> <ConditionalPathway>.->ConceptNameCodeSequence->CodeMeaning</ConditionalPathway> <ConditionalRegex>Tr.*[e-a]{2}tment</ConditionalRegex> </Conditional> </TagElevationRequest> </TagElevationRequestCollection>"; var collection = new TagElevationRequestCollection(xml); Assert.AreEqual(1, collection.Requests.Count); Assert.AreEqual("ContentSequence->TextValue", collection.Requests[0].ElevationPathway); Assert.AreEqual("ContentSequenceDescriptions", collection.Requests[0].ColumnName); Assert.AreEqual(".->ConceptNameCodeSequence->CodeMeaning", collection.Requests[0].ConditionalPathway); Assert.AreEqual("Tr.*[e-a]{2}tment", collection.Requests[0].ConditionalRegex); }
private void RunChecks() { RagSmiley1.Reset(); try { var collection = new TagElevationRequestCollection(queryEditor.Text); RagSmiley1.OnCheckPerformed(new CheckEventArgs("Succesfully created elevator", CheckResult.Success)); } catch (Exception ex) { RagSmiley1.Fatal(ex); } }
/// <summary> /// Iterates through each DicomItem in the DicomDataset and creates a corresponding column in the DataTable (if it is a novel tag) and then populates /// the DataTable with the data for the image. /// </summary> /// <param name="filename"></param> /// <param name="ds"></param> /// <param name="dt"></param> /// <param name="listener"></param> /// <param name="otherValuesToStoreInRow"></param> protected void ProcessDataset(string filename, DicomDataset ds, DataTable dt, IDataLoadEventListener listener, Dictionary <string, string> otherValuesToStoreInRow = null) { if (_elevationRequests == null) { _elevationRequests = LoadElevationRequestsFile(); } filename = ApplyArchiveRootToMakeRelativePath(filename); var rowValues = new Dictionary <string, object>(); foreach (DicomItem item in ds) { //get the tag name (human readable) var entry = item.Tag.DictionaryEntry; string header = entry.Keyword; if (ShouldSkip(dt, item.Tag)) { continue; } object value; switch (InvalidDataHandlingStrategy) { case InvalidDataHandling.ThrowException: //enforce types and leave any Exceptions uncaught value = DicomTypeTranslater.Flatten(DicomTypeTranslaterReader.GetCSharpValue(ds, item)); break; case InvalidDataHandling.MarkCorrupt: try { //try to enforce types value = DicomTypeTranslater.Flatten(DicomTypeTranslaterReader.GetCSharpValue(ds, item)); } catch (Exception ex) { //something went wrong pulling out the value //mark it as corrupt MarkCorrupt(ds); //but make sure to warn people listening listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Warning, "Could not GetCSharpValue for DicomItem " + item.Tag + "(" + entry.Keyword + ") for " + GetProblemFileDescription(filename, otherValuesToStoreInRow), ex)); //do not add the row to the table return; } break; case InvalidDataHandling.ConvertToNullAndWarn: //try to enforce types try { value = DicomTypeTranslater.Flatten(DicomTypeTranslaterReader.GetCSharpValue(ds, item)); } catch (Exception ex) { //but make sure to warn people listening listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Warning, "Could not GetCSharpValue for DicomItem " + item.Tag + "(" + entry.Keyword + ") for " + GetProblemFileDescription(filename, otherValuesToStoreInRow), ex)); if (InvalidDataHandlingStrategy == InvalidDataHandling.MarkCorrupt) { MarkCorrupt(ds); continue; } //It went wrong, we couldn't enforce the type so just use null instead value = DBNull.Value; } break; default: throw new ArgumentOutOfRangeException(); } if (value is string && DataTooLongHandlingStrategy != DataTooWideHandling.None) { if (!IsValidLength(listener, item.Tag, (string)value)) { //the string is too long! switch (DataTooLongHandlingStrategy) { case DataTooWideHandling.TruncateAndWarn: lock (_oDictLock) { value = ((string)value).Substring(0, _maxTagLengths[item.Tag]); } break; case DataTooWideHandling.MarkCorrupt: MarkCorrupt(ds); continue; case DataTooWideHandling.ConvertToNullAndWarn: value = DBNull.Value; break; default: throw new ArgumentOutOfRangeException(); } } } try { rowValues.Add(header, value); } catch (Exception e) { listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Warning, "Error getting Tag '" + header + "' item.ValueRepresentation is (" + item.ValueRepresentation + ") " + GetProblemFileDescription(filename, otherValuesToStoreInRow), e)); } } //now run elevation requests if (_elevationRequests != null) { foreach (TagElevationRequest request in _elevationRequests.Requests) { try { object value; switch (InvalidDataHandlingStrategy) { case InvalidDataHandling.ThrowException: value = request.Elevator.GetValue(ds); break; case InvalidDataHandling.ConvertToNullAndWarn: try { value = request.Elevator.GetValue(ds); } catch (Exception e) { if (e is TagNavigationException) { throw; } listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Warning, "Error getting Tag for ElevationRequest '" + request.ColumnName + "' for " + GetProblemFileDescription(filename, otherValuesToStoreInRow), e)); value = DBNull.Value; } break; default: throw new ArgumentOutOfRangeException(); } rowValues.Add(request.ColumnName, value); } catch (TagNavigationException e) { listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Warning, "Rule for column " + request.ColumnName + " failed to resolve GetValue for " + GetProblemFileDescription(filename, otherValuesToStoreInRow), e)); } } } //do not add rows if there is nothing to store if (!rowValues.Any()) { return; } lock (_oDataTableLock) { var row = dt.Rows.Add(); row[FilenameField] = filename; if (otherValuesToStoreInRow != null) { foreach (KeyValuePair <string, string> kvp in otherValuesToStoreInRow) { if (!dt.Columns.Contains(kvp.Key)) { dt.Columns.Add(kvp.Key); } row[kvp.Key] = kvp.Value; } } foreach (KeyValuePair <string, object> keyValuePair in rowValues) { Add(dt, row, keyValuePair.Key, keyValuePair.Value); } } }