diff --git a/README.md b/README.md index 295c13c..a8b0808 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,8 @@ forms: form_field: - name: "formularfeld" hide: true + - name: "otherformfield" + default_value: "T" form_references: - name: "ref_first_mtb" referenced_data_form: "Formularverweis.Variante" @@ -218,6 +220,9 @@ Ein zuvor bestehender Filter wird ersetzt. Weiterhin wird die Eigenschaft "Speichern" des Formularfelds auf "Immer speichern" gesetzt um sicherzustellen, dass zuvor enthaltene Daten weiterhin gespeichert bleiben und werden, auch wenn das Formularfeld nicht sichtbar ist. +Der Standardwert des Feldes `otherformfield` ist nun auf `T` gesetzt. +Zum Löschen eines Standardwerts ist `""` anzugeben. + **Achtung!** Diese Anwendung überprüft keine Scripts und verwendet angegebene Scripts als "valid" im resultierenden OSC-File. Zudem kann die Menükategorie angepasst werden. diff --git a/src/model/data_form.rs b/src/model/data_form.rs index 76080fd..a038a13 100644 --- a/src/model/data_form.rs +++ b/src/model/data_form.rs @@ -28,16 +28,16 @@ use std::collections::HashSet; use console::style; use serde::{Deserialize, Serialize}; +use crate::checks::{Checkable, CheckNotice}; use crate::checks::CheckNotice::ErrorWithCode; -use crate::checks::{CheckNotice, Checkable}; -use crate::model::onkostar_editor::OnkostarEditor; -use crate::model::requirements::{Requirement, Requires}; use crate::model::{ - apply_profile_to_form_entry, apply_profile_to_form_field, Ansichten, Comparable, Entries, + Ansichten, apply_profile_to_form_entry, apply_profile_to_form_field, Comparable, Entries, Filter, FolderContent, FormEntry, FormEntryContainer, Kennzahlen, Listable, MenuCategory, PlausibilityRules, PunkteKategorien, RefEntries, Script, Sortable, }; use crate::model::{Haeufigkeiten, Ordner}; +use crate::model::onkostar_editor::OnkostarEditor; +use crate::model::requirements::{Requirement, Requires}; use crate::profile::Profile; #[derive(Serialize, Deserialize, Debug)] @@ -190,7 +190,7 @@ impl FormEntryContainer for DataForm { apply_profile_to_form_entry(entry, form_reference) }); - // Hide form field using filter set to "false" if requested + // Hide form field using filter set to "false" if requested and change default value profile_form .form_fields .iter() @@ -647,6 +647,10 @@ impl FormEntry for Entry { }); } + fn update_default_value(&mut self, value: String) { + self.default_value = value + } + fn hide(&mut self) { self.filter = Some(Filter { condition: "false".into(), @@ -683,3 +687,112 @@ pub struct DataFormEntries { #[serde(rename = "EntryName")] entry_name: Option>, } + +#[cfg(test)] +mod tests { + use std::str::FromStr; + + use crate::model::onkostar_editor::OnkostarEditor; + use crate::profile::Profile; + + #[test] + fn should_change_dataform_entry_default_value() { + let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc")); + + assert!(onkostar_editor.is_ok()); + let mut onkostar_editor = onkostar_editor.unwrap(); + + let profile = "forms: + - name: 'Hauptformular' + form_fields: + - name: Auswahl + default_value: 'B' + "; + + let profile = Profile::from_str(profile); + assert!(profile.is_ok()); + let profile = profile.unwrap(); + + assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].name, "Auswahl"); + assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].default_value, ""); + + onkostar_editor.apply_profile(&profile); + + assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].name, "Auswahl"); + assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].default_value, "B") + } + + #[test] + fn should_not_change_dataform_entry_default_value() { + let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc")); + + assert!(onkostar_editor.is_ok()); + let mut onkostar_editor = onkostar_editor.unwrap(); + + let profile = "forms: + - name: 'Hauptformular' + "; + + let profile = Profile::from_str(profile); + assert!(profile.is_ok()); + let profile = profile.unwrap(); + + assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].name, "Auswahl"); + assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].default_value, ""); + + onkostar_editor.apply_profile(&profile); + + assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].name, "Auswahl"); + assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].default_value, "") + } + + #[test] + fn should_change_menu_category() { + let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc")); + + assert!(onkostar_editor.is_ok()); + let mut onkostar_editor = onkostar_editor.unwrap(); + + let profile = "forms: + - name: 'Hauptformular' + menu_category: + name: Testformulare + position: 1.0 + column: 1 + "; + + let profile = Profile::from_str(profile); + assert!(profile.is_ok()); + let profile = profile.unwrap(); + + onkostar_editor.apply_profile(&profile); + + match &onkostar_editor.editor.data_form[0].menu_category { + Some(menu_category) => assert_eq!(menu_category.name, "Testformulare"), + _ => panic!("Test failed: MenuCategory not found!") + } + } + + #[test] + fn should_keep_menu_category() { + let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc")); + + assert!(onkostar_editor.is_ok()); + let mut onkostar_editor = onkostar_editor.unwrap(); + + let profile = "forms: + - name: 'Hauptformular' + "; + + let profile = Profile::from_str(profile); + assert!(profile.is_ok()); + let profile = profile.unwrap(); + + onkostar_editor.apply_profile(&profile); + + match &onkostar_editor.editor.data_form[0].menu_category { + Some(menu_category) => assert_eq!(menu_category.name, "Test"), + _ => panic!("Test failed: MenuCategory not found!") + } + } +} diff --git a/src/model/mod.rs b/src/model/mod.rs index 78d2977..6767cf6 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -335,6 +335,12 @@ where if entry.get_name() == form_field.name && form_field.hide { entry.hide() } + + if entry.get_name() == form_field.name { + if let Some(new_default_value) = &form_field.default_value { + entry.update_default_value(new_default_value.to_string()) + } + } } pub trait FormEntryContainer { @@ -379,6 +385,7 @@ pub trait FormEntry { fn update_anzeige(&mut self, value: String); fn update_anzeige_auswahl(&mut self, value: String); fn update_scripts_code(&mut self, value: String); + fn update_default_value(&mut self, value: String); fn hide(&mut self); } diff --git a/src/model/onkostar_editor.rs b/src/model/onkostar_editor.rs index 71fb985..61707c8 100644 --- a/src/model/onkostar_editor.rs +++ b/src/model/onkostar_editor.rs @@ -45,7 +45,7 @@ pub struct OnkostarEditor { #[serde(rename = "InfoXML")] info_xml: InfoXML, #[serde(rename = "Editor")] - editor: Editor, + pub editor: Editor, } impl OnkostarEditor { @@ -506,13 +506,13 @@ pub struct InfoXML { #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] -struct Editor { +pub struct Editor { #[serde(rename = "PropertyCatalogue", default)] property_catalogue: Vec, #[serde(rename = "DataCatalogue", default)] data_catalogue: Vec, #[serde(rename = "Unterformular", default)] - unterformular: Vec, + pub unterformular: Vec, #[serde(rename = "DataForm", default)] - data_form: Vec, + pub data_form: Vec, } diff --git a/src/model/unterformular.rs b/src/model/unterformular.rs index b75fcc2..fc4a721 100644 --- a/src/model/unterformular.rs +++ b/src/model/unterformular.rs @@ -25,19 +25,19 @@ use std::cmp::Ordering; use std::collections::HashSet; -use crate::checks::CheckNotice::ErrorWithCode; -use crate::checks::{CheckNotice, Checkable}; use console::style; use serde::{Deserialize, Serialize}; -use crate::model::onkostar_editor::OnkostarEditor; -use crate::model::requirements::{Requirement, Requires}; +use crate::checks::{Checkable, CheckNotice}; +use crate::checks::CheckNotice::ErrorWithCode; use crate::model::{ - apply_profile_to_form_entry, apply_profile_to_form_field, Ansichten, Comparable, Entries, + Ansichten, apply_profile_to_form_entry, apply_profile_to_form_field, Comparable, Entries, Filter, FolderContent, FormEntry, FormEntryContainer, Kennzahlen, Listable, MenuCategory, PlausibilityRules, PunkteKategorien, RefEntries, Script, Sortable, }; use crate::model::{Haeufigkeiten, Ordner}; +use crate::model::onkostar_editor::OnkostarEditor; +use crate::model::requirements::{Requirement, Requires}; use crate::profile::Profile; #[derive(Serialize, Deserialize, Debug)] @@ -193,7 +193,7 @@ impl FormEntryContainer for Unterformular { apply_profile_to_form_entry(entry, form_reference) }); - // Hide form field using filter set to "false" if requested + // Hide form field using filter set to "false" if requested and change default value profile_form .form_fields .iter() @@ -645,6 +645,10 @@ impl FormEntry for Entry { }); } + fn update_default_value(&mut self, value: String) { + self.default_value = value + } + fn hide(&mut self) { self.filter = Some(Filter { condition: "false".into(), @@ -682,3 +686,82 @@ pub struct DataFormEntries { #[serde(skip_serializing_if = "Option::is_none")] entry_name: Option>, } + +#[cfg(test)] +mod tests { + use std::str::FromStr; + + use crate::model::onkostar_editor::OnkostarEditor; + use crate::profile::Profile; + + #[test] + fn should_change_dataform_entry_default_value() { + let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc")); + + assert!(onkostar_editor.is_ok()); + let mut onkostar_editor = onkostar_editor.unwrap(); + + let profile = "forms: + - name: 'Unterformular' + form_fields: + - name: Termin + default_value: '2024-03-18' + "; + + let profile = Profile::from_str(profile); + assert!(profile.is_ok()); + let profile = profile.unwrap(); + + assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].name, "Termin"); + assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].default_value, ""); + + onkostar_editor.apply_profile(&profile); + + assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].name, "Termin"); + assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].default_value, "2024-03-18") + } + + #[test] + fn should_not_change_dataform_entry_default_value() { + let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc")); + + assert!(onkostar_editor.is_ok()); + let mut onkostar_editor = onkostar_editor.unwrap(); + + let profile = "forms: + - name: 'Unterformular' + "; + + let profile = Profile::from_str(profile); + assert!(profile.is_ok()); + let profile = profile.unwrap(); + + assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].name, "Termin"); + assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].default_value, ""); + + onkostar_editor.apply_profile(&profile); + + assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].name, "Termin"); + assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].default_value, "") + } + + #[test] + fn should_ignore_menu_category_for_subform() { + let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc")); + + assert!(onkostar_editor.is_ok()); + let mut onkostar_editor = onkostar_editor.unwrap(); + + let profile = "forms: + - name: 'Unterformular' + "; + + let profile = Profile::from_str(profile); + assert!(profile.is_ok()); + let profile = profile.unwrap(); + + onkostar_editor.apply_profile(&profile); + + assert!(&onkostar_editor.editor.unterformular[0].menu_category.is_none()); + } +} \ No newline at end of file diff --git a/src/profile.rs b/src/profile.rs index 41af8f1..f26d01e 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -88,6 +88,7 @@ pub struct FormField { pub name: String, #[serde(default)] pub hide: bool, + pub default_value: Option } #[derive(Deserialize)] @@ -263,4 +264,24 @@ mod tests { Err(e) => panic!("Cannot deserialize profile: {}", e), } } + + #[test] + fn should_deserialize_form_fields_with_default_value() { + let content = "forms: + - name: 'DNPM Therapieplan' + form_fields: + - name: formularfeld_to_keep + default_value: 'X' + "; + + match Profile::from_str(content) { + Ok(profile) => { + assert_eq!(profile.forms.len(), 1); + assert_eq!(profile.forms[0].name, "DNPM Therapieplan"); + assert_eq!(profile.forms[0].form_fields[0].name, "formularfeld_to_keep"); + assert_eq!(profile.forms[0].form_fields[0].default_value, Some("X".to_string())); + } + Err(e) => panic!("Cannot deserialize profile: {}", e), + } + } } diff --git a/tests/test.osc b/tests/test.osc new file mode 100644 index 0000000..e8da0fc --- /dev/null +++ b/tests/test.osc @@ -0,0 +1,672 @@ + + + + 2024-03-18Z + OnkoStar + 2.12.4 + + + + TEST.Property + Testproperty + SIMPLE + true + 1001 + 6461ffdd-d708-11e5-b199-0050568f1add + 1 + + + 1 + 2024-03-18Z + TEST.Property.v1 + true + V1 + 1001 + 646601a4-d708-11e5-b199-0050568f1add + 1 + + + A + A-Wert + Der A-Wert + + 2.0 + + + B + B-Wert + Der B-Wert + + 3.0 + + + + + + + + ONKOSTAR Bibliothek + + Test + 1 + + + ONKOSTAR Bibliothek + + Basis + 1 + + + + + Hauptformulardaten + Hauptformulardaten + -3 + Hauptformulardaten + + false + false + 20119 + 4d1488e7-c4d6-459a-be1e-330097d815ce + 4 + + + Auswahl + Auswahl + Auswahl A oder B + shorttext + + + 255 + + true + false + false + false + 0.0 + + false + false + 0 + false + + 20119 + ee1e178f-cdb1-4d7f-a415-7b19cc5da21d + 1 + + + Datum + Datum + Datum + date + + + 0 + + true + false + false + false + 0.0 + + false + false + 0 + false + + 20119 + 34874112-d651-457b-a554-178a43ed38ed + 1 + + + + + Benutzer Bibliothek + + Test + 2 + + + + Unterformulardaten + Unterformulardaten + -3 + Unterformulardaten + + false + false + 20119 + 4d1488e7-c4d6-459a-be1e-330097d815cd + 4 + + + Termin + Termin + Termin + date + + + 0 + + true + false + false + false + 0.0 + + false + false + 0 + false + + 20119 + 44874112-e651-457b-a554-178a43ed38ed + 1 + + + + + Benutzer Bibliothek + + Test + 2 + + + + + Unterformulardaten + + -3 + Unterformular + 1 + Unterformular + Unterformulardaten + Unterformular + + false + true + 0 + -1 + false + + + + + + + + + + 2 + false + true + false + true + false + false + false + false + false + 20119 + b6c986ee-9f6e-4052-be71-f5c20efbb7d8 + 1 + true + + + group + Feldgruppe1 + Feldgruppe1 + true + false + true + 1.0 + + + + false + false + + 0 + 0 + + + none + 0 + 0 + false + false + + + + 1 + true + false + 0 + 0 + false + + + true + false + false + + + 0 + 0 + 0 + 0 + 0 + false + false + false + 1 + 0 + 0 + true + + false + false + 0 + 0 + true + false + 0 + 20119 + 317f11b6-41be-4d80-abf6-78d7100f2f58 + 2 + false + false + + + datefield + Termin + Termin + true + false + true + 1.0 + + + + false + false + + 0 + 0 + Termin + Unterformulardaten + Feldgruppe1 + date + 0 + 0 + false + false + + + + 1 + true + false + 0 + 0 + false + + + true + false + false + + + 0 + 0 + 0 + 0 + 0 + false + false + false + 1 + 0 + 0 + true + + false + false + 0 + 0 + true + false + 0 + 20119 + c95c0989-4489-4eb6-916e-000e1bfc8453 + 1 + false + false + + + + + + + + Benutzer Bibliothek + + DNPM + 3 + + + + + Hauptformulardaten + + -3 + Hauptformular + 1 + Hauptformular + Hauptformular + Test Hauptformular + + false + true + 0 + 0 + false + <div style="margin-left:10px">&#10;<font style="font-weight: bold;color: orange">Hauptformular</font> &#10;<p>Datum: {datum}</p>&#10;</div> + <div style="margin-left:10px">&#10;<font style="font-weight: bold;color: orange">Hauptformular</font> &#10;<p>Datum: {datum}</p>&#10;</div> + + + + + + + + 0 + false + false + true + true + false + false + false + false + true + 20119 + 24dd2b02-a9ed-4bc3-8d6f-e82e037068cd + 166 + true + + + group + Feldgruppe2 + Feldgruppe2 + true + false + true + 1.0 + + + + false + false + + 0 + 0 + + + none + 0 + 0 + false + false + + + + 1 + true + false + 0 + 0 + false + + + true + false + false + + + 0 + 0 + 0 + 0 + 0 + false + false + false + 1 + 0 + 0 + true + + false + false + 0 + 0 + true + false + 0 + 20119 + 317f11b6-41be-4d80-abf6-78d7100f2f58 + 2 + false + false + + + button + Button1 + Testbutton + true + false + false + 10.0 + + + + false + false + + 0 + 0 + + Feldgruppe2 + none + 0 + 0 + false + false + + + + 1 + true + false + 0 + 0 + false + + + true + false + false + + + 0 + 0 + 0 + -1 + 0 + false + false + false + 1 + 0 + 0 + true + + false + false + 0 + 0 + true + false + 0 + 20119 + 8a7627c4-79ff-4d01-a0ae-915dfb9be0be + 4 + false + false + + + combobox + Auswahl + Aktueller Wert + true + false + true + 1.0 + + false + + 1 + 0 + Auswahl + TEST.Property + + none + 0 + 0 + mandatory + false + + + + 1 + true + false + 0 + 0 + false + Wählen Sie hier den Wert aus. + + true + false + false + code + code,kurz + 0 + 0 + 0 + 0 + 0 + false + false + false + 1 + 0 + 0 + true + + false + false + 1 + 0 + true + false + 0 + 20119 + 9c9cf713-efd9-4089-b615-1519d7701af9 + 6 + false + false + + + subform + Unterformular + Unterformular + true + false + false + 7.5 + + false + + 0 + 0 + + + none + 0 + 0 + false + false + Unterformular + + + + 1 + true + false + 0 + 0 + false + + + true + false + false + + + 0 + 0 + 0 + 0 + 0 + false + false + false + 1 + 0 + 0 + true + + false + false + 0 + 0 + true + false + 0 + 20119 + 443217be-d7c4-45c0-b9a1-0cc59483ef8d + 3 + false + false + + + + + + + + Benutzer Bibliothek + + Test + 3 + + + Test + 3.0 + 1 + + + + \ No newline at end of file