/// <summary> /// Search for name /// if something is found, /// get the described thing for it /// </summary> /// <param name="thingGraph"></param> /// <param name="name"></param> /// <param name="exact"></param> /// <returns></returns> public static IEnumerable <IThing> Search(this IThingGraph thingGraph, object name, bool exact) { var schemaGraph = thingGraph as SchemaThingGraph; bool isSchemaGraph = schemaGraph != null; var schema = new CommonSchema(); foreach (var thing in thingGraph.GetByData(name, exact)) { IThing described = null; if (isSchemaGraph) { described = schemaGraph.DescribedThing(thing); } else { described = schema.GetTheRoot(thingGraph, thing, CommonSchema.DescriptionMarker); } if (described != null) { yield return(described); } else { yield return(thing); } } }
/// <summary> /// assigns the content's descriptions to the thing /// assign = set content.Description to the leaf with CommonSchema.DescriptionMarker /// set content.Source to the leaf with CommonSchema.SourceMarker /// if the leaf doesn't exist, it will be created /// </summary> /// <param name="thing"></param> /// <param name="content"></param> /// <param name="thingGraph"></param> public virtual void AssignContentDescription(IThing thing, Content <Stream> content, IThingGraph thingGraph) { var streamThing = thing as IStreamThing; if (streamThing == null) { return; } var schema = new CommonSchema(thingGraph, streamThing); if (content.Description != null) { if (schema.Description != null) { schema.Description.Data = content.Description; } else { schema.Description = Factory.CreateItem(content.Description);; } } if (content.Source != null) { var sourceThing = schema.GetTheLeaf(CommonSchema.SourceMarker); if (sourceThing == null) { sourceThing = Factory.CreateItem(content.Source); schema.SetTheLeaf(CommonSchema.SourceMarker, sourceThing); } else { sourceThing.Data = content.Source; } } }
public static object SearchOptionValidIdMapper(ValidOptionId type) { var dict = new Dictionary <ValidOptionId, object>() { [ValidOptionId.SingleExistOptionHasNoChild] = SingleOption.Data.Id, [ValidOptionId.SingleExistOptionHasOneChild] = OptionWithChild.Data.Id, [ValidOptionId.ListExistOptionId] = new int[] { SingleOption.Data.Id, OptionWithChild.Data.Id }, [ValidOptionId.ListMixedOptionId] = new int[] { SingleOption.Data.Id, OptionWithChild.Data.Id, DeletedOption.Data.Id }, [ValidOptionId.StringNumber] = SingleOption.Data.Id.ToString(), [ValidOptionId.StringOptionGuid] = CommonSchema.GetUuid("Option.P_Gender", true), [ValidOptionId.ListDuplicatedOptionId] = new int[] { SingleOption.Data.Id, SingleOption.Data.Id }, [ValidOptionId.LeadingZero] = SingleOption.Data.Id.ToString().PadLeft(2, '0') }; return(dict[type]); }
public static void SetSource(this IThingGraph source, IThing thing, object name) { if (source != null) { var schema = new CommonSchema(source, thing); var desc = schema.GetTheLeaf(CommonSchema.SourceMarker); if (desc != null) { if (desc != thing) { source.DoChangeData(desc, name); } } else { var factory = Registry.Pooled <IThingFactory>(); schema.SetTheLeaf(CommonSchema.SourceMarker, factory.CreateItem(source, name)); } } }
public virtual WebContent GetContentFromGraph(IThingGraph graph, IThing thing, Uri uri) { var schema = new CommonSchema(); WebContent result = null; try { var schemaGraph = graph as SchemaThingGraph; if (thing != null && schemaGraph != null) { var searchGraph = schemaGraph.Source; var content = uri.Segments[uri.Segments.Length - 1]; foreach (var link in searchGraph.Edges(thing)) { var adj = link.Leaf; if (adj != thing && (adj is IStreamThing)) { var desc = schemaGraph.Description(adj); if (desc != null && desc.ToString() == content) { return(GetContentFromThing(schemaGraph, adj)); } } } } if (schemaGraph != null) { foreach (var found in schemaGraph.GetByData(uri.AbsoluteUri, true)) { var target = schema.GetTheRoot(schemaGraph, found, CommonSchema.SourceMarker); if (target is IStreamThing) { return(GetContentFromThing(schemaGraph, target)); } } } } catch (Exception e) { Trace.WriteLine(e.Message); return(null); } return(result); }
public virtual void TestDescription() { string testName = "TestDescription"; this.ReportDetail(testName); IThingGraph graph = new ThingGraph(); IThing thing = Factory.CreateItem(); graph.Add(thing); IThing description = Factory.CreateItem("Description1"); CommonSchema schema = new CommonSchema(graph, thing); // test the new description: schema.Description = description; ValidateDescription(schema, graph, thing, description); ValidateDescription(schema, graph, thing, description); // add same description again: schema.Description = description; ValidateDescription(schema, graph, thing, description); // the first description will be an orphan: IThing orphan = description; // make a new description: description = Factory.CreateItem("Description2"); schema.Description = description; ValidateDescription(schema, graph, thing, description); // test if orphan is deleted: Assert.IsFalse(graph.Contains(orphan), "Orphan not deleted"); // take a new schema: schema = new CommonSchema(graph, thing); ValidateDescription(schema, graph, thing, description); ReportSummary(); }
public void ValidateDescription(CommonSchema schema, IThingGraph graph, IThing thing, IThing description) { Assert.AreSame(schema.Description, description); this.ReportDetail(GraphTestUtils.ReportGraph <IThing, ILink>(graph, "* Descriptioin added")); bool found = false; bool firstMarker = false; foreach (ILink link in graph.Edges(thing)) { if (link.Marker == CommonSchema.DescriptionMarker) { Assert.IsFalse(firstMarker, "second descriptionmarker found"); firstMarker = true; } if (link.Leaf == schema.Description && link.Marker == CommonSchema.DescriptionMarker && link.Leaf == description) { Assert.IsFalse(found); found = true; } } Assert.IsTrue(found); }