private void FillCommand( HypermediaClientObject hypermediaObjectInstance, PropertyInfo propertyInfo, IToken rootObject, IHypermediaResolver resolver) { var commandAttribute = propertyInfo.GetCustomAttribute <HypermediaCommandAttribute>(); if (commandAttribute == null) { throw new Exception($"Hypermedia command '{propertyInfo.Name}' requires a {nameof(HypermediaCommandAttribute)} "); } // create instance in any case so CanExecute can be called var commandInstance = this.CreateHypermediaClientCommand(propertyInfo.PropertyType); propertyInfo.SetValue(hypermediaObjectInstance, commandInstance); var actions = rootObject["actions"]; var desiredAction = actions.FirstOrDefault(e => this.IsDesiredAction(e, commandAttribute.Name)); if (actions == null || desiredAction == null) { if (IsMandatoryHypermediaProperty(propertyInfo)) { throw new Exception($"Mandatory hypermedia command '{propertyInfo.Name}' not found."); } return; } this.FillCommandParameters(commandInstance, desiredAction, commandAttribute.Name, resolver); }
// todo attribute with different property name private static void FillProperty(HypermediaClientObject hypermediaObjectInstance, PropertyInfo propertyInfo, IToken rootObject) { var properties = rootObject["properties"]; if (properties == null) { if (IsMandatoryHypermediaProperty(propertyInfo)) { throw new Exception($"Mandatory property not found {propertyInfo.Name}"); } return; } var propertyValue = properties[propertyInfo.Name]; if (propertyValue == null) { if (IsMandatoryHypermediaProperty(propertyInfo)) { throw new Exception($"Mandatory property not found {propertyInfo.Name}"); } return; } propertyInfo.SetValue(hypermediaObjectInstance, propertyValue.ToObject(propertyInfo.PropertyType)); }
private void FillCommand(HypermediaClientObject hypermediaObjectInstance, PropertyInfo propertyInfo, JObject jObject) { var commandAttribute = propertyInfo.GetCustomAttribute <HypermediaCommandAttribute>(); if (commandAttribute == null) { throw new Exception($"Hypermedia command '{propertyInfo.Name}' requires a {typeof(HypermediaCommandAttribute).Name} "); } // create instance in any case so CanExecute can be called var commandInstance = CreateHypermediaClientCommand(propertyInfo.PropertyType); commandInstance.Resolver = resolver; propertyInfo.SetValue(hypermediaObjectInstance, commandInstance); var actions = jObject["actions"] as JArray; var jAction = actions.Cast <JObject>().FirstOrDefault(e => IsDesiredAction(e, commandAttribute.Name)); if (actions == null || jAction == null) { if (IsMandatoryHypermediaProperty(propertyInfo)) { throw new Exception($"Mandatory hypermedia command '{propertyInfo.Name}' not found."); } return; } FillCommandParameters(commandInstance, jAction, commandAttribute.Name); }
//todo linked entities //todo no derived types considered private void FillEntities( HypermediaClientObject hypermediaObjectInstance, PropertyInfo propertyInfo, IToken rootObject, IHypermediaResolver resolver) { var relationsAttribute = propertyInfo.GetCustomAttribute <HypermediaRelationsAttribute>(); var classes = this.GetClassesFromEntitiesListProperty(propertyInfo); var entityCollection = Activator.CreateInstance(propertyInfo.PropertyType); propertyInfo.SetValue(hypermediaObjectInstance, entityCollection); var entities = rootObject["entities"]; if (entities == null) { return; } var matchingEntities = entities.Where(e => this.EntityRelationsMatch(e, relationsAttribute.Relations) && this.EntityClassMatch(e, classes, propertyInfo.Name)); var genericAddFunction = entityCollection.GetType().GetTypeInfo().GetMethod("Add"); foreach (var match in matchingEntities) { var entity = this.ReadHypermediaObject(match, resolver); genericAddFunction.Invoke(entityCollection, new object[] { entity }); } }
//todo linked entities //todo no derived types considered private void FillEntities(HypermediaClientObject hypermediaObjectInstance, PropertyInfo propertyInfo, JObject jObject) { var relationsAttribute = propertyInfo.GetCustomAttribute <HypermediaRelationsAttribute>(); var classes = GetClassesFromEntitiesListProperty(propertyInfo); var entityCollection = Activator.CreateInstance(propertyInfo.PropertyType); propertyInfo.SetValue(hypermediaObjectInstance, entityCollection); var entities = jObject["entities"] as JArray; if (entities == null) { return; } var jEntities = entities.Cast <JObject>().Where(e => EntityRelationsMatch(e, relationsAttribute.Relations) && EntityClassMatch(e, classes, propertyInfo.Name)); var genericAddFunction = entityCollection.GetType().GetMethod("Add"); foreach (var jEntity in jEntities) { var entity = ReadHypermediaObject(jEntity); genericAddFunction.Invoke(entityCollection, new object[] { entity }); } }
private void ReadTitle(HypermediaClientObject hypermediaObjectInstance, IToken rootObject) { var title = rootObject["title"]; if (title == null) { return; } hypermediaObjectInstance.Title = title.ValueAsString(); }
private void ReadRelations(HypermediaClientObject hypermediaObjectInstance, IToken rootObject) { var relations = rootObject["rel"]?.ChildrenAsStrings(); if (relations == null) { return; } hypermediaObjectInstance.Relations = relations.ToList(); }
private void ReadTitle(HypermediaClientObject hypermediaObjectInstance, JObject jobject) { var title = jobject["title"]; if (title == null) { return; } hypermediaObjectInstance.Title = title.Value <string>(); }
private void ReadRelations(HypermediaClientObject hypermediaObjectInstance, JObject jobject) { var rels = jobject["rel"]?.Values <string>(); if (rels == null) { return; } hypermediaObjectInstance.Relations = rels.ToList(); }
private void FillHypermediaProperties( HypermediaClientObject hypermediaObjectInstance, IToken rootObject, IHypermediaResolver resolver) { var typeInfo = hypermediaObjectInstance.GetType().GetTypeInfo(); var properties = typeInfo.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var propertyInfo in properties) { var ignore = propertyInfo.GetCustomAttribute <ClientIgnoreHypermediaPropertyAttribute>() != null; if (ignore) { continue; } var hypermediaPropertyType = GetHypermediaPropertyType(propertyInfo); switch (hypermediaPropertyType) { case HypermediaPropertyType.Property: FillProperty(hypermediaObjectInstance, propertyInfo, rootObject); break; case HypermediaPropertyType.Link: this.FillLink(hypermediaObjectInstance, propertyInfo, rootObject, resolver); break; case HypermediaPropertyType.Entity: this.FillEntity(hypermediaObjectInstance, propertyInfo, rootObject, resolver); break; case HypermediaPropertyType.EntityCollection: this.FillEntities(hypermediaObjectInstance, propertyInfo, rootObject, resolver); break; case HypermediaPropertyType.Command: this.FillCommand(hypermediaObjectInstance, propertyInfo, rootObject, resolver); break; default: throw new ArgumentOutOfRangeException(); } } }
private void FillLink( HypermediaClientObject hypermediaObjectInstance, PropertyInfo propertyInfo, IToken rootObject, IHypermediaResolver resolver) { var linkAttribute = propertyInfo.GetCustomAttribute <HypermediaRelationsAttribute>(); if (linkAttribute == null) { throw new Exception($"{nameof(IHypermediaLink)} requires a {nameof(HypermediaRelationsAttribute)} Attribute."); } var hypermediaLink = (IHypermediaLink)Activator.CreateInstance(propertyInfo.PropertyType); propertyInfo.SetValue(hypermediaObjectInstance, hypermediaLink); var links = rootObject["links"]; if (links == null) { if (IsMandatoryHypermediaLink(propertyInfo)) { throw new Exception($"Mandatory link not found {propertyInfo.Name}"); } return; } var link = links.FirstOrDefault(l => this.distinctOrderedStringCollectionComparer.Equals(new DistinctOrderedStringCollection(l["rel"].ChildrenAsStrings()), linkAttribute.Relations)); if (link == null) { if (IsMandatoryHypermediaLink(propertyInfo)) { throw new Exception($"Mandatory link not found {propertyInfo.Name}"); } return; } hypermediaLink.Uri = new Uri(link["href"].ValueAsString()); hypermediaLink.Relations = link["rel"].ChildrenAsStrings().ToList(); hypermediaLink.Resolver = resolver; }
private void FillLink(HypermediaClientObject hypermediaObjectInstance, PropertyInfo propertyInfo, JObject jObject) { var linkAttribute = propertyInfo.GetCustomAttribute <HypermediaRelationsAttribute>(); if (linkAttribute == null) { throw new Exception($"{typeof(IHypermediaLink).Name} requires a {typeof(HypermediaRelationsAttribute).Name} Attribute."); } var hypermediaLink = (IHypermediaLink)Activator.CreateInstance(propertyInfo.PropertyType); hypermediaLink.Resolver = resolver; propertyInfo.SetValue(hypermediaObjectInstance, hypermediaLink); var links = jObject["links"]; if (links == null) { if (IsMandatoryHypermediaLink(propertyInfo)) { throw new Exception($"Mandatory link not found {propertyInfo.Name}"); } return; } var link = links.FirstOrDefault(l => stringCollectionComparer.Equals(l["rel"].Values <string>().ToList(), linkAttribute.Relations.ToList())); if (link == null) { if (IsMandatoryHypermediaLink(propertyInfo)) { throw new Exception($"Mandatory link not found {propertyInfo.Name}"); } return; } hypermediaLink.Uri = new Uri(link["href"].Value <string>()); hypermediaLink.Relations = link["rel"].Values <string>().ToList(); }
private void FillEntity(HypermediaClientObject hypermediaObjectInstance, PropertyInfo propertyInfo, JObject jObject) { var relationsAttribute = propertyInfo.GetCustomAttribute <HypermediaRelationsAttribute>(); var classes = GetClassesFromEntityProperty(propertyInfo.PropertyType.GetTypeInfo()); var entities = jObject["entities"] as JArray; if (entities == null) { if (IsMandatoryHypermediaProperty(propertyInfo)) { throw new Exception($"Mandatory hypermedia property not found {propertyInfo.Name}"); } return; } var jEntity = entities.Cast <JObject>().FirstOrDefault(e => EntityRelationsMatch(e, relationsAttribute.Relations) && EntityClassMatch(e, classes, propertyInfo.Name)); var entity = ReadHypermediaObject(jEntity); propertyInfo.SetValue(hypermediaObjectInstance, entity); }
private void FillEntity( HypermediaClientObject hypermediaObjectInstance, PropertyInfo propertyInfo, IToken rootObject, IHypermediaResolver resolver) { var relationsAttribute = propertyInfo.GetCustomAttribute <HypermediaRelationsAttribute>(); var classes = GetClassesFromEntityProperty(propertyInfo.PropertyType.GetTypeInfo()); var entities = rootObject["entities"]; if (entities == null) { if (IsMandatoryHypermediaProperty(propertyInfo)) { throw new Exception($"Mandatory hypermedia property can not be filled {propertyInfo.Name}: server object contains no entities."); } return; } var jEntity = entities.FirstOrDefault(e => this.EntityRelationsMatch(e, relationsAttribute.Relations) && this.EntityClassMatch(e, classes, propertyInfo.Name)); if (jEntity == null) { if (IsMandatoryHypermediaProperty(propertyInfo)) { throw new Exception($"Mandatory hypermedia property can not be filled {propertyInfo.Name}: server object contains no entity of matching type (relation and class)."); } return; } var entity = this.ReadHypermediaObject(jEntity, resolver); propertyInfo.SetValue(hypermediaObjectInstance, entity); }