예제 #1
0
        public static string ReadSirenAndConvertToForm(SirenDotNet.Entity entity)
        {
            var html = BuildComponentsForEntity(entity, 1);
            var json = "<pre>" + string.Join(Environment.NewLine, JObject.FromObject(entity).ToString(Newtonsoft.Json.Formatting.Indented)) + "</pre>";

            html = SplitContainer(html, json);
            return(html);
        }
예제 #2
0
        public SirenDotNet.Entity BuildFromSemanticDocument(SemanticDocument semanticDocument, FindUriForTerm uriTermFinder)
        {
            var links   = new List <SirenDotNet.Link>();
            var actions = new List <Action>();

            var subEntities = new List <SubEntity>();

            var jo = new JObject();

            string sirenTitle = null;

            foreach (var property in semanticDocument.Value)
            {
                property.Value.Switch(set =>
                {
                    if (property.Term.Means(TermFactory.From <Operation>()))
                    {
                        Func <SemanticProperty, Field> buildField = mp =>
                        {
                            var fieldSet  = mp.Value.AsT0;
                            var fieldKind = fieldSet[TermFactory.From <Operation.FieldKind>()].Value.AsT2;

                            var field = new Field()
                            {
                                Name = fieldSet[TermFactory.From <FieldName>()].Value.AsT1.Value <string>(),
                            };
                            if (fieldKind == TermFactory.From <Operation.TextField>())
                            {
                                field.Type = SirenDotNet.FieldTypes.Text;
                            }
                            else if (fieldKind == TermFactory.From <Operation.PasswordField>())
                            {
                                field.Type = SirenDotNet.FieldTypes.Password;
                            }
                            else if (fieldKind == TermFactory.From <Operation.SelectField>())
                            {
                                field.Type  = new FieldTypes("select");
                                var options = fieldSet[TermFactory.From <Operation.Options>()].Value.AsT3;

                                field.ExtensionData["options"] = options.Select(o => o.AsT0)
                                                                 .Select(
                                    o => new
                                {
                                    name  = o[TermFactory.From <DisplayText>()].Value.AsT1.Value <string>(),
                                    value = o[TermFactory.From <Operation.FieldValue>()].Value.AsT1.Value <string>()
                                })
                                                                 .Select(JObject.FromObject).ToArray();
                            }


                            return(field);
                        };
                        ;
                        var href   = new Uri(set[TermFactory.From <Operation.ActionUrl>()].Value.AsT1.Value <string>(), UriKind.Relative);
                        var action = new SirenDotNet.Action(uriTermFinder(property.Term).ToString(), href)
                        {
                            Method = HttpVerbs.POST,
                            Title  = set[TermFactory.From <DisplayText>()].Value.AsT1.Value <string>(),
                            Fields = set[TermFactory.From <Operation.Fields>()].Value.AsT0.Select(x => buildField(x)).ToArray()
                        };
                        actions.Add(action);
                    }
                },
                                      value =>
                {
                    var isTitle = property.Term == Terms.Title;
                    if (isTitle)
                    {
                        sirenTitle = value.ToObject <string>();
                    }
                    else
                    {
                        jo[uriTermFinder(property.Term).ToString()] = value.ToString();
                    }
                },
                                      term => { },
                                      list =>
                {
                    if (property.Term == TermFactory.From <Vocab.Links>())
                    {
                        foreach (var value in list)
                        {
                            var set         = value.AsT0;
                            var displayName = set[TermFactory.From <DisplayText>()].Value.AsT1.Value <string>();
                            Term rel        = set[TermFactory.From <Vocab.Links.Rel>()].Value.AsT2;
                            var href        = new Uri(set[TermFactory.From <Vocab.Links.Href>()].Value.AsT1.Value <string>(), UriKind.Relative);
                            var action      = new SirenDotNet.Link(href, uriTermFinder(rel).ToString())
                            {
                                Title = displayName
                            };
                            links.Add(action);
                        }
                    }
                });
            }



            if (sirenTitle == null)
            {
                throw new Exception("Title cannot be null for siren, attach a Term.Title property");
            }

            var entity = new SirenDotNet.Entity
            {
                Title = sirenTitle,
                //Class = semanticDocument.Class?.Select(c => c.ToString()),
                Links      = links.Any() ? links : null,
                Actions    = actions.Any() ? actions : null,
                Entities   = subEntities.Any() ? subEntities : null,
                Properties = jo.HasValues ? jo : null
            };

            return(entity);
        }