public static SchemaAttributeResponse ToDomain(this SchemaAttribute attr) { if (attr == null) { throw new ArgumentNullException(nameof(attr)); } var result = new SchemaAttributeResponse(); SetData(result, attr); return(result); }
public static void SetData(this SchemaAttributeResponse resp, SchemaAttribute attr) { resp.Id = attr.Id; resp.CaseExact = attr.CaseExact; resp.Description = attr.Description; resp.MultiValued = attr.MultiValued; resp.Name = attr.Name; resp.Required = attr.Required; resp.Mutability = attr.Mutability; resp.Returned = attr.Returned; resp.Type = attr.Type; resp.Uniqueness = attr.Uniqueness; resp.CanonicalValues = SplitList(attr.CanonicalValues); resp.ReferenceTypes = SplitList(attr.ReferenceTypes); }
private static RepresentationAttribute GetEmptyToken <T>(SchemaAttributeResponse attr, T defaultValue) { try { if (attr.MultiValued) { return(new SingularRepresentationAttribute <IEnumerable <T> >(attr, new T[0])); } return(new SingularRepresentationAttribute <T>(attr, defaultValue)); } catch { return(null); } }
private static RepresentationAttribute GetSingularToken <T>(JArray jArr, SchemaAttributeResponse attribute, JToken token) { try { if (jArr != null) { return(new SingularRepresentationAttribute <IEnumerable <T> >(attribute, jArr.Values <T>())); } return(new SingularRepresentationAttribute <T>(attribute, token.Value <T>())); } catch (FormatException) { return(null); } }
private static ParseRepresentationAttrResult GetEmptyToken(SchemaAttributeResponse attr) { RepresentationAttribute result = null; switch (attr.Type) { case Common.Constants.SchemaAttributeTypes.String: case Common.Constants.SchemaAttributeTypes.Reference: result = GetEmptyToken(attr, string.Empty); break; case Common.Constants.SchemaAttributeTypes.Boolean: result = GetEmptyToken(attr, default(bool)); break; case Common.Constants.SchemaAttributeTypes.Decimal: result = GetEmptyToken(attr, default(decimal)); break; case Common.Constants.SchemaAttributeTypes.DateTime: result = GetEmptyToken(attr, default(DateTime)); break; case Common.Constants.SchemaAttributeTypes.Integer: result = GetEmptyToken(attr, default(int)); break; case Common.Constants.SchemaAttributeTypes.Complex: result = new ComplexRepresentationAttribute(attr); break; default: return(new ParseRepresentationAttrResult { IsParsed = false, ErrorMessage = string.Format(ErrorMessages.TheAttributeTypeIsNotSupported, attr.Type) }); } return(new ParseRepresentationAttrResult { IsParsed = true, RepresentationAttribute = result }); }
public static SchemaAttribute ToModel(this SchemaAttributeResponse attr) { if (attr == null) { throw new ArgumentNullException(nameof(attr)); } return(new SchemaAttribute { CaseExact = attr.CaseExact, Description = attr.Description, Id = attr.Id, MultiValued = attr.MultiValued, Mutability = attr.Mutability, Name = attr.Name, Required = attr.Required, Returned = attr.Returned, Uniqueness = attr.Uniqueness, Type = attr.Type, CanonicalValues = ConcatList(attr.CanonicalValues), ReferenceTypes = ConcatList(attr.ReferenceTypes) }); }
private static JToken GetSingularToken <T>(SchemaAttributeResponse attribute, RepresentationAttribute attr, bool isArray) { if (isArray) { var enumSingularRepresentation = attr as SingularRepresentationAttribute <IEnumerable <T> >; if (enumSingularRepresentation == null) { throw new InvalidOperationException(string.Format(ErrorMessages.TheAttributeTypeIsNotCorrect, attribute.Name, attribute.Type)); } return(new JProperty(enumSingularRepresentation.SchemaAttribute.Name, enumSingularRepresentation.Value)); } else { var singularRepresentation = attr as SingularRepresentationAttribute <T>; if (singularRepresentation == null) { throw new InvalidOperationException(string.Format(ErrorMessages.TheAttributeTypeIsNotCorrect, attribute.Name, attribute.Type)); } return(new JProperty(singularRepresentation.SchemaAttribute.Name, singularRepresentation.Value)); } }
public RepresentationAttribute(SchemaAttributeResponse schemaAttribute) { SchemaAttribute = schemaAttribute; }
public ComplexRepresentationAttribute(SchemaAttributeResponse type) : base(type) { }
public SingularRepresentationAttribute(SchemaAttributeResponse type, T value) : base(type) { Value = value; }
public RepresentationAttribute Transform(Model.RepresentationAttribute attr) { if (attr == null || (attr.Children == null && string.IsNullOrWhiteSpace(attr.Value)) && attr.ValueNumber == default(double)) { return(null); } SchemaAttributeResponse schemaAttr = null; if (attr.SchemaAttribute != null) { schemaAttr = Transform(attr.SchemaAttribute); } if (attr.SchemaAttribute != null && attr.SchemaAttribute.Type == Common.Constants.SchemaAttributeTypes.Complex || attr.SchemaAttribute == null && attr.Children != null && attr.Children.Any()) { ComplexRepresentationAttribute result = new ComplexRepresentationAttribute(schemaAttr); result.Values = new List <RepresentationAttribute>(); foreach (var child in attr.Children) { var transformed = Transform(child); if (transformed == null) { continue; } transformed.Parent = result; result.Values = result.Values.Concat(new[] { transformed }); } return(result); } var isArr = attr.SchemaAttribute.MultiValued; switch (attr.SchemaAttribute.Type) { case Common.Constants.SchemaAttributeTypes.String: if (isArr) { var values = new List <string>(); if (attr.Values != null) { foreach (var value in attr.Values) { values.Add(value.Value); } } return(new SingularRepresentationAttribute <IEnumerable <string> >(schemaAttr, values)); } return(new SingularRepresentationAttribute <string>(schemaAttr, attr.Value)); case Common.Constants.SchemaAttributeTypes.Boolean: bool r = false; if (isArr) { var values = new List <bool>(); if (attr.Values != null) { foreach (var value in attr.Values) { if (bool.TryParse(value.Value, out r)) { values.Add(r); } } } return(new SingularRepresentationAttribute <IEnumerable <bool> >(schemaAttr, values)); } bool.TryParse(attr.Value, out r); return(new SingularRepresentationAttribute <bool>(schemaAttr, r)); case Common.Constants.SchemaAttributeTypes.DateTime: DateTime dateTime = DateTime.Now; double d; if (isArr) { var values = new List <DateTime>(); if (attr.Values != null) { foreach (var value in attr.Values) { if (double.TryParse(value.Value, out d)) { values.Add(d.ToDateTime()); } } } return(new SingularRepresentationAttribute <IEnumerable <DateTime> >(schemaAttr, values)); } if (attr.ValueNumber != default(double)) { dateTime = attr.ValueNumber.ToDateTime(); } return(new SingularRepresentationAttribute <DateTime>(schemaAttr, dateTime)); case Common.Constants.SchemaAttributeTypes.Decimal: decimal dec; if (isArr) { var values = new List <decimal>(); if (attr.Values != null) { foreach (var value in attr.Values) { if (decimal.TryParse(value.Value, out dec)) { values.Add(dec); } } } return(new SingularRepresentationAttribute <IEnumerable <decimal> >(schemaAttr, values)); } return(new SingularRepresentationAttribute <decimal>(schemaAttr, (decimal)attr.ValueNumber)); case Common.Constants.SchemaAttributeTypes.Integer: int i; if (isArr) { var values = new List <int>(); if (attr.Values != null) { foreach (var value in attr.Values) { if (int.TryParse(value.Value, out i)) { values.Add(i); } } } return(new SingularRepresentationAttribute <IEnumerable <int> >(schemaAttr, values)); } return(new SingularRepresentationAttribute <int>(schemaAttr, (int)attr.ValueNumber)); } return(null); }
/// <summary> /// Parse json and returns the representation. /// </summary> /// <exception cref="ArgumentNullException">Thrown when one of the parameter is null.</exception> /// <param name="jObj">JSON</param> /// <param name="attribute">Schema attribute</param> /// <param name="checkStrategy">Strategy used to check the parameters.</param> /// <returns>Representation or null.</returns> public ParseRepresentationAttrResult GetRepresentation(JToken jObj, SchemaAttributeResponse attribute, CheckStrategies checkStrategy) { if (jObj == null) { throw new ArgumentNullException(nameof(jObj)); } if (attribute == null) { throw new ArgumentNullException(nameof(attribute)); } Action <ComplexSchemaAttributeResponse, List <RepresentationAttribute>, JToken, RepresentationAttribute> setRepresentationCallback = (attr, lst, tok, reprAttr) => { foreach (var subAttribute in attr.SubAttributes) { var rep = GetRepresentation(tok, subAttribute, checkStrategy); if (rep.IsParsed) { rep.RepresentationAttribute.Parent = reprAttr; lst.Add(rep.RepresentationAttribute); } } }; var token = jObj.SelectToken(attribute.Name); // 1. Check the attribute is required if (token == null) { if (attribute.Required && checkStrategy == CheckStrategies.Strong) { return(new ParseRepresentationAttrResult { IsParsed = false, ErrorMessage = string.Format(ErrorMessages.TheAttributeIsRequired, attribute.Name) }); } // Add empty attribute. return(GetEmptyToken(attribute)); } // 2. Check is an array JArray jArr = null; if (attribute.MultiValued) { jArr = token as JArray; if (jArr == null) { return(new ParseRepresentationAttrResult { IsParsed = false, ErrorMessage = string.Format(ErrorMessages.TheAttributeIsNotAnArray, attribute.Name) }); } } // 3. Create complex attribute var complexAttribute = attribute as ComplexSchemaAttributeResponse; if (complexAttribute != null) { var representation = new ComplexRepresentationAttribute(complexAttribute); var values = new List <RepresentationAttribute>(); if (complexAttribute.MultiValued) { // 3.1 Contains an array if (jArr.Count > 0) { foreach (var subToken in token) { var subRepresentation = new ComplexRepresentationAttribute(null); var subValues = new List <RepresentationAttribute>(); setRepresentationCallback(complexAttribute, subValues, subToken, subRepresentation); subRepresentation.Values = subValues; values.Add(subRepresentation); subRepresentation.Parent = representation; } } else { values.Add(new ComplexRepresentationAttribute(null) { Values = new List <RepresentationAttribute>() }); } } else { // 3.2 Doesn't contain array setRepresentationCallback(complexAttribute, values, token, representation); } representation.Values = values; return(new ParseRepresentationAttrResult { IsParsed = true, RepresentationAttribute = representation }); } RepresentationAttribute result = null; // 4. Create singular attribute. // Note : Don't cast to object to avoid unecessaries boxing operations ... switch (attribute.Type) { case Common.Constants.SchemaAttributeTypes.String: result = GetSingularToken <string>(jArr, attribute, token); break; case Common.Constants.SchemaAttributeTypes.Boolean: result = GetSingularToken <bool>(jArr, attribute, token); break; case Common.Constants.SchemaAttributeTypes.Decimal: result = GetSingularToken <decimal>(jArr, attribute, token); break; case Common.Constants.SchemaAttributeTypes.DateTime: result = GetSingularToken <DateTime>(jArr, attribute, token); break; case Common.Constants.SchemaAttributeTypes.Integer: result = GetSingularToken <int>(jArr, attribute, token); break; default: return(new ParseRepresentationAttrResult { IsParsed = false, ErrorMessage = string.Format(ErrorMessages.TheAttributeTypeIsNotSupported, attribute.Type) }); } if (result == null) { return(new ParseRepresentationAttrResult { IsParsed = false, ErrorMessage = string.Format(ErrorMessages.TheAttributeTypeIsNotCorrect, attribute.Name, attribute.Type) }); } return(new ParseRepresentationAttrResult { RepresentationAttribute = result, IsParsed = true }); }
public RepresentationAttribute Transform(Model.RepresentationAttribute attr) { if (attr == null || (attr.Children == null && string.IsNullOrWhiteSpace(attr.Value))) { return(null); } SchemaAttributeResponse schemaAttr = null; if (attr.SchemaAttribute != null) { schemaAttr = Transform(attr.SchemaAttribute); } if (attr.SchemaAttribute != null && attr.SchemaAttribute.Type == Common.Constants.SchemaAttributeTypes.Complex || attr.SchemaAttribute == null && attr.Children != null && attr.Children.Any()) { ComplexRepresentationAttribute result = new ComplexRepresentationAttribute(schemaAttr); result.Values = new List <RepresentationAttribute>(); foreach (var child in attr.Children) { var transformed = Transform(child); if (transformed == null) { continue; } transformed.Parent = result; result.Values = result.Values.Concat(new[] { transformed }); } return(result); } var isArr = attr.SchemaAttribute.MultiValued; switch (attr.SchemaAttribute.Type) { case Common.Constants.SchemaAttributeTypes.String: if (isArr) { var record = JsonConvert.DeserializeObject <IEnumerable <string> >(attr.Value); return(new SingularRepresentationAttribute <IEnumerable <string> >(schemaAttr, record)); } var str = JsonConvert.DeserializeObject <string>(attr.Value); return(new SingularRepresentationAttribute <string>(schemaAttr, str)); case Common.Constants.SchemaAttributeTypes.Boolean: if (isArr) { var record = JsonConvert.DeserializeObject <IEnumerable <bool> >(attr.Value); return(new SingularRepresentationAttribute <IEnumerable <bool> >(schemaAttr, record)); } var b = JsonConvert.DeserializeObject <bool>(attr.Value); return(new SingularRepresentationAttribute <bool>(schemaAttr, b)); case Common.Constants.SchemaAttributeTypes.DateTime: if (isArr) { var record = JsonConvert.DeserializeObject <IEnumerable <DateTime> >(attr.Value); return(new SingularRepresentationAttribute <IEnumerable <DateTime> >(schemaAttr, record)); } var datetime = JsonConvert.DeserializeObject <DateTime>(attr.Value); return(new SingularRepresentationAttribute <DateTime>(schemaAttr, datetime)); case Common.Constants.SchemaAttributeTypes.Decimal: if (isArr) { var record = JsonConvert.DeserializeObject <IEnumerable <decimal> >(attr.Value); return(new SingularRepresentationAttribute <IEnumerable <decimal> >(schemaAttr, record)); } var dec = JsonConvert.DeserializeObject <decimal>(attr.Value); return(new SingularRepresentationAttribute <decimal>(schemaAttr, dec)); case Common.Constants.SchemaAttributeTypes.Integer: if (isArr) { var record = JsonConvert.DeserializeObject <IEnumerable <int> >(attr.Value); return(new SingularRepresentationAttribute <IEnumerable <int> >(schemaAttr, record)); } var i = JsonConvert.DeserializeObject <int>(attr.Value); return(new SingularRepresentationAttribute <int>(schemaAttr, i)); } return(null); }
private static JToken GetToken(RepresentationAttribute attr, SchemaAttributeResponse attribute) { // 1. Check the attribute is required if (attr == null) { if (attribute.Required) { throw new InvalidOperationException(string.Format(ErrorMessages.TheAttributeIsRequired, attribute.Name)); } return(null); } // 2. Create complex attribute var complexAttribute = attribute as ComplexSchemaAttributeResponse; if (complexAttribute != null) { var complexRepresentation = attr as ComplexRepresentationAttribute; if (complexRepresentation == null) { throw new InvalidOperationException(string.Format(ErrorMessages.TheAttributeIsNotComplex, attribute.Name)); } // 2.1 Complex attribute[Complex attribute] if (attribute.MultiValued) { var array = new JArray(); if (complexRepresentation.Values != null) { foreach (var subRepresentation in complexRepresentation.Values) { var subComplex = subRepresentation as ComplexRepresentationAttribute; if (subComplex == null) { throw new InvalidOperationException(ErrorMessages.TheComplexAttributeArrayShouldContainsOnlyComplexAttribute); } var obj = new JObject(); foreach (var subAttr in subComplex.Values) { var att = complexAttribute.SubAttributes.FirstOrDefault(a => a.Name == subAttr.SchemaAttribute.Name); if (att == null) { continue; } obj.Add(GetToken(subAttr, att)); } array.Add(obj); } } return(new JProperty(complexRepresentation.SchemaAttribute.Name, array)); } var properties = new List <JToken>(); // 2.2 Complex attribute if (complexRepresentation.Values != null) { foreach (var subRepresentation in complexRepresentation.Values) { var subAttribute = complexAttribute.SubAttributes.FirstOrDefault(a => a.Name == subRepresentation.SchemaAttribute.Name); if (subAttribute == null) { continue; } properties.Add(GetToken(subRepresentation, subAttribute)); } } var props = new JObject(properties); return(new JProperty(complexRepresentation.SchemaAttribute.Name, props)); } // 3. Create singular attribute switch (attribute.Type) { case Common.Constants.SchemaAttributeTypes.String: case Common.Constants.SchemaAttributeTypes.Reference: return(GetSingularToken <string>(attribute, attr, attribute.MultiValued)); case Common.Constants.SchemaAttributeTypes.Boolean: return(GetSingularToken <bool>(attribute, attr, attribute.MultiValued)); case Common.Constants.SchemaAttributeTypes.Decimal: return(GetSingularToken <decimal>(attribute, attr, attribute.MultiValued)); case Common.Constants.SchemaAttributeTypes.DateTime: return(GetSingularToken <DateTime>(attribute, attr, attribute.MultiValued)); case Common.Constants.SchemaAttributeTypes.Integer: return(GetSingularToken <int>(attribute, attr, attribute.MultiValued)); default: throw new InvalidOperationException(string.Format(ErrorMessages.TheAttributeTypeIsNotSupported, attribute.Type)); } }
private void UpdateRepresentationAttribute(Model.RepresentationAttribute representationAttribute, SchemaAttributeResponse schemaAttr) { representationAttribute.SchemaAttribute = schemaAttr.ToModel(); if (schemaAttr.Type != "complex") { return; } var complex = schemaAttr as ComplexSchemaAttributeResponse; foreach (var child in representationAttribute.Children) { if (schemaAttr.MultiValued) { foreach (var c in child.Children) { var sra = complex.SubAttributes.First(s => s.Id == c.SchemaAttributeId); UpdateRepresentationAttribute(c, sra); } } else { var subAttr = complex.SubAttributes.First(s => s.Id == child.SchemaAttributeId); UpdateRepresentationAttribute(child, subAttr); } } }
private void UpdateRepresentationAttribute(RepresentationAttribute representationAttribute, SchemaAttributeResponse schemaAttr) { representationAttribute.SchemaAttribute = schemaAttr; if (schemaAttr.Type != "complex") { return; } var complex = representationAttribute as ComplexRepresentationAttribute; var complexSchema = schemaAttr as ComplexSchemaAttributeResponse; foreach (var child in complex.Values) { if (schemaAttr.MultiValued) { var subComplex = child as ComplexRepresentationAttribute; foreach (var c in subComplex.Values) { var sra = complexSchema.SubAttributes.First(s => s.Id == c.SchemaAttribute.Id); UpdateRepresentationAttribute(c, sra); } } else { var subRepresentationAttr = complexSchema.SubAttributes.First(s => s.Id == child.SchemaAttribute.Id); UpdateRepresentationAttribute(child, subRepresentationAttr); } } }