private static object GenerateForEmbeddedRule(EntityRule rule, IActionConfiguration actionConfiguration,
            Dictionary<string, List<string>> routeRelations, object originalObject)
        {
            var actionExecutedContext = ActionCallContext.Get<HttpActionExecutedContext>();
            var entityActionConfiguration = HypermediaControllerConfiguration.Instance.GetcontrollerActionConfiguration(rule.ControllerType, rule.ControllerAction, actionExecutedContext.Request.Headers.Accept);
            if (entityActionConfiguration == null)
                return null;

            var referencedObject = rule.GetReferencedObjectInstance(originalObject);

            if (referencedObject == null)
                return null;

            var transformed = ActionResponseTransformer.TransformPayload(entityActionConfiguration, referencedObject);

            if (transformed == null)
                return null;

            if (transformed is IEnumerable)
                throw new Exception(string.Format("Unable to generate enumerable entity object from controller {0} action {1}. Consider using outer object.", actionConfiguration.ControllerType.FullName, actionConfiguration.ActionMethodInfo));

            AssignRelName(transformed, rule.Rel);

            return transformed;
        }
        public void AddNewLinkedEntityMapping(Expression embeddedEntityExpression, Expression actionSelector, string[] rel)
        {
            var handlingSelector = actionSelector as MethodCallExpression;
            var rule             = new EntityRule(embeddedEntityExpression, handlingSelector, EntityRule.EmbeddingRule.Linked, rel);

            ActionConfiguration.AddEntityRule(rule);
        }
        private static object GenerateForLinkedRule(EntityRule rule,
            IActionConfiguration actionConfiguration,
            Dictionary<string, List<string>> routeRelations, object originalObject)
        {
            var actionExecutedContext = ActionCallContext.Get<HttpActionExecutedContext>();
            var entityActionConfiguration = HypermediaControllerConfiguration.Instance.GetcontrollerActionConfiguration(rule.ControllerType, rule.ControllerAction, actionExecutedContext.Request.Headers.Accept);
            if (entityActionConfiguration == null)
                return null;

            var referencedObject = rule.GetReferencedObjectInstance(originalObject);

            if (referencedObject == null)
                return null;
            var selfRule = entityActionConfiguration.MappingRules.FirstOrDefault(r => r.Names.Contains("self"));

            if (selfRule == null)
                throw new Exception(string.Format("Unable to generate link to entity object from controller {0} action {1}. Can't find self rel.", actionConfiguration.ControllerType.FullName, actionConfiguration.ActionMethodInfo));

            var selfApi = selfRule.ApiDescriptions.OrderBy(d => d.RelativePath.Length).FirstOrDefault();

            if (selfApi == null)
                throw new Exception(string.Format("Unable to generate link to entity object from controller {0} action {1}. Can't find self API.", actionConfiguration.ControllerType.FullName, actionConfiguration.ActionMethodInfo));

            var routeNameSubstitution = new DefaultRouteValueSubstitution();

            var absolutePath =
                LinkHelper.MakeAbsolutePath(routeNameSubstitution.Substitute(selfApi.RelativePath, selfRule,
                    referencedObject));

            return new LinkedEntity()
            {
                Rels = rule.Rel,
                ClassName = entityActionConfiguration.Class,
                Href = absolutePath
            };
        }
 public void AddEntityRule(EntityRule entityRule)
 {
     _entityRules.Add(entityRule);
 }
        private static object GenerateForRule(EntityRule rule, IActionConfiguration actionConfiguration,
            Dictionary<string, List<string>> routeRelations, object originalObject)
        {
            if (rule.EntityEmbeddingRule == EntityRule.EmbeddingRule.Embedded)
                return GenerateForEmbeddedRule(rule, actionConfiguration, routeRelations, originalObject);

            return GenerateForLinkedRule(rule, actionConfiguration, routeRelations, originalObject);
        }
 public void AddEntityRule(EntityRule entityRule)
 {
     _entityRules.Add(entityRule);
 }