Ejemplo n.º 1
0
        /// <summary>
        /// Graph object <paramref name="o"/> onto the stream
        /// </summary>
        public void Graph(XmlWriter s, object o, DatatypeR2FormatterGraphResult result)
        {
            // Graph ANY
            QTYFormatter baseFormatter = new QTYFormatter();

            baseFormatter.Host = this.Host;

            // Null ?
            IAny anyInstance        = o as IAny;
            IPrimitiveDataValue pdv = o as IPrimitiveDataValue;
            IRealValue          rv  = o as IRealValue;

            // Format
            if (pdv.Value != null && anyInstance.NullFlavor == null)
            {
                if (rv == null || rv.Precision == 0)
                {
                    s.WriteAttributeString("value", Util.ToWireFormat(pdv.Value));
                }
                else if (rv != null)
                {
                    s.WriteAttributeString("value", String.Format(DatatypeR2Formatter.FormatterCulture, String.Format("{{0:0.{0}}}", new String('0', rv.Precision), DatatypeR2Formatter.FormatterCulture.NumberFormat.NumberDecimalSeparator), rv.Value));
                }
            }

            // Format the base
            baseFormatter.Graph(s, o, result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parse an object from <paramref name="s"/>
        /// </summary>
        public object Parse(System.Xml.XmlReader s, DatatypeR2FormatterParseResult result)
        {
            PDVFormatter baseFormatter = new PDVFormatter();

            baseFormatter.Host = this.Host;
            var retVal = baseFormatter.ParseAttributes <CO>(s, result);

            // Process the parts
            #region Elements
            if (!s.IsEmptyElement)
            {
                int    sDepth = s.Depth;
                string sName  = s.Name;
                s.Read();

                QTYFormatter qtyf = new QTYFormatter();

                // string Name
                while (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName))
                {
                    string oldName = s.Name; // Name
                    try
                    {
                        // Numerator
                        if (s.LocalName == "code" && retVal.Code == null)
                        {
                            var parseResult = Host.Parse(s, typeof(CD <String>));
                            result.Code = parseResult.Code;
                            result.AddResultDetail(parseResult.Details);
                            retVal.Code = parseResult.Structure as CD <String>;
                        }
                        else if (s.LocalName == "code")
                        {
                            result.AddResultDetail(new NotImplementedResultDetail(ResultDetailType.Warning, "Code can only be assigned once", s.ToString()));
                        }
                        else
                        {
                            qtyf.ParseElementsInline(s, retVal, result);
                        }
                    }
                    catch (MessageValidationException e)
                    {
                        result.AddResultDetail(new ResultDetail(MARC.Everest.Connectors.ResultDetailType.Error, e.Message, e));
                    }
                    finally
                    {
                        if (s.Name == oldName)
                        {
                            s.Read();
                        }
                    }
                }
            }
            #endregion

            // Validate
            new ANYFormatter().Validate(retVal, s.ToString(), result);
            return(retVal);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the list of supported properties
        /// </summary>
        /// <returns></returns>
        public List <System.Reflection.PropertyInfo> GetSupportedProperties()
        {
            var retVal = new QTYFormatter().GetSupportedProperties();

            retVal.AddRange(new PropertyInfo[] {
                typeof(CO).GetProperty("Code"),
                typeof(CO).GetProperty("Value"),
            });
            return(retVal);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets the list of supported properties
        /// </summary>
        /// <returns></returns>
        public List <System.Reflection.PropertyInfo> GetSupportedProperties()
        {
            var retVal = new QTYFormatter().GetSupportedProperties();

            retVal.AddRange(new PropertyInfo[] {
                typeof(REAL).GetProperty("Value"),
                typeof(REAL).GetProperty("Expression"),
                typeof(REAL).GetProperty("OriginalText"),
                typeof(REAL).GetProperty("Uncertainty"),
                typeof(REAL).GetProperty("UncertainRange")
            });
            return(retVal);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Parse an object from <paramref name="s"/>
        /// </summary>
        public T Parse <T>(XmlReader r, DatatypeR2FormatterParseResult result) where T : ANY, IPrimitiveDataValue, IQuantity, new()
        {
            // Prepare QTY formatting
            QTYFormatter baseFormatter = new QTYFormatter();

            baseFormatter.Host = this.Host;

            // Parse attributes part of QTY
            T retVal = this.ParseAttributes <T>(r, result);

            // Parse the elements
            baseFormatter.ParseElements <T>(r, retVal, result);

            // Validate
            new ANYFormatter().Validate(retVal, r.ToString(), result);

            return(retVal);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Parse an object from <paramref name="s"/>
        /// </summary>
        public T ParseAttributes <T>(XmlReader r, DatatypeR2FormatterParseResult result) where T : ANY, IPrimitiveDataValue, IQuantity, new()
        {
            string val = null;

            // Get the value
            if (r.GetAttribute("value") != null)
            {
                val = r.GetAttribute("value");
            }

            // Parse the QTY part
            QTYFormatter baseFormatter = new QTYFormatter();

            baseFormatter.Host = this.Host;

            // Parse attributes part of QTY
            T retVal = baseFormatter.ParseAttributes <T>(r, result);

            IRealValue rvRetVal = retVal as IRealValue;

            // If the value was interpreted
            if (!String.IsNullOrEmpty(val))
            {
                try
                {
                    retVal.Value = Util.FromWireFormat(val, retVal.GetType().GetProperty("Value").PropertyType);
                    if (rvRetVal != null && val.Contains(DatatypeR2Formatter.FormatterCulture.NumberFormat.NumberDecimalSeparator))
                    {
                        rvRetVal.Precision = val.Length - val.IndexOf(DatatypeR2Formatter.FormatterCulture.NumberFormat.NumberDecimalSeparator) - 1;
                    }
                }
                catch (Exception e)
                {
                    result.Code = ResultCode.Error;
                    result.AddResultDetail(new ResultDetail(ResultDetailType.Error, e.Message, r.ToString(), e));
                }
            }

            // REturn the retVal
            return(retVal);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Parse the PQ back into a structure
        /// </summary>
        public object Parse(System.Xml.XmlReader s, DatatypeR2FormatterParseResult result)
        {
            // Create the base formatter
            PDVFormatter baseFormatter = new PDVFormatter();

            baseFormatter.Host = this.Host;

            // Read temporary values
            string tUnit = null;

            if (s.GetAttribute("unit") != null)
            {
                tUnit = s.GetAttribute("unit");
            }
            SET <CodingRationale> tRationale = null;

            if (s.GetAttribute("codingRationale") != null)
            {
                tRationale = Util.Convert <SET <CodingRationale> >(s.GetAttribute("codingRationale"));
            }

            // Parse PDV content (only attributes)
            var retVal = baseFormatter.ParseAttributes <PQ>(s, result);

            // Set PDV content
            retVal.Unit            = tUnit;
            retVal.CodingRationale = tRationale;

            // Process elements
            // This requires a QTY formatter as QTY elements may be
            // in the stream as well
            #region Elements
            if (!s.IsEmptyElement)
            {
                // Prepare a formatter to process QTY elements
                QTYFormatter qtyFormatter = new QTYFormatter();
                qtyFormatter.Host = this.Host;

                // Exit markers
                int    sDepth = s.Depth;
                string sName  = s.Name;

                // Translations
                SET <PQR> translations = new SET <PQR>();

                // Read the next element
                s.Read();

                // Read until exit condition is fulfilled
                while (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName))
                {
                    string oldName = s.Name; // Name
                    try
                    {
                        if (s.LocalName == "translation") // Format using ED
                        {
                            var hostResult = Host.Parse(s, typeof(PQR));
                            result.Code = hostResult.Code;
                            result.AddResultDetail(hostResult.Details);
                            translations.Add(hostResult.Structure as PQR);
                        }
                        else
                        {
                            qtyFormatter.ParseElementsInline(s, retVal, result);
                        }
                    }
                    catch (MessageValidationException e)
                    {
                        result.AddResultDetail(new MARC.Everest.Connectors.ResultDetail(MARC.Everest.Connectors.ResultDetailType.Error, e.Message, s.ToString(), e));
                    }
                    finally
                    {
                        if (s.Name == oldName)
                        {
                            s.Read();
                        }
                    }
                }

                // Set translations
                if (!translations.IsEmpty)
                {
                    retVal.Translation = translations;
                }
            }
            #endregion

            // Validate
            ANYFormatter anyFormatter = new ANYFormatter();
            string       pathName     = s is XmlStateReader ? (s as XmlStateReader).CurrentPath : s.Name;
            anyFormatter.Validate(retVal as ANY, pathName, result);

            // REturn instance
            return(retVal);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Graph object <paramref name="o"/> onto <paramref name="s"/>
        /// </summary>
        /// <param name="s">The stream being graphed to</param>
        /// <param name="o">The object being graphed</param>
        /// <param name="result">The result of the graph operation</param>
        public void Graph(System.Xml.XmlWriter s, object o, DatatypeR2FormatterGraphResult result)
        {
            // Graph the QTY portions
            QTYFormatter qty = new QTYFormatter();

            qty.Host = this.Host;
            qty.Graph(s, o, result);

            // Any null flavors
            IAny anyO = o as IAny;
            IPrimitiveDataValue ipdv = o as IPrimitiveDataValue;

            if (anyO.NullFlavor != null)
            {
                return;
            }

            // PDV Value does not support value
            if (ipdv.Value != null)
            {
                result.AddResultDetail(new NotPermittedDatatypeR2EntityResultDetail(MARC.Everest.Connectors.ResultDetailType.Warning, "Value", "RTO", s.ToString()));
            }

            // Get the numerator / denominator
            PropertyInfo numeratorProperty   = o.GetType().GetProperty("Numerator"),
                         denominatorProperty = o.GetType().GetProperty("Denominator");
            object numerator   = numeratorProperty.GetValue(o, null),
                   denominator = denominatorProperty.GetValue(o, null);

            // Serialize the numerator
            if (numerator != null)
            {
                s.WriteStartElement("numerator", "urn:hl7-org:v3");

                // Write the XSI type
                string xsiTypeName = DatatypeR2Formatter.CreateXSITypeName(numerator.GetType());

                // Write the type
                if (this.Host.Host == null)
                {
                    s.WriteAttributeString("type", DatatypeR2Formatter.NS_XSI, xsiTypeName.ToString());
                }
                else
                {
                    s.WriteAttributeString("xsi", "type", DatatypeR2Formatter.NS_XSI, xsiTypeName.ToString());
                }

                var hostResult = this.Host.Graph(s, (IGraphable)numerator);
                result.Code = hostResult.Code;
                result.AddResultDetail(hostResult.Details);
                s.WriteEndElement();
            }
            // Serialize Denominator
            if (denominator != null)
            {
                s.WriteStartElement("denominator", "urn:hl7-org:v3");

                // Write the XSI type
                string xsiTypeName = DatatypeR2Formatter.CreateXSITypeName(denominator.GetType());

                // Write the type
                if (this.Host.Host == null)
                {
                    s.WriteAttributeString("type", DatatypeR2Formatter.NS_XSI, xsiTypeName.ToString());
                }
                else
                {
                    s.WriteAttributeString("xsi", "type", DatatypeR2Formatter.NS_XSI, xsiTypeName.ToString());
                }

                var hostResult = this.Host.Graph(s, (IGraphable)denominator);
                result.Code = hostResult.Code;
                result.AddResultDetail(hostResult.Details);
                s.WriteEndElement();
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Parse the RTO
        /// </summary>
        public object Parse(System.Xml.XmlReader s, DatatypeR2FormatterParseResult result)
        {
            // Create the types
            Type rtoType = typeof(RTO <,>);

            QTYFormatter baseFormatter = (new QTYFormatter()
            {
                Host = this.Host
            });
            // Quantity value, just read into a REAL
            var qty = baseFormatter.ParseAttributes <REAL>(s, result);

            // Temporary values
            IGraphable denominator = null,
                       numerator   = null;

            // Elements
            // This type is a little different in R2, basically we can't determine the generic
            // parameters by the XSI Type so we have to parse the num/denom manually, and
            // ensure that we have both, then we construct the object and set the properties
            #region Elements
            if (!s.IsEmptyElement)
            {
                int    sDepth = s.Depth;
                string sName  = s.Name;

                s.Read();
                // string Name
                while (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName))
                {
                    string oldName = s.Name; // Name
                    try
                    {
                        // Numerator
                        if (s.LocalName == "numerator")
                        {
                            var parseResult = Host.Parse(s, GenericArguments[0]);
                            result.Code = parseResult.Code;
                            result.AddResultDetail(parseResult.Details);
                            numerator = parseResult.Structure;
                        }
                        // Denominator
                        else if (s.LocalName == "denominator")
                        {
                            var parseResult = Host.Parse(s, GenericArguments[1]);
                            result.Code = parseResult.Code;
                            result.AddResultDetail(parseResult.Details);
                            denominator = parseResult.Structure;
                        }
                        else
                        {
                            baseFormatter.ParseElementsInline(s, qty, result);
                        }
                    }
                    catch (MessageValidationException e)
                    {
                        result.AddResultDetail(new MARC.Everest.Connectors.ResultDetail(MARC.Everest.Connectors.ResultDetailType.Error, e.Message, e));
                    }
                    finally
                    {
                        if (s.Name == oldName)
                        {
                            s.Read();
                        }
                    }
                }
            }
            #endregion

            try
            {
                // Construct the generic type (if possible)
                Type rtoGenericType = rtoType.MakeGenericType(new Type[]
                {
                    numerator == null ? typeof(IQuantity) : numerator.GetType(),
                    denominator == null ? typeof(IQuantity) : denominator.GetType()
                });

                // Create an instance of rto from the rtoType
                object instance = rtoGenericType.GetConstructor(Type.EmptyTypes).Invoke(null);

                // Get the values from the QTY and copy
                IQuantity rtoQty = instance as IQuantity;
                DatatypeR2Formatter.CopyBaseAttributes(qty, rtoQty as ANY);
                rtoQty.Expression      = qty.Expression;
                rtoQty.OriginalText    = qty.OriginalText;
                rtoQty.UncertainRange  = qty.UncertainRange;
                rtoQty.Uncertainty     = qty.Uncertainty;
                rtoQty.UncertaintyType = qty.UncertaintyType;

                // rto properties
                PropertyInfo numeratorProperty   = rtoGenericType.GetProperty("Numerator"),
                             denominatorProperty = rtoGenericType.GetProperty("Denominator");

                // Set the previously found properties
                numeratorProperty.SetValue(instance, Util.FromWireFormat(numerator, numeratorProperty.PropertyType), null);
                denominatorProperty.SetValue(instance, Util.FromWireFormat(denominator, denominatorProperty.PropertyType), null);

                // Validate
                ANYFormatter anyFormatter = new ANYFormatter();
                string       pathName     = s is XmlStateReader ? (s as XmlStateReader).CurrentPath : s.Name;
                anyFormatter.Validate(instance as ANY, pathName, result);

                return(instance);
            }
            catch (Exception e)
            {
                result.AddResultDetail(new ResultDetail(ResultDetailType.Error, e.Message, e));
                return(null);
            }
        }