diff --git a/src/model/data_form.rs b/src/model/data_form.rs index 76080fd..adae6e3 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,62 @@ 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, "") + } +} 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..bb095d1 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,62 @@ 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, "") + } +} \ No newline at end of file