Skip to content

Commit

Permalink
Implement Sequence Reference ID Service (CO-1676)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benn Oshrin committed Dec 9, 2018
1 parent 4d81c95 commit e17d86c
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/config/schema/schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
<field name="description" type="C" size="128" />
<field name="table_name" type="C" size="128" />
<field name="status" type="C" size="2" />
<!-- XXX add this -->
<field name="referenceid_method" type="C" size="2" />
<field name="referenceid_start" type="I" />
<field name="referenceid_prefix" type="C" size="32" />
<field name="created" type="T" />
<field name="modified" type="T" />

Expand Down
12 changes: 11 additions & 1 deletion app/src/Lib/Identifier/ReferenceIdService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,15 @@
namespace App\Lib\Identifier;

abstract class ReferenceIdService {
abstract public function generate();
/**
* Generate a Reference Identifier.
*
* @since COmanage Match v1.0.0
* @param Matchgrid $mgConfig Matchgrid Configuration
* @param ADOConnection $dbc ADODB Database Connection Handle
* @return string Reference Identifier
* @throws RuntimeException
*/

abstract public function generate(\App\Model\Entity\Matchgrid $mgConfig, \ADOConnection $dbc);
}
23 changes: 19 additions & 4 deletions app/src/Lib/Identifier/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,25 @@
namespace App\Lib\Identifier;

class Sequence extends ReferenceIdService {
public function generate() {
// XXX we need access to the database config
/**
* Generate a Reference Identifier.
*
* @since COmanage Match v1.0.0
* @param Matchgrid $mgConfig Matchgrid Configuration
* @param ADOConnection $dbc ADODB Database Connection Handle
* @return string Reference Identifier
* @throws RuntimeException
*/

public function generate(\App\Model\Entity\Matchgrid $mgConfig, \ADOConnection $dbc) {
// We'll use the matchgrid ID rather than name in the sequence name to avoid
// issues if the matchgrid is renamed for some reason.

// Use ADOdb genId() to create/read from sequence http://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:genid
// genId('matchgrid_<id>_sequence') or something
$seq = "mg_seq_" . (string)$mgConfig->id;
$prefix = $mgConfig->referenceid_prefix ?: "";

$refId = $prefix . (string)$dbc->genId($seq, $mgConfig->referenceid_start);

return $refId;
}
}
12 changes: 11 additions & 1 deletion app/src/Lib/Identifier/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@
namespace App\Lib\Identifier;

class Uuid extends ReferenceIdService {
public function generate() {
/**
* Generate a Reference Identifier.
*
* @since COmanage Match v1.0.0
* @param Matchgrid $mgConfig Matchgrid Configuration
* @param ADOConnection $dbc ADODB Database Connection Handle
* @return string Reference Identifier
* @throws RuntimeException
*/

public function generate(\App\Model\Entity\Matchgrid $mgConfig, \ADOConnection $dbc) {
// GUID generation based on https://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid

// could use openssl_random_pseudo_bytes(16)
Expand Down
16 changes: 7 additions & 9 deletions app/src/Lib/Match/MatchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Cake\Utility\Xml;

use \App\Lib\Enum\ConfidenceModeEnum;
use \App\Lib\Enum\ReferenceIdEnum;
use \App\Lib\Enum\SearchTypeEnum;

require(ROOT . DS . "vendor" . DS . "adodb" . DS . "adodb-php" . DS . "adodb-xmlschema03.inc.php");
Expand Down Expand Up @@ -103,16 +104,13 @@ public function attachReferenceId(string $sor, string $sorid, AttributeManager $
*/

protected function generateReferenceId() {
// XXX read the matchgrid config and instantiate the appropriate backend
// need to pass database config/connection to Sequence (or maybe to all, and they
// can ignore it if not needed, eg UUID)
// Note $this->dbc is available via PostgresService
// We could also recreate matchgrid with reference_id column type uuid (supported by postgres)
// Should we build matchgrid with the appropriate column type (int or uuid) and then make it not-changeable?
$IdService = new \App\Lib\Identifier\Uuid;
if($this->mgConfig->referenceid_method == ReferenceIdEnum::Sequence) {
$IdService = new \App\Lib\Identifier\Sequence;
} else {
$IdService = new \App\Lib\Identifier\Uuid;
}

return $IdService->generate();
return $IdService->generate($this->mgConfig, $this->dbc);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions app/src/Locale/en_US/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ msgstr "Reference ID"
msgid "match.fd.referenceid_method"
msgstr "Reference ID Assignment Method"

msgid "match.fd.referenceid_prefix"
msgstr "Reference ID Prefix"

msgid "match.fd.referenceid_prefix.desc"
msgstr "For sequence based Reference IDs, the prefix to prepend to the assigned integer"

msgid "match.fd.referenceid_start"
msgstr "Reference ID Initial Value"

Expand Down
7 changes: 7 additions & 0 deletions app/src/Model/Table/MatchgridsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ public function validationDefault(Validator $validator) {
);
$validator->allowEmpty('referenceid_start');

$validator->add(
'referenceid_prefix',
'content',
[ 'rule' => [ 'maxLength', 32 ] ]
);
$validator->allowEmpty('referenceid_start');

return $validator;
}
}
6 changes: 6 additions & 0 deletions app/src/Template/Matchgrids/fields.inc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ use \App\Lib\Enum\ReferenceIdEnum;
var method = document.getElementById('referenceid-method').value;

if(method == '<?= ReferenceIdEnum::Sequence ?>') {
$("#referenceid-prefix").closest('li').show('fade');
$("#referenceid-start").closest('li').show('fade');
} else {
$("#referenceid-prefix").closest('li').hide();
$("#referenceid-start").closest('li').hide();
}
}
Expand All @@ -62,4 +64,8 @@ if($action == 'add' || $action == 'edit') {

print $this->Field->control('referenceid_start',
['default' => 1001]);

print $this->Field->control('referenceid_prefix',
[],
false);
}

0 comments on commit e17d86c

Please sign in to comment.