/// <summary> /// Validates the specified object /// </summary> public bool Validate(MARC.Everest.Interfaces.IGraphable o, string location, out MARC.Everest.Connectors.IResultDetail[] details) { List <IResultDetail> dtls = new List <IResultDetail>(10); bool isValid = true; // Null return bool if (o == null) { details = dtls.ToArray(); return(true); } PropertyInfo nullFlavorAttrib = o.GetType().GetProperty("NullFlavor"); if (nullFlavorAttrib != null && nullFlavorAttrib.GetValue(o, null) != null) { details = dtls.ToArray(); return(true); } // Scan property info for violations foreach (var pi in this.GetBuildProperties(o.GetType())) { if (pi.GetGetMethod().GetParameters().Length != 0) { dtls.Add(new NotImplementedResultDetail( ResultDetailType.Warning, String.Format("Could not validate '{0}' as the property is indexed", pi.Name), location )); continue; } object piValue = pi.GetValue(o, null); object[] propAttributes = pi.GetCustomAttributes(typeof(PropertyAttribute), true); if (propAttributes.Length > 0) { PropertyAttribute pa = propAttributes[0] as PropertyAttribute; if (pa.Conformance == PropertyAttribute.AttributeConformanceType.Mandatory && pi.PropertyType.GetInterface(typeof(IImplementsNullFlavor).FullName, false) != null && (piValue == null || (piValue as IImplementsNullFlavor).NullFlavor != null)) { isValid = false; dtls.Add(new MandatoryElementMissingResultDetail(ResultDetailType.Error, String.Format("Property {0} in {1} is marked mandatory and is either not assigned, or is assigned a null flavor. This is not permitted.", pi.Name, o.GetType().FullName), location)); } else if (pa.Conformance == PropertyAttribute.AttributeConformanceType.Populated && piValue == null) { isValid &= Host.CreateRequiredElements; dtls.Add(new RequiredElementMissingResultDetail(isValid ? ResultDetailType.Warning : ResultDetailType.Error, String.Format("Property {0} in {1} is marked 'populated' and isn't assigned (you must at minimum, assign a nullFlavor for this attribute)!", pi.Name, o.GetType().FullName), location)); } else if (pa.MinOccurs != 0) { int minOccurs = pa.MinOccurs, maxOccurs = pa.MaxOccurs < 0 ? Int32.MaxValue : pa.MaxOccurs; var piCollection = piValue as ICollection; if (piCollection != null && (piCollection.Count > maxOccurs || piCollection.Count < minOccurs)) { isValid = false; dtls.Add(new InsufficientRepetitionsResultDetail(ResultDetailType.Error, String.Format("Property {0} in {2} does not have enough elements in the list, need between {1} and {3} elements!", pi.Name, minOccurs, o.GetType().FullName, maxOccurs), location)); } } } } details = dtls.ToArray(); return(isValid); }
/// <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(); } }