12using System.Collections.Generic;
15using System.Threading.Tasks;
17using System.Windows.Controls;
18using System.Windows.Data;
19using System.Windows.Input;
20using System.Windows.Markup;
21using System.Windows.Media;
23using TAG.Content.Microsoft;
43 private const string defaultParameterDescription =
"Enter parameter description as **Markdown**";
44 private const string defaultParameterLabel =
"Enter label as **Markdown**";
70 private readonly
Command addRole;
71 private readonly
Command addPart;
72 private readonly
Command addNumericParameter;
73 private readonly
Command addStringParameter;
74 private readonly
Command addBooleanParameter;
75 private readonly
Command addDateParameter;
76 private readonly
Command addDateTimeParameter;
77 private readonly
Command addTimeParameter;
78 private readonly
Command addDurationParameter;
79 private readonly
Command addCalcParameter;
80 private readonly
Command addRoleReference;
81 private readonly
Command addContractReference;
84 private readonly
Command import;
86 private readonly
Command propose;
87 private readonly
Command addLanguage;
88 private readonly
Command removeLanguage;
91 private string lastLanguage =
string.Empty;
121 this.roles.PropertyChanged += this.Roles_PropertyChanged;
122 this.parameters.PropertyChanged += this.Parameters_PropertyChanged;
123 this.roleReferenceParameters.PropertyChanged += this.Parameters_PropertyChanged;
124 this.contractReferenceParameters.PropertyChanged += this.Parameters_PropertyChanged;
125 this.parts.PropertyChanged += this.Parts_PropertyChanged;
129 this.machineReadable.OnAction += this.NormalizeMachineReadableXml;
130 this.humanReadableMarkdown.OnAction += this.RenderHumanReadableMarkdown;
144 this.@
new =
new Command(this.ExecuteNewContract);
145 this.load =
new Command(this.ExecuteLoadContract);
146 this.import =
new Command(this.ExecuteImportContract);
147 this.save =
new Command(this.ExecuteSaveContract);
148 this.propose =
new Command(this.CanExecuteProposeContract, this.ExecuteProposeContract);
149 this.addLanguage =
new Command(this.ExecuteAddLanguage);
150 this.removeLanguage =
new Command(this.CanExecuteRemoveLanguage, this.ExecuteRemoveLanguage);
152 this.GenerateContract().Wait();
155 private async Task GenerateContract()
157 await this.SetContract(
new Contract()
162 CanActAsTemplate =
true,
175 TemplateId =
string.Empty,
176 Visibility = ContractVisibility.PublicSearchable
182 if (this.contract is not
null)
183 this.contract.FormatParameterDisplay -= this.Contract_FormatParameterDisplay;
186 this.contract.FormatParameterDisplay += this.Contract_FormatParameterDisplay;
197 List<PartInfo>
Parts =
new();
205 this.Parts =
Parts.ToArray();
207 List<RoleInfo>
Roles =
new();
215 this.Roles =
Roles.ToArray();
217 List<ParameterInfo> ParameterList =
new();
218 List<RoleReferenceParameterInfo> RoleReferenceParameterList =
new();
219 List<ContractReferenceParameterInfo> ContractReferenceParameterList =
new();
242 RoleReferenceParameterList.Add(this.GetParameterInfo(RP));
247 ContractReferenceParameterList.Add(this.GetParameterInfo(CRP));
256 this.Parameters = ParameterList.ToArray();
257 this.RoleReferenceParameters = RoleReferenceParameterList.ToArray();
258 this.ContractReferenceParameters = ContractReferenceParameterList.ToArray();
264 this.MachineReadable = s.Replace(
"\n\t",
"\n");
265 this.ForMachines = E;
270 if (
string.IsNullOrEmpty(this.
Language) && this.Languages.Length == 0)
272 this.Languages =
new string[] {
"en" }.ToIso639_1();
273 this.Language =
"en";
284 return Task.CompletedTask;
297 MainWindow.currentInstance.DesignTab.DataContext =
this;
298 MainWindow.currentInstance.DesignTab.OpenAiKey.Password = this.
OpenAiKey;
306 get => this.visibility.Value;
309 this.visibility.Value = value;
310 this.contract.Visibility = value;
324 get => this.partsMode.Value;
327 this.partsMode.Value = value;
328 this.contract.PartsMode = value;
330 this.propose.RaiseCanExecuteChanged();
344 get => this.archiveOptional.
Value;
347 this.archiveOptional.Value = value;
348 this.contract.ArchiveOptional = value;
357 get => this.archiveRequired.
Value;
360 this.archiveRequired.Value = value;
361 this.contract.ArchiveRequired = value;
370 get => this.duration.
Value;
373 this.duration.Value = value;
374 this.contract.Duration = value;
383 get => this.language.Value;
384 set => this.SetLanguage(value);
387 private async
void SetLanguage(
string Language)
392 this.removeLanguage.RaiseCanExecuteChanged();
396 string FromLanguage = this.lastLanguage;
398 if (
string.IsNullOrEmpty(FromLanguage) ||
string.IsNullOrEmpty(
Language))
400 if (!
string.IsNullOrEmpty(
Language))
409 PI.DescriptionAsMarkdown =
string.Empty;
419 RI.DescriptionAsMarkdown =
string.Empty;
426 this.HumanReadableMarkdown =
string.Empty;
437 List<TranslationItem> Items =
new();
440 await Add(Items, PI, FromLanguage,
Language);
443 await Add(Items, RI, FromLanguage,
Language);
445 await Add(Items,
this, FromLanguage,
Language);
449 List<string> AllTexts =
new();
451 foreach (TranslationItem Item
in Items)
452 AllTexts.AddRange(Item.Original);
454 string[] AllTranslated = await Translator.Translate(AllTexts.ToArray(), FromLanguage,
Language, this.
OpenAiKey);
461 foreach (TranslationItem Item
in Items)
463 string[] Translated =
new string[c = Item.Original.Length];
465 Array.Copy(AllTranslated, i, Translated, 0, c);
468 Item.Object.SetTranslatableTexts(Translated,
Language);
471 this.removeLanguage.RaiseCanExecuteChanged();
473 return Task.CompletedTask;
478 this.removeLanguage.RaiseCanExecuteChanged();
488 private static async Task Add(List<TranslationItem> Items, ITranslatable Translatable,
string From,
string To)
490 string[] Texts = await Translatable.GetTranslatableTexts(To);
491 if (Texts is not
null)
492 Translatable.SetTranslatableTexts(Texts, To);
495 Texts = await Translatable.GetTranslatableTexts(From);
496 if (Texts is not
null)
498 Items.Add(
new TranslationItem()
500 Object = Translatable,
507 private class TranslationItem
509 public ITranslatable Object;
510 public string[] Original;
534 if (Texts.Length > 0)
535 this.HumanReadableMarkdown = Texts[0].Trim();
543 get => this.languages.
Value;
546 this.languages.Value = value;
547 this.removeLanguage.RaiseCanExecuteChanged();
556 get => this.signBefore.Value;
559 this.signBefore.Value = value;
560 this.contract.SignBefore = value;
569 get => this.signAfter.Value;
572 this.signAfter.Value = value;
573 this.contract.SignAfter = value;
582 get => this.parametersOk.Value;
585 this.parametersOk.Value = value;
586 this.propose.RaiseCanExecuteChanged();
595 get => this.machineReadable.Value;
596 set => this.machineReadable.Value = value;
599 private void NormalizeMachineReadableXml(
object sender, EventArgs e)
605 this.contract.ForMachines = this.machineReadable.Value.NormalizeXml();
610 this.contract.ForMachines =
null;
614 this.propose.RaiseCanExecuteChanged();
616 return Task.CompletedTask;
625 get => this.forMachines.Value;
628 this.forMachines.Value = value;
629 this.forMachinesLocalName.Value = value?.LocalName;
630 this.forMachinesNamespace.Value = value?.NamespaceURI;
639 get => this.forMachinesLocalName.Value;
647 get => this.forMachinesNamespace.Value;
655 get => this.contractId.Value;
658 this.contractId.Value = value;
659 this.contract.ContractId = value;
661 this.propose.RaiseCanExecuteChanged();
670 get => this.humanReadableMarkdown.Value;
671 set => this.humanReadableMarkdown.Value = value;
679 get => this.humanReadable.Value;
680 set => this.humanReadable.Value = value;
683 private void RenderHumanReadableMarkdown(
object sender, EventArgs e)
692 this.contract.ForHumans = this.contract.
ForHumans.Remove(this.Language);
693 this.HumanReadable =
null;
697 this.contract.ForHumans = this.contract.
ForHumans.Append(Text);
698 string Xaml = await Text.GenerateXAML(this.Contract);
699 this.HumanReadable = XamlReader.
Parse(Xaml);
719 foreach (ParameterInfo P
in this.AllParameterInfos)
728 P.Control.Background = Brushes.Salmon;
733 this.ParametersOk = Ok;
741 get => this.roles.Value;
742 set => this.roles.Value = value;
755 return Array.Empty<
string>();
757 int i, c =
Roles.Length;
758 string[] Result =
new string[c];
760 for (i = 0; i < c; i++)
761 Result[i] =
Roles[i].Name;
777 if (this.roles?.Value is
null)
780 foreach (
RoleInfo R
in this.roles.Value)
792 private void Roles_PropertyChanged(
object sender, System.ComponentModel.PropertyChangedEventArgs e)
794 this.contract.Roles = this.roles.Value.ToRoles();
802 get => this.parts.Value;
803 set => this.parts.Value = value;
806 private void Parts_PropertyChanged(
object sender, System.ComponentModel.PropertyChangedEventArgs e)
808 this.contract.Parts = this.parts.Value.ToParts();
816 get => this.parameters.
Value;
817 set => this.parameters.Value = value;
825 get => this.roleReferenceParameters.
Value;
826 set => this.roleReferenceParameters.Value = value;
834 get => this.contractReferenceParameters.
Value;
835 set => this.contractReferenceParameters.Value = value;
847 Parameters.AddRange(this.contractReferenceParameters.Value);
849 Parameters.AddRange(this.roleReferenceParameters.Value);
864 Parameters.AddRange(this.contractReferenceParameters.Value.ToParameters());
865 Parameters.AddRange(this.parameters.Value.ToParameters());
866 Parameters.AddRange(this.roleReferenceParameters.Value.ToParameters());
872 private void Parameters_PropertyChanged(
object sender, System.ComponentModel.PropertyChangedEventArgs e)
888 int c =
Roles.Length;
890 Array.Resize(ref
Roles, c + 1);
893 Name = FindNewName(
"Role", this.Roles),
894 Descriptions =
new HumanReadableText[] { await
"Enter role description as **Markdown**".ToHumanReadableText(
"en") },
909 private static string FindNewName(
string ProposedName,
INamedItem[] NamedItems)
916 string Suffix = i.ToString();
919 Result = ProposedName + Suffix;
923 if (Item.
Name == Result)
948 int c =
Roles.Length;
951 Array.Copy(
Roles, i + 1,
Roles, i, c - i - 1);
953 Array.Resize(ref
Roles, c - 1);
969 int c =
Parts.Length;
971 Array.Resize(ref
Parts, c + 1);
974 LegalId =
string.Empty,
976 },
this, this.parts);
981 return Task.CompletedTask;
995 int c =
Parts.Length;
998 Array.Copy(
Parts, i + 1,
Parts, i, c - i - 1);
1000 Array.Resize(ref
Parts, c - 1);
1022 Name = FindNewName(Name, this.AllParameterInfos),
1023 Descriptions = await MarkdownDescription.ToHumanReadableText(
"en"),
1025 Guide =
string.Empty,
1027 MaxIncluded =
false,
1029 MinIncluded =
false,
1031 Protection = ProtectionLevel.Normal
1034 this.AddParameter(this.GetParameterInfo(NP));
1039 TextBox ValueControl =
new();
1040 Binding Binding =
new(
"Value")
1044 ValueControl.SetBinding(TextBox.TextProperty, Binding);
1045 ValueControl.SetBinding(TextBox.TextProperty,
"Value");
1046 ValueControl.TextChanged += this.Parameter_TextChanged;
1048 TextBox MinControl =
new();
1049 Binding =
new(
"Value")
1053 MinControl.SetBinding(TextBox.TextProperty, Binding);
1054 MinControl.SetBinding(TextBox.TextProperty,
"Min");
1055 MinControl.TextChanged += this.Parameter_MinTextChanged;
1057 TextBox MaxControl =
new();
1058 Binding =
new Binding(
"Value")
1062 MaxControl.SetBinding(TextBox.TextProperty, Binding);
1063 MaxControl.SetBinding(TextBox.TextProperty,
"Max");
1064 MaxControl.TextChanged += this.Parameter_MaxTextChanged;
1066 CheckBox MinIncludedControl =
new();
1067 MinIncludedControl.SetBinding(CheckBox.IsCheckedProperty,
"MinIncluded");
1068 MinIncludedControl.Checked += this.Parameter_MinIncludedCheckedChanged;
1069 MinIncludedControl.Unchecked += this.Parameter_MinIncludedCheckedChanged;
1071 CheckBox MaxIncludedControl =
new();
1072 MaxIncludedControl.SetBinding(CheckBox.IsCheckedProperty,
"MaxIncluded");
1073 MaxIncludedControl.Checked += this.Parameter_MaxIncludedCheckedChanged;
1074 MaxIncludedControl.Unchecked += this.Parameter_MaxIncludedCheckedChanged;
1077 MaxControl, MaxIncludedControl,
this, this.parameters);
1100 private async Task
ExecuteAddStringParameter(
string Name,
string RegEx,
int? MaxLength, params
string[] MarkdownDescription)
1104 Name = FindNewName(Name, this.AllParameterInfos),
1105 Descriptions = await MarkdownDescription.ToHumanReadableText(
"en"),
1107 Guide =
string.Empty,
1109 MaxIncluded =
false,
1111 MinIncluded =
false,
1114 MaxLength = MaxLength,
1116 Protection = ProtectionLevel.Normal
1119 this.AddParameter(this.GetParameterInfo(SP));
1124 TextBox ValueControl =
new();
1125 ValueControl.SetBinding(TextBox.TextProperty,
"Value");
1126 ValueControl.TextChanged += this.Parameter_TextChanged;
1128 TextBox MinControl =
new();
1129 MinControl.SetBinding(TextBox.TextProperty,
"Min");
1130 MinControl.TextChanged += this.Parameter_MinTextChanged;
1132 TextBox MaxControl =
new();
1133 MaxControl.SetBinding(TextBox.TextProperty,
"Max");
1134 MaxControl.TextChanged += this.Parameter_MaxTextChanged;
1136 CheckBox MinIncludedControl =
new();
1137 MinIncludedControl.SetBinding(CheckBox.IsCheckedProperty,
"MinIncluded");
1138 MinIncludedControl.Checked += this.Parameter_MinIncludedCheckedChanged;
1139 MinIncludedControl.Unchecked += this.Parameter_MinIncludedCheckedChanged;
1141 CheckBox MaxIncludedControl =
new();
1142 MaxIncludedControl.SetBinding(CheckBox.IsCheckedProperty,
"MaxIncluded");
1143 MaxIncludedControl.Checked += this.Parameter_MaxIncludedCheckedChanged;
1144 MaxIncludedControl.Unchecked += this.Parameter_MaxIncludedCheckedChanged;
1146 TextBox MinLengthControl =
new();
1147 MinLengthControl.SetBinding(TextBox.TextProperty,
"MinLength");
1148 MinLengthControl.TextChanged += this.Parameter_MinLengthTextChanged;
1150 TextBox MaxLengthControl =
new();
1151 MaxLengthControl.SetBinding(TextBox.TextProperty,
"MaxLength");
1152 MaxLengthControl.TextChanged += this.Parameter_MaxLengthTextChanged;
1154 TextBox RegExControl =
new();
1155 RegExControl.SetBinding(TextBox.TextProperty,
"RegEx");
1156 RegExControl.TextChanged += this.Parameter_RegExTextChanged;
1159 MaxControl, MaxIncludedControl, MinLengthControl, MaxLengthControl, RegExControl,
this, this.parameters);
1170 private async
void Parameter_TextChanged(
object sender, RoutedEventArgs e)
1186 TextBox.Background = Brushes.Salmon;
1189 this.RenderHumanReadableMarkdown(
this, EventArgs.Empty);
1191 catch (Exception ex)
1197 private async
void Parameter_MinTextChanged(
object sender, RoutedEventArgs e)
1213 TextBox.Background = Brushes.Salmon;
1216 catch (Exception ex)
1222 private async
void Parameter_MaxTextChanged(
object sender, RoutedEventArgs e)
1238 TextBox.Background = Brushes.Salmon;
1241 catch (Exception ex)
1247 private async
void Parameter_MinLengthTextChanged(
object sender, RoutedEventArgs e)
1256 string Value = TextBox.Text;
1258 if (
string.IsNullOrEmpty(Value))
1259 ParameterInfo.MinLength =
null;
1261 ParameterInfo.MinLength =
int.Parse(Value);
1268 TextBox.Background = Brushes.Salmon;
1271 catch (Exception ex)
1277 private async
void Parameter_MaxLengthTextChanged(
object sender, RoutedEventArgs e)
1286 string Value = TextBox.Text;
1288 if (
string.IsNullOrEmpty(Value))
1289 ParameterInfo.MinLength =
null;
1291 ParameterInfo.MinLength =
int.Parse(Value);
1298 TextBox.Background = Brushes.Salmon;
1301 catch (Exception ex)
1307 private async
void Parameter_RegExTextChanged(
object sender, RoutedEventArgs e)
1316 ParameterInfo.RegEx = TextBox.Text;
1323 TextBox.Background = Brushes.Salmon;
1326 catch (Exception ex)
1349 Name = FindNewName(Name, this.AllParameterInfos),
1350 Descriptions = await MarkdownDescription.ToHumanReadableText(
"en"),
1352 Guide =
string.Empty,
1354 Protection = ProtectionLevel.Normal
1357 this.AddParameter(this.GetParameterInfo(BP));
1362 CheckBox CheckBox =
new()
1364 VerticalContentAlignment = VerticalAlignment.Center,
1365 HorizontalContentAlignment = HorizontalAlignment.Center
1368 CheckBox.SetBinding(CheckBox.IsCheckedProperty,
"Value");
1369 CheckBox.Checked += this.Parameter_CheckedChanged;
1370 CheckBox.Unchecked += this.Parameter_CheckedChanged;
1378 private async
void Parameter_CheckedChanged(
object sender, RoutedEventArgs e)
1385 ParameterInfo.Value = CheckBox.IsChecked;
1388 this.RenderHumanReadableMarkdown(
this, EventArgs.Empty);
1390 catch (Exception ex)
1396 private async
void Parameter_MinIncludedCheckedChanged(
object sender, RoutedEventArgs e)
1403 ParameterInfo.MinIncluded = CheckBox.IsChecked ??
false;
1407 catch (Exception ex)
1413 private async
void Parameter_MaxIncludedCheckedChanged(
object sender, RoutedEventArgs e)
1420 ParameterInfo.MaxIncluded = CheckBox.IsChecked ??
false;
1424 catch (Exception ex)
1447 Name = FindNewName(Name, this.AllParameterInfos),
1448 Descriptions = await MarkdownDescription.ToHumanReadableText(
"en"),
1450 Guide =
string.Empty,
1452 MaxIncluded =
false,
1454 MinIncluded =
false,
1456 Protection = ProtectionLevel.Normal
1459 this.AddParameter(this.GetParameterInfo(DP));
1464 TextBox ValueControl =
new();
1465 Binding Binding =
new(
"Value")
1469 ValueControl.SetBinding(TextBox.TextProperty, Binding);
1470 ValueControl.TextChanged += this.Parameter_TextChanged;
1472 TextBox MinControl =
new();
1473 Binding =
new(
"Min")
1477 MinControl.SetBinding(TextBox.TextProperty, Binding);
1478 MinControl.TextChanged += this.Parameter_MinTextChanged;
1480 TextBox MaxControl =
new();
1481 Binding =
new Binding(
"Max")
1485 MaxControl.SetBinding(TextBox.TextProperty, Binding);
1486 MaxControl.TextChanged += this.Parameter_MaxTextChanged;
1488 CheckBox MinIncludedControl =
new();
1489 MinIncludedControl.SetBinding(CheckBox.IsCheckedProperty,
"MinIncluded");
1490 MinIncludedControl.Checked += this.Parameter_MinIncludedCheckedChanged;
1491 MinIncludedControl.Unchecked += this.Parameter_MinIncludedCheckedChanged;
1493 CheckBox MaxIncludedControl =
new();
1494 MaxIncludedControl.SetBinding(CheckBox.IsCheckedProperty,
"MaxIncluded");
1495 MaxIncludedControl.Checked += this.Parameter_MaxIncludedCheckedChanged;
1496 MaxIncludedControl.Unchecked += this.Parameter_MaxIncludedCheckedChanged;
1499 MaxControl, MaxIncludedControl,
this, this.parameters);
1521 Name = FindNewName(
"DateTime", this.AllParameterInfos),
1522 Descriptions =
new HumanReadableText[] { await defaultParameterDescription.ToHumanReadableText(
"en") },
1524 Guide =
string.Empty,
1526 MaxIncluded =
false,
1528 MinIncluded =
false,
1530 Protection = ProtectionLevel.Normal
1533 this.AddParameter(this.GetParameterInfo(DP));
1538 TextBox ValueControl =
new();
1539 Binding Binding =
new(
"Value")
1543 ValueControl.SetBinding(TextBox.TextProperty, Binding);
1544 ValueControl.TextChanged += this.Parameter_TextChanged;
1546 TextBox MinControl =
new();
1547 Binding =
new Binding(
"Min")
1551 MinControl.SetBinding(TextBox.TextProperty, Binding);
1552 MinControl.TextChanged += this.Parameter_MinTextChanged;
1554 TextBox MaxControl =
new();
1555 Binding =
new Binding(
"Max")
1559 MaxControl.SetBinding(TextBox.TextProperty, Binding);
1560 MaxControl.TextChanged += this.Parameter_MaxTextChanged;
1562 CheckBox MinIncludedControl =
new();
1563 MinIncludedControl.SetBinding(CheckBox.IsCheckedProperty,
"MinIncluded");
1564 MinIncludedControl.Checked += this.Parameter_MinIncludedCheckedChanged;
1565 MinIncludedControl.Unchecked += this.Parameter_MinIncludedCheckedChanged;
1567 CheckBox MaxIncludedControl =
new();
1568 MaxIncludedControl.SetBinding(CheckBox.IsCheckedProperty,
"MaxIncluded");
1569 MaxIncludedControl.Checked += this.Parameter_MaxIncludedCheckedChanged;
1570 MaxIncludedControl.Unchecked += this.Parameter_MaxIncludedCheckedChanged;
1573 MaxControl, MaxIncludedControl,
this, this.parameters);
1600 Name = FindNewName(Name, this.AllParameterInfos),
1601 Descriptions = await MarkdownDescription.ToHumanReadableText(
"en"),
1603 Guide =
string.Empty,
1605 MaxIncluded =
false,
1607 MinIncluded =
false,
1609 Protection = ProtectionLevel.Normal
1612 this.AddParameter(this.GetParameterInfo(DP));
1617 TextBox ValueControl =
new();
1618 Binding Binding =
new(
"Value")
1622 ValueControl.SetBinding(TextBox.TextProperty, Binding);
1623 ValueControl.TextChanged += this.Parameter_TextChanged;
1625 TextBox MinControl =
new();
1626 Binding =
new Binding(
"Min")
1630 MinControl.SetBinding(TextBox.TextProperty, Binding);
1631 MinControl.TextChanged += this.Parameter_MinTextChanged;
1633 TextBox MaxControl =
new();
1634 Binding =
new(
"Min")
1638 MaxControl.SetBinding(TextBox.TextProperty, Binding);
1639 MaxControl.TextChanged += this.Parameter_MaxTextChanged;
1641 CheckBox MinIncludedControl =
new();
1642 MinIncludedControl.SetBinding(CheckBox.IsCheckedProperty,
"MinIncluded");
1643 MinIncludedControl.Checked += this.Parameter_MinIncludedCheckedChanged;
1644 MinIncludedControl.Unchecked += this.Parameter_MinIncludedCheckedChanged;
1646 CheckBox MaxIncludedControl =
new();
1647 MaxIncludedControl.SetBinding(CheckBox.IsCheckedProperty,
"MaxIncluded");
1648 MaxIncludedControl.Checked += this.Parameter_MaxIncludedCheckedChanged;
1649 MaxIncludedControl.Unchecked += this.Parameter_MaxIncludedCheckedChanged;
1652 MaxControl, MaxIncludedControl,
this, this.parameters);
1674 Name = FindNewName(
"Duration", this.AllParameterInfos),
1675 Descriptions =
new HumanReadableText[] { await defaultParameterDescription.ToHumanReadableText(
"en") },
1677 Guide =
string.Empty,
1679 MaxIncluded =
false,
1681 MinIncluded =
false,
1683 Protection = ProtectionLevel.Normal
1686 this.AddParameter(this.GetParameterInfo(DP));
1691 TextBox ValueControl =
new();
1692 Binding Binding =
new(
"Value")
1696 ValueControl.SetBinding(TextBox.TextProperty, Binding);
1697 ValueControl.TextChanged += this.Parameter_TextChanged;
1699 TextBox MinControl =
new();
1700 Binding =
new Binding(
"Min")
1704 MinControl.SetBinding(TextBox.TextProperty, Binding);
1705 MinControl.TextChanged += this.Parameter_MinTextChanged;
1707 TextBox MaxControl =
new();
1708 Binding =
new Binding(
"Max")
1712 MaxControl.SetBinding(TextBox.TextProperty, Binding);
1713 MaxControl.TextChanged += this.Parameter_MaxTextChanged;
1715 CheckBox MinIncludedControl =
new();
1716 MinIncludedControl.SetBinding(CheckBox.IsCheckedProperty,
"MinIncluded");
1717 MinIncludedControl.Checked += this.Parameter_MinIncludedCheckedChanged;
1718 MinIncludedControl.Unchecked += this.Parameter_MinIncludedCheckedChanged;
1720 CheckBox MaxIncludedControl =
new();
1721 MaxIncludedControl.SetBinding(CheckBox.IsCheckedProperty,
"MaxIncluded");
1722 MaxIncludedControl.Checked += this.Parameter_MaxIncludedCheckedChanged;
1723 MaxIncludedControl.Unchecked += this.Parameter_MaxIncludedCheckedChanged;
1726 MaxControl, MaxIncludedControl,
this, this.parameters);
1748 Name = FindNewName(
"Calc", this.AllParameterInfos),
1749 Descriptions =
new HumanReadableText[] { await defaultParameterDescription.ToHumanReadableText(
"en") },
1751 Guide =
string.Empty,
1752 Protection = ProtectionLevel.Normal
1755 this.AddParameter(this.GetParameterInfo(CP));
1770 Name = FindNewName(
"RoleRef", this.AllParameterInfos),
1771 Descriptions =
new HumanReadableText[] { await defaultParameterDescription.ToHumanReadableText(
"en") },
1773 Guide =
string.Empty,
1776 Role =
string.Empty,
1778 Protection = ProtectionLevel.Normal
1781 this.AddRoleReferenceParameter(this.GetParameterInfo(RP));
1786 TextBox ValueControl =
new()
1809 Name = FindNewName(
"ContractRef", this.AllParameterInfos),
1810 Descriptions =
new HumanReadableText[] { await defaultParameterDescription.ToHumanReadableText(
"en") },
1813 Guide =
string.Empty,
1814 CreatorRole =
string.Empty,
1815 LocalName =
string.Empty,
1816 Namespace =
string.Empty,
1817 Provider =
string.Empty,
1819 TemplateId =
string.Empty,
1821 Value = string.Empty
1824 this.AddContractReferenceParameter(this.GetParameterInfo(CRP));
1829 TextBox ValueControl =
new();
1830 ValueControl.SetBinding(TextBox.TextProperty,
"ContractId");
1831 ValueControl.TextChanged += this.Parameter_TextChanged;
1841 TextBox ValueControl =
new()
1845 Binding Binding =
new(
"Value")
1849 ValueControl.SetBinding(TextBox.TextProperty, Binding);
1929 this.ContractReferenceParameters =
Parameters;
1950 this.ContractReferenceParameters =
Parameters;
1956 public ICommand
New => this.@
new;
1958 private Task ExecuteNewContract()
1960 return this.ExecuteNewContract(
true);
1963 private async Task ExecuteNewContract(
bool QueryUser)
1968 MessageBox.Show(
"Are you sure you want clear the form and lose all data that has not been saved?",
"Confirm",
1969 MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.No) != MessageBoxResult.Yes)
1979 this.Language =
"en";
1981 this.SignBefore =
null;
1982 this.SignAfter =
null;
1983 this.Roles = Array.Empty<
RoleInfo>();
1984 this.Parts = Array.Empty<
PartInfo>();
1987 this.MachineReadable =
string.Empty;
1988 this.ForMachines =
null;
1989 this.ContractId =
string.Empty;
1990 this.HumanReadableMarkdown =
string.Empty;
1991 this.HumanReadable =
null;
1993 await this.GenerateContract();
1995 catch (Exception ex)
2006 private async Task ExecuteLoadContract()
2010 OpenFileDialog Dialog =
new()
2012 CheckFileExists =
true,
2013 CheckPathExists =
true,
2015 Filter =
"XML Files (*.xml)|*.xml|All Files (*.*)|*.*",
2016 Multiselect =
false,
2017 ShowReadOnly =
true,
2018 Title =
"Load Smart Contract"
2021 bool? Result = Dialog.ShowDialog(
MainWindow.currentInstance);
2022 if (!Result.HasValue || !Result.Value)
2025 XmlDocument Doc =
new()
2027 PreserveWhitespace =
true
2030 Doc.Load(Dialog.FileName);
2033 Contract
Contract = Parsed.Contract
2034 ??
throw new InvalidOperationException(
"Not a valid Smart Contract file.");
2037 throw new InvalidOperationException(
"Contract is not a template.");
2041 catch (Exception ex)
2052 private Task ExecuteSaveContract()
2056 SaveFileDialog Dialog =
new()
2058 AddExtension =
true,
2059 CheckFileExists =
false,
2060 CheckPathExists =
true,
2061 CreatePrompt =
false,
2063 Filter =
"XML Files (*.xml)|*.xml|All Files (*.*)|*.*",
2064 OverwritePrompt =
true,
2065 Title =
"Save Smart Contract"
2068 bool? Result = Dialog.ShowDialog(
MainWindow.currentInstance);
2069 if (!Result.HasValue || !Result.Value)
2070 return Task.CompletedTask;
2072 string Xml = this.contract.ToXml();
2073 (
string Pretty, XmlElement Parsed) = Xml.ToPrettyXml();
2075 File.WriteAllText(Dialog.FileName, Pretty);
2077 catch (Exception ex)
2082 return Task.CompletedTask;
2090 private async Task ExecuteImportContract()
2094 OpenFileDialog Dialog =
new()
2096 CheckFileExists =
true,
2097 CheckPathExists =
true,
2098 DefaultExt =
"docx",
2099 Filter =
"Microsoft Word Files (*.docx)|*.docx",
2100 Multiselect =
false,
2101 ShowReadOnly =
true,
2102 Title =
"Import from Microsoft Word"
2105 bool? Result = Dialog.ShowDialog(
MainWindow.currentInstance);
2106 if (!Result.HasValue || !Result.Value)
2110 string TempFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString().Replace(
"-",
string.Empty)) +
".docx";
2115 File.Copy(Dialog.FileName, TempFileName,
true);
2116 Markdown = WordUtilities.ExtractAsMarkdown(TempFileName, out
Language);
2120 File.Delete(TempFileName);
2123 await this.ExecuteNewContract(
false);
2125 if (ContractUtilities.ExtractParameters(ref Markdown, out Dictionary<string, ParameterInformation> HeaderInfo))
2127 foreach (KeyValuePair<string, ParameterInformation> P
in HeaderInfo)
2129 switch (P.Value.Type)
2131 case ParameterType.String:
2134 GetDescriptions(P.Value.Values));
2137 case ParameterType.Boolean:
2138 if ((P.Value.Values?.Count ?? 0) > 0 &&
CommonTypes.
TryParse(P.Value.Values[0], out
bool b))
2144 case ParameterType.StringWithOptions:
2145 StringBuilder RegEx =
new();
2148 foreach (OptionInformation Option
in P.Value.Options)
2159 RegEx.Append(
"|.*");
2162 GetDescriptions(P.Value.Values));
2165 case ParameterType.ListOfOptions:
2171 foreach (OptionInformation Option
in P.Value.Options)
2184 GetDescriptions(P.Value.Values));
2187 case ParameterType.Date:
2188 if ((P.Value.Values?.Count ?? 0) > 0 && DateTime.TryParse(P.Value.Values[0], out DateTime TP))
2194 case ParameterType.Time:
2195 if ((P.Value.Values?.Count ?? 0) > 0 && TimeSpan.TryParse(P.Value.Values[0], out TimeSpan TS))
2201 case ParameterType.Number:
2202 if ((P.Value.Values?.Count ?? 0) > 0 &&
CommonTypes.
TryParse(P.Value.Values[0], out decimal d))
2211 if (
string.IsNullOrEmpty(
Language))
2215 this.Languages =
new string[] {
Language }.ToIso639_1();
2222 StringBuilder sb =
new();
2225 XmlDocument Doc =
new();
2226 Doc.LoadXml(sb.ToString());
2228 LinkedList<XmlElement> Elements =
new();
2229 Elements.AddLast(Doc.DocumentElement);
2231 while (Elements.First is not
null)
2233 XmlElement E = Elements.First.Value;
2234 Elements.RemoveFirst();
2236 if (E.LocalName ==
"parameter")
2239 if (!
string.IsNullOrEmpty(ParameterName))
2240 Markdown = Markdown.Replace(
"`" + ParameterName +
"`",
"[%" + ParameterName +
"]");
2243 foreach (XmlNode N
in E.ChildNodes)
2245 if (N is XmlElement E2)
2246 Elements.AddLast(E2);
2250 this.HumanReadableMarkdown = Markdown;
2251 this.MachineReadable =
"<Nop xmlns=\"https://paiwise.tagroot.io/Schema/PaymentInstructions.xsd\" />";
2252 this.ContractId = Path.GetFileName(Path.ChangeExtension(Dialog.FileName,
string.Empty));
2256 catch (Exception ex)
2262 private static string[] GetDescriptions(List<string> Descriptions)
2264 if (Descriptions is
null || Descriptions.Count == 0 ||
2265 (Descriptions.Count == 1 &&
string.IsNullOrEmpty(Descriptions[0])))
2267 return new string[] { defaultParameterDescription };
2270 return Descriptions.ToArray();
2278 private Task ExecuteAddLanguage()
2283 bool? Result = Dialog.ShowDialog();
2284 if (!Result.HasValue || !Result.Value)
2285 return Task.CompletedTask;
2288 if (
string.IsNullOrEmpty(
Language))
2289 return Task.CompletedTask;
2291 this.Languages =
this.Languages.ToCodes().Append(
Language).ToIso639_1();
2292 this.Language =
Language.ToLower();
2294 return Task.CompletedTask;
2302 private bool CanExecuteRemoveLanguage()
2304 return !
string.IsNullOrEmpty(this.Language) && this.Languages.Length >= 2;
2307 private Task ExecuteRemoveLanguage()
2311 if (MessageBox.Show(
"Are you sure you want to remove language " +
Language +
" from the contract?",
"Confirm",
2312 MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.No) != MessageBoxResult.Yes)
2314 return Task.CompletedTask;
2320 foreach (
RoleInfo RI
in this.Roles)
2325 this.Languages = this.
Languages.ToCodes().Remove(Language).ToIso639_1();
2328 return Task.CompletedTask;
2336 get => this.openAiKey.Value;
2337 set => this.openAiKey.Value = value;
2345 private bool CanExecuteProposeContract()
2347 return this.Connected &&
2348 (this.ParametersOk || this.PartsMode ==
ContractParts.TemplateOnly) &&
2350 this.contract.ForMachines is not
null;
2356 this.propose.RaiseCanExecuteChanged();
2357 return base.StateChanged(NewState);
2360 private async Task ExecuteProposeContract()
2367 MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.No) != MessageBoxResult.Yes)
2375 this.contract.Parts,
this.contract.Parameters,
this.contract.Visibility,
this.contract.PartsMode,
this.contract.Duration,
2376 this.contract.ArchiveRequired,
this.contract.ArchiveOptional,
this.contract.SignAfter,
this.contract.SignBefore,
2377 this.contract.CanActAsTemplate);
2385 catch (Exception ex)
Converts DateTime values to XML strings.
Converts the date part of a DateTime values to XML strings.
Converts Duration values to strings.
static string ToString(Duration Duration)
Converts a Duration to a human-readable text.
Converts Duration values to XML strings.
Converts values to strings.
Converts TimeSpan values to XML strings.
Interaction logic for AddLanguageDialog.xaml
View model for the Add Language dialog.
Interaction logic for MainWindow.xaml
static void MouseHourglass()
Displays an hourglass cursor (or similar for waiting purposes).
static void ErrorBox(string ErrorMessage)
Displays an error message on the screen, on the main UI thread.
static void SuccessBox(string Message)
Displays a success message on the screen, on the main UI thread.
static void UpdateGui(GuiDelegate Method)
Calls a method from the Main UI thread.
Defines a custom command.
Generic class for persistant properties
Task ExecuteAddNumericParameter()
Adds a numeric parameter to the design.
static string[] PartsModes
Contract parts modes
override Task StateChanged(XmppState NewState)
Method called when connection state has changed.
RoleInfo[] Roles
Roles defined the contract.
Task ExecuteAddDateParameter()
Adds a date parameter to the design.
async Task ExecuteAddDateTimeParameter()
Adds a date and time parameter to the design.
string ForMachinesLocalName
Local Name of top element of Machine-Readable XML in the contract
Contract Contract
Current contract
ICommand AddContractReference
Command for adding a contract reference parameter
void SetTranslatableTexts(string[] Texts, string Language)
Sets translated texts.
Task ExecuteAddBooleanParameter()
Adds a boolean parameter to the design.
ContractVisibility Visibility
Contract visibility
ICommand AddStringParameter
Command for adding a string parameter
async Task ExecuteAddRole()
Adds a role to the design.
DateTime? SignBefore
If contract must be signed before a spceific time.
ICommand AddTimeParameter
Command for adding a time parameter
ICommand SaveCommand
Save command
PartInfo[] Parts
Parts defined the contract.
Parameter[] AllParameters
An array of both Parameters and RoleReferenceParameters.
ICommand AddDateTimeParameter
Command for adding a date and time parameter
XmlElement ForMachines
Parsed Machine-Readable XML in the contract
ICommand ImportCommand
Import command
ICommand LoadCommand
Load command
ICommand AddCalcParameter
Command for adding a calculation parameter
ICommand AddNumericParameter
Command for adding a numeric parameter
Waher.Content.? Duration ArchiveOptional
Optional archiving time
void RemoveParameter(ParameterInfo Parameter)
Removes a parameter from the design
string HumanReadableMarkdown
Human-readable markdown
string Language
Currently selected language.
void RemoveContractReferenceParameter(ContractReferenceParameterInfo Parameter)
Removes a contract reference parameter from the design
ICommand AddRole
Command for adding a role to the design.
ICommand Propose
Propose command
void RemoveRole(RoleInfo Role)
Removes a role from the design
object HumanReadable
Human-readable markdown
ICommand AddBooleanParameter
Command for adding a boolean parameter
static string[] Visibilities
Contract visibilities.
string[] RoleNames
Role names
Task ExecuteAddTimeParameter()
Adds a time parameter to the design.
async Task ValidateParameters()
Validates parameters.
async Task ExecuteAddCalcParameter()
Adds a calculation parameter to the design.
Task ExecuteAddStringParameter()
Adds a string parameter to the design.
ICommand AddRoleReference
Command for adding a role reference parameter
ICommand RemoveLanguage
Command for removing languages
Waher.Content.? Duration Duration
Duration of contract, once signed
async Task< string[]> GetTranslatableTexts(string Language)
Gets associated texts to translate.
string ForMachinesNamespace
Namespace of top element of Machine-Readable XML in the contract
ICommand AddDurationParameter
Command for adding a duration parameter
async Task ExecuteAddContractReference()
Adds a contract reference parameter to the design.
Task ExecuteAddPart()
Adds a part to the design.
DateTime? SignAfter
If contract must be signed after a spceific time.
string ContractId
ID of contract
bool TryGetRole(string Name, out RoleInfo Role)
Tries to get information about a given role.
void RemoveRoleReferenceParameter(RoleReferenceParameterInfo Parameter)
Removes a role reference parameter from the design
string OpenAiKey
Key to use when calling the OpenAI service.
ICommand AddDateParameter
Command for adding a date parameter
async Task ExecuteAddRoleReference()
Adds a role reference parameter to the design.
string MachineReadable
Machine-Readable XML in the contract
ParameterInfo[] AllParameterInfos
An array of both Parameters and RoleReferenceParameters.
ParameterInfo[] Parameters
Parameters defined the contract.
Iso__639_1.Record[] Languages
Available languages.
ICommand AddPart
Command for adding a part to the design.
ContractReferenceParameterInfo[] ContractReferenceParameters
Contract reference Parameters defined the contract.
ICommand AddLanguage
Command for adding languages
bool ParametersOk
If all parameters are OK or not
Waher.Content.? Duration ArchiveRequired
Required archiving time
DesignModel()
Design model
RoleReferenceParameterInfo[] RoleReferenceParameters
Role reference Parameters defined the contract.
void RemovePart(PartInfo Part)
Removes a part from the design
override async Task Start()
Starts the model.
async Task ExecuteAddDurationParameter()
Adds a duration parameter to the design.
ContractParts PartsMode
Contract parts mode
Contains information about a parameter
virtual object Value
Parameter value
abstract void SetValue(string Value)
Sets the value.
virtual async Task< bool > ValidateParameter(Variables Variables)
Method called when the parameter needs to be validated.
Parameter Parameter
Original parameter object in contract
ProtectionLevel Protection
Parameter protection level
Control Control
Generated control.
Contains information about a boolean parameter
Contains information about a calculation parameter
Contains information about a contract reference parameter
Contains information about a date parameter
Contains information about a date and time parameter
Contains information about a duration parameter
Contains information about a numerical parameter
Contains information about a string parameter
Contains information about a role reference parameter
Contains information about a string parameter
Contains information about a time parameter
Contains information about a part in the contract
Contains information about a role
override void RaisePropertyChanged(string PropertyName)
Raises the PropertyChanged event.
Role Role
Role reference object.
ContractsClient Contracts
Contracts client.
void ContractTemplateAdded(string TemplateName, Contract Contract)
A contract template has been added.
Abstract base class for view models
A model that is sensitive to the connection state.
NetworkModel Network
Network model
LegalModel Legal
Legal ID model
Generic class for persistant properties
Generic class for properties
virtual T Value
Current value of the property
Contains one record of the ISO 639-1 data set.
Static class containing ISO 639-1 language codes
Helps with parsing of commong data types.
static string RegexStringEncode(string s)
Encodes a string for inclusion in a regular expression.
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Helps with common XML-related tasks.
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Contains a reference to an attachment assigned to a legal object.
Boolean contractual parameter
Calculation contractual parameter
Represents a digital signature on a contract.
Contains the definition of a contract
static Task< ParsedContract > Parse(XmlDocument Xml)
Validates a contract XML Document, and returns the contract definition in it.
string[] GetLanguages()
Gets available languages encoded in contract.
Parameter[] Parameters
Defined parameters for the smart contract.
Duration? ArchiveRequired
Requied time to archive a signed smart contract, after it becomes obsolete.
HumanReadableText[] ForHumans
Human-readable contents of the contract.
Duration? ArchiveOptional
Optional time to archive a signed smart contract, after it becomes obsolete, and after its required a...
bool CanActAsTemplate
If the contract can act as a template for other contracts.
XmlElement ForMachines
Machine-readable contents of the contract.
Role[] Roles
Roles defined in the smart contract.
DateTime? SignAfter
Signatures will only be accepted after this point in time.
string DefaultLanguage
Default language for contract.
ContractParts PartsMode
How parts are defined in the smart contract.
Task< string > ToMarkdown(string Language)
Creates a human-readable Markdown document for the contract.
string ContractId
Contract identity
Part[] Parts
Defined parts for the smart contract.
Duration? Duration
Duration of the contract. Is counted from the time it is signed by the required parties.
DateTime? SignBefore
Signatures will only be accepted until this point in time.
ContractVisibility Visibility
Contrat Visibility
Contract-reference parameter
Task< Contract > CreateContractAsync(XmlElement ForMachines, HumanReadableText[] ForHumans, Role[] Roles, Part[] Parts, Parameter[] Parameters, ContractVisibility Visibility, ContractParts PartsMode, Duration? Duration, Duration? ArchiveRequired, Duration? ArchiveOptional, DateTime? SignAfter, DateTime? SignBefore, bool CanActAsTemplate)
Creates a new contract.
string ComponentAddress
Component address.
Date contractual parameter
Date and Time contractual parameter
Duration contractual parameter
Class representing human-readable text.
Task< string > GenerateMarkdown(Contract Contract)
Generates markdown for the human-readable text.
static HumanReadableText Parse(XmlElement Xml)
Class representing human-readable text.
override void Serialize(StringBuilder Xml)
Serializes the element in normalized form.
HumanReadableText[] Descriptions
Discriptions of the object, in different languages.
Numerical contractual parameter
Abstract base class for contractual parameters
abstract void Populate(Variables Variables)
Populates a variable collection with the value of the parameter.
Contains information about a parsed contract.
Class defining a part in a contract
Role-reference contractual parameter
Represents a server signature on a contract.
String-valued contractual parameter
Time contractual parameter
Static class managing persistent settings.
static async Task< bool > SetAsync(string Key, string Value)
Sets a string-valued setting.
Class managing a script expression.
Interface for named items
Interface for models allowing editing of parts
Interface for translatable objects.
MarkdownType
Type of Markdown to generate
ContractParts
How the parts of the contract are defined.
ProtectionLevel
Parameter protection levels
ContractVisibility
Visibility types for contracts.
XmppState
State of XMPP connection.
Represents a duration value, as defined by the xsd:duration data type: http://www....
static readonly Duration Zero
Zero value