Skip to content

Commit

Permalink
Require lowercase (CO-2477) and unique (CO-2440) matchgrid table names
Browse files Browse the repository at this point in the history
  • Loading branch information
Benn Oshrin committed Dec 27, 2022
1 parent bd348bb commit 33f0a30
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/resources/locales/en_US/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ msgstr "When this value is selected, {0} cannot be empty"
msgid "match.er.input.invalid"
msgstr "Invalid character found"

msgid "match.er.mg.inuse"
msgstr "A Matchgrid with table name {0} already exists"

msgid "match.er.mg.notfound"
msgstr "Matchgrid table not found, was the Matchgrid built? ({0})"

Expand Down
78 changes: 75 additions & 3 deletions app/src/Model/Table/MatchgridsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@

namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use \Cake\Event\EventInterface;
use \Cake\ORM\Query;
use \Cake\ORM\RulesChecker;
use \Cake\ORM\Table;
use \Cake\Validation\Validator;

use \App\Lib\Enum\ConfidenceModeEnum;
use \App\Lib\Enum\StatusEnum;
Expand Down Expand Up @@ -94,6 +96,24 @@ public function initialize(array $config): void {

$this->setAllowLookupPrimaryLink(['build', 'manage', 'configure', 'pending', 'reconcile']);
}

/**
* Actions to perform before validation.
*
* @since @COmanage Match v1.1.0
* @param EventInterface $event Event
* @param ArrayObject $data Data
* @param ArrayObject $options Options
*/

public function beforeMarshal(EventInterface $event, \ArrayObject $data, \ArrayObject $options) {
// We convert the table name to lowercase to avoid problems with database
// queries due to capital letters (see CO-2477)

if(isset($data['table_name'])) {
$data['table_name'] = mb_strtolower($data['table_name']);
}
}

/**
* Build the specified Matchgrid.
Expand All @@ -113,6 +133,27 @@ public function build(int $id) {
return true;
}

/**
* Define business rules.
*
* @since COmanage Match v1.1.0
* @param RulesChecker $rules RulesChecker object
* @return RulesChecker
*/

public function buildRules(RulesChecker $rules): RulesChecker {
// Two Matchgrids cannot have the same name. Since the name is used
// to generate the matchgrid table name, and tablenames are usually
// treated as case insensitive, we perform a case insensitive check
// (and so can't use isUnique()).

$rules->add([$this, 'ruleIsUnique'],
'isUnique',
['errorField' => 'table_name']);

return $rules;
}

/**
* Calculate the Matchgrid ID associated with the requested object ID.
*
Expand Down Expand Up @@ -162,6 +203,37 @@ public function getMatchgridConfig($id) {
]]);
}

/**
* Application Rule to determine if the current entity is uniquely named
* (case insensitive).
*
* @param Entity $entity Entity to be validated
* @param array $options Application rule options
*
* @return bool|string true if the Rule check passes, false otherwise
* @since COmanage Match v1.1.0
*/

public function ruleIsUnique($entity, array $options): bool|string {
// We don't want to perform the uniqueness check on an update where
// the table name is not changed

if(!$entity->id || $entity->isDirty('table_name')) {
$c = $this->find('all', [
'conditions' => [
'LOWER(Matchgrids.table_name)' => strtolower($entity->table_name)
]
])
->count();

if($c > 0) {
return __('match.er.mg.inuse', [$entity->table_name]);
}
}

return true;
}

/**
* Set validation rules.
*
Expand Down

0 comments on commit 33f0a30

Please sign in to comment.