Esempio n. 1
0
        /// <summary>
        /// Graphs an object to the specified stream
        /// </summary>
        public void Graph(System.Xml.XmlWriter s, object o, MARC.Everest.Interfaces.IGraphable context, XmlIts1FormatterGraphResult resultContext)
        {
            ResultCode rc = ResultCode.Accepted;
            Type instanceType = o.GetType();

            // Verify o is not null
            if (o == null) throw new System.ArgumentNullException("o");

            // Attempt to get null flavor
            
            var nfp = o.GetType().GetProperty("NullFlavor");
            bool isInstanceNull = false, 
                isEntryPoint = false;
            if (nfp != null)
                isInstanceNull = nfp.GetValue(o, null) != null;

            // Interaction?
            object[] structureAttributes = instanceType.GetCustomAttributes(typeof(StructureAttribute), false);
            StructureAttribute structureAttribute = structureAttributes[0] as StructureAttribute;
            if (structureAttribute.StructureType == StructureAttribute.StructureAttributeType.Interaction)
            {
                isEntryPoint = true;
                s.WriteStartElement(structureAttribute.Name, structureAttribute.NamespaceUri);
                s.WriteAttributeString("ITSVersion","XML_1.0"); // Add ITS version
                s.WriteAttributeString("xmlns", "xsi", null, XmlIts1Formatter.NS_XSI);
            }
            else if (structureAttribute.IsEntryPoint && (s is MARC.Everest.Xml.XmlStateWriter && (s as MARC.Everest.Xml.XmlStateWriter).ElementStack.Count == 0 || s.WriteState == System.Xml.WriteState.Start))
            {
                isEntryPoint = true;
                if (isEntryPoint)
                {
                    s.WriteStartElement(structureAttribute.Name, structureAttribute.NamespaceUri);
                    s.WriteAttributeString("xmlns", "xsi", null, XmlIts1Formatter.NS_XSI);
                }
            }
                
            // Validate
            this.Host.ValidateHelper(s, o as IGraphable, this, resultContext);

            // Reflect the properties and ensure they are in the appropriate order
            List<PropertyInfo> buildProperties = GetBuildProperties(instanceType);
            
            // Attributes first
            foreach (var pi in buildProperties)
            {

                object[] propertyAttributes = pi.GetCustomAttributes(typeof(PropertyAttribute), false);
                object instance = pi.GetValue(o, null);

                if (propertyAttributes.Length == 1) // Not a choice
                {
                    PropertyAttribute pa = propertyAttributes[0] as PropertyAttribute;

                    // Validation Rule Change: We'll require the user to perform this
                    // Is this a required attribute that is null? We'll set a null flavor 
                    if ((pa.Conformance == PropertyAttribute.AttributeConformanceType.Required || pa.Conformance == PropertyAttribute.AttributeConformanceType.Populated) &&
                        pa.PropertyType != PropertyAttribute.AttributeAttributeType.Structural &&
                        pi.PropertyType.GetProperty("NullFlavor") != null &&
                        !pi.PropertyType.IsAbstract &&
                        pi.CanWrite)
                    {
                        var nullFlavorProperty = pi.PropertyType.GetProperty("NullFlavor");
                        // Locate the default property 
                        if (instance == null && Host.CreateRequiredElements && nullFlavorProperty != null)
                        {
                            ConstructorInfo ci = pi.PropertyType.GetConstructor(Type.EmptyTypes);
                            instance = ci.Invoke(null);
                            nullFlavorProperty.SetValue(instance, Util.FromWireFormat("NI", nullFlavorProperty.PropertyType), null);
                        }
                    }


                    // Property type
                    switch (pa.PropertyType)
                    {
                        case PropertyAttribute.AttributeAttributeType.Structural:
                            if ((Host.Settings & SettingsType.SuppressNullEnforcement) == 0)
                            {
                                if (instance != null && !isInstanceNull)
                                    s.WriteAttributeString(pa.Name, Util.ToWireFormat(instance));
                                else if (isInstanceNull && pi.Name == "NullFlavor")
                                    Host.WriteNullFlavorUtil(s, (IGraphable)instance);
                            }
                            else if (instance != null)
                            {
                                if (instance != null && pi.Name == "NullFlavor")
                                    Host.WriteNullFlavorUtil(s, (IGraphable)instance);
                                else if (instance != null)
                                    s.WriteAttributeString(pa.Name, Util.ToWireFormat(instance));
                            }

                            break;
                        default:

                            // Instance is null
                            if (instance == null)
                                continue;
                            else if (isInstanceNull && (Host.Settings & (SettingsType.SuppressNullEnforcement | SettingsType.SuppressXsiNil)) != 0)
                                resultContext.AddResultDetail(new FormalConstraintViolationResultDetail(ResultDetailType.Information, "The context is null however SuppressNullEnforcement and SuppressXsiNil are set, therefore elements will be graphed. This is not necessarily HL7v3 compliant", s.ToString(), null));
                            else if (isInstanceNull)
                                continue;

                            // Impose flavors or code?
                            if (pa.DefaultUpdateMode != MARC.Everest.DataTypes.UpdateMode.Unknown &&
                                pi.PropertyType.GetProperty("UpdateMode") != null &&
                                pi.PropertyType.GetProperty("UpdateMode").GetValue(instance, null) == null &&
                                (this.Host.Settings & SettingsType.AllowUpdateModeImposing) == SettingsType.AllowUpdateModeImposing)
                                pi.PropertyType.GetProperty("UpdateMode").SetValue(instance, Util.FromWireFormat(pa.DefaultUpdateMode, pi.PropertyType.GetProperty("UpdateMode").PropertyType), null);
                            if (pa.ImposeFlavorId != null &&
                                instance is IAny &&
                                (Host.Settings & SettingsType.AllowFlavorImposing) == SettingsType.AllowFlavorImposing)
                                (instance as IAny).Flavor = pa.ImposeFlavorId;
                            if (pa.SupplierDomain != null &&
                                instance is ICodedValue &&
                                (instance as ICodedSimple).CodeValue != null &&
                                (instance as ICodedValue).CodeSystem == null &&
                                (instance as IImplementsNullFlavor).NullFlavor == null &&
                                (Host.Settings & SettingsType.AllowSupplierDomainImposing) == SettingsType.AllowSupplierDomainImposing)
                                (instance as ICodedValue).CodeSystem = pa.SupplierDomain;

                            // Instance is graphable
                            if (instance is IGraphable)
                            {
                                // Ensure the data is not empty
                                if (instance is IColl && (instance as IColl).IsEmpty && (instance as IImplementsNullFlavor).NullFlavor == null)
                                    continue;
                                Host.WriteElementUtil(s, pa.NamespaceUri, pa.Name, instance as IGraphable, pi.PropertyType, context, resultContext);
                            }
                            else if (instance is ICollection)
                            {
                                Type genType = pi.PropertyType.GetGenericArguments()[0];
                                foreach (object itm in (instance as ICollection))
                                    Host.WriteElementUtil(s, pa.NamespaceUri, pa.Name, itm as IGraphable, genType, context, resultContext);
                            }
                            else
                                s.WriteElementString(pa.Name, instance.ToString());
                            break;
                    }
                }
                else if(propertyAttributes.Length > 1) // Choice
                {
                    // Instance is null
                    if (instance == null)
                        continue;
                    else if (isInstanceNull && (Host.Settings & (SettingsType.SuppressNullEnforcement | SettingsType.SuppressXsiNil)) != 0)
                        resultContext.AddResultDetail(new FormalConstraintViolationResultDetail(ResultDetailType.Information, "The context is null however SuppressNullEnforcement and SuppressXsiNil are set, therefore elements will be graphed. This is not necessarily HL7v3 compliant", s.ToString(), null));
                    else if (isInstanceNull)
                        continue;
#if WINDOWS_PHONE
                    PropertyAttribute formatAs = propertyAttributes.Find(cpa => (cpa as PropertyAttribute).Type == null) as PropertyAttribute;
#else
                    PropertyAttribute formatAs = Array.Find(propertyAttributes, cpa => (cpa as PropertyAttribute).Type == null) as PropertyAttribute;
#endif
                    // Search by type and interaction
                    foreach (PropertyAttribute pa in propertyAttributes)
                    {
                        if (pa.Type != null && instance.GetType() == pa.Type && (context != null && context.GetType() == pa.InteractionOwner || (pa.InteractionOwner == null && formatAs == null)))
                        {
                            formatAs = pa;
                            if(context == null || context.GetType() == formatAs.InteractionOwner) 
                                break;
                        }
                    }

                    // Slow check
                    if (formatAs == null && (this.Host.Settings & SettingsType.AlwaysCheckForOverrides) != 0)
                    {
                        foreach (PropertyAttribute pa in propertyAttributes)
                        {
                            if (pa.Type != null && pa.Type.IsAssignableFrom(instance.GetType()) && (context != null && context.GetType() == pa.InteractionOwner || (pa.InteractionOwner == null && formatAs == null)))
                            {
                                formatAs = pa;
                                if (context == null || context.GetType() == formatAs.InteractionOwner)
                                    break;
                            }
                        }
                    }

                    //if(formatAs == null) // try to find a regular choice
                    //    foreach(PropertyAttribute pa in propertyAttributes)
                    //        if (pa.Type != null && instance.GetType() == pa.Type)
                    //        {
                    //            formatAs = pa;
                    //            break;
                    //        }


                    // Format
                    if (formatAs == null)
                        resultContext.AddResultDetail(new NotSupportedChoiceResultDetail(ResultDetailType.Error, String.Format("Type {0} is not a valid choice according to available choice elements and won't be formatted", instance.GetType()), s.ToString(), null));
                    else if (instance.GetType().GetInterface("MARC.Everest.Interfaces.IGraphable", false) != null) // Non Graphable
                        Host.WriteElementUtil(s, formatAs.NamespaceUri, formatAs.Name, (MARC.Everest.Interfaces.IGraphable)instance, formatAs.Type, context, resultContext);
                    else if (instance.GetType().GetInterface("System.Collections.IEnumerable", false) != null) // List
                        foreach (MARC.Everest.Interfaces.IGraphable ig in instance as IEnumerable) { Host.WriteElementUtil(s, formatAs.NamespaceUri, formatAs.Name, ig, instance.GetType(), context, resultContext); }
                    else // Not recognized
                        s.WriteElementString(formatAs.Name, formatAs.NamespaceUri, instance.ToString());

                }
            }

            // Is Entry point
            if (isEntryPoint)
                s.WriteEndElement();

        }
        /// <summary>
        /// Graphs an object to the specified stream
        /// </summary>
        public void Graph(System.Xml.XmlWriter s, object o, MARC.Everest.Interfaces.IGraphable context, XmlIts1FormatterGraphResult resultContext)
        {
            ResultCode rc           = ResultCode.Accepted;
            Type       instanceType = o.GetType();

            // Verify o is not null
            if (o == null)
            {
                throw new System.ArgumentNullException("o");
            }

            // Attempt to get null flavor

            var  nfp            = o.GetType().GetProperty("NullFlavor");
            bool isInstanceNull = false,
                 isEntryPoint   = false;

            if (nfp != null)
            {
                isInstanceNull = nfp.GetValue(o, null) != null;
            }

            // Interaction?
            object[]           structureAttributes = instanceType.GetCustomAttributes(typeof(StructureAttribute), false);
            StructureAttribute structureAttribute  = structureAttributes[0] as StructureAttribute;

            if (structureAttribute.StructureType == StructureAttribute.StructureAttributeType.Interaction)
            {
                isEntryPoint = true;
                s.WriteStartElement(structureAttribute.Name, structureAttribute.NamespaceUri);
                s.WriteAttributeString("ITSVersion", "XML_1.0"); // Add ITS version
                s.WriteAttributeString("xmlns", "xsi", null, XmlIts1Formatter.NS_XSI);
            }
            else if (structureAttribute.IsEntryPoint && (s is MARC.Everest.Xml.XmlStateWriter && (s as MARC.Everest.Xml.XmlStateWriter).ElementStack.Count == 0 || s.WriteState == System.Xml.WriteState.Start))
            {
                isEntryPoint = true;
                if (isEntryPoint)
                {
                    s.WriteStartElement(structureAttribute.Name, structureAttribute.NamespaceUri);
                    s.WriteAttributeString("xmlns", "xsi", null, XmlIts1Formatter.NS_XSI);
                }
            }

            // Validate
            this.Host.ValidateHelper(s, o as IGraphable, this, resultContext);

            // Reflect the properties and ensure they are in the appropriate order
            List <PropertyInfo> buildProperties = GetBuildProperties(instanceType);

            // Attributes first
            foreach (var pi in buildProperties)
            {
                object[] propertyAttributes = pi.GetCustomAttributes(typeof(PropertyAttribute), false);
                object   instance           = pi.GetValue(o, null);

                if (propertyAttributes.Length == 1) // Not a choice
                {
                    PropertyAttribute pa = propertyAttributes[0] as PropertyAttribute;

                    // Validation Rule Change: We'll require the user to perform this
                    // Is this a required attribute that is null? We'll set a null flavor
                    if ((pa.Conformance == PropertyAttribute.AttributeConformanceType.Required || pa.Conformance == PropertyAttribute.AttributeConformanceType.Populated) &&
                        pa.PropertyType != PropertyAttribute.AttributeAttributeType.Structural &&
                        pi.PropertyType.GetProperty("NullFlavor") != null &&
                        !pi.PropertyType.IsAbstract &&
                        pi.CanWrite)
                    {
                        var nullFlavorProperty = pi.PropertyType.GetProperty("NullFlavor");
                        // Locate the default property
                        if (instance == null && Host.CreateRequiredElements && nullFlavorProperty != null)
                        {
                            ConstructorInfo ci = pi.PropertyType.GetConstructor(Type.EmptyTypes);
                            instance = ci.Invoke(null);
                            nullFlavorProperty.SetValue(instance, Util.FromWireFormat("NI", nullFlavorProperty.PropertyType), null);
                        }
                    }


                    // Property type
                    switch (pa.PropertyType)
                    {
                    case PropertyAttribute.AttributeAttributeType.Structural:
                        if ((Host.Settings & SettingsType.SuppressNullEnforcement) == 0)
                        {
                            if (instance != null && !isInstanceNull)
                            {
                                s.WriteAttributeString(pa.Name, Util.ToWireFormat(instance));
                            }
                            else if (isInstanceNull && pi.Name == "NullFlavor")
                            {
                                Host.WriteNullFlavorUtil(s, (IGraphable)instance);
                            }
                        }
                        else if (instance != null)
                        {
                            if (instance != null && pi.Name == "NullFlavor")
                            {
                                Host.WriteNullFlavorUtil(s, (IGraphable)instance);
                            }
                            else if (instance != null)
                            {
                                s.WriteAttributeString(pa.Name, Util.ToWireFormat(instance));
                            }
                        }

                        break;

                    default:

                        // Instance is null
                        if (instance == null)
                        {
                            continue;
                        }
                        else if (isInstanceNull && (Host.Settings & (SettingsType.SuppressNullEnforcement | SettingsType.SuppressXsiNil)) != 0)
                        {
                            resultContext.AddResultDetail(new FormalConstraintViolationResultDetail(ResultDetailType.Information, "The context is null however SuppressNullEnforcement and SuppressXsiNil are set, therefore elements will be graphed. This is not necessarily HL7v3 compliant", s.ToString(), null));
                        }
                        else if (isInstanceNull)
                        {
                            continue;
                        }

                        // Impose flavors or code?
                        if (pa.DefaultUpdateMode != MARC.Everest.DataTypes.UpdateMode.Unknown &&
                            pi.PropertyType.GetProperty("UpdateMode") != null &&
                            pi.PropertyType.GetProperty("UpdateMode").GetValue(instance, null) == null &&
                            (this.Host.Settings & SettingsType.AllowUpdateModeImposing) == SettingsType.AllowUpdateModeImposing)
                        {
                            pi.PropertyType.GetProperty("UpdateMode").SetValue(instance, Util.FromWireFormat(pa.DefaultUpdateMode, pi.PropertyType.GetProperty("UpdateMode").PropertyType), null);
                        }
                        if (pa.ImposeFlavorId != null &&
                            instance is IAny &&
                            (Host.Settings & SettingsType.AllowFlavorImposing) == SettingsType.AllowFlavorImposing)
                        {
                            (instance as IAny).Flavor = pa.ImposeFlavorId;
                        }
                        if (pa.SupplierDomain != null &&
                            instance is ICodedValue &&
                            (instance as ICodedSimple).CodeValue != null &&
                            (instance as ICodedValue).CodeSystem == null &&
                            (instance as IImplementsNullFlavor).NullFlavor == null &&
                            (Host.Settings & SettingsType.AllowSupplierDomainImposing) == SettingsType.AllowSupplierDomainImposing)
                        {
                            (instance as ICodedValue).CodeSystem = pa.SupplierDomain;
                        }

                        // Instance is graphable
                        if (instance is IGraphable)
                        {
                            // Ensure the data is not empty
                            if (instance is IColl && (instance as IColl).IsEmpty && (instance as IImplementsNullFlavor).NullFlavor == null)
                            {
                                continue;
                            }
                            Host.WriteElementUtil(s, pa.NamespaceUri, pa.Name, instance as IGraphable, pi.PropertyType, context, resultContext);
                        }
                        else if (instance is ICollection)
                        {
                            Type genType = pi.PropertyType.GetGenericArguments()[0];
                            foreach (object itm in (instance as ICollection))
                            {
                                Host.WriteElementUtil(s, pa.NamespaceUri, pa.Name, itm as IGraphable, genType, context, resultContext);
                            }
                        }
                        else
                        {
                            s.WriteElementString(pa.Name, instance.ToString());
                        }
                        break;
                    }
                }
                else if (propertyAttributes.Length > 1) // Choice
                {
                    // Instance is null
                    if (instance == null)
                    {
                        continue;
                    }
                    else if (isInstanceNull && (Host.Settings & (SettingsType.SuppressNullEnforcement | SettingsType.SuppressXsiNil)) != 0)
                    {
                        resultContext.AddResultDetail(new FormalConstraintViolationResultDetail(ResultDetailType.Information, "The context is null however SuppressNullEnforcement and SuppressXsiNil are set, therefore elements will be graphed. This is not necessarily HL7v3 compliant", s.ToString(), null));
                    }
                    else if (isInstanceNull)
                    {
                        continue;
                    }
#if WINDOWS_PHONE
                    PropertyAttribute formatAs = propertyAttributes.Find(cpa => (cpa as PropertyAttribute).Type == null) as PropertyAttribute;
#else
                    PropertyAttribute formatAs = Array.Find(propertyAttributes, cpa => (cpa as PropertyAttribute).Type == null) as PropertyAttribute;
#endif
                    // Search by type and interaction
                    foreach (PropertyAttribute pa in propertyAttributes)
                    {
                        if (pa.Type != null && instance.GetType() == pa.Type && (context != null && context.GetType() == pa.InteractionOwner || (pa.InteractionOwner == null && formatAs == null)))
                        {
                            formatAs = pa;
                            if (context == null || context.GetType() == formatAs.InteractionOwner)
                            {
                                break;
                            }
                        }
                    }

                    // Slow check
                    if (formatAs == null && (this.Host.Settings & SettingsType.AlwaysCheckForOverrides) != 0)
                    {
                        foreach (PropertyAttribute pa in propertyAttributes)
                        {
                            if (pa.Type != null && pa.Type.IsAssignableFrom(instance.GetType()) && (context != null && context.GetType() == pa.InteractionOwner || (pa.InteractionOwner == null && formatAs == null)))
                            {
                                formatAs = pa;
                                if (context == null || context.GetType() == formatAs.InteractionOwner)
                                {
                                    break;
                                }
                            }
                        }
                    }

                    //if(formatAs == null) // try to find a regular choice
                    //    foreach(PropertyAttribute pa in propertyAttributes)
                    //        if (pa.Type != null && instance.GetType() == pa.Type)
                    //        {
                    //            formatAs = pa;
                    //            break;
                    //        }


                    // Format
                    if (formatAs == null)
                    {
                        resultContext.AddResultDetail(new NotSupportedChoiceResultDetail(ResultDetailType.Error, String.Format("Type {0} is not a valid choice according to available choice elements and won't be formatted", instance.GetType()), s.ToString(), null));
                    }
                    else if (instance.GetType().GetInterface("MARC.Everest.Interfaces.IGraphable", false) != null) // Non Graphable
                    {
                        Host.WriteElementUtil(s, formatAs.NamespaceUri, formatAs.Name, (MARC.Everest.Interfaces.IGraphable)instance, formatAs.Type, context, resultContext);
                    }
                    else if (instance.GetType().GetInterface("System.Collections.IEnumerable", false) != null) // List
                    {
                        foreach (MARC.Everest.Interfaces.IGraphable ig in instance as IEnumerable)
                        {
                            Host.WriteElementUtil(s, formatAs.NamespaceUri, formatAs.Name, ig, instance.GetType(), context, resultContext);
                        }
                    }
                    else // Not recognized
                    {
                        s.WriteElementString(formatAs.Name, formatAs.NamespaceUri, instance.ToString());
                    }
                }
            }

            // Is Entry point
            if (isEntryPoint)
            {
                s.WriteEndElement();
            }
        }