#region License // Copyright (c) 2010, ClearCanvas Inc. // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // * Neither the name of ClearCanvas Inc. nor the names of its contributors // may be used to endorse or promote products derived from this software without // specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY // OF SUCH DAMAGE. #endregion #region Backport // This file is a backport from a more recent version of a referenced library. // For a complete file change history, please consult the original file. // // File URL: svn://svn.clearcanvas.ca/source/Xian/Trunk/Dicom/Iod/ContextGroups/ContextGroupBase.cs // Revision: 12512 // Backport Date: 2010-07-27 13:57:59 #endregion using System; using System.Collections; using System.Collections.Generic; using ClearCanvas.Common; using ClearCanvas.Common.Utilities; using ClearCanvas.Dicom.Iod.Macros; namespace Nullstack.ClearCanvasEx.ViewerEx.AnnotationProviders.Backports { /// /// Base class representing a single DICOM context group. /// /// /// /// Defines the baseline items that part of the context group, as well as provides methods for looking up /// the specific based on a . /// /// For additional information, please refer to the DICOM Standard 2008 PS 3.16. /// public abstract partial class ContextGroupBase : IEnumerable where T : ContextGroupBase.ContextGroupItemBase { /// /// Gets the context ID of this group. /// public readonly int ContextId; /// /// Gets the name of this context group. /// public readonly string ContextGroupName; /// /// Gets a value indicating whether or not this context group is extensible. /// public readonly bool IsExtensible; /// /// Gets the version date of this context group. /// public readonly DateTime Version; /// /// Constructs a . /// /// The context ID of this group. /// The name of this context group. /// A value indicating whether or not this context group is extensible. /// The value indicating whether or not this context group is extensible. /// Thrown if is null or empty. protected ContextGroupBase(int contextId, string contextGroupName, bool isExtensible, DateTime version) { Platform.CheckForEmptyString(contextGroupName, "contextGroupName"); this.ContextId = contextId; this.ContextGroupName = contextGroupName; this.IsExtensible = isExtensible; this.Version = version; } /// /// Gets an enumerator that iterates through the defined items of this context group. /// /// A object that can be used to iterate through the items of this context group. IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } /// /// Gets an enumerator that iterates through the defined items of this context group. /// /// A object that can be used to iterate through the items of this context group. public abstract IEnumerator GetEnumerator(); /// /// Creates a new code item to extend this context group. /// /// /// /// Called by the base item when a lookup is performed on an extensible context group and the code does not exist in the group /// (as determined by ). /// /// /// Inheritors must override this method to call an appropriate constructor if the group is extensible. /// The default implementation always returns null. /// /// /// The designator of the coding scheme in which this code is defined. /// The version of the coding scheme in which this code is defined, if known. Should be null if not explicitly specified. /// The value of this code. /// The Human-readable meaning of this code. /// A new code item. protected virtual T CreateContextGroupItem(string codingSchemeDesignator, string codingSchemeVersion, string codeValue, string codeMeaning) { return null; } /// /// Writes the value of the specified code item to the given code sequence. /// /// /// The default implementation calls . /// /// The code item whose value should be written. /// The code sequence to which the code is to be written. public virtual void WriteToCodeSequence(T value, CodeSequenceMacro codeSequence) { value.WriteToCodeSequence(codeSequence); } /// /// Looks up a code item in the context group given the details specified by a code sequence. /// /// /// The default implementation calls . /// /// The code sequence containing the code that is to be looked up. /// A matching baseline code item if one is found, an extending code item if the context group is extensible and a match wasn't found, or null otherwise. public virtual T Lookup(CodeSequenceMacro codeSequence) { return Lookup(codeSequence, false); } /// /// Looks up a code item in the context group given the details specified by a code sequence. /// /// /// The default implementation iterates through and calls to find a match. /// /// The code sequence containing the code that is to be looked up. /// A value indicating whether or not the coding scheme version should be compared when looking for a match. /// A matching baseline code item if one is found, an extending code item if the context group is extensible and a match wasn't found, or null otherwise. public virtual T Lookup(CodeSequenceMacro codeSequence, bool compareCodingSchemeVersion) { T result = CollectionUtils.SelectFirst(this, c => c.Equals(codeSequence, compareCodingSchemeVersion)); if (result == null && this.IsExtensible) result = CreateContextGroupItem(codeSequence.CodingSchemeDesignator, codeSequence.CodingSchemeVersion, codeSequence.CodeValue, codeSequence.CodeMeaning); return result; } /// /// Looks up a code item in the context group given the details specified by a code sequence. /// /// /// The default implementation iterates through and calls to find a match. /// /// The designator of the coding scheme of the code to be looked up. /// The version of the coding scheme of the code to be looked up. /// The value of this code of the code to be looked up. /// The Human-readable meaning of this code of the code to be looked up. /// A value indicating whether or not the coding scheme version should be compared when looking for a match. /// A matching baseline code item if one is found, an extending code item if the context group is extensible and a match wasn't found, or null otherwise. public virtual T Lookup(string codingSchemeDesignator, string codeValue, string codeMeaning, string codingSchemeVersion, bool compareCodingSchemeVersion) { T result = CollectionUtils.SelectFirst(this, c => c.Equals(codingSchemeDesignator, codeValue, codeMeaning, codingSchemeVersion, compareCodingSchemeVersion)); if (result == null && this.IsExtensible) result = CreateContextGroupItem(codingSchemeDesignator, codingSchemeVersion, codeValue, codeMeaning); return result; } } }