1
0
mirror of https://github.com/pcvolkmer/osc-variant.git synced 2025-04-19 11:46:50 +00:00

refactor: merge DataForm and Unterformular (#36)

* refactor: merge DataForm and Unterformular
* refactor: change visibility
This commit is contained in:
Paul-Christian Volkmer 2024-11-07 19:06:06 +01:00 committed by GitHub
parent 15c79f8109
commit 622811df5a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 335 additions and 733 deletions

View File

@ -17,12 +17,13 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
use std::cmp::Ordering;
use std::collections::HashSet;
use console::style;
use serde::{Deserialize, Serialize};
use std::any::TypeId;
use std::cmp::Ordering;
use std::collections::HashSet;
use std::fmt::Debug;
use std::marker::PhantomData;
use crate::checks::CheckNotice::ErrorWithCode;
use crate::checks::{CheckNotice, Checkable};
@ -37,9 +38,18 @@ use crate::model::{
use crate::model::{Haeufigkeiten, Ordner};
use crate::profile::Profile;
#[derive(Debug)]
pub struct DataFormType;
#[derive(Debug)]
pub struct UnterformularType;
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct DataForm {
pub struct Form<Type> {
#[serde(skip)]
form_type: PhantomData<Type>,
#[serde(rename = "DataCatalogues")]
data_catalogues: DataCatalogues,
#[serde(rename = "Category")]
@ -191,7 +201,7 @@ pub struct DataForm {
ansichten: Option<Ansichten>,
}
impl FormEntryContainer for DataForm {
impl<Type: 'static> FormEntryContainer for Form<Type> {
fn apply_profile(&mut self, profile: &Profile) {
profile.forms.iter().for_each(|profile_form| {
if self.name == profile_form.name {
@ -222,10 +232,15 @@ impl FormEntryContainer for DataForm {
}
}
impl Listable for DataForm {
impl<Type: 'static> Listable for Form<Type> {
fn to_listed_string(&self) -> String {
format!(
"Formular ({}) '{}' in Revision '{}'",
"{} ({}) '{}' in Revision '{}'",
if TypeId::of::<Type>() == TypeId::of::<DataFormType>() {
"Formular"
} else {
"Unterformular"
},
match self.is_system_library_content() {
true => style("S").yellow(),
_ => style("u"),
@ -236,7 +251,7 @@ impl Listable for DataForm {
}
}
impl Sortable for DataForm {
impl<Type: 'static> Sortable for Form<Type> {
fn sorting_key(&self) -> String {
self.name.clone()
}
@ -265,7 +280,10 @@ impl Sortable for DataForm {
}
}
impl Comparable for DataForm {
impl<Type> Comparable for Form<Type>
where
Type: Debug + 'static,
{
fn get_name(&self) -> String {
self.name.clone()
}
@ -290,7 +308,10 @@ impl Comparable for DataForm {
}
}
impl Requires for DataForm {
impl<Type> Requires for Form<Type>
where
Self: Listable + 'static,
{
fn requires_form_reference(&self, name: &str) -> bool {
self.entries
.entry
@ -385,13 +406,13 @@ impl Requires for DataForm {
}
}
impl FolderContent for DataForm {
impl<Type: 'static> FolderContent for Form<Type> {
fn get_library_folder(&self) -> String {
self.ordner.bibliothek.name.to_string()
}
}
impl Checkable for DataForm {
impl Checkable for Form<DataFormType> {
fn check(&self) -> Vec<CheckNotice> {
if self
.entries
@ -415,6 +436,24 @@ impl Checkable for DataForm {
}
}
impl Checkable for Form<UnterformularType> {
fn check(&self) -> Vec<CheckNotice> {
if self.hat_unterformulare {
return vec![ErrorWithCode {
code: "2023-0001".to_string(),
description: format!(
"Unterformular '{}' mit Markierung 'hat Unterformulare'",
self.name
),
line: None,
example: None,
}];
}
vec![]
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct DataCatalogues {
@ -770,4 +809,269 @@ mod tests {
None
);
}
#[test]
fn should_change_unterformular_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_unterformular_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());
}
#[test]
fn should_change_unterformular_entry_scripts_code_with_form_fields() {
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
scripts_code: |-
// Example code
console.log(42);
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].scripts,
None
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].scripts,
Some(Script {
code: "// Example code&#10;console.log(42);".into(),
valid: true
})
);
}
#[test]
fn should_change_unterformular_entry_scripts_code_with_form_references() {
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
scripts_code: |-
// Example code
console.log(42);
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].scripts,
None
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].scripts,
Some(Script {
code: "// Example code&#10;console.log(42);".into(),
valid: true
})
);
}
#[test]
fn should_remove_unterformular_entry_filter_with_form_fields() {
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
remove_filter: true
scripts_code: |-
// Example code
console.log(42);
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].filter,
Some(Filter {
condition: "getGlobalSetting('mehrere_mtb_in_mtbepisode') = 'true'".into(),
valid: true,
ref_entries: Some(RefEntries { ref_entry: None })
})
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].filter,
None
);
}
#[test]
fn should_remove_unterformular_entry_filter_with_form_references() {
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
remove_filter: true
scripts_code: |-
// Example code
console.log(42);
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].filter,
Some(Filter {
condition: "getGlobalSetting('mehrere_mtb_in_mtbepisode') = 'true'".into(),
valid: true,
ref_entries: Some(RefEntries { ref_entry: None })
})
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].filter,
None
);
}
}

View File

@ -29,12 +29,11 @@ use crate::model::requirements::Requires;
use crate::profile::{FormField, FormReference, Profile, WithScriptsCode};
pub mod data_catalogue;
pub mod data_form;
pub mod form;
pub mod onkostar_editor;
pub mod other;
pub mod property_catalogue;
pub mod requirements;
pub mod unterformular;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(deny_unknown_fields)]

View File

@ -28,11 +28,10 @@ use serde::{Deserialize, Serialize};
use crate::checks::{CheckNotice, Checkable};
use crate::model::data_catalogue::DataCatalogue;
use crate::model::data_form::DataForm;
use crate::model::form::{DataFormType, Form, UnterformularType};
use crate::model::other::{Ablaufschema, Akte, RecordLinkage, Rskript, SidGuid};
use crate::model::property_catalogue::PropertyCatalogue;
use crate::model::requirements::{Requirement, Requires};
use crate::model::unterformular::Unterformular;
use crate::model::{Comparable, FolderContent, FormEntryContainer, Listable, Sortable};
use crate::profile::Profile;
@ -72,7 +71,7 @@ impl OnkostarEditor {
}
}
pub fn find_data_form(&self, name: &str) -> Option<&DataForm> {
pub fn find_data_form(&self, name: &str) -> Option<&Form<DataFormType>> {
match self
.editor
.data_form
@ -85,7 +84,7 @@ impl OnkostarEditor {
}
}
pub fn find_unterformular(&self, name: &str) -> Option<&Unterformular> {
pub fn find_unterformular(&self, name: &str) -> Option<&Form<UnterformularType>> {
match self
.editor
.unterformular
@ -233,7 +232,7 @@ impl OnkostarEditor {
self.editor
.data_form
.sort_unstable_by(DataForm::compare_by_requirement);
.sort_unstable_by(Form::compare_by_requirement);
self.editor.data_form.iter_mut().for_each(|item| {
item.sorted();
@ -245,7 +244,7 @@ impl OnkostarEditor {
self.editor
.unterformular
.sort_unstable_by(Unterformular::compare_by_requirement);
.sort_unstable_by(Form::compare_by_requirement);
self.editor.unterformular.iter_mut().for_each(|item| {
item.sorted();
@ -519,9 +518,9 @@ pub struct Editor {
#[serde(rename = "DataCatalogue", default)]
data_catalogue: Vec<DataCatalogue>,
#[serde(rename = "Unterformular", default)]
pub unterformular: Vec<Unterformular>,
pub unterformular: Vec<Form<UnterformularType>>,
#[serde(rename = "DataForm", default)]
pub data_form: Vec<DataForm>,
pub data_form: Vec<Form<DataFormType>>,
#[serde(rename = "Ablaufschema", default)]
#[serde(skip_serializing_if = "Option::is_none")]

View File

@ -270,9 +270,9 @@ pub struct Entry {
#[serde(skip_serializing_if = "Option::is_none")]
parent_ref_id: Option<u32>,
#[serde(rename = "Type")]
pub type_: String,
pub(crate) type_: String,
#[serde(rename = "Name")]
pub name: String,
pub(crate) name: String,
#[serde(rename = "Description")]
description: String,
#[serde(rename = "Active")]
@ -297,7 +297,7 @@ pub struct Entry {
#[serde(rename = "MultipleChoice")]
multiple_choice: bool,
#[serde(rename = "DefaultValue")]
pub default_value: String,
pub(crate) default_value: String,
#[serde(rename = "Alignment")]
alignment: String,
#[serde(rename = "Direction")]
@ -310,7 +310,7 @@ pub struct Entry {
#[serde(rename = "ElementParent")]
element_parent: String,
#[serde(rename = "ProcedureDateStatus")]
pub procedure_date_status: String,
pub(crate) procedure_date_status: String,
#[serde(rename = "ZuordnungErkrankung")]
zuordnung_erkrankung: String,
#[serde(rename = "Grafik")]
@ -326,7 +326,7 @@ pub struct Entry {
datenart: Option<String>,
#[serde(rename = "Filter")]
#[serde(skip_serializing_if = "Option::is_none")]
pub filter: Option<Filter>,
pub(crate) filter: Option<Filter>,
#[serde(rename = "NotSpecified")]
not_specified: bool,
#[serde(rename = "Scripts")]

View File

@ -19,10 +19,9 @@
*/
use crate::model::data_catalogue::DataCatalogue;
use crate::model::data_form::DataForm;
use crate::model::form::{DataFormType, Form, UnterformularType};
use crate::model::onkostar_editor::OnkostarEditor;
use crate::model::property_catalogue::PropertyCatalogue;
use crate::model::unterformular::Unterformular;
use crate::model::{Comparable, Listable, Sortable};
use std::fmt::Display;
@ -32,14 +31,14 @@ pub enum Requirement<'a> {
DataCatalogue(&'a DataCatalogue),
ExternalPropertyCatalogue(String),
ExternalDataCatalogue(String),
DataFormReference(&'a DataForm),
UnterformularReference(&'a Unterformular),
DataFormReference(&'a Form<DataFormType>),
UnterformularReference(&'a Form<UnterformularType>),
#[allow(dead_code)]
ExternalDataFormReference(String),
ExternalUnterformularReference(String),
DataFormSubform(&'a DataForm),
UnterformularSubform(&'a Unterformular),
DataFormSubform(&'a Form<DataFormType>),
UnterformularSubform(&'a Form<UnterformularType>),
#[allow(dead_code)]
ExternalDataFormSubform(String),
ExternalUnterformularSubform(String),

View File

@ -1,699 +0,0 @@
/*
* This file is part of osc-variant
*
* Copyright (C) 2023-2024 the original author or authors.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
use std::cmp::Ordering;
use std::collections::HashSet;
use console::style;
use serde::{Deserialize, Serialize};
use crate::checks::CheckNotice::ErrorWithCode;
use crate::checks::{CheckNotice, Checkable};
use crate::model::onkostar_editor::OnkostarEditor;
use crate::model::other::Entry;
use crate::model::requirements::{Requirement, Requires};
use crate::model::{
apply_profile_to_form_entry, apply_profile_to_form_field, Ansichten, Comparable, Entries,
FolderContent, FormEntry, FormEntryContainer, Kennzahlen, Listable, MenuCategory,
PlausibilityRules, PunkteKategorien, Script, Sortable,
};
use crate::model::{Haeufigkeiten, Ordner};
use crate::profile::Profile;
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Unterformular {
#[serde(rename = "DataCatalogues")]
data_catalogues: DataCatalogues,
#[serde(rename = "Category")]
category: String,
#[serde(rename = "Name")]
name: String,
#[serde(rename = "Version")]
version: String,
#[serde(rename = "MenuEntry")]
menu_entry: String,
#[serde(rename = "Title")]
title: String,
#[serde(rename = "Description")]
description: String,
#[serde(rename = "Note")]
note: String,
#[serde(rename = "Readonly")]
readonly: bool,
#[serde(rename = "Active")]
active: bool,
#[serde(rename = "TudokPosition")]
tudok_position: String,
#[serde(rename = "Aktenbereich")]
#[serde(skip_serializing_if = "Option::is_none")]
aktenbereich: Option<String>,
#[serde(rename = "BefragungRelevant")]
#[serde(skip_serializing_if = "Option::is_none")]
befragung_relevant: Option<bool>,
#[serde(rename = "Hotkey")]
#[serde(skip_serializing_if = "Option::is_none")]
hotkey: Option<String>,
#[serde(rename = "Summary")]
summary: String,
#[serde(rename = "BigSummary")]
big_summary: String,
#[serde(rename = "KalenderSchnipsel")]
#[serde(skip_serializing_if = "Option::is_none")]
kalender_schnipsel: Option<String>,
#[serde(rename = "EmailTemplate")]
#[serde(skip_serializing_if = "Option::is_none")]
mail_template: Option<String>,
#[serde(rename = "ErkrankungText", default)]
#[serde(skip_serializing_if = "Option::is_none")]
erkrankung_text: Option<String>,
#[serde(rename = "ErkrankungTextLong")]
#[serde(skip_serializing_if = "Option::is_none")]
erkrankung_text_long: Option<String>,
#[serde(rename = "ErkrankungProzedurText")]
#[serde(skip_serializing_if = "Option::is_none")]
erkrankung_prozedur_text: Option<String>,
#[serde(rename = "ErkrankungSummary")]
#[serde(skip_serializing_if = "Option::is_none")]
erkrankung_summary: Option<String>,
#[serde(rename = "ErkrankungBigSummary")]
#[serde(skip_serializing_if = "Option::is_none")]
erkrankung_big_summary: Option<String>,
#[serde(rename = "Kontext")]
#[serde(skip_serializing_if = "Option::is_none")]
kontext: Option<i32>,
#[serde(rename = "Datenart")]
#[serde(skip_serializing_if = "Option::is_none")]
datenart: Option<String>,
#[serde(rename = "ShowHistoryButton", default)]
#[serde(skip_serializing_if = "Option::is_none")]
show_history_button: Option<bool>,
#[serde(rename = "TudokReadonly")]
#[serde(skip_serializing_if = "Option::is_none")]
tudok_readonly: Option<bool>,
#[serde(rename = "VitalstatusRelevant")]
#[serde(skip_serializing_if = "Option::is_none")]
vitalstatus_relevant: Option<bool>,
#[serde(rename = "AutoNummerierung")]
#[serde(skip_serializing_if = "Option::is_none")]
auto_nummerierung: Option<bool>,
#[serde(rename = "Zwischenspeichern")]
#[serde(skip_serializing_if = "Option::is_none")]
zwischenspeichern: Option<bool>,
#[serde(rename = "Zurueckblaettern")]
#[serde(skip_serializing_if = "Option::is_none")]
zurueckblaettern: Option<bool>,
#[serde(rename = "Datenbankexport")]
#[serde(skip_serializing_if = "Option::is_none")]
datenbankexport: Option<bool>,
#[serde(rename = "DatenschutzRelevant")]
#[serde(skip_serializing_if = "Option::is_none")]
datenschutz_relevant: Option<bool>,
#[serde(rename = "KonferenzRelevant")]
#[serde(skip_serializing_if = "Option::is_none")]
konferenz_relevant: Option<bool>,
#[serde(rename = "Drucken")]
#[serde(skip_serializing_if = "Option::is_none")]
drucken: Option<String>,
#[serde(rename = "hatUnterformulare")]
hat_unterformulare: bool,
#[serde(rename = "ScriptBeimSchliessen")]
#[serde(skip_serializing_if = "Option::is_none")]
script_beim_schliessen: Option<Script>,
#[serde(rename = "ScriptBeimSpeichern")]
#[serde(skip_serializing_if = "Option::is_none")]
script_beim_speichern: Option<Script>,
#[serde(rename = "ScriptBeimNeuanlegen")]
#[serde(skip_serializing_if = "Option::is_none")]
script_beim_neuanlegen: Option<Script>,
#[serde(rename = "ScriptBeimBearbeiten")]
#[serde(skip_serializing_if = "Option::is_none")]
script_beim_bearbeiten: Option<Script>,
#[serde(rename = "ScriptBeimKopieren")]
#[serde(skip_serializing_if = "Option::is_none")]
script_beim_kopieren: Option<Script>,
#[serde(rename = "ScriptBeimImport")]
#[serde(skip_serializing_if = "Option::is_none")]
script_beim_import: Option<Script>,
#[serde(rename = "ScriptBeimAnonymisieren")]
#[serde(skip_serializing_if = "Option::is_none")]
script_beim_anonymisieren: Option<Script>,
#[serde(rename = "SID")]
sid: String,
#[serde(rename = "GUID")]
guid: String,
#[serde(rename = "Revision")]
revision: u16,
#[serde(rename = "maxAnzahl")]
#[serde(skip_serializing_if = "Option::is_none")]
max_anzahl: Option<u16>,
#[serde(rename = "VerknuepftGUID")]
#[serde(skip_serializing_if = "Option::is_none")]
verknuepft_guid: Option<String>,
#[serde(rename = "SeitenzahlSichtbar")]
#[serde(skip_serializing_if = "Option::is_none")]
seitenanzahl_sichtbar: Option<bool>,
#[serde(rename = "Entries")]
entries: Entries<Entry>,
#[serde(rename = "PlausibilityRules")]
plausibility_rules: PlausibilityRules<DataFormEntries>,
#[serde(rename = "Haeufigkeiten")]
haeufigkeiten: Haeufigkeiten,
#[serde(rename = "Kennzahlen")]
kennzahlen: Kennzahlen,
#[serde(rename = "Ordner")]
ordner: Ordner,
#[serde(rename = "MenuCategory")]
#[serde(skip_serializing_if = "Option::is_none")]
menu_category: Option<MenuCategory>,
#[serde(rename = "PunkteKategorien")]
#[serde(skip_serializing_if = "Option::is_none")]
punkte_kategorien: Option<PunkteKategorien>,
#[serde(rename = "Ansichten")]
#[serde(skip_serializing_if = "Option::is_none")]
ansichten: Option<Ansichten>,
}
impl FormEntryContainer for Unterformular {
fn apply_profile(&mut self, profile: &Profile) {
profile.forms.iter().for_each(|profile_form| {
if self.name == profile_form.name {
self.entries.entry.iter_mut().for_each(|entry| {
profile_form
.form_references
.iter()
.for_each(|form_reference| {
apply_profile_to_form_entry(entry, form_reference)
});
// Hide form field using filter set to "false" if requested and change default value
profile_form
.form_fields
.iter()
.for_each(|form_field| apply_profile_to_form_field(entry, form_field));
if let Some(menu_category) = &profile_form.menu_category {
self.menu_category = Some(MenuCategory {
name: menu_category.name.clone(),
position: menu_category.position.clone(),
column: menu_category.column.clone(),
});
}
})
}
});
}
}
impl Listable for Unterformular {
fn to_listed_string(&self) -> String {
format!(
"Unterformular ({}) '{}' in Revision '{}'",
match self.is_system_library_content() {
true => style("S").yellow(),
_ => style("u"),
},
style(&self.name).yellow(),
style(&self.revision).yellow(),
)
}
}
impl Sortable for Unterformular {
fn sorting_key(&self) -> String {
self.name.clone()
}
fn sorted(&mut self) -> &Self {
self.data_catalogues.data_catalogue.sort_unstable();
self.entries
.entry
.sort_unstable_by_key(|item| item.sorting_key());
self.entries.entry.iter_mut().for_each(|item| {
item.sorted();
});
if let Some(ref mut plausibility_rule) = self.plausibility_rules.plausibility_rule {
plausibility_rule.sort_unstable_by_key(|item| item.bezeichnung.clone());
plausibility_rule.iter_mut().for_each(|item| {
if let Some(ref mut data_form_entry_names) = item.data_form_entries.entry_name {
data_form_entry_names.sort_unstable();
}
});
}
self
}
}
impl Comparable for Unterformular {
fn get_name(&self) -> String {
self.name.clone()
}
fn get_revision(&self) -> u16 {
self.revision
}
fn compare_by_requirement(a: &Self, b: &Self) -> Ordering {
if a.get_name() == b.get_name()
|| a.is_system_library_content()
|| b.is_system_library_content()
{
return Ordering::Equal;
}
if a.requires_form_reference(&b.get_name()) || a.requires_subform(&b.get_name()) {
return Ordering::Greater;
}
Ordering::Less
}
}
impl Requires for Unterformular {
fn requires_form_reference(&self, name: &str) -> bool {
self.entries
.entry
.iter()
.map(|item| {
item.type_ == "formReference"
&& match item.referenced_data_form.as_ref() {
Some(refname) => refname == name,
_ => false,
}
})
.filter(|&it| it)
.last()
.unwrap_or_default()
}
fn requires_subform(&self, name: &str) -> bool {
self.entries
.entry
.iter()
.map(|item| {
item.type_ == "subform"
&& match item.referenced_data_form.as_ref() {
Some(refname) => refname == name,
_ => false,
}
})
.filter(|&it| it)
.last()
.unwrap_or_default()
}
fn get_required_entries<'a>(&'a self, all: &'a OnkostarEditor) -> Vec<Requirement> {
let mut result = self
.data_catalogues
.data_catalogue
.iter()
.collect::<HashSet<_>>()
.into_iter()
.map(|entry| match all.find_data_catalogue(entry.as_str()) {
Some(contained) => Requirement::DataCatalogue(contained),
None => Requirement::ExternalDataCatalogue(entry.to_string()),
})
.collect::<Vec<_>>();
result.sort_unstable_by_key(|item| item.sorting_key());
let referenced_forms = &mut self
.entries
.entry
.iter()
.filter(|&entry| entry.get_type() == "formReference")
.filter_map(|entry| match &entry.referenced_data_form {
Some(name) => Some(name),
None => None,
})
.collect::<HashSet<_>>()
.into_iter()
.map(|entry| match all.find_data_form(entry.as_str()) {
Some(contained) => Requirement::DataFormReference(contained),
None => match all.find_unterformular(entry.as_str()) {
Some(contained) => Requirement::UnterformularReference(contained),
None => Requirement::ExternalUnterformularReference(entry.to_string()),
},
})
.collect::<Vec<_>>();
referenced_forms.sort_unstable_by_key(|item| item.sorting_key());
result.append(referenced_forms);
let sub_forms = &mut self
.entries
.entry
.iter()
.filter(|&entry| entry.get_type() == "subform")
.filter_map(|entry| match &entry.referenced_data_form {
Some(name) => Some(name),
None => None,
})
.collect::<HashSet<_>>()
.into_iter()
.map(|entry| match all.find_data_form(entry.as_str()) {
Some(contained) => Requirement::DataFormSubform(contained),
None => match all.find_unterformular(entry.as_str()) {
Some(contained) => Requirement::UnterformularSubform(contained),
None => Requirement::ExternalUnterformularSubform(entry.to_string()),
},
})
.collect::<Vec<_>>();
sub_forms.sort_unstable_by_key(|item| item.sorting_key());
result.append(sub_forms);
result
}
}
impl FolderContent for Unterformular {
fn get_library_folder(&self) -> String {
self.ordner.bibliothek.name.to_string()
}
}
impl Checkable for Unterformular {
fn check(&self) -> Vec<CheckNotice> {
if self.hat_unterformulare {
return vec![ErrorWithCode {
code: "2023-0001".to_string(),
description: format!(
"Unterformular '{}' mit Markierung 'hat Unterformulare'",
self.name
),
line: None,
example: None,
}];
}
vec![]
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct DataCatalogues {
#[serde(rename = "DataCatalogue")]
data_catalogue: Vec<String>,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct DataFormEntries {
#[serde(rename = "EntryName")]
#[serde(skip_serializing_if = "Option::is_none")]
entry_name: Option<Vec<String>>,
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use crate::model::onkostar_editor::OnkostarEditor;
use crate::model::{Filter, RefEntries, Script};
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());
}
#[test]
fn should_change_unterformular_entry_scripts_code_with_form_fields() {
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
scripts_code: |-
// Example code
console.log(42);
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].scripts,
None
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].scripts,
Some(Script {
code: "// Example code&#10;console.log(42);".into(),
valid: true
})
);
}
#[test]
fn should_change_unterformular_entry_scripts_code_with_form_references() {
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
scripts_code: |-
// Example code
console.log(42);
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].scripts,
None
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].scripts,
Some(Script {
code: "// Example code&#10;console.log(42);".into(),
valid: true
})
);
}
#[test]
fn should_remove_unterformular_entry_filter_with_form_fields() {
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
remove_filter: true
scripts_code: |-
// Example code
console.log(42);
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].filter,
Some(Filter {
condition: "getGlobalSetting('mehrere_mtb_in_mtbepisode') = 'true'".into(),
valid: true,
ref_entries: Some(RefEntries { ref_entry: None })
})
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].filter,
None
);
}
#[test]
fn should_remove_unterformular_entry_filter_with_form_references() {
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
remove_filter: true
scripts_code: |-
// Example code
console.log(42);
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].filter,
Some(Filter {
condition: "getGlobalSetting('mehrere_mtb_in_mtbepisode') = 'true'".into(),
valid: true,
ref_entries: Some(RefEntries { ref_entry: None })
})
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].filter,
None
);
}
}