private void RenderRealmCodes(PartBridge tealBean) { if (tealBean.GetRealmCode() != null) { string type = "CS"; PropertyFormatter formatter = this.formatterRegistry.Get(type); Relationship placeholderRelationship = new Relationship("realmCode", type, Cardinality.Create("0-*")); placeholderRelationship.DomainType = "Realm"; FormatContext context = Ca.Infoway.Messagebuilder.Marshalling.FormatContextImpl.Create(this.result, null, placeholderRelationship , version, null, null, null, this.isCda); foreach (Realm realm in tealBean.GetRealmCode()) { BareANY any = (BareANY)DataTypeFactory.CreateDataType(type, this.isCda && this.isR2); if (this.isR2) { ((BareANYImpl)any).BareValue = new CodedTypeR2 <Realm>(realm); } else { ((BareANYImpl)any).BareValue = realm; } string xmlFragment = formatter.Format(context, any, GetIndent()); CurrentBuffer().GetChildBuilder().Append(xmlFragment); } } }
protected virtual string CreateElement(FormatContext context, string name, QTY <T> value, Boolean?inclusive, bool isSxcm, int indentLevel) { string type = Hl7DataTypeName.GetParameterizedType(context.Type); if (isSxcm) { type = "SXCM<" + type + ">"; } PropertyFormatter formatter = FormatterR2Registry.GetInstance().Get(type); if (formatter != null) { bool isSpecializationType = false; FormatContext newContext = new Ca.Infoway.Messagebuilder.Marshalling.HL7.Formatter.FormatContextImpl(type, isSpecializationType , Ca.Infoway.Messagebuilder.Xml.ConformanceLevel.POPULATED, Cardinality.Create("1"), name, context); string result = formatter.Format(newContext, value, indentLevel); if (inclusive != null) { // TM - small hack to add in the inclusive attribute (low/high) (operator, simple only, is already formatted by using the SXCM type) result = result.ReplaceFirst(" value=", " inclusive=\"" + inclusive.ToString().ToLower() + "\" value="); } return(result); } else { throw new ModelToXmlTransformationException("No formatter found for " + type); } }
protected virtual string FormatAllElements(FormatContext originalContext, FormatContext subContext, ICollection <BareANY> collection, int indentLevel) { StringBuilder builder = new StringBuilder(); ValidateCardinality(originalContext, collection); PropertyFormatter formatter = this.formatterRegistry.Get(subContext.Type); if (collection.IsEmpty()) { builder.Append(formatter.Format(subContext, null, indentLevel)); } else { foreach (BareANY hl7Value in EmptyIterable <object> .NullSafeIterable <BareANY>(collection)) { string type = DetermineActualType(subContext.Type, hl7Value, originalContext.GetModelToXmlResult(), originalContext.GetPropertyPath (), originalContext.IsCda()); if (!StringUtils.Equals(type, subContext.Type)) { subContext = new Ca.Infoway.Messagebuilder.Marshalling.HL7.Formatter.FormatContextImpl(type, true, subContext); formatter = this.formatterRegistry.Get(type); } builder.Append(formatter.Format(subContext, hl7Value, indentLevel)); } } return(builder.ToString()); }
public void ShouldParseQwFormattedName() { var value = "SOME_VALUE"; var expected = "SomeValue"; var formatter = new PropertyFormatter(); formatter.Format(value).ShouldBe(expected); }
public void ShouldParseQwDates() { var value = "REVERSE_DTG"; var expected = "ReverseDate"; var formatter = new PropertyFormatter(); formatter.Format(value).ShouldBe(expected); }
public void ShouldFormatOnePropertyIntoAString() { var formatter = new PropertyFormatter<Person>("Hello {0}!", x => x.Name); var formattedString = formatter.Format(defaultPerson); Assert.That(formattedString, Is.EqualTo("Hello Henry!")); }
public void AddDefault <TProperty>(Func <TProperty, string> formatter) { PropertyFormatter pf = context => formatter((TProperty)context.ItemValue); //DefaultTypeFormatters.Add(inputType, pf); _globalDefaultFormatters.Add(typeof(TProperty), pf); }
public void ShouldFormatMyHtmlStringWithProperties() { var htmlFormatter = new PropertyFormatter<Person>("<div class=\"name\">{0}</div><div class=\"age\">{1}</div>", x => x.Name, x => x.Age.ToString(CultureInfo.InvariantCulture)); var formattedHtml = htmlFormatter.Format(defaultPerson); Assert.That(formattedHtml, Is.EqualTo("<div class=\"name\">Henry</div><div class=\"age\">23</div>")); }
public void ShouldFormatMultiplePropertiesIntoAString() { var formatter = new PropertyFormatter<Person>("Hello {0}, You are {1} this year!", x => x.Name, x => x.Age.ToString(CultureInfo.InvariantCulture)); var formattedString = formatter.Format(defaultPerson); Assert.That(formattedString, Is.EqualTo("Hello Henry, You are 23 this year!")); }
private void ConfigureRestOfProperties(Type objectType, FileExportSpecification fileExportSpecification) { // fallback formatter for anything that doesn't fit int he custom, or "global default" formatters. var globalDefaultFormatter = new PropertyFormatter(context => { if (context.ItemValue == null) { return string.Empty; } return context.ItemValue.ToString(); }); var properties = objectType.GetProperties(); for (int i = 0; i < properties.Length; i++) { var propertyInfo = properties[i]; var propertyName = propertyInfo.Name; TypeConfiguration props = fileExportSpecification.GetTypeConfiguration(objectType); if (!props.IsPropertyExcluded(propertyName)) { if (props.IsPropertyDefined(propertyName)) { props.Properties[propertyName].Order = i; continue; } var propertyType = propertyInfo.PropertyType; PropertyFormatter defaultPropertyFormatter = globalDefaultFormatter; if (DefaultPropertyFormatters.ContainsKey(propertyType)) { defaultPropertyFormatter = DefaultPropertyFormatters[propertyType]; } if (fileExportSpecification.DefaultPropertyFormatters.ContainsKey(propertyType)) { defaultPropertyFormatter = fileExportSpecification.DefaultPropertyFormatters[propertyType]; } // If there's a default if (props.DefaultTypeFormatters.ContainsKey(propertyType)) { defaultPropertyFormatter = props.DefaultTypeFormatters[propertyType]; } var property = new Property(objectType, propertyName, defaultPropertyFormatter, fileExportSpecification.DefaultHeaderFormatter, i); props.AddProperty(property); } } }
private void ConfigureRestOfProperties(Type objectType, FileExportSpecification fileExportSpecification) { // fallback formatter for anything that doesn't fit int he custom, or "global default" formatters. var globalDefaultFormatter = new PropertyFormatter(context => { if (context.ItemValue == null) { return(string.Empty); } return(context.ItemValue.ToString()); }); var properties = objectType.GetProperties(); for (int i = 0; i < properties.Length; i++) { var propertyInfo = properties[i]; var propertyName = propertyInfo.Name; TypeConfiguration props = fileExportSpecification.GetTypeConfiguration(objectType); if (!props.IsPropertyExcluded(propertyName)) { if (props.IsPropertyDefined(propertyName)) { props.Properties[propertyName].Order = i; continue; } var propertyType = propertyInfo.PropertyType; PropertyFormatter defaultPropertyFormatter = globalDefaultFormatter; if (DefaultPropertyFormatters.ContainsKey(propertyType)) { defaultPropertyFormatter = DefaultPropertyFormatters[propertyType]; } if (fileExportSpecification.DefaultPropertyFormatters.ContainsKey(propertyType)) { defaultPropertyFormatter = fileExportSpecification.DefaultPropertyFormatters[propertyType]; } // If there's a default if (props.DefaultTypeFormatters.ContainsKey(propertyType)) { defaultPropertyFormatter = props.DefaultTypeFormatters[propertyType]; } var property = new Property(objectType, propertyName, defaultPropertyFormatter, fileExportSpecification.DefaultHeaderFormatter, i); props.AddProperty(property); } } }
public Property(Type objectType, string propertyName, PropertyFormatter formatter, HeaderFormatter headerFormatter, int order = 0) { if (propertyName == null) throw new ArgumentNullException("propertyName"); if (formatter == null) throw new ArgumentNullException("formatter"); if (headerFormatter == null) throw new ArgumentNullException("headerFormatter"); PropertyName = propertyName; _propertyReader = new PropertyReader(objectType, propertyName); HeaderFormatter = headerFormatter; ValueFormatter = formatter; Order = order; }
protected virtual string CreateElement(FormatContext context, string name, QTY <T> value, int indentLevel) { string type = Hl7DataTypeName.GetParameterizedType(context.Type); PropertyFormatter formatter = FormatterRegistry.GetInstance().Get(type); if (formatter != null) { bool isSpecializationType = false; return(formatter.Format(new Ca.Infoway.Messagebuilder.Marshalling.HL7.Formatter.FormatContextImpl(type, isSpecializationType , Ca.Infoway.Messagebuilder.Xml.ConformanceLevel.POPULATED, Cardinality.Create("1"), name, context), value, indentLevel)); } else { throw new ModelToXmlTransformationException("No formatter found for " + type); } }
protected override void EmitBatch(IEnumerable <LogEvent> events) { DB = Realm.GetInstance(Config); foreach (var _event in events) { var(Category, Application, Identity, ClassType, MethodName, ActivityId, ProcessId, ThreadId, Stacktrace, Properties) = Helper.ExtractEventInfo(_event); try { using (var properties = new StringWriter()) using (var messageText = new StringWriter()) { PropertyFormatter.Format(Properties, properties); MessageFormatter.Format(_event, messageText); var logMsg = new LogDBMessage { Timestamp = _event.Timestamp, Message = messageText.ToString(), ActivityId = ActivityId, MethodName = MethodName, ClassType = ClassType, Properties = properties.ToString(), ProcessId = ProcessId, ThreadId = ThreadId, Stacktrace = Stacktrace, MessageCategory = (short)Category, MessageType = (int)_event.Level.ToMessageType(), Application = Application, Identity = Identity, }; DB.Write(() => { DB.Add(logMsg); }); } } catch (Exception) { } } }
public virtual void TestAllFormattersWithNullValue() { IDictionary <string, PropertyFormatter> formatters = FormatterR2Registry.GetInstance().GetProtectedRegistryMap(); foreach (string key in formatters.Keys) { PropertyFormatter formatter = formatters.SafeGet(key); FormatContext context = GetContext("name", key); try { formatter.Format(context, null); formatter.Format(context, null, 0); } catch (Exception e) { Assert.Fail(key + " (" + formatter.GetType().Name + "): formatter failed when given a null value: " + e.Message); } } }
public Property(Type objectType, string propertyName, PropertyFormatter formatter, HeaderFormatter headerFormatter, int order = 0) { if (propertyName == null) { throw new ArgumentNullException("propertyName"); } if (formatter == null) { throw new ArgumentNullException("formatter"); } if (headerFormatter == null) { throw new ArgumentNullException("headerFormatter"); } PropertyName = propertyName; _propertyReader = new PropertyReader(objectType, propertyName); HeaderFormatter = headerFormatter; ValueFormatter = formatter; Order = order; }
public override string Format(FormatContext context, BareANY hl7Value, int indentLevel) { if (hl7Value == null) { return(string.Empty); } StandardDataType specializationType = hl7Value.DataType; bool valueOmitted = hl7Value.HasNullFlavor() && hl7Value.BareValue == null; ValidateSpecializationType(specializationType, valueOmitted, context); PropertyFormatter formatter = fullDateTimeFormatter; string formatterSpecializationType = StandardDataType.TS_FULLDATETIME.Type; if (StandardDataType.TS_FULLDATE == specializationType) { formatter = fullDateFormatter; formatterSpecializationType = StandardDataType.TS_FULLDATE.Type; } return(formatter.Format(new Ca.Infoway.Messagebuilder.Marshalling.HL7.Formatter.FormatContextImpl(formatterSpecializationType , true, context), hl7Value, indentLevel)); }
protected virtual string CreateWidthElement(FormatContext context, string name, BareDiff diff, int indentLevel) { if (IsTimestamp(context)) { return(CreateTimestampWidthElement(context, name, diff, indentLevel)); } else { string type = Hl7DataTypeName.GetParameterizedType(context.Type); PropertyFormatter formatter = FormatterRegistry.GetInstance().Get(type); if (formatter != null) { bool isSpecializationType = false; return(formatter.Format(new Ca.Infoway.Messagebuilder.Marshalling.HL7.Formatter.FormatContextImpl(type, isSpecializationType , Ca.Infoway.Messagebuilder.Xml.ConformanceLevel.MANDATORY, Cardinality.Create("1"), name, context), WrapWithHl7DataType (type, diff), indentLevel)); } else { throw new ModelToXmlTransformationException("No formatter found for " + type); } } }
public override string Format(FormatContext formatContext, BareANY hl7Value, int indentLevel) { if (hl7Value == null) { return(string.Empty); } string specializationType = hl7Value.DataType.Type; StandardDataType specializationTypeAsEnum = StandardDataType.GetByTypeName(specializationType); if (specializationTypeAsEnum != null && StandardDataType.ANY.Equals(specializationTypeAsEnum.RootDataType)) { // specializationType has been determined to be an ANY variant; this (most likely) means specializationType has not been specified, so don't do any special processing return(base.Format(formatContext, hl7Value, indentLevel)); } else { string mappedSpecializationType = this.polymorphismHandler.MapCdaR1Type(hl7Value.DataType, formatContext.IsCda()); PropertyFormatter formatter = FormatterRegistry.GetInstance().Get(mappedSpecializationType); string parentType = formatContext.Type; if (formatter == null || !AnyHelper.IsValidTypeForAny(parentType, specializationType)) { string errorText = "Cannot support properties of type " + specializationType + " for " + parentType + ". Please specify a specializationType applicable for " + parentType + " in the appropriate message bean."; throw new ModelToXmlTransformationException(errorText); } else { // pass processing off to the formatter applicable for the given specializationType StandardDataType type = hl7Value.DataType; return(formatter.Format(new Ca.Infoway.Messagebuilder.Marshalling.HL7.Formatter.FormatContextImpl(formatContext.GetModelToXmlResult (), formatContext.GetPropertyPath(), formatContext.GetElementName(), mappedSpecializationType, type.Coded ? "Code" : formatContext .GetDomainType(), formatContext.GetConformanceLevel(), formatContext.GetCardinality(), true, formatContext.GetVersion(), formatContext.GetDateTimeZone(), formatContext.GetDateTimeTimeZone(), null, formatContext.GetConstraints(), formatContext .IsCda()), hl7Value, indentLevel)); } } }
protected override string FormatNonNullDataType(FormatContext context, BareANY hl7Value, int indentLevel) { string actualType = hl7Value.DataType.Type; StandardDataType actualTypeAsEnum = StandardDataType.GetByTypeName(actualType); string result = null; if (actualTypeAsEnum == null) { RecordError("Could not determine type for ANY relationship." + (actualType == null ? "'" : actualType + "' not found in StandardDataType enum." ), context); } else { if (StandardDataType.ANY.Equals(actualTypeAsEnum.RootDataType)) { // actual type has been determined to be ANY; this (most likely) means a concrete type has not been specified RecordError("A concrete data type must be specified for a relationship of type ANY.", context); } else { PropertyFormatter formatter = FormatterR2Registry.GetInstance().Get(actualType); if (formatter == null) { string errorText = "Cannot support properties of type " + actualType + " for relationship of type ANY. Please specify a type applicable for ANY in the appropriate message bean."; RecordError(errorText, context); } else { // adjust context and pass processing off to the formatter applicable for the specified type result = formatter.Format(new Ca.Infoway.Messagebuilder.Marshalling.HL7.Formatter.FormatContextImpl(actualType, true, context ), hl7Value, indentLevel); } } } return(result); }
public void AddDefault(Type inputType, PropertyFormatter formatter) { DefaultTypeFormatters.Add(inputType, formatter); }
public void ShouldThrowAnErrorWhenThereIsACountMismatch() { var formatter = new PropertyFormatter<Person>("{0} {1} {2}", x => x.Name); formatter.Format(defaultPerson); }
private void RenderNonStructuralAttribute(AttributeBridge tealBean, Relationship relationship, ConstrainedDatatype constraints , TimeZoneInfo dateTimeZone, TimeZoneInfo dateTimeTimeZone) { string propertyPath = BuildPropertyPath(); BareANY hl7Value = tealBean.GetHl7Value(); string type = DetermineActualType(relationship, hl7Value, this.result, propertyPath); PropertyFormatter formatter = this.formatterRegistry.Get(type); if (formatter == null) { throw new RenderingException("Cannot support properties of type " + type); } else { string xmlFragment = string.Empty; try { BareANY any = null; bool isMandatoryOrPopulated = ConformanceLevelUtil.IsMandatory(relationship) || ConformanceLevelUtil.IsPopulated(relationship ); if (relationship.HasFixedValue()) { // suppress rendering fixed values for optional or required if (isMandatoryOrPopulated) { any = (BareANY)DataTypeFactory.CreateDataType(relationship.Type, this.isCda && this.isR2); object fixedValue = NonStructuralHl7AttributeRenderer.GetFixedValue(relationship, version, this.isR2, this.result, propertyPath ); ((BareANYImpl)any).BareValue = fixedValue; } } else { any = hl7Value; any = this.adapterProvider.GetAdapter(any != null ? any.GetType() : null, type).Adapt(type, any); } // TODO - CDA - TM - implement default value handling // boolean valueNotProvided = (any.getBareValue() == null && !any.hasNullFlavor()); // if (valueNotProvided && relationship.hasDefaultValue() && isMandatoryOrPopulated) { // // FIXME - CDA - TM - this doesn't work - will have a class cast exception (put Object convert(Object/String?) on ANY, implement trivial in ANYImpl, implement where necessary?) // // any.setBareValue(relationship.getDefaultValue()); // } if (hl7Value != null && Hl7ValueHasContent(hl7Value)) { HandleNotAllowedAndIgnored(relationship, propertyPath); } FormatContext context = Ca.Infoway.Messagebuilder.Marshalling.FormatContextImpl.Create(this.result, propertyPath, relationship , version, dateTimeZone, dateTimeTimeZone, constraints, this.isCda); if (!StringUtils.Equals(type, relationship.Type)) { context = new Ca.Infoway.Messagebuilder.Marshalling.HL7.Formatter.FormatContextImpl(type, true, context); } xmlFragment += formatter.Format(context, any, GetIndent()); // if relationship specifies a namespace, need to add it to xml if (StringUtils.IsNotBlank(xmlFragment) & StringUtils.IsNotBlank(relationship.Namespaze)) { xmlFragment = System.Text.RegularExpressions.Regex.Replace(xmlFragment, "<" + relationship.Name + " ", "<" + relationship .Namespaze + ":" + relationship.Name + " "); xmlFragment = System.Text.RegularExpressions.Regex.Replace(xmlFragment, "<" + relationship.Name + ">", "<" + relationship .Namespaze + ":" + relationship.Name + ">"); xmlFragment = System.Text.RegularExpressions.Regex.Replace(xmlFragment, "</" + relationship.Name + ">", "</" + relationship .Namespaze + ":" + relationship.Name + ">"); } } catch (ModelToXmlTransformationException e) { Hl7Error hl7Error = new Hl7Error(Hl7ErrorCode.DATA_TYPE_ERROR, e.Message, propertyPath); this.result.AddHl7Error(hl7Error); } RenderNewErrorsToXml(CurrentBuffer().GetChildBuilder()); CurrentBuffer().GetChildBuilder().Append(xmlFragment); } }
public void AddDefault(Type inputType, Func <object, string> formatter) { PropertyFormatter pf = context => formatter(context.ItemValue); DefaultTypeFormatters.Add(inputType, pf); }