Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
JidsParameter.cs
2using System.Threading.Tasks;
3using System.Xml;
13using Waher.Script;
14
16{
21 {
22 private readonly ReportStringAttribute[] defaultValue;
23 private readonly ReportUInt16Attribute minCount;
24 private readonly ReportUInt16Attribute maxCount;
25 private readonly int nrDefaultValues;
26
31 public JidsParameter(XmlElement Xml)
32 : base(Xml)
33 {
34 this.minCount = new ReportUInt16Attribute(Xml, "minCount");
35 this.maxCount = new ReportUInt16Attribute(Xml, "maxCount");
36
37 List<ReportStringAttribute> DefaultValues = new List<ReportStringAttribute>();
38
39 foreach (XmlNode N in Xml.ChildNodes)
40 {
41 if (N is XmlElement E && E.LocalName == "DefaultValue")
42 DefaultValues.Add(new ReportStringAttribute(E, null));
43 }
44
45 this.defaultValue = DefaultValues.ToArray();
46 this.nrDefaultValues = this.defaultValue.Length;
47 }
48
56 {
58 ushort? MinCount = this.minCount.IsEmpty ? null : (ushort?)await this.minCount.Evaluate(Variables);
59 ushort? MaxCount = this.maxCount.IsEmpty ? null : (ushort?)await this.maxCount.Evaluate(Variables);
60 ValidationMethod Validation = new BasicValidation();
62
63 if (MinCount.HasValue || MaxCount.HasValue)
64 Validation = new ListRangeValidation(Validation, MinCount ?? 0, MaxCount ?? ushort.MaxValue);
65
66 if (Attributes.RestrictToOptions)
67 {
68 Field = new ListMultiField(Parameters, Attributes.Name, Attributes.Label, Attributes.Required,
69 await this.GetDefaultValue(Variables), Attributes.Options, Attributes.Description,
70 StringDataType.Instance, Validation, string.Empty, false, false, false);
71 }
72 else
73 {
74 Field = new JidMultiField(Parameters, Attributes.Name, Attributes.Label, Attributes.Required,
75 await this.GetDefaultValue(Variables), Attributes.Options, Attributes.Description,
76 StringDataType.Instance, Validation, string.Empty, false, false, false);
77 }
78
79 Parameters.Add(Field);
80
81 Page Page = Parameters.GetPage(Attributes.Page);
82 Page.Add(Field);
83 }
84
85 private async Task<string[]> GetDefaultValue(Variables Variables)
86 {
87 string[] DefaultValue = new string[this.nrDefaultValues];
88 int i;
89
90 for (i = 0; i < this.nrDefaultValues; i++)
91 DefaultValue[i] = await this.defaultValue[i].Evaluate(Variables);
92
93 return DefaultValue;
94 }
95
105 public override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Variables,
107 {
108 string Name = await this.GetName(Variables);
109 bool Required = await this.IsRequired(Variables);
110 ushort? MinCount = this.minCount.IsEmpty ? null : (ushort?)await this.minCount.Evaluate(Variables);
111 ushort? MaxCount = this.maxCount.IsEmpty ? null : (ushort?)await this.maxCount.Evaluate(Variables);
112 Field Field = Parameters[Name];
113
114 if (Field is null)
115 {
116 if (Required)
117 Result.AddError(Name, await Language.GetStringAsync(typeof(ReportFileNode), 1, "Required parameter."));
118
119 Variables[Name] = null;
120 }
121 else
122 {
123 string[] s = Field.ValueStrings;
124
125 if (s is null || s.Length == 0)
126 {
127 if (Required)
128 Result.AddError(Name, await Language.GetStringAsync(typeof(ReportFileNode), 1, "Required parameter."));
129
130 Variables[Name] = null;
131 }
132 else
133 {
134 Variables[Name] = s;
135
136 foreach (string Jid in s)
137 {
138 if (!XmppClient.BareJidRegEx.IsMatch(Jid))
139 Result.AddError(Name, (await Language.GetStringAsync(typeof(ReportFileNode), 7, "Invalid JID: %0%")).Replace("%0%", Jid));
140 }
141
142 if (MinCount.HasValue && s.Length < MinCount.Value)
143 Result.AddError(Name, await Language.GetStringAsync(typeof(ReportFileNode), 5, "Too few rows."));
144 else if (MaxCount.HasValue && s.Length > MaxCount.Value)
145 Result.AddError(Name, await Language.GetStringAsync(typeof(ReportFileNode), 6, "Too many rows."));
146 }
147 }
148 }
149 }
150}
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
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:58
static readonly Regex BareJidRegEx
Regular expression for Bare JIDs
Definition: XmppClient.cs:187
Represents a multiple JIDs-valued parameter.
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.
override async Task PopulateForm(DataForm Parameters, Language Language, Variables Variables)
Populates a data form with parameters for the object.
JidsParameter(XmlElement Xml)
Represents a multiple JIDs-valued parameter.
Represents a parameter with possible options on a command.
async Task< ReportParameterWithOptionsAttributes > GetReportParameterWithOptionsAttributes(Variables Variables)
Evaluates the attributes of the report parameter with options.
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