Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TextParameter.cs
2using System.Threading.Tasks;
3using System.Xml;
13using Waher.Script;
14
16{
21 {
22 private readonly ReportStringAttribute[] defaultValue;
23 private readonly ReportStringAttribute contentType;
24 private readonly ReportUInt16Attribute minCount;
25 private readonly ReportUInt16Attribute maxCount;
26 private readonly int nrDefaultValues;
27
32 public TextParameter(XmlElement Xml)
33 : base(Xml)
34 {
35 this.contentType = new ReportStringAttribute(Xml, "contentType");
36 this.minCount = new ReportUInt16Attribute(Xml, "minCount");
37 this.maxCount = new ReportUInt16Attribute(Xml, "maxCount");
38
39 List<ReportStringAttribute> DefaultValues = new List<ReportStringAttribute>();
40
41 foreach (XmlNode N in Xml.ChildNodes)
42 {
43 if (N is XmlElement E && E.LocalName == "DefaultValue")
44 DefaultValues.Add(new ReportStringAttribute(E, null));
45 }
46
47 this.defaultValue = DefaultValues.ToArray();
48 this.nrDefaultValues = this.defaultValue.Length;
49 }
50
58 {
60 string ContentType = await this.contentType.Evaluate(Variables, PlainTextCodec.DefaultContentType);
61 ushort? MinCount = this.minCount.IsEmpty ? null : (ushort?)await this.minCount.Evaluate(Variables);
62 ushort? MaxCount = this.maxCount.IsEmpty ? null : (ushort?)await this.maxCount.Evaluate(Variables);
63 ValidationMethod Validation = new BasicValidation();
65
66 if (MinCount.HasValue || MaxCount.HasValue)
67 Validation = new ListRangeValidation(Validation, MinCount ?? 0, MaxCount ?? ushort.MaxValue);
68
69 if (Attributes.RestrictToOptions)
70 {
71 Field = new ListMultiField(Parameters, Attributes.Name, Attributes.Label, Attributes.Required,
72 await this.GetDefaultValue(Variables), Attributes.Options, Attributes.Description,
73 StringDataType.Instance, Validation, string.Empty, false, false, false);
74 }
75 else
76 {
77 Field = new TextMultiField(Parameters, Attributes.Name, Attributes.Label, Attributes.Required,
78 await this.GetDefaultValue(Variables), Attributes.Options, Attributes.Description,
79 StringDataType.Instance, Validation, string.Empty, false, false, false, ContentType);
80 }
81
82 Parameters.Add(Field);
83
84 Page Page = Parameters.GetPage(Attributes.Page);
85 Page.Add(Field);
86 }
87
88 private async Task<string[]> GetDefaultValue(Variables Variables)
89 {
90 string[] DefaultValue = new string[this.nrDefaultValues];
91 int i;
92
93 for (i = 0; i < this.nrDefaultValues; i++)
94 DefaultValue[i] = await this.defaultValue[i].Evaluate(Variables);
95
96 return DefaultValue;
97 }
98
108 public override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Variables,
110 {
111 string Name = await this.GetName(Variables);
112 bool Required = await this.IsRequired(Variables);
113 ushort? MinCount = this.minCount.IsEmpty ? null : (ushort?)await this.minCount.Evaluate(Variables);
114 ushort? MaxCount = this.maxCount.IsEmpty ? null : (ushort?)await this.maxCount.Evaluate(Variables);
115 Field Field = Parameters[Name];
116
117 if (Field is null)
118 {
119 if (Required)
120 Result.AddError(Name, await Language.GetStringAsync(typeof(ReportFileNode), 1, "Required parameter."));
121
122 Variables[Name] = null;
123 }
124 else
125 {
126 string[] s = Field.ValueStrings;
127
128 if (s is null || s.Length == 0)
129 {
130 if (Required)
131 Result.AddError(Name, await Language.GetStringAsync(typeof(ReportFileNode), 1, "Required parameter."));
132
133 Variables[Name] = null;
134 }
135 else
136 {
137 Variables[Name] = s;
138
139 if (MinCount.HasValue && s.Length < MinCount.Value)
140 Result.AddError(Name, await Language.GetStringAsync(typeof(ReportFileNode), 5, "Too few rows."));
141 else if (MaxCount.HasValue && s.Length > MaxCount.Value)
142 Result.AddError(Name, await Language.GetStringAsync(typeof(ReportFileNode), 6, "Too many rows."));
143 }
144 }
145 }
146
147 }
148}
Plain text encoder/decoder.
const string DefaultContentType
text/plain
Static class managing editable parameters in objects. Editable parameters are defined by using the at...
Definition: Parameters.cs:26
void AddError(string Key, string Value)
Adds an error to the list of errors.
Implements support for data forms. Data Forms are defined in the following XEPs:
Definition: DataForm.cs:42
static readonly StringDataType Instance
Public instance of data type.
Base class for form fields
Definition: Field.cs:17
string[] ValueStrings
Values for the field (string representations).
Definition: Field.cs:97
Class managing a page in a data form layout.
Definition: Page.cs:11
void Add(LayoutElement Element)
Adds a layout element.
Definition: Section.cs:135
Represents a parameter with possible options on a command.
async Task< ReportParameterWithOptionsAttributes > GetReportParameterWithOptionsAttributes(Variables Variables)
Evaluates the attributes of the report parameter with options.
Represents a text-valued parameter.
TextParameter(XmlElement Xml)
Represents a text-valued parameter.
override async Task PopulateForm(DataForm Parameters, Language Language, Variables Variables)
Populates a data form with parameters for the object.
override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Variables, SetEditableFormResult Result)
Sets the parameters of the object, based on contents in the data form.
Task< string > GetName(Variables Variables)
Name of the parameter.
Task< bool > IsRequired(Variables Variables)
If parameter is required.
Report node based on the contents of a report file.
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Collection of variables.
Definition: Variables.cs:25