From 5c0396311650cd5bd649eb2f3e308697f24b1697 Mon Sep 17 00:00:00 2001 From: Dr Masroor Ehsan Date: Wed, 22 Jan 2025 11:52:20 +0600 Subject: [PATCH] wip --- app/Domain/Rule/MatchCondition.php | 9 ++++++ app/Domain/Rule/MatchMode.php | 12 ++++++++ app/Domain/Rule/StringMatchMode.php | 12 -------- app/Services/Pacs/InstituteMapperDicom.php | 8 ++--- app/Services/StringMatcher.php | 16 +++++----- ...5040_create_dicom_routing_rules_table.php} | 16 +++++----- ...754_create_dicom_rule_conditions_table.php | 30 +++++++++++++++++++ database/seeders/InstituteSeeder.php | 16 +++++----- 8 files changed, 80 insertions(+), 39 deletions(-) create mode 100644 app/Domain/Rule/MatchCondition.php create mode 100644 app/Domain/Rule/MatchMode.php delete mode 100644 app/Domain/Rule/StringMatchMode.php rename database/migrations/{2024_12_28_175040_create_dicom_mapping_rules_table.php => 2024_12_28_175040_create_dicom_routing_rules_table.php} (55%) create mode 100644 database/migrations/2024_12_29_073754_create_dicom_rule_conditions_table.php diff --git a/app/Domain/Rule/MatchCondition.php b/app/Domain/Rule/MatchCondition.php new file mode 100644 index 0000000..7768231 --- /dev/null +++ b/app/Domain/Rule/MatchCondition.php @@ -0,0 +1,9 @@ +addMinutes(15), - fn () => DB::table('dicom_mapping_rules') - ->orderByDesc('sort_order') + fn () => DB::table('dicom_routing_rules') + ->orderByDesc('priority') ->get()->toArray() ); self::$catchAll = DB::table('institutes')->first('id')->id; @@ -32,7 +32,7 @@ public static function map(?string $input): array $input = strtolower($input); foreach (self::$patterns as $pattern) { - if (StringMatcher::match($input, $pattern->name, StringMatchMode::from($pattern->match_mode))) { + if (StringMatcher::match($input, $pattern->name, MatchMode::from($pattern->match_mode))) { return [$pattern->institute_id, $pattern->facility_id]; } } diff --git a/app/Services/StringMatcher.php b/app/Services/StringMatcher.php index 4ae0fe5..30e9d60 100644 --- a/app/Services/StringMatcher.php +++ b/app/Services/StringMatcher.php @@ -2,27 +2,27 @@ namespace App\Services; -use App\Domain\Rule\StringMatchMode; +use App\Domain\Rule\MatchMode; final class StringMatcher { - public static function match(string $input, string $pattern, StringMatchMode $mode): bool + public static function match(string $input, string $pattern, MatchMode $mode): bool { if (blank($input) || blank($pattern)) { return false; } - if ($mode !== StringMatchMode::Regex) { + if ($mode !== MatchMode::Regex) { $input = strtolower($input); $pattern = strtolower($pattern); } return match ($mode) { - StringMatchMode::Exact => strcasecmp($input, $pattern) === 0, - StringMatchMode::Contains => str_contains($input, $pattern) , - StringMatchMode::StartsWith => str_starts_with($input, $pattern), - StringMatchMode::EndsWith => str_ends_with($input, $pattern), - StringMatchMode::Regex => preg_match($pattern, $input) === 1, + MatchMode::Exact => strcasecmp($input, $pattern) === 0, + MatchMode::Contains => str_contains($input, $pattern) , + MatchMode::StartsWith => str_starts_with($input, $pattern), + MatchMode::EndsWith => str_ends_with($input, $pattern), + MatchMode::Regex => preg_match($pattern, $input) === 1, default => false, }; } diff --git a/database/migrations/2024_12_28_175040_create_dicom_mapping_rules_table.php b/database/migrations/2024_12_28_175040_create_dicom_routing_rules_table.php similarity index 55% rename from database/migrations/2024_12_28_175040_create_dicom_mapping_rules_table.php rename to database/migrations/2024_12_28_175040_create_dicom_routing_rules_table.php index ae49b07..c01f156 100644 --- a/database/migrations/2024_12_28_175040_create_dicom_mapping_rules_table.php +++ b/database/migrations/2024_12_28_175040_create_dicom_routing_rules_table.php @@ -1,6 +1,6 @@ id(); + $table->boolean('is_active')->default(true); $table->foreignIdFor(Institute::class)->constrained()->cascadeOnDelete(); $table->foreignIdFor(Facility::class)->nullable()->constrained()->cascadeOnDelete(); - $table->string('dicom_tag'); - $table->string('search_pattern'); - $table->unsignedTinyInteger('match_mode')->default(StringMatchMode::Exact->value); - $table->unsignedTinyInteger('sort_order')->default(0); + $table->string('description')->nullable(); + $table->string('match_condition')->default(MatchCondition::Or->value); + $table->unsignedTinyInteger('priority')->default(0); $table->timestamps(); - $table->unique(['institute_id', 'dicom_tag']); + $table->index(['is_active', 'priority']); }); } public function down(): void { - Schema::dropIfExists('dicom_mapping_rules'); + Schema::dropIfExists('dicom_routing_rules'); } }; diff --git a/database/migrations/2024_12_29_073754_create_dicom_rule_conditions_table.php b/database/migrations/2024_12_29_073754_create_dicom_rule_conditions_table.php new file mode 100644 index 0000000..0b628ab --- /dev/null +++ b/database/migrations/2024_12_29_073754_create_dicom_rule_conditions_table.php @@ -0,0 +1,30 @@ +id(); + $table->foreignId('dicom_routing_rule_id')->constrained()->cascadeOnDelete(); + $table->string('dicom_tag'); + $table->string('search_pattern'); + $table->boolean('case_sensitive')->default(false); + $table->string('match_mode')->default(MatchMode::Exact->value); + $table->unsignedTinyInteger('priority')->default(0); + $table->timestamps(); + + $table->index(['dicom_mapping_rules_id', 'priority']); + }); + } + + public function down(): void + { + Schema::dropIfExists('dicom_rule_conditions'); + } +}; diff --git a/database/seeders/InstituteSeeder.php b/database/seeders/InstituteSeeder.php index 5cb4414..fcb8f4f 100644 --- a/database/seeders/InstituteSeeder.php +++ b/database/seeders/InstituteSeeder.php @@ -2,7 +2,7 @@ namespace Database\Seeders; -use App\Domain\Rule\StringMatchMode; +use App\Domain\Rule\MatchMode; use App\Models\DicomServer; use App\Models\Facility; use App\Models\Institute; @@ -45,15 +45,17 @@ public function run(): void ] ); - DB::table('institute_names')->insert([ + DB::table('dicom_mapping_rules')->insert([ + 'dicom_tag' => 'InstitutionName', 'name' => 'CHEVRON CLINICAL LABORATORY , PANCHLAISH,CTG', - 'institute_id' => $chev->id, - 'match_mode' => StringMatchMode::Exact->value, + 'search_pattern' => $chev->id, + 'match_mode' => MatchMode::Exact->value, ]); - DB::table('institute_names')->insert([ - 'name' => 'CHEVRON CLINICAL', + DB::table('dicom_mapping_rules')->insert([ + 'dicom_tag' => 'InstitutionName', + 'search_pattern' => 'CHEVRON CLINICAL', 'institute_id' => $chev->id, - 'match_mode' => StringMatchMode::StartsWith->value, + 'match_mode' => MatchMode::StartsWith->value, ]); DicomServer::create(